Submission #223431

# Submission time Handle Problem Language Result Execution time Memory
223431 2020-04-15T09:15:13 Z jamielim Klasika (COCI20_klasika) C++14
33 / 110
5000 ms 35960 KB
#include <bits/stdc++.h>
using namespace std;

const int MAXN=200005;
int n=1,q;
pair<int,pair<int,int> > qu[MAXN];
vector<pair<int,int> > adj[MAXN];

int sub[MAXN];
int depth[MAXN];
int heavy[MAXN];
int pos[MAXN];
int head[MAXN];
int par[MAXN][20];
int cur=1;
 
struct node{
	int s,e,m;long long v,lazy;
	node *l,*r;
	node(int S,int E){
		s=S;e=E;m=(s+e)/2;v=lazy=0;
		if(s!=e){
			l=new node(s,m);
			r=new node(m+1,e);
		}
	}
	void laze(){
		if(s==e){v^=lazy;lazy=0;return;}
		v^=lazy;
		l->lazy+=lazy;r->lazy+=lazy;
		lazy=0;return;
	}
	void upd(int x,int y,int nv){
		//printf("u %d %d\n",x,y);
		if(x>y)return;
		if((s==x&&e==y)||s==e){
			lazy^=nv;laze();return;
		}
		if(y<=m)l->upd(x,y,nv);
		else if(x>m)r->upd(x,y,nv);
		else{l->upd(x,m,nv);r->upd(m+1,y,nv);}
		laze();
		v=l->v^r->v;
	}
	long long qry(int x,int y){
		//printf("q %d %d\n",x,y);
		if(x>y)return 0;
		laze();
		if(s==x&&e==y)return v;
		if(y<=m)return l->qry(x,y);
		if(x>m)return r->qry(x,y);
		return l->qry(x,m)^r->qry(m+1,y);
	}
}*root;
 
void dfs(int x,int p){
	sub[x]=1;par[x][0]=p;int maxc=0;
	for(int i=0;i<(int)adj[x].size();i++){
		if(adj[x][i].first!=p){
			depth[adj[x][i].first]=depth[x]+1;
			dfs(adj[x][i].first,x);
			sub[x]+=sub[adj[x][i].first];
			if(sub[adj[x][i].first]>maxc){maxc=sub[adj[x][i].first];heavy[x]=adj[x][i].first;}
		}
	}
}
void dcmp(int x,int h){
	head[x]=h;pos[x]=cur++;
	if(heavy[x]!=-1)dcmp(heavy[x],h);
	for(int i=0;i<(int)adj[x].size();i++){
		if(heavy[x]==adj[x][i].first)continue;
		if(par[x][0]==adj[x][i].first)continue;
		dcmp(adj[x][i].first,adj[x][i].first);
	}
}
void upd(int a,int b,int nv){
	//printf("%d %d\n",a,b);
	if(depth[a]>depth[b])swap(a,b);
	for(;head[a]!=head[b];b=par[head[b]][0]){
		if(depth[head[a]]>depth[head[b]])swap(a,b);
		root->upd(pos[head[b]],pos[b],nv);
	}
	if(depth[a]>depth[b])swap(a,b);
	root->upd(pos[a]+1,pos[b],nv);
}
int qry(int a,int b){
	int res=0;
	if(depth[a]>depth[b])swap(a,b);
	for(;head[a]!=head[b];b=par[head[b]][0]){
		if(depth[head[a]]>depth[head[b]])swap(a,b);
		res^=root->qry(pos[head[b]],pos[b]);
	}
	if(depth[a]>depth[b])swap(a,b);
	res^=root->qry(pos[a]+1,pos[b]);
	return res;
}
int main(){
	scanf("%d",&q);
	char op[15];int a,b;
	for(int i=0;i<q;i++){
		scanf("%s%d%d",op,&a,&b);
		if(op[0]=='A'){
			n++;
			qu[i]=make_pair(n,make_pair(a,b));
			adj[n].push_back(make_pair(a,b));
			adj[a].push_back(make_pair(n,b));
		}else qu[i]=make_pair(-1,make_pair(a,b));
	}
	root=new node(0,n);
	memset(heavy,-1,sizeof(heavy));memset(par,-1,sizeof(par));
	dfs(1,-1);
	dcmp(1,1);
	for(int i=0;i<q;i++){
		if(qu[i].first<0){
			int ans=0;
			for(int j=1;j<=n;j++){
				if(pos[j]>=pos[qu[i].second.second]&&pos[j]<pos[qu[i].second.second]+sub[qu[i].second.second]){
					ans=max(ans,qry(qu[i].second.first,j));
				}
			}
			printf("%d\n",ans);
		}else{
			upd(qu[i].second.first,qu[i].first,qu[i].second.second);
		}
	}
}

Compilation message

klasika.cpp: In function 'int main()':
klasika.cpp:98:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&q);
  ~~~~~^~~~~~~~~
klasika.cpp:101:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s%d%d",op,&a,&b);
   ~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 17 ms 21504 KB Output is correct
2 Correct 17 ms 21504 KB Output is correct
3 Correct 17 ms 21504 KB Output is correct
4 Correct 18 ms 21504 KB Output is correct
5 Correct 16 ms 21504 KB Output is correct
6 Correct 19 ms 21504 KB Output is correct
7 Correct 17 ms 21504 KB Output is correct
8 Correct 16 ms 21504 KB Output is correct
9 Correct 16 ms 21504 KB Output is correct
10 Correct 17 ms 21504 KB Output is correct
11 Correct 18 ms 21504 KB Output is correct
12 Correct 17 ms 21504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 21504 KB Output is correct
2 Correct 17 ms 21504 KB Output is correct
3 Correct 17 ms 21504 KB Output is correct
4 Correct 18 ms 21504 KB Output is correct
5 Correct 16 ms 21504 KB Output is correct
6 Correct 19 ms 21504 KB Output is correct
7 Correct 17 ms 21504 KB Output is correct
8 Correct 16 ms 21504 KB Output is correct
9 Correct 16 ms 21504 KB Output is correct
10 Correct 17 ms 21504 KB Output is correct
11 Correct 18 ms 21504 KB Output is correct
12 Correct 17 ms 21504 KB Output is correct
13 Correct 115 ms 21632 KB Output is correct
14 Correct 186 ms 21880 KB Output is correct
15 Correct 192 ms 21888 KB Output is correct
16 Correct 143 ms 22008 KB Output is correct
17 Correct 32 ms 21632 KB Output is correct
18 Correct 39 ms 21632 KB Output is correct
19 Correct 31 ms 21760 KB Output is correct
20 Correct 27 ms 21888 KB Output is correct
21 Correct 60 ms 21632 KB Output is correct
22 Correct 74 ms 21752 KB Output is correct
23 Correct 90 ms 21760 KB Output is correct
24 Correct 62 ms 21888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5076 ms 35960 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 21504 KB Output is correct
2 Correct 17 ms 21504 KB Output is correct
3 Correct 17 ms 21504 KB Output is correct
4 Correct 18 ms 21504 KB Output is correct
5 Correct 16 ms 21504 KB Output is correct
6 Correct 19 ms 21504 KB Output is correct
7 Correct 17 ms 21504 KB Output is correct
8 Correct 16 ms 21504 KB Output is correct
9 Correct 16 ms 21504 KB Output is correct
10 Correct 17 ms 21504 KB Output is correct
11 Correct 18 ms 21504 KB Output is correct
12 Correct 17 ms 21504 KB Output is correct
13 Correct 115 ms 21632 KB Output is correct
14 Correct 186 ms 21880 KB Output is correct
15 Correct 192 ms 21888 KB Output is correct
16 Correct 143 ms 22008 KB Output is correct
17 Correct 32 ms 21632 KB Output is correct
18 Correct 39 ms 21632 KB Output is correct
19 Correct 31 ms 21760 KB Output is correct
20 Correct 27 ms 21888 KB Output is correct
21 Correct 60 ms 21632 KB Output is correct
22 Correct 74 ms 21752 KB Output is correct
23 Correct 90 ms 21760 KB Output is correct
24 Correct 62 ms 21888 KB Output is correct
25 Execution timed out 5076 ms 35960 KB Time limit exceeded
26 Halted 0 ms 0 KB -