답안 #201098

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
201098 2020-02-09T10:47:24 Z mraron Klasika (COCI20_klasika) C++14
33 / 110
1094 ms 216696 KB
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cassert>
#include<cassert>
#include<unordered_map>
#include<unordered_set>
#include<functional>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<sstream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<numeric>
#include<random>
#include<chrono>
#include<bitset>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define xx first
#define yy second
#define sz(x) (int)(x).size()
#define gc getchar
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mp make_pair

#ifndef ONLINE_JUDGE
#  define LOG(x) (cerr << #x << " = " << (x) << endl)
#else
#  define LOG(x) ((void)0)
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const double PI=3.1415926535897932384626433832795;
const ll INF = 1LL<<62;
const ll MINF = -1LL<<62;

template<typename T> T getint() {
	T val=0;
	char c;
	
	bool neg=false;
	while((c=gc()) && !(c>='0' && c<='9')) {
		neg|=c=='-';
	}

	do {
		val=(val*10)+c-'0';
	} while((c=gc()) && (c>='0' && c<='9'));

	return val*(neg?-1:1);
}

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int>(0, n-1)(rng)


int q;

struct op {
	int typ,a,b;
};

const int MAXN=200001;

int n=1;
vector<op> lst;
vector<pair<int,int>> adj[MAXN];

int sz[MAXN], par[MAXN], d[MAXN];
void dfs_pre(int x) {
	sz[x]=1;
	for(auto i:adj[x]) {
		if(!sz[i.xx]) {
			par[i.xx]=x;
			d[i.xx]=i.yy^d[x];
			dfs_pre(i.xx);
			sz[x]+=sz[i.xx];
		}
	}
}

int hld_top[MAXN], hld_id[MAXN], hld_pos[MAXN], pos=0, id=1;
void dfs_hld(int x) {
//	cerr<<x<<" "<<pos<<"\n";
	hld_id[x]=id;
	hld_pos[x]=pos++;
	if(!hld_top[hld_id[x]]) {
		hld_top[hld_id[x]]=x;
	}
	
	int best=0;
	for(auto i:adj[x]) {
		if(par[x]==i.xx) continue ;
		if(!best || sz[best]<sz[i.xx]) best=i.xx;
	}
	
	if(best>0) dfs_hld(best);
	for(auto i:adj[x]) {
		if(par[x]!=i.xx && i.xx!=best) {
			id++;
			dfs_hld(i.xx);
		}	
	}
}


struct node {
	int sub[2];
	node() {
		sub[0]=sub[1]=0;
	}
} trie[30*4*MAXN];
int nxt=1;

void insert(int root, int num) {
	for(int i=30;i>=0;i--) {
		int c=(num&(1<<i))!=0;
		if(trie[root].sub[c]==0) {
			trie[root].sub[c]=nxt++;
		}
		
		root=trie[root].sub[c];
	}
}

int mx(int root, int with) {
	if(root==0) return 0;
	if(trie[root].sub[0]==0 && trie[root].sub[1]==0) return 0;

	
	int ans=0;
	for(int i=30;i>=0;i--) {
		int c=(with&(1<<i))!=0;
		if(trie[root].sub[1-c]!=0) {
			ans|=1<<i;
			root=trie[root].sub[1-c];
		}else {
			root=trie[root].sub[c];
		}
	}
	
	return ans;
}

int tree[4*MAXN];

void add(int ind, int L, int R, int i, int j, int mit) {
	//cerr<<ind<<" "<<L<<" "<<R<<" "<<i<<" "<<j<<" "<<mit<<"add\n"<<flush;
	if(R<i || j<L) return ;
	if(i<=L && R<=j) {
		if(tree[ind]==0) tree[ind]=nxt++;
		insert(tree[ind], mit);
		return ;
	}
	
	add(2*ind, L, (L+R)/2, i, j, mit);
	add(2*ind+1, (L+R)/2+1, R, i, j, mit);
}

int mx(int ind, int L, int R, int i, int with) {
	//cerr<<ind<<" "<<L<<" "<<R<<" "<<i<<" "<<i<<" "<<with<<"mx\n"<<flush;

	if(R<i || i<L) return 0;
	
	int ans=mx(tree[ind], with);
	if(L!=R) ans=max(ans, max(mx(2*ind, L, (L+R)/2, i, with), mx(2*ind+1, (L+R)/2+1, R, i, with)));
	
	return ans;
}

void add(int root, int dd) {
	while(root>0) {
		int blk=hld_id[root];
		int tp=hld_top[blk];
		add(1,0,pos-1,hld_pos[tp],hld_pos[root],dd);
		root=par[tp];
	}
}

int query(int a, int b) {
	int base=d[a];
	//cerr<<base<<"\n";
	//cerr<<hld_pos[b]<<"\n";
	return mx(1,0,pos-1,hld_pos[b],base);
}

int main() {
	IO;
	cin>>q;
	lst.resize(q);
	for(int i=0;i<q;++i) {
		string t;
		cin>>t;
		lst[i].typ=(t=="Add"?0:1);
		cin>>lst[i].a>>lst[i].b;
		
		if(lst[i].typ==0) {
			adj[lst[i].a].push_back({n+1, lst[i].b});
			adj[n+1].push_back({lst[i].a, lst[i].b});
			lst[i].a=n+1;
			
			n++;
		}
	}
	
	dfs_pre(1);
	dfs_hld(1);
	
	add(1,0,pos-1,0,0,0);
	for(auto i:lst) {
		if(i.typ==0) {
			//cerr<<"typ0\n";
			add(i.a, d[i.a]);
		}else {
			//cerr<<"typ1\n";
			cout<<query(i.a,i.b)<<"\n";
		}
	}
	
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 118 ms 193096 KB Output is correct
2 Correct 116 ms 192920 KB Output is correct
3 Correct 119 ms 193016 KB Output is correct
4 Correct 114 ms 193016 KB Output is correct
5 Correct 119 ms 192888 KB Output is correct
6 Correct 112 ms 193016 KB Output is correct
7 Correct 114 ms 192888 KB Output is correct
8 Correct 114 ms 192888 KB Output is correct
9 Correct 118 ms 193016 KB Output is correct
10 Correct 112 ms 192888 KB Output is correct
11 Correct 113 ms 192888 KB Output is correct
12 Correct 116 ms 192888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 118 ms 193096 KB Output is correct
2 Correct 116 ms 192920 KB Output is correct
3 Correct 119 ms 193016 KB Output is correct
4 Correct 114 ms 193016 KB Output is correct
5 Correct 119 ms 192888 KB Output is correct
6 Correct 112 ms 193016 KB Output is correct
7 Correct 114 ms 192888 KB Output is correct
8 Correct 114 ms 192888 KB Output is correct
9 Correct 118 ms 193016 KB Output is correct
10 Correct 112 ms 192888 KB Output is correct
11 Correct 113 ms 192888 KB Output is correct
12 Correct 116 ms 192888 KB Output is correct
13 Correct 118 ms 193020 KB Output is correct
14 Correct 119 ms 193016 KB Output is correct
15 Correct 115 ms 193144 KB Output is correct
16 Correct 120 ms 193272 KB Output is correct
17 Correct 119 ms 192968 KB Output is correct
18 Correct 118 ms 193016 KB Output is correct
19 Correct 119 ms 193016 KB Output is correct
20 Correct 119 ms 193144 KB Output is correct
21 Correct 118 ms 193016 KB Output is correct
22 Correct 118 ms 193016 KB Output is correct
23 Correct 111 ms 193016 KB Output is correct
24 Correct 121 ms 193016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1078 ms 201692 KB Output is correct
2 Correct 1094 ms 205688 KB Output is correct
3 Correct 1000 ms 212344 KB Output is correct
4 Incorrect 664 ms 216696 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 118 ms 193096 KB Output is correct
2 Correct 116 ms 192920 KB Output is correct
3 Correct 119 ms 193016 KB Output is correct
4 Correct 114 ms 193016 KB Output is correct
5 Correct 119 ms 192888 KB Output is correct
6 Correct 112 ms 193016 KB Output is correct
7 Correct 114 ms 192888 KB Output is correct
8 Correct 114 ms 192888 KB Output is correct
9 Correct 118 ms 193016 KB Output is correct
10 Correct 112 ms 192888 KB Output is correct
11 Correct 113 ms 192888 KB Output is correct
12 Correct 116 ms 192888 KB Output is correct
13 Correct 118 ms 193020 KB Output is correct
14 Correct 119 ms 193016 KB Output is correct
15 Correct 115 ms 193144 KB Output is correct
16 Correct 120 ms 193272 KB Output is correct
17 Correct 119 ms 192968 KB Output is correct
18 Correct 118 ms 193016 KB Output is correct
19 Correct 119 ms 193016 KB Output is correct
20 Correct 119 ms 193144 KB Output is correct
21 Correct 118 ms 193016 KB Output is correct
22 Correct 118 ms 193016 KB Output is correct
23 Correct 111 ms 193016 KB Output is correct
24 Correct 121 ms 193016 KB Output is correct
25 Correct 1078 ms 201692 KB Output is correct
26 Correct 1094 ms 205688 KB Output is correct
27 Correct 1000 ms 212344 KB Output is correct
28 Incorrect 664 ms 216696 KB Output isn't correct
29 Halted 0 ms 0 KB -