Submission #938705

# Submission time Handle Problem Language Result Execution time Memory
938705 2024-03-05T12:57:09 Z Baizho Birthday gift (IZhO18_treearray) C++14
100 / 100
1022 ms 84920 KB
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
  
#define ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
 
// #pragma GCC optimize("Ofast,unroll-loops,fast-math")
// #pragma GCC target("popcnt,sse3,avx")
 
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pll;
 
#define sz size()
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define pb push_back
 
const int mod = ll(1e9)+7; //(b + (a%b)) % b (to mod -1%(10^9+7) correctly in c++ its -1 but its suppose to be 10^9+6
const ll MOD = 998244353;  // (a%mod)*(binpow(b,mod-2,mod) = (a/b)%mod
const int N = ll(2e5)+100;
const int M = ll(2e5) + 100;
const long long inf = 2e18;
//const long double eps = 1e-15L;
 
ll lcm(ll a, ll b) { return (a / __gcd(a,b))*b; }
 
ll binpow(ll a, ll b, ll m) { ll res=1; a %= m; while(b>0){ if(b&1)res=(res * a) % m; a=(a * a) % m; b/=2; } return res%m;}
 
void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
 
int n, m, q, a[N], d[N], up[21][N];
vector<int> g[N];
set<int> pos[N], pos1[N];

void dfs(int v, int p = 0) {
	d[v] = d[p] + 1;
	up[0][v] = p;
	for(int j = 1; j <= 20; j ++) up[j][v] = up[j - 1][up[j - 1][v]];
	for(auto to : g[v]) {
		if(to == p) continue;
		dfs(to, v);
	}
}

int lca(int a, int b) {
	if(d[a] < d[b]) swap(a, b);
	for(int j = 20; j >= 0; j --) {
		if((d[a] - d[b]) & (1 << j)) {
			a = up[j][a];
		}
	}
	if(a == b) return a;
	for(int j = 20; j >= 0; j --) {
		if(up[j][a] != up[j][b]) {
			a = up[j][a], b = up[j][b];
		}
	}
	return up[0][a];
}

void Baizho() {
	cin>>n>>m>>q;
	for(int i = 1; i <= n - 1; i ++) {
		int a, b; cin>>a>>b;
		g[a].pb(b);
		g[b].pb(a);
	}
	dfs(1);
	for(int i = 1; i <= m; i ++) {
		cin>>a[i];
		pos1[a[i]].insert(i);
		if(i > 1) {
			pos[lca(a[i], a[i - 1])].insert(i);
		}
	}
	while(q --) {
		int t; cin>>t;
		if(t == 1) {
			int p, v; cin>>p>>v;
			if(p > 1) pos[lca(a[p], a[p - 1])].erase(p);
			if(p < m) pos[lca(a[p], a[p + 1])].erase(p + 1);
			pos1[a[p]].erase(p);
			a[p] = v;
			pos1[a[p]].insert(p);
			if(p > 1) pos[lca(a[p], a[p - 1])].insert(p);
			if(p < m) pos[lca(a[p], a[p + 1])].insert(p + 1);
		} else {
			int l, r, v; cin>>l>>r>>v;
			if(!pos1[v].empty() && *pos1[v].begin() <= r) {
				auto tr = -- pos1[v].lower_bound(r + 1);
				if(*tr >= l) {
					cout<<*tr<<" "<<*tr<<"\n";
					continue; 
				}
			}
			l ++;
			if(!pos[v].empty() && *pos[v].begin() <= r) {
				auto tr = -- pos[v].lower_bound(r + 1);
				if(*tr >= l) {
					cout<<*tr - 1<<" "<<*tr<<"\n";
					continue; 
				}
			}
			cout<<"-1 -1\n";
		}
	}
}
 
signed main() {		
// 	Freopen("nondec");
    ios_base::sync_with_stdio(false);   
    cin.tie(0);cout.tie(0); 
//   	precalc();
   	
    int ttt = 1;
//    cin>>ttt;
 
    for(int i=1; i<=ttt; i++) {Baizho(); }
}

Compilation message

treearray.cpp: In function 'void Freopen(std::string)':
treearray.cpp:35:34: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 | void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
      |                           ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:35:76: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 | void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
      |                                                                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 39516 KB n=5
2 Correct 7 ms 39516 KB n=100
3 Correct 7 ms 39516 KB n=100
4 Correct 7 ms 39516 KB n=100
5 Correct 7 ms 39548 KB n=100
6 Correct 7 ms 39516 KB n=100
7 Correct 7 ms 39608 KB n=100
8 Correct 7 ms 39512 KB n=100
9 Correct 8 ms 39768 KB n=100
10 Correct 8 ms 39768 KB n=100
11 Correct 7 ms 39516 KB n=100
12 Correct 7 ms 39512 KB n=100
13 Correct 10 ms 39516 KB n=100
14 Correct 8 ms 39620 KB n=100
15 Correct 8 ms 39516 KB n=100
16 Correct 7 ms 39516 KB n=100
17 Correct 8 ms 39640 KB n=100
18 Correct 8 ms 39516 KB n=100
19 Correct 7 ms 39516 KB n=100
20 Correct 7 ms 39516 KB n=100
21 Correct 7 ms 39512 KB n=100
22 Correct 7 ms 39512 KB n=100
23 Correct 7 ms 39516 KB n=100
24 Correct 7 ms 39516 KB n=100
25 Correct 7 ms 39592 KB n=100
26 Correct 8 ms 39516 KB n=12
27 Correct 7 ms 39512 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 9 ms 39516 KB n=5
2 Correct 7 ms 39516 KB n=100
3 Correct 7 ms 39516 KB n=100
4 Correct 7 ms 39516 KB n=100
5 Correct 7 ms 39548 KB n=100
6 Correct 7 ms 39516 KB n=100
7 Correct 7 ms 39608 KB n=100
8 Correct 7 ms 39512 KB n=100
9 Correct 8 ms 39768 KB n=100
10 Correct 8 ms 39768 KB n=100
11 Correct 7 ms 39516 KB n=100
12 Correct 7 ms 39512 KB n=100
13 Correct 10 ms 39516 KB n=100
14 Correct 8 ms 39620 KB n=100
15 Correct 8 ms 39516 KB n=100
16 Correct 7 ms 39516 KB n=100
17 Correct 8 ms 39640 KB n=100
18 Correct 8 ms 39516 KB n=100
19 Correct 7 ms 39516 KB n=100
20 Correct 7 ms 39516 KB n=100
21 Correct 7 ms 39512 KB n=100
22 Correct 7 ms 39512 KB n=100
23 Correct 7 ms 39516 KB n=100
24 Correct 7 ms 39516 KB n=100
25 Correct 7 ms 39592 KB n=100
26 Correct 8 ms 39516 KB n=12
27 Correct 7 ms 39512 KB n=100
28 Correct 7 ms 39516 KB n=500
29 Correct 7 ms 39516 KB n=500
30 Correct 8 ms 39724 KB n=500
31 Correct 9 ms 39516 KB n=500
32 Correct 8 ms 39516 KB n=500
33 Correct 9 ms 39600 KB n=500
34 Correct 8 ms 39516 KB n=500
35 Correct 9 ms 39768 KB n=500
36 Correct 7 ms 39512 KB n=500
37 Correct 8 ms 39516 KB n=500
38 Correct 8 ms 39516 KB n=500
39 Correct 7 ms 39516 KB n=500
40 Correct 7 ms 39516 KB n=500
41 Correct 8 ms 39516 KB n=500
42 Correct 7 ms 39516 KB n=500
43 Correct 8 ms 39540 KB n=500
44 Correct 8 ms 39512 KB n=500
45 Correct 8 ms 39516 KB n=500
46 Correct 8 ms 39516 KB n=500
47 Correct 8 ms 39516 KB n=500
48 Correct 8 ms 39516 KB n=500
49 Correct 7 ms 39512 KB n=500
50 Correct 8 ms 39616 KB n=500
51 Correct 8 ms 39516 KB n=500
52 Correct 7 ms 39516 KB n=500
53 Correct 8 ms 39516 KB n=500
54 Correct 8 ms 39516 KB n=500
55 Correct 7 ms 39512 KB n=278
56 Correct 7 ms 39512 KB n=500
57 Correct 7 ms 39512 KB n=500
58 Correct 9 ms 39512 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 9 ms 39516 KB n=5
2 Correct 7 ms 39516 KB n=100
3 Correct 7 ms 39516 KB n=100
4 Correct 7 ms 39516 KB n=100
5 Correct 7 ms 39548 KB n=100
6 Correct 7 ms 39516 KB n=100
7 Correct 7 ms 39608 KB n=100
8 Correct 7 ms 39512 KB n=100
9 Correct 8 ms 39768 KB n=100
10 Correct 8 ms 39768 KB n=100
11 Correct 7 ms 39516 KB n=100
12 Correct 7 ms 39512 KB n=100
13 Correct 10 ms 39516 KB n=100
14 Correct 8 ms 39620 KB n=100
15 Correct 8 ms 39516 KB n=100
16 Correct 7 ms 39516 KB n=100
17 Correct 8 ms 39640 KB n=100
18 Correct 8 ms 39516 KB n=100
19 Correct 7 ms 39516 KB n=100
20 Correct 7 ms 39516 KB n=100
21 Correct 7 ms 39512 KB n=100
22 Correct 7 ms 39512 KB n=100
23 Correct 7 ms 39516 KB n=100
24 Correct 7 ms 39516 KB n=100
25 Correct 7 ms 39592 KB n=100
26 Correct 8 ms 39516 KB n=12
27 Correct 7 ms 39512 KB n=100
28 Correct 7 ms 39516 KB n=500
29 Correct 7 ms 39516 KB n=500
30 Correct 8 ms 39724 KB n=500
31 Correct 9 ms 39516 KB n=500
32 Correct 8 ms 39516 KB n=500
33 Correct 9 ms 39600 KB n=500
34 Correct 8 ms 39516 KB n=500
35 Correct 9 ms 39768 KB n=500
36 Correct 7 ms 39512 KB n=500
37 Correct 8 ms 39516 KB n=500
38 Correct 8 ms 39516 KB n=500
39 Correct 7 ms 39516 KB n=500
40 Correct 7 ms 39516 KB n=500
41 Correct 8 ms 39516 KB n=500
42 Correct 7 ms 39516 KB n=500
43 Correct 8 ms 39540 KB n=500
44 Correct 8 ms 39512 KB n=500
45 Correct 8 ms 39516 KB n=500
46 Correct 8 ms 39516 KB n=500
47 Correct 8 ms 39516 KB n=500
48 Correct 8 ms 39516 KB n=500
49 Correct 7 ms 39512 KB n=500
50 Correct 8 ms 39616 KB n=500
51 Correct 8 ms 39516 KB n=500
52 Correct 7 ms 39516 KB n=500
53 Correct 8 ms 39516 KB n=500
54 Correct 8 ms 39516 KB n=500
55 Correct 7 ms 39512 KB n=278
56 Correct 7 ms 39512 KB n=500
57 Correct 7 ms 39512 KB n=500
58 Correct 9 ms 39512 KB n=500
59 Correct 9 ms 39768 KB n=2000
60 Correct 10 ms 40028 KB n=2000
61 Correct 9 ms 39772 KB n=2000
62 Correct 10 ms 39964 KB n=2000
63 Correct 9 ms 39772 KB n=2000
64 Correct 10 ms 39772 KB n=2000
65 Correct 10 ms 39768 KB n=2000
66 Correct 10 ms 40024 KB n=2000
67 Correct 10 ms 39768 KB n=2000
68 Correct 9 ms 39772 KB n=2000
69 Correct 9 ms 39768 KB n=2000
70 Correct 10 ms 39772 KB n=2000
71 Correct 9 ms 39876 KB n=2000
72 Correct 9 ms 39772 KB n=2000
73 Correct 9 ms 39772 KB n=2000
74 Correct 9 ms 40136 KB n=1844
75 Correct 11 ms 39952 KB n=2000
76 Correct 10 ms 39768 KB n=2000
77 Correct 10 ms 39772 KB n=2000
78 Correct 10 ms 39772 KB n=2000
79 Correct 10 ms 39768 KB n=2000
80 Correct 10 ms 40024 KB n=2000
81 Correct 11 ms 39772 KB n=2000
82 Correct 10 ms 39768 KB n=2000
83 Correct 9 ms 40024 KB n=2000
84 Correct 10 ms 39768 KB n=2000
85 Correct 10 ms 39772 KB n=2000
86 Correct 10 ms 39772 KB n=2000
87 Correct 10 ms 39772 KB n=2000
88 Correct 10 ms 40028 KB n=2000
89 Correct 11 ms 40056 KB n=2000
90 Correct 12 ms 40028 KB n=2000
91 Correct 12 ms 39756 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 9 ms 39516 KB n=5
2 Correct 7 ms 39516 KB n=100
3 Correct 7 ms 39516 KB n=100
4 Correct 7 ms 39516 KB n=100
5 Correct 7 ms 39548 KB n=100
6 Correct 7 ms 39516 KB n=100
7 Correct 7 ms 39608 KB n=100
8 Correct 7 ms 39512 KB n=100
9 Correct 8 ms 39768 KB n=100
10 Correct 8 ms 39768 KB n=100
11 Correct 7 ms 39516 KB n=100
12 Correct 7 ms 39512 KB n=100
13 Correct 10 ms 39516 KB n=100
14 Correct 8 ms 39620 KB n=100
15 Correct 8 ms 39516 KB n=100
16 Correct 7 ms 39516 KB n=100
17 Correct 8 ms 39640 KB n=100
18 Correct 8 ms 39516 KB n=100
19 Correct 7 ms 39516 KB n=100
20 Correct 7 ms 39516 KB n=100
21 Correct 7 ms 39512 KB n=100
22 Correct 7 ms 39512 KB n=100
23 Correct 7 ms 39516 KB n=100
24 Correct 7 ms 39516 KB n=100
25 Correct 7 ms 39592 KB n=100
26 Correct 8 ms 39516 KB n=12
27 Correct 7 ms 39512 KB n=100
28 Correct 7 ms 39516 KB n=500
29 Correct 7 ms 39516 KB n=500
30 Correct 8 ms 39724 KB n=500
31 Correct 9 ms 39516 KB n=500
32 Correct 8 ms 39516 KB n=500
33 Correct 9 ms 39600 KB n=500
34 Correct 8 ms 39516 KB n=500
35 Correct 9 ms 39768 KB n=500
36 Correct 7 ms 39512 KB n=500
37 Correct 8 ms 39516 KB n=500
38 Correct 8 ms 39516 KB n=500
39 Correct 7 ms 39516 KB n=500
40 Correct 7 ms 39516 KB n=500
41 Correct 8 ms 39516 KB n=500
42 Correct 7 ms 39516 KB n=500
43 Correct 8 ms 39540 KB n=500
44 Correct 8 ms 39512 KB n=500
45 Correct 8 ms 39516 KB n=500
46 Correct 8 ms 39516 KB n=500
47 Correct 8 ms 39516 KB n=500
48 Correct 8 ms 39516 KB n=500
49 Correct 7 ms 39512 KB n=500
50 Correct 8 ms 39616 KB n=500
51 Correct 8 ms 39516 KB n=500
52 Correct 7 ms 39516 KB n=500
53 Correct 8 ms 39516 KB n=500
54 Correct 8 ms 39516 KB n=500
55 Correct 7 ms 39512 KB n=278
56 Correct 7 ms 39512 KB n=500
57 Correct 7 ms 39512 KB n=500
58 Correct 9 ms 39512 KB n=500
59 Correct 9 ms 39768 KB n=2000
60 Correct 10 ms 40028 KB n=2000
61 Correct 9 ms 39772 KB n=2000
62 Correct 10 ms 39964 KB n=2000
63 Correct 9 ms 39772 KB n=2000
64 Correct 10 ms 39772 KB n=2000
65 Correct 10 ms 39768 KB n=2000
66 Correct 10 ms 40024 KB n=2000
67 Correct 10 ms 39768 KB n=2000
68 Correct 9 ms 39772 KB n=2000
69 Correct 9 ms 39768 KB n=2000
70 Correct 10 ms 39772 KB n=2000
71 Correct 9 ms 39876 KB n=2000
72 Correct 9 ms 39772 KB n=2000
73 Correct 9 ms 39772 KB n=2000
74 Correct 9 ms 40136 KB n=1844
75 Correct 11 ms 39952 KB n=2000
76 Correct 10 ms 39768 KB n=2000
77 Correct 10 ms 39772 KB n=2000
78 Correct 10 ms 39772 KB n=2000
79 Correct 10 ms 39768 KB n=2000
80 Correct 10 ms 40024 KB n=2000
81 Correct 11 ms 39772 KB n=2000
82 Correct 10 ms 39768 KB n=2000
83 Correct 9 ms 40024 KB n=2000
84 Correct 10 ms 39768 KB n=2000
85 Correct 10 ms 39772 KB n=2000
86 Correct 10 ms 39772 KB n=2000
87 Correct 10 ms 39772 KB n=2000
88 Correct 10 ms 40028 KB n=2000
89 Correct 11 ms 40056 KB n=2000
90 Correct 12 ms 40028 KB n=2000
91 Correct 12 ms 39756 KB n=2000
92 Correct 643 ms 75604 KB n=200000
93 Correct 804 ms 80720 KB n=200000
94 Correct 800 ms 83536 KB n=200000
95 Correct 622 ms 75376 KB n=200000
96 Correct 638 ms 75460 KB n=200000
97 Correct 912 ms 79312 KB n=200000
98 Correct 636 ms 75292 KB n=200000
99 Correct 844 ms 75568 KB n=200000
100 Correct 630 ms 75644 KB n=200000
101 Correct 696 ms 84920 KB n=200000
102 Correct 387 ms 76708 KB n=200000
103 Correct 339 ms 76664 KB n=200000
104 Correct 332 ms 76680 KB n=200000
105 Correct 344 ms 76932 KB n=200000
106 Correct 367 ms 76984 KB n=200000
107 Correct 361 ms 77136 KB n=200000
108 Correct 707 ms 75440 KB n=200000
109 Correct 702 ms 75348 KB n=200000
110 Correct 731 ms 75348 KB n=200000
111 Correct 645 ms 74972 KB n=200000
112 Correct 773 ms 84032 KB n=200000
113 Correct 882 ms 79420 KB n=200000
114 Correct 586 ms 74944 KB n=200000
115 Correct 1022 ms 77392 KB n=200000
116 Correct 598 ms 75372 KB n=200000
117 Correct 752 ms 84308 KB n=200000
118 Correct 906 ms 78216 KB n=200000
119 Correct 657 ms 75452 KB n=200000
120 Correct 672 ms 83952 KB n=200000
121 Correct 678 ms 84024 KB n=200000
122 Correct 683 ms 84400 KB n=200000
123 Correct 364 ms 76620 KB n=200000
124 Correct 176 ms 52860 KB n=25264