Submission #938706

# Submission time Handle Problem Language Result Execution time Memory
938706 2024-03-05T12:58:29 Z Baizho Birthday gift (IZhO18_treearray) C++14
100 / 100
918 ms 78624 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,avx2")
 
 
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 7 ms 39516 KB n=5
2 Correct 7 ms 39516 KB n=100
3 Correct 7 ms 39512 KB n=100
4 Correct 8 ms 39516 KB n=100
5 Correct 7 ms 39512 KB n=100
6 Correct 8 ms 39516 KB n=100
7 Correct 7 ms 39516 KB n=100
8 Correct 7 ms 39516 KB n=100
9 Correct 7 ms 39516 KB n=100
10 Correct 7 ms 39512 KB n=100
11 Correct 7 ms 39516 KB n=100
12 Correct 7 ms 39588 KB n=100
13 Correct 7 ms 39516 KB n=100
14 Correct 7 ms 39524 KB n=100
15 Correct 7 ms 39516 KB n=100
16 Correct 8 ms 39524 KB n=100
17 Correct 7 ms 39516 KB n=100
18 Correct 7 ms 39524 KB n=100
19 Correct 8 ms 39600 KB n=100
20 Correct 8 ms 39520 KB n=100
21 Correct 7 ms 39516 KB n=100
22 Correct 7 ms 39580 KB n=100
23 Correct 7 ms 39516 KB n=100
24 Correct 7 ms 39588 KB n=100
25 Correct 8 ms 39516 KB n=100
26 Correct 7 ms 39516 KB n=12
27 Correct 7 ms 39516 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 7 ms 39516 KB n=5
2 Correct 7 ms 39516 KB n=100
3 Correct 7 ms 39512 KB n=100
4 Correct 8 ms 39516 KB n=100
5 Correct 7 ms 39512 KB n=100
6 Correct 8 ms 39516 KB n=100
7 Correct 7 ms 39516 KB n=100
8 Correct 7 ms 39516 KB n=100
9 Correct 7 ms 39516 KB n=100
10 Correct 7 ms 39512 KB n=100
11 Correct 7 ms 39516 KB n=100
12 Correct 7 ms 39588 KB n=100
13 Correct 7 ms 39516 KB n=100
14 Correct 7 ms 39524 KB n=100
15 Correct 7 ms 39516 KB n=100
16 Correct 8 ms 39524 KB n=100
17 Correct 7 ms 39516 KB n=100
18 Correct 7 ms 39524 KB n=100
19 Correct 8 ms 39600 KB n=100
20 Correct 8 ms 39520 KB n=100
21 Correct 7 ms 39516 KB n=100
22 Correct 7 ms 39580 KB n=100
23 Correct 7 ms 39516 KB n=100
24 Correct 7 ms 39588 KB n=100
25 Correct 8 ms 39516 KB n=100
26 Correct 7 ms 39516 KB n=12
27 Correct 7 ms 39516 KB n=100
28 Correct 7 ms 39516 KB n=500
29 Correct 8 ms 39512 KB n=500
30 Correct 7 ms 39516 KB n=500
31 Correct 7 ms 39516 KB n=500
32 Correct 8 ms 39516 KB n=500
33 Correct 8 ms 39516 KB n=500
34 Correct 7 ms 39512 KB n=500
35 Correct 8 ms 39512 KB n=500
36 Correct 8 ms 39516 KB n=500
37 Correct 8 ms 39512 KB n=500
38 Correct 8 ms 39516 KB n=500
39 Correct 8 ms 39516 KB n=500
40 Correct 8 ms 39516 KB n=500
41 Correct 8 ms 39592 KB n=500
42 Correct 8 ms 39516 KB n=500
43 Correct 8 ms 39516 KB n=500
44 Correct 8 ms 39516 KB n=500
45 Correct 8 ms 39516 KB n=500
46 Correct 9 ms 39516 KB n=500
47 Correct 8 ms 39768 KB n=500
48 Correct 7 ms 39640 KB n=500
49 Correct 8 ms 39512 KB n=500
50 Correct 8 ms 39512 KB n=500
51 Correct 8 ms 39516 KB n=500
52 Correct 8 ms 39516 KB n=500
53 Correct 9 ms 39516 KB n=500
54 Correct 7 ms 39516 KB n=500
55 Correct 7 ms 39512 KB n=278
56 Correct 8 ms 39516 KB n=500
57 Correct 7 ms 39516 KB n=500
58 Correct 8 ms 39512 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 7 ms 39516 KB n=5
2 Correct 7 ms 39516 KB n=100
3 Correct 7 ms 39512 KB n=100
4 Correct 8 ms 39516 KB n=100
5 Correct 7 ms 39512 KB n=100
6 Correct 8 ms 39516 KB n=100
7 Correct 7 ms 39516 KB n=100
8 Correct 7 ms 39516 KB n=100
9 Correct 7 ms 39516 KB n=100
10 Correct 7 ms 39512 KB n=100
11 Correct 7 ms 39516 KB n=100
12 Correct 7 ms 39588 KB n=100
13 Correct 7 ms 39516 KB n=100
14 Correct 7 ms 39524 KB n=100
15 Correct 7 ms 39516 KB n=100
16 Correct 8 ms 39524 KB n=100
17 Correct 7 ms 39516 KB n=100
18 Correct 7 ms 39524 KB n=100
19 Correct 8 ms 39600 KB n=100
20 Correct 8 ms 39520 KB n=100
21 Correct 7 ms 39516 KB n=100
22 Correct 7 ms 39580 KB n=100
23 Correct 7 ms 39516 KB n=100
24 Correct 7 ms 39588 KB n=100
25 Correct 8 ms 39516 KB n=100
26 Correct 7 ms 39516 KB n=12
27 Correct 7 ms 39516 KB n=100
28 Correct 7 ms 39516 KB n=500
29 Correct 8 ms 39512 KB n=500
30 Correct 7 ms 39516 KB n=500
31 Correct 7 ms 39516 KB n=500
32 Correct 8 ms 39516 KB n=500
33 Correct 8 ms 39516 KB n=500
34 Correct 7 ms 39512 KB n=500
35 Correct 8 ms 39512 KB n=500
36 Correct 8 ms 39516 KB n=500
37 Correct 8 ms 39512 KB n=500
38 Correct 8 ms 39516 KB n=500
39 Correct 8 ms 39516 KB n=500
40 Correct 8 ms 39516 KB n=500
41 Correct 8 ms 39592 KB n=500
42 Correct 8 ms 39516 KB n=500
43 Correct 8 ms 39516 KB n=500
44 Correct 8 ms 39516 KB n=500
45 Correct 8 ms 39516 KB n=500
46 Correct 9 ms 39516 KB n=500
47 Correct 8 ms 39768 KB n=500
48 Correct 7 ms 39640 KB n=500
49 Correct 8 ms 39512 KB n=500
50 Correct 8 ms 39512 KB n=500
51 Correct 8 ms 39516 KB n=500
52 Correct 8 ms 39516 KB n=500
53 Correct 9 ms 39516 KB n=500
54 Correct 7 ms 39516 KB n=500
55 Correct 7 ms 39512 KB n=278
56 Correct 8 ms 39516 KB n=500
57 Correct 7 ms 39516 KB n=500
58 Correct 8 ms 39512 KB n=500
59 Correct 12 ms 39804 KB n=2000
60 Correct 9 ms 40024 KB n=2000
61 Correct 9 ms 39772 KB n=2000
62 Correct 10 ms 39772 KB n=2000
63 Correct 9 ms 39772 KB n=2000
64 Correct 10 ms 39808 KB n=2000
65 Correct 9 ms 39772 KB n=2000
66 Correct 10 ms 39772 KB n=2000
67 Correct 10 ms 39768 KB n=2000
68 Correct 11 ms 40540 KB n=2000
69 Correct 9 ms 39772 KB n=2000
70 Correct 9 ms 39772 KB n=2000
71 Correct 9 ms 39772 KB n=2000
72 Correct 9 ms 39772 KB n=2000
73 Correct 9 ms 39968 KB n=2000
74 Correct 9 ms 39772 KB n=1844
75 Correct 10 ms 39768 KB n=2000
76 Correct 10 ms 39772 KB n=2000
77 Correct 10 ms 39772 KB n=2000
78 Correct 10 ms 39964 KB n=2000
79 Correct 10 ms 39768 KB n=2000
80 Correct 10 ms 39772 KB n=2000
81 Correct 9 ms 39768 KB n=2000
82 Correct 10 ms 39772 KB n=2000
83 Correct 9 ms 39772 KB n=2000
84 Correct 9 ms 39772 KB n=2000
85 Correct 10 ms 39768 KB n=2000
86 Correct 10 ms 39772 KB n=2000
87 Correct 10 ms 39772 KB n=2000
88 Correct 9 ms 40028 KB n=2000
89 Correct 10 ms 39804 KB n=2000
90 Correct 10 ms 40028 KB n=2000
91 Correct 9 ms 39768 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 7 ms 39516 KB n=5
2 Correct 7 ms 39516 KB n=100
3 Correct 7 ms 39512 KB n=100
4 Correct 8 ms 39516 KB n=100
5 Correct 7 ms 39512 KB n=100
6 Correct 8 ms 39516 KB n=100
7 Correct 7 ms 39516 KB n=100
8 Correct 7 ms 39516 KB n=100
9 Correct 7 ms 39516 KB n=100
10 Correct 7 ms 39512 KB n=100
11 Correct 7 ms 39516 KB n=100
12 Correct 7 ms 39588 KB n=100
13 Correct 7 ms 39516 KB n=100
14 Correct 7 ms 39524 KB n=100
15 Correct 7 ms 39516 KB n=100
16 Correct 8 ms 39524 KB n=100
17 Correct 7 ms 39516 KB n=100
18 Correct 7 ms 39524 KB n=100
19 Correct 8 ms 39600 KB n=100
20 Correct 8 ms 39520 KB n=100
21 Correct 7 ms 39516 KB n=100
22 Correct 7 ms 39580 KB n=100
23 Correct 7 ms 39516 KB n=100
24 Correct 7 ms 39588 KB n=100
25 Correct 8 ms 39516 KB n=100
26 Correct 7 ms 39516 KB n=12
27 Correct 7 ms 39516 KB n=100
28 Correct 7 ms 39516 KB n=500
29 Correct 8 ms 39512 KB n=500
30 Correct 7 ms 39516 KB n=500
31 Correct 7 ms 39516 KB n=500
32 Correct 8 ms 39516 KB n=500
33 Correct 8 ms 39516 KB n=500
34 Correct 7 ms 39512 KB n=500
35 Correct 8 ms 39512 KB n=500
36 Correct 8 ms 39516 KB n=500
37 Correct 8 ms 39512 KB n=500
38 Correct 8 ms 39516 KB n=500
39 Correct 8 ms 39516 KB n=500
40 Correct 8 ms 39516 KB n=500
41 Correct 8 ms 39592 KB n=500
42 Correct 8 ms 39516 KB n=500
43 Correct 8 ms 39516 KB n=500
44 Correct 8 ms 39516 KB n=500
45 Correct 8 ms 39516 KB n=500
46 Correct 9 ms 39516 KB n=500
47 Correct 8 ms 39768 KB n=500
48 Correct 7 ms 39640 KB n=500
49 Correct 8 ms 39512 KB n=500
50 Correct 8 ms 39512 KB n=500
51 Correct 8 ms 39516 KB n=500
52 Correct 8 ms 39516 KB n=500
53 Correct 9 ms 39516 KB n=500
54 Correct 7 ms 39516 KB n=500
55 Correct 7 ms 39512 KB n=278
56 Correct 8 ms 39516 KB n=500
57 Correct 7 ms 39516 KB n=500
58 Correct 8 ms 39512 KB n=500
59 Correct 12 ms 39804 KB n=2000
60 Correct 9 ms 40024 KB n=2000
61 Correct 9 ms 39772 KB n=2000
62 Correct 10 ms 39772 KB n=2000
63 Correct 9 ms 39772 KB n=2000
64 Correct 10 ms 39808 KB n=2000
65 Correct 9 ms 39772 KB n=2000
66 Correct 10 ms 39772 KB n=2000
67 Correct 10 ms 39768 KB n=2000
68 Correct 11 ms 40540 KB n=2000
69 Correct 9 ms 39772 KB n=2000
70 Correct 9 ms 39772 KB n=2000
71 Correct 9 ms 39772 KB n=2000
72 Correct 9 ms 39772 KB n=2000
73 Correct 9 ms 39968 KB n=2000
74 Correct 9 ms 39772 KB n=1844
75 Correct 10 ms 39768 KB n=2000
76 Correct 10 ms 39772 KB n=2000
77 Correct 10 ms 39772 KB n=2000
78 Correct 10 ms 39964 KB n=2000
79 Correct 10 ms 39768 KB n=2000
80 Correct 10 ms 39772 KB n=2000
81 Correct 9 ms 39768 KB n=2000
82 Correct 10 ms 39772 KB n=2000
83 Correct 9 ms 39772 KB n=2000
84 Correct 9 ms 39772 KB n=2000
85 Correct 10 ms 39768 KB n=2000
86 Correct 10 ms 39772 KB n=2000
87 Correct 10 ms 39772 KB n=2000
88 Correct 9 ms 40028 KB n=2000
89 Correct 10 ms 39804 KB n=2000
90 Correct 10 ms 40028 KB n=2000
91 Correct 9 ms 39768 KB n=2000
92 Correct 702 ms 72676 KB n=200000
93 Correct 843 ms 75324 KB n=200000
94 Correct 767 ms 77740 KB n=200000
95 Correct 618 ms 72468 KB n=200000
96 Correct 619 ms 72540 KB n=200000
97 Correct 873 ms 74836 KB n=200000
98 Correct 629 ms 72496 KB n=200000
99 Correct 873 ms 72084 KB n=200000
100 Correct 666 ms 72612 KB n=200000
101 Correct 740 ms 78624 KB n=200000
102 Correct 353 ms 73556 KB n=200000
103 Correct 338 ms 73556 KB n=200000
104 Correct 338 ms 73612 KB n=200000
105 Correct 372 ms 73524 KB n=200000
106 Correct 360 ms 73748 KB n=200000
107 Correct 358 ms 73672 KB n=200000
108 Correct 723 ms 72148 KB n=200000
109 Correct 713 ms 72020 KB n=200000
110 Correct 731 ms 72296 KB n=200000
111 Correct 633 ms 72264 KB n=200000
112 Correct 714 ms 77908 KB n=200000
113 Correct 827 ms 74756 KB n=200000
114 Correct 621 ms 72268 KB n=200000
115 Correct 918 ms 73236 KB n=200000
116 Correct 597 ms 72408 KB n=200000
117 Correct 789 ms 78032 KB n=200000
118 Correct 869 ms 73760 KB n=200000
119 Correct 629 ms 72284 KB n=200000
120 Correct 716 ms 78448 KB n=200000
121 Correct 676 ms 78396 KB n=200000
122 Correct 666 ms 78328 KB n=200000
123 Correct 377 ms 73088 KB n=200000
124 Correct 146 ms 51536 KB n=25264