Submission #938700

# Submission time Handle Problem Language Result Execution time Memory
938700 2024-03-05T12:47:55 Z Baizho Birthday gift (IZhO18_treearray) C++14
0 / 100
9 ms 39516 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][a];
		}
	}
	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(n > 100) assert(0);
			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 39512 KB n=5
2 Correct 7 ms 39516 KB n=100
3 Incorrect 8 ms 39516 KB Wrong range.
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 39512 KB n=5
2 Correct 7 ms 39516 KB n=100
3 Incorrect 8 ms 39516 KB Wrong range.
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 39512 KB n=5
2 Correct 7 ms 39516 KB n=100
3 Incorrect 8 ms 39516 KB Wrong range.
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 39512 KB n=5
2 Correct 7 ms 39516 KB n=100
3 Incorrect 8 ms 39516 KB Wrong range.
4 Halted 0 ms 0 KB -