Submission #334836

# Submission time Handle Problem Language Result Execution time Memory
334836 2020-12-10T05:03:14 Z amunduzbaev Birthday gift (IZhO18_treearray) C++14
12 / 100
4000 ms 620 KB
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define ll long long 
#define ld long double 
#define pii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define prc(n) fixed << setprecision(n)
#define fastios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pi acos(-1);
const int inf = 1e9+7;
const int N = 2000;

int n, m, q, a[N], par[N], used[N], s, dis[N], fir[N];
vector<int>edges[N];
vector<pii>tree;
vector<int>order;

// ff = min
// ss = which

void build(int lx, int rx, int x){
	if(lx == rx){
		if(lx > sz(order)) return;
		tree[x].ff = dis[order[lx-1]];
		tree[x].ss = order[lx-1];;
		return;
	}
	int m = (lx + rx)/2;
	build(lx, m, x*2);
	build(m+1, rx, x*2+1);
	if(tree[x*2].ff <= tree[x*2+1].ff) tree[x] = tree[x*2];
	else tree[x] = tree[x*2+1];
}

pii find(int l, int r, int lx, int rx, int x){
	if(lx > r || rx < l) return {inf, inf};
	if(lx >= l && rx <= r) return tree[x];
	int m = (lx + rx)/2;
	pii res1 = find(l, r, lx, m, x*2);
	pii res2 = find(l, r, m+1, rx, x*2+1);
	return (res1.ff < res2.ff ? res1 : res2);
}


void dfs(int u, int p, int d){
	order.pb(u);
	dis[u] = d;
	if(fir[u] == -1) fir[u] = sz(order);
	for(auto x:edges[u]){
		if(x == p) continue;
		dfs(x, u, d+1);
		order.pb(u);
	}
}


void solve(){
	cin>>n>>m>>q;
	for(int i=1;i<n;i++){
		int a, b;
		cin>>a>>b;
		edges[a].pb(b);
		edges[b].pb(a);
	}
	
	memset(fir, -1, sizeof(fir));
	dfs(1, 0, 0);
	s = 2;
	while(s < sz(order)) s*=2;
	tree.assign(2*s, {inf, inf});
	build(1, s, 1);
	for(int i=1;i<=m;i++) cin>>a[i];
	
	while(q--){
		int t;
		cin>>t;
		if(t == 1){
			int pos, val;
			cin>>pos>>val;
			a[pos] = val;
		}
		else{
			int l, r, val, rr;
			cin>>l>>r>>val;
			int la = -1, ra = -1;
			for(;l<= r; l++){
				int prev = -1;
				for(rr = l; rr <= r; rr++){
					int lx = min(fir[a[l]], fir[a[rr]]), rx = max(fir[a[l]], fir[a[rr]]);
					int res = find(lx, rx, 1, s, 1).ss;
					if(rr - l){
						lx = min(fir[prev], fir[res]), rx = max(fir[prev], fir[res]);
						res = find(lx, rx, 1, s, 1).ss;
					}
					//cout<<lx<<" "<<rx<<" "<<l<<" "<<rr<<" "<<res.ss<<endl;
					if(res == val){
						if(rr - l +1 >= ra - la +1) la = l, ra = rr;
					}
					prev = res;
				}
			}
			cout<<la<<" "<<ra<<"\n";
		}
	}
	
	return;
}

int main(){
	fastios
	int t = 1;
	//cin>>t;
	while(t--) solve();
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB n=5
2 Correct 16 ms 364 KB n=100
3 Correct 19 ms 364 KB n=100
4 Correct 17 ms 364 KB n=100
5 Correct 15 ms 364 KB n=100
6 Correct 96 ms 364 KB n=100
7 Correct 103 ms 364 KB n=100
8 Correct 12 ms 364 KB n=100
9 Correct 9 ms 364 KB n=100
10 Correct 50 ms 364 KB n=100
11 Correct 50 ms 364 KB n=100
12 Correct 10 ms 364 KB n=100
13 Correct 10 ms 364 KB n=100
14 Correct 11 ms 364 KB n=100
15 Correct 8 ms 364 KB n=100
16 Correct 14 ms 364 KB n=100
17 Correct 23 ms 364 KB n=100
18 Correct 12 ms 364 KB n=100
19 Correct 13 ms 492 KB n=100
20 Correct 13 ms 364 KB n=100
21 Correct 14 ms 364 KB n=100
22 Correct 12 ms 364 KB n=100
23 Correct 19 ms 364 KB n=100
24 Correct 22 ms 364 KB n=100
25 Correct 36 ms 364 KB n=100
26 Correct 2 ms 364 KB n=12
27 Correct 33 ms 364 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB n=5
2 Correct 16 ms 364 KB n=100
3 Correct 19 ms 364 KB n=100
4 Correct 17 ms 364 KB n=100
5 Correct 15 ms 364 KB n=100
6 Correct 96 ms 364 KB n=100
7 Correct 103 ms 364 KB n=100
8 Correct 12 ms 364 KB n=100
9 Correct 9 ms 364 KB n=100
10 Correct 50 ms 364 KB n=100
11 Correct 50 ms 364 KB n=100
12 Correct 10 ms 364 KB n=100
13 Correct 10 ms 364 KB n=100
14 Correct 11 ms 364 KB n=100
15 Correct 8 ms 364 KB n=100
16 Correct 14 ms 364 KB n=100
17 Correct 23 ms 364 KB n=100
18 Correct 12 ms 364 KB n=100
19 Correct 13 ms 492 KB n=100
20 Correct 13 ms 364 KB n=100
21 Correct 14 ms 364 KB n=100
22 Correct 12 ms 364 KB n=100
23 Correct 19 ms 364 KB n=100
24 Correct 22 ms 364 KB n=100
25 Correct 36 ms 364 KB n=100
26 Correct 2 ms 364 KB n=12
27 Correct 33 ms 364 KB n=100
28 Correct 1764 ms 620 KB n=500
29 Correct 2222 ms 620 KB n=500
30 Correct 1836 ms 524 KB n=500
31 Correct 1993 ms 528 KB n=500
32 Correct 2028 ms 512 KB n=500
33 Correct 2008 ms 620 KB n=500
34 Correct 1986 ms 620 KB n=500
35 Correct 2095 ms 536 KB n=500
36 Execution timed out 4081 ms 492 KB Time limit exceeded
37 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB n=5
2 Correct 16 ms 364 KB n=100
3 Correct 19 ms 364 KB n=100
4 Correct 17 ms 364 KB n=100
5 Correct 15 ms 364 KB n=100
6 Correct 96 ms 364 KB n=100
7 Correct 103 ms 364 KB n=100
8 Correct 12 ms 364 KB n=100
9 Correct 9 ms 364 KB n=100
10 Correct 50 ms 364 KB n=100
11 Correct 50 ms 364 KB n=100
12 Correct 10 ms 364 KB n=100
13 Correct 10 ms 364 KB n=100
14 Correct 11 ms 364 KB n=100
15 Correct 8 ms 364 KB n=100
16 Correct 14 ms 364 KB n=100
17 Correct 23 ms 364 KB n=100
18 Correct 12 ms 364 KB n=100
19 Correct 13 ms 492 KB n=100
20 Correct 13 ms 364 KB n=100
21 Correct 14 ms 364 KB n=100
22 Correct 12 ms 364 KB n=100
23 Correct 19 ms 364 KB n=100
24 Correct 22 ms 364 KB n=100
25 Correct 36 ms 364 KB n=100
26 Correct 2 ms 364 KB n=12
27 Correct 33 ms 364 KB n=100
28 Correct 1764 ms 620 KB n=500
29 Correct 2222 ms 620 KB n=500
30 Correct 1836 ms 524 KB n=500
31 Correct 1993 ms 528 KB n=500
32 Correct 2028 ms 512 KB n=500
33 Correct 2008 ms 620 KB n=500
34 Correct 1986 ms 620 KB n=500
35 Correct 2095 ms 536 KB n=500
36 Execution timed out 4081 ms 492 KB Time limit exceeded
37 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB n=5
2 Correct 16 ms 364 KB n=100
3 Correct 19 ms 364 KB n=100
4 Correct 17 ms 364 KB n=100
5 Correct 15 ms 364 KB n=100
6 Correct 96 ms 364 KB n=100
7 Correct 103 ms 364 KB n=100
8 Correct 12 ms 364 KB n=100
9 Correct 9 ms 364 KB n=100
10 Correct 50 ms 364 KB n=100
11 Correct 50 ms 364 KB n=100
12 Correct 10 ms 364 KB n=100
13 Correct 10 ms 364 KB n=100
14 Correct 11 ms 364 KB n=100
15 Correct 8 ms 364 KB n=100
16 Correct 14 ms 364 KB n=100
17 Correct 23 ms 364 KB n=100
18 Correct 12 ms 364 KB n=100
19 Correct 13 ms 492 KB n=100
20 Correct 13 ms 364 KB n=100
21 Correct 14 ms 364 KB n=100
22 Correct 12 ms 364 KB n=100
23 Correct 19 ms 364 KB n=100
24 Correct 22 ms 364 KB n=100
25 Correct 36 ms 364 KB n=100
26 Correct 2 ms 364 KB n=12
27 Correct 33 ms 364 KB n=100
28 Correct 1764 ms 620 KB n=500
29 Correct 2222 ms 620 KB n=500
30 Correct 1836 ms 524 KB n=500
31 Correct 1993 ms 528 KB n=500
32 Correct 2028 ms 512 KB n=500
33 Correct 2008 ms 620 KB n=500
34 Correct 1986 ms 620 KB n=500
35 Correct 2095 ms 536 KB n=500
36 Execution timed out 4081 ms 492 KB Time limit exceeded
37 Halted 0 ms 0 KB -