Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
kickstartd
#include<bits/stdc++.h> using namespace std; #define MOD #define pb push_back #define inf 10e17 #define ff first #define ss second int t1=1; #define f(i,n) for(int i=0;i<n;i++) #define MAX 200005 long long int L[MAX],par[MAX],A[MAX],M[MAX],lazy[4*MAX],t[4*MAX],tin[MAX],tout[MAX],timer=0,res[MAX]; vector<pair<int,pair<long long int,long long int>>> v[MAX]; void dfs(int node,int p){ par[node] = p; timer++; tin[node]= timer; for(auto it:v[node]){ if(it.ff!=p){ M[it.ss.ff] = it.ff; L[it.ff] = it.ss.ff; A[it.ff] = it.ss.ss; dfs(it.ff,node); } } tout[node] =timer; } void push(int v) { t[v*2] = __gcd(t[v*2],lazy[v]); t[v*2+1] = __gcd(t[v*2+1],lazy[v]); lazy[v*2] = __gcd(lazy[v*2],lazy[v]); lazy[v*2+1] = __gcd(lazy[v*2+1],lazy[v]); lazy[v] = 0; } void update(int v, int tl, int tr, int l, int r, long long int addend) { if (l > r) return; if (l == tl && tr == r) { t[v] = __gcd(t[v],addend); lazy[v] = __gcd(lazy[v],addend); } else { push(v); int tm = (tl + tr) / 2; update(v*2, tl, tm, l, min(r, tm), addend); update(v*2+1, tm+1, tr, max(l, tm+1), r, addend); t[v] = __gcd(t[v*2], t[v*2+1]); } } int query(int v, int tl, int tr, int l, int r) { if (l > r) return 0; if (l <= tl && tr <= r) return t[v]; push(v); int tm = (tl + tr) / 2; return __gcd(query(v*2, tl, tm, l, min(r, tm)), query(v*2+1, tm+1, tr, max(l, tm+1), r)); } void solve() { int n,q; cin>>n>>q; for(int i=0;i<=n;i++){ L[i] = 0; A[i] = 0; par[i] = 0; v[i].clear(); } for(int i=1;i<=200000;i++){ M[i] = 0; } for(int i=0;i<n-1;i++){ long long int x,y,z,w; cin>>x>>y>>z>>w; v[x].push_back({y,{z,w}}); v[y].push_back({x,{z,w}}); } timer = 0; dfs(1,0); cout<<"Case #"<<t1<<":"<<" "; t1++; /* for(int i=1;i<=n;i++){ cout<<tin[i]<<" "; } cout<<endl; for(int i=1;i<=n;i++){ cout<<tout[i]<<" "; } cout<<endl;*/ pair<int,pair<int,int>> q1[MAX]; for(int i=0;i<q;i++){ int c,w; cin>>c>>w; q1[i] = {w,{c,i}}; } sort(q1,q1+q); int ptr = 0; vector<int> v1; for(int i=1;i<=200000;i++){ if(M[i]!=0) { cout<<tin[M[i] update(0,0,n-1,tin[M[i]]-1,tout[M[i]]-1,A[M[i]]); for(int i=0;i<=20;i++){ cout<<t[i]<<" "; } cout<<endl; } while(ptr<q && q1[ptr].ff>=i){ res[q1[ptr].ss.ss] = query(0,0,n-1,tin[q1[ptr].ss.ff]-1,tin[q1[ptr].ss.ff]-1); ptr++; v1.clear(); } } for(int i=0;i<q;i++){ cout<<res[i]<<" "; } cout<<endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin>>t; while(t--) { solve(); } }
run
|
edit
|
history
|
help
0
lambda demo
Working variables benchmark
merge without extra space Gap method ALgorithm
Dar
ExceptionHandling1
Średnia bez zera
Stream generalization
C++ User Input #1
lock
Print reverese string non repeated chars