Submission #489280

# Submission time Handle Problem Language Result Execution time Memory
489280 2021-11-21T22:05:37 Z CSQ31 Inside information (BOI21_servers) C++17
100 / 100
521 ms 44384 KB
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define all(a) a.begin(),a.end()
typedef pair<int,int> pii;
const int MAXN = 2e5;
vector<pii>adj[MAXN];
int dep[MAXN],par[18][MAXN],asc[MAXN],dsc[MAXN],val[MAXN],n,k;
char c[250000];
int a[250000],d[250000],ans[250000];
void dfs0(int v,int u,int ww){
	for(int i=1;i<=17;i++)par[i][v] = par[i-1][par[i-1][v]];
	for(pii z:adj[v]){
		int x = z.fi;
		int w = z.se;
		if(x == u)continue;
		val[x] = w;
		asc[x] = dsc[x] = dep[v];
		if(w > ww || v==1)asc[x] = asc[v];	
		if(w < ww || v==1)dsc[x] = dsc[v];
		
		dep[x] = dep[v]+1;
		par[0][x] = v;
		dfs0(x,v,w);
	}
}
int jump(int v,int d){
	for(int i=0;i<=17 && d;i++){
		if(d&1)v=par[i][v];
		d/=2;
	}
	return v;
}
int lca(int v,int u){
	if(dep[v] < dep[u])swap(v,u);
	v = jump(v,dep[v] - dep[u]);
	
	if(v==u)return v;
	for(int i=17;i>=0;i--){
		if(par[i][v] != par[i][u]){
			v = par[i][v];
			u = par[i][u];
		}
	}
	return par[0][v];
}
//want to count number of paths from i to j
//such that i->j is increasing
//and the last edge that touches j is <= t
//let c be the centroid
//call node x a start node if c->x is increasing
//call node x an end node if c->x is decreasing 

//can sweepline on their time but overcounting????
//call the join time of node x the time where x is first connected to c
//the add time of end node x the time where the first edge from path c->i is added
//contribution for start node i is number of end nodes with join time > its join time and add time <= t
//ok so sweepline backwards by join time then pick whatever sum aggre ds,fenwick should be fast enough 
vector<int>cq[MAXN];
int sub[MAXN],join[MAXN],reach[MAXN],fen[250000];
bool ded[MAXN];
void upd(int i,int x){
	for(;i<=240000;i+=i&(-i))fen[i]+=x;
}
int query(int i){
	int res = 0;
	for(;i;i-=i&(-i))res+=fen[i];
	return res;
}

void get(int v,int u){
	sub[v] = 1;
	for(auto x:adj[v]){
		if(x.fi==u ||ded[x.fi])continue;
		get(x.fi,v);
		sub[v]+=sub[x.fi];
	}
}
int cen(int v,int u,int n){
	for(auto x:adj[v]){
		if(ded[x.fi] || x.fi == u)continue;
		if(sub[x.fi] > n/2)return cen(x.fi,v,n);
	}
	return v;
}
vector<int>st,ed;
void dfs(int v,int u,int type,int ww){ //type 1 increasing,type 2 decreasing
	if(type<=1)st.pb(v);
	if(type!=1)ed.pb(v);
	
	join[v] = max(join[u],ww);
	reach[v] = min(reach[u],ww);
	for(pii z:adj[v]){
		int x = z.fi;
		int w = z.se;
		if(x==u || ded[x])continue;
		if((!type || type ==1) && ww > w)dfs(x,v,1,w);
		if((!type || type ==2) && ww < w)dfs(x,v,2,w);
	}
	
}
void solve(int v){
	get(v,0);
	v = cen(v,0,sub[v]);
	ded[v] = 1;
	join[v] = 0;
	reach[v] = 1e9;
	for(auto z:adj[v]){
		int x = z.fi;
		int w = z.se;
		if(ded[x])continue;
		dfs(x,v,0,w);
	}
	//for a st node i,want number of ed nodes j st reach[j] > join[i] and join[j] <= current ime
	//first ineq eliminate by sweep line
	//second ineq can eliminate by [insert ds]
	st.pb(v);
	sort(all(st),[&](int i,int j){return join[i] > join[j];});
	sort(all(ed),[&](int i,int j){return reach[i] > reach[j];});
	
	int ptr = 0;
	for(int i=0;i<sz(st);i++){
		while(ptr<sz(ed) && reach[ed[ptr]] > join[st[i]]){
			upd(join[ed[ptr]],1);
			ptr++;
		}
		
		for(int tim : cq[st[i]]){
			/*
			for(int j=0;j<ptr;j++){
				if(join[ed[j]] <= tim+1)ans[tim]++;
			}*/
			ans[tim]+=query(tim+1);
			if(st[i]!=v && join[st[i]] <= tim+1)ans[tim]++;
		}
	}
	for(int i=0;i<ptr;i++)upd(join[ed[i]],-1);
	st.clear();
	ed.clear();
	for(auto z:adj[v])if(!ded[z.fi])solve(z.fi);
}
int main()
{
	cin>>n>>k;
	for(int i=0;i<n+k-1;i++){
		cin>>c[i]>>a[i];
		if(c[i]!='C')cin>>d[i];
		if(c[i] == 'S'){
			adj[a[i]].pb({d[i],i+1});
			adj[d[i]].pb({a[i],i+1});
		}
		if(c[i]=='C')cq[a[i]].pb(i);
	}
	dfs0(1,0,0);
	solve(1);
	for(int i=0;i<n+k-1;i++){
		if(c[i]=='C')cout<<ans[i]+1<<'\n';
		else if(c[i]=='Q'){ //this solves for Q queries now for C queries
			int v = a[i];
			int u = d[i];

			int m = lca(v,u),last = -1e9;
			if(v!=m)last = val[v];
			else if(u!=m)last = val[jump(u,dep[u]-dep[m]-1)];
			bool ok= (last<=i);
			
			if(asc[v] > dep[m] || dsc[u] > dep[m])ok = 0;
			if(v!=m && u!= m){
				v = jump(v,dep[v]-dep[m]-1);
				u = jump(u,dep[u]-dep[m]-1);
				if(val[u] > val[v])ok = 0;
			}
			if(ok)cout<<"yes"<<'\n';
			else cout<<"no"<<'\n';
		}
		
	}
}
# Verdict Execution time Memory Grader output
1 Correct 62 ms 11588 KB Output is correct
2 Correct 89 ms 12260 KB Output is correct
3 Correct 81 ms 12372 KB Output is correct
4 Correct 98 ms 12368 KB Output is correct
5 Correct 86 ms 12136 KB Output is correct
6 Correct 86 ms 12016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 11588 KB Output is correct
2 Correct 89 ms 12260 KB Output is correct
3 Correct 81 ms 12372 KB Output is correct
4 Correct 98 ms 12368 KB Output is correct
5 Correct 86 ms 12136 KB Output is correct
6 Correct 86 ms 12016 KB Output is correct
7 Correct 62 ms 11844 KB Output is correct
8 Correct 78 ms 13204 KB Output is correct
9 Correct 70 ms 13220 KB Output is correct
10 Correct 83 ms 13288 KB Output is correct
11 Correct 75 ms 12992 KB Output is correct
12 Correct 70 ms 13032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 11556 KB Output is correct
2 Correct 278 ms 31176 KB Output is correct
3 Correct 273 ms 31172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 11556 KB Output is correct
2 Correct 278 ms 31176 KB Output is correct
3 Correct 273 ms 31172 KB Output is correct
4 Correct 59 ms 11960 KB Output is correct
5 Correct 261 ms 32152 KB Output is correct
6 Correct 186 ms 32828 KB Output is correct
7 Correct 195 ms 34628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 11556 KB Output is correct
2 Correct 289 ms 38200 KB Output is correct
3 Correct 289 ms 38392 KB Output is correct
4 Correct 303 ms 38848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 11556 KB Output is correct
2 Correct 289 ms 38200 KB Output is correct
3 Correct 289 ms 38392 KB Output is correct
4 Correct 303 ms 38848 KB Output is correct
5 Correct 60 ms 11912 KB Output is correct
6 Correct 309 ms 40524 KB Output is correct
7 Correct 312 ms 41244 KB Output is correct
8 Correct 324 ms 44132 KB Output is correct
9 Correct 313 ms 43900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 11588 KB Output is correct
2 Correct 295 ms 30292 KB Output is correct
3 Correct 262 ms 30228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 11588 KB Output is correct
2 Correct 295 ms 30292 KB Output is correct
3 Correct 262 ms 30228 KB Output is correct
4 Correct 63 ms 12000 KB Output is correct
5 Correct 298 ms 32616 KB Output is correct
6 Correct 275 ms 31940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 11668 KB Output is correct
2 Correct 288 ms 38308 KB Output is correct
3 Correct 282 ms 38304 KB Output is correct
4 Correct 312 ms 38864 KB Output is correct
5 Correct 60 ms 11608 KB Output is correct
6 Correct 291 ms 30116 KB Output is correct
7 Correct 270 ms 30560 KB Output is correct
8 Correct 353 ms 30276 KB Output is correct
9 Correct 329 ms 30808 KB Output is correct
10 Correct 400 ms 35468 KB Output is correct
11 Correct 470 ms 34704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 11668 KB Output is correct
2 Correct 288 ms 38308 KB Output is correct
3 Correct 282 ms 38304 KB Output is correct
4 Correct 312 ms 38864 KB Output is correct
5 Correct 60 ms 11608 KB Output is correct
6 Correct 291 ms 30116 KB Output is correct
7 Correct 270 ms 30560 KB Output is correct
8 Correct 353 ms 30276 KB Output is correct
9 Correct 329 ms 30808 KB Output is correct
10 Correct 400 ms 35468 KB Output is correct
11 Correct 470 ms 34704 KB Output is correct
12 Correct 60 ms 11920 KB Output is correct
13 Correct 311 ms 40512 KB Output is correct
14 Correct 327 ms 41356 KB Output is correct
15 Correct 317 ms 43932 KB Output is correct
16 Correct 325 ms 43928 KB Output is correct
17 Correct 60 ms 12720 KB Output is correct
18 Correct 309 ms 35540 KB Output is correct
19 Correct 292 ms 35016 KB Output is correct
20 Correct 325 ms 35476 KB Output is correct
21 Correct 331 ms 36040 KB Output is correct
22 Correct 442 ms 39380 KB Output is correct
23 Correct 521 ms 39296 KB Output is correct
24 Correct 469 ms 38488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 11548 KB Output is correct
2 Correct 89 ms 12232 KB Output is correct
3 Correct 81 ms 12336 KB Output is correct
4 Correct 99 ms 12356 KB Output is correct
5 Correct 88 ms 12136 KB Output is correct
6 Correct 82 ms 12068 KB Output is correct
7 Correct 59 ms 11588 KB Output is correct
8 Correct 263 ms 31112 KB Output is correct
9 Correct 281 ms 31364 KB Output is correct
10 Correct 60 ms 11644 KB Output is correct
11 Correct 295 ms 38212 KB Output is correct
12 Correct 303 ms 38252 KB Output is correct
13 Correct 315 ms 38836 KB Output is correct
14 Correct 58 ms 11544 KB Output is correct
15 Correct 304 ms 30392 KB Output is correct
16 Correct 263 ms 30280 KB Output is correct
17 Correct 339 ms 30212 KB Output is correct
18 Correct 309 ms 30676 KB Output is correct
19 Correct 382 ms 35552 KB Output is correct
20 Correct 448 ms 34556 KB Output is correct
21 Correct 303 ms 30628 KB Output is correct
22 Correct 300 ms 31044 KB Output is correct
23 Correct 322 ms 30276 KB Output is correct
24 Correct 320 ms 30436 KB Output is correct
25 Correct 374 ms 34692 KB Output is correct
26 Correct 353 ms 30176 KB Output is correct
27 Correct 334 ms 30144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 11548 KB Output is correct
2 Correct 89 ms 12232 KB Output is correct
3 Correct 81 ms 12336 KB Output is correct
4 Correct 99 ms 12356 KB Output is correct
5 Correct 88 ms 12136 KB Output is correct
6 Correct 82 ms 12068 KB Output is correct
7 Correct 59 ms 11588 KB Output is correct
8 Correct 263 ms 31112 KB Output is correct
9 Correct 281 ms 31364 KB Output is correct
10 Correct 60 ms 11644 KB Output is correct
11 Correct 295 ms 38212 KB Output is correct
12 Correct 303 ms 38252 KB Output is correct
13 Correct 315 ms 38836 KB Output is correct
14 Correct 58 ms 11544 KB Output is correct
15 Correct 304 ms 30392 KB Output is correct
16 Correct 263 ms 30280 KB Output is correct
17 Correct 339 ms 30212 KB Output is correct
18 Correct 309 ms 30676 KB Output is correct
19 Correct 382 ms 35552 KB Output is correct
20 Correct 448 ms 34556 KB Output is correct
21 Correct 303 ms 30628 KB Output is correct
22 Correct 300 ms 31044 KB Output is correct
23 Correct 322 ms 30276 KB Output is correct
24 Correct 320 ms 30436 KB Output is correct
25 Correct 374 ms 34692 KB Output is correct
26 Correct 353 ms 30176 KB Output is correct
27 Correct 334 ms 30144 KB Output is correct
28 Correct 62 ms 11844 KB Output is correct
29 Correct 83 ms 13196 KB Output is correct
30 Correct 70 ms 13168 KB Output is correct
31 Correct 83 ms 13312 KB Output is correct
32 Correct 75 ms 13116 KB Output is correct
33 Correct 71 ms 12880 KB Output is correct
34 Correct 62 ms 11964 KB Output is correct
35 Correct 259 ms 32180 KB Output is correct
36 Correct 183 ms 32804 KB Output is correct
37 Correct 198 ms 34616 KB Output is correct
38 Correct 59 ms 12744 KB Output is correct
39 Correct 310 ms 43592 KB Output is correct
40 Correct 332 ms 44384 KB Output is correct
41 Correct 348 ms 44000 KB Output is correct
42 Correct 332 ms 44280 KB Output is correct
43 Correct 61 ms 12752 KB Output is correct
44 Correct 301 ms 35528 KB Output is correct
45 Correct 280 ms 34892 KB Output is correct
46 Correct 325 ms 35412 KB Output is correct
47 Correct 385 ms 36060 KB Output is correct
48 Correct 435 ms 39428 KB Output is correct
49 Correct 509 ms 39300 KB Output is correct
50 Correct 463 ms 38288 KB Output is correct
51 Correct 236 ms 35388 KB Output is correct
52 Correct 213 ms 35516 KB Output is correct
53 Correct 205 ms 35008 KB Output is correct
54 Correct 229 ms 36208 KB Output is correct
55 Correct 225 ms 35376 KB Output is correct
56 Correct 285 ms 34444 KB Output is correct
57 Correct 315 ms 38944 KB Output is correct
58 Correct 377 ms 35296 KB Output is correct
59 Correct 347 ms 34368 KB Output is correct