Submission #909641

# Submission time Handle Problem Language Result Execution time Memory
909641 2024-01-17T10:07:07 Z daoquanglinh2007 Birthday gift (IZhO18_treearray) C++17
Compilation error
0 ms 0 KB
#pragma GCC optimize("O3")
#pragma GCC target("tune=native")
 
#include <bits/stdc++.h>
using namespace std;
 
#define pii pair <int, int>
#define fi first
#define se second
#define mp make_pair
 
const int NM = 2e5, LOG = 18, BL = 50;
 
int n, m, q;
vector <int> adj[NM+5];
int dep[NM+5], timer = 0, tin[NM+5], tout[NM+5];
pii f[LOG+5][2*NM+5];
int lg[2*NM+5];
int a[NM+5], g[NM/BL+5];
int pref[NM+5], suff[NM+5];
vector <int> arr[NM/BL+5];
vector <int> st;
 
void dfs(int u, int p){
	dep[u] = (p == -1 ? 0 : dep[p]+1);
	tin[u] = ++timer;
	f[0][timer] = mp(dep[u], u);
	for (int v : adj[u]){
		if (v == p) continue;
		dfs(v, u);
		f[0][++timer] = mp(dep[u], u);
	}
	tout[u] = timer;
}
 
void build_sparse(){
	for (int i = 1; i <= LOG; i++)
		for (int j = 1; j+(1<<i)-1 <= timer; j++)
			f[i][j] = min(f[i-1][j], f[i-1][j+(1<<(i-1))]);
			
	lg[1] = 0;
	for (int i = 2; i <= timer; i++)
		lg[i] = lg[i>>1]+1;
}
 
int lca(int u, int v){
	int l = tin[u], r = tin[v];
	if (l > r) swap(l, r);
	int k = __lg(r-l+1);
	return min(f[k][l], f[k][r-(1<<k)+1]).se;
}
 
bool is_ancestor(int a, int u){
	return tin[a] <= tin[u] && tout[a] >= tout[u];
}
 
void build(int id){
	arr[id].clear();
	st.clear();
	for (int i = id*BL; i < min((id+1)*BL, m); i++){
		if (st.empty()){
			st.push_back(a[i]);
			arr[id].push_back(a[i]);
			continue;
		}
		int tmp = lca(a[i], st.back());
		while (!st.empty() && is_ancestor(tmp, st.back())) st.pop_back();
		st.push_back(tmp);
		arr[id].push_back(tmp);
		if (tmp != a[i]){
			st.push_back(a[i]);
			arr[id].push_back(a[i]);
		}
	}
	sort(arr[id].begin(), arr[id].end());
	arr[id].erase(unique(arr[id].begin(), arr[id].end()), arr[id].end());
	
	pref[id*BL] = a[id*BL];
	for (int i = id*BL+1; i < min((id+1)*BL, m); i++)
		pref[i] = lca(pref[i-1], a[i]);
	suff[min((id+1)*BL, m)-1] = a[min((id+1)*BL, m)-1];
	for (int i = min((id+1)*BL, m)-2; i >= id*BL; i--)
		suff[i] = lca(suff[i+1], a[i]);
	g[id] = suff[id*BL];
}
 
void preprocess(){
	for (int i = 0; i*BL < m; i++){
		build(i);
	}
}
 
void update(int x, int val){
	a[x] = val;
	build(x/BL);
}
 
pii manual_query(int l, int r, int v){
	for (int i = l; i <= r; i++)
		if (is_ancestor(v, a[i])){
			int cur = a[i], j = i;
			while (j < r){
				int ne = lca(cur, a[j+1]);
				if (is_ancestor(v, ne)){
					cur = ne;
					j++;
				}
				else break;
			}
			if (cur == v) return mp(i, j);
			i = j+1;
		}
	return mp(-1, -1);
}
 
pii query(int l, int r, int v){
	int L = (l+BL-1)/BL, R = (r-BL+1)/BL;
	if (l/BL == r/BL || L > R){
		return manual_query(l, r, v);
	}
	for (int i = L; i <= R; i++)
		if (arr[i].back() >= v && arr[i][lower_bound(arr[i].begin(), arr[i].end(), v)-arr[i].begin()] == v){
			return manual_query(i*BL, min((i+1)*BL, m)-1, v);
		}
	pii tmp = manual_query(l, L*BL-1, v);
	if (tmp.fi != -1) return tmp;
	tmp = manual_query((R+1)*BL, r, v);
	if (tmp.fi != -1) return tmp;
	
	for (int i = L; i <= R+1; i++)
		if (i <= R && is_ancestor(v, g[i])){
			int j = i, cur = g[i];
			while (j < R){
				int ne = lca(cur, g[j+1]);
				if (is_ancestor(v, ne)){
					cur = ne;
					j++;
				}
				else break;
			}
			if (cur == v){
				return mp(i*BL, min((j+1)*BL, m)-1);
			}
			int lo = max(l, (i-1)*BL), hi = i*BL-1, resl = i*BL;
			if (lo <= hi && is_ancestor(v, a[hi])){
				while (lo <= hi){
					int mid = (lo+hi)/2;
					if (is_ancestor(v, suff[mid])){
						resl = mid;
						hi = mid-1;
					}
					else lo = mid+1;
				}
			}
			if (resl < i*BL) cur = lca(cur, suff[resl]);
			
			lo = (j+1)*BL, hi = min(r, (j+2)*BL-1);
			int resr = (j+1)*BL-1;
			if (lo <= hi && is_ancestor(v, a[lo])){
				while (lo <= hi){
					int mid = (lo+hi)/2;
					if (is_ancestor(v, pref[mid])){
						resr = mid;
						lo = mid+1;
					}
					else hi = mid-1;
				}
			}
			if (resr >= (j+1)*BL) cur = lca(cur, pref[resr]);
			
			if (cur == v) return mp(resl, resr);
			
			i = j;
		}
		else{
			int lo = max(l, (i-1)*BL), hi = min(r, i*BL-1), resl = -1;
			if (lo <= hi && is_ancestor(v, a[hi])){
				while (lo <= hi){
					int mid = (lo+hi)/2;
					if (is_ancestor(v, suff[mid])){
						resl = mid;
						hi = mid-1;
					}
					else lo = mid+1;
				}
			}
			if (resl == -1) continue;
			lo = max(l, i*BL), hi = min(r, (i+1)*BL-1); int resr = -1;
			if (lo <= hi && is_ancestor(v, a[lo])){
				while (lo <= hi){
					int mid = (lo+hi)/2;
					if (is_ancestor(v, pref[mid])){
						resr = mid;
						lo = mid+1;
					}
					else hi = mid-1;
				}
			}
			if (resr == -1) continue;
			
			if (lca(suff[resl], pref[resr]) == v){
				return mp(resl, resr);
			}
		}
	return mp(-1, -1);
}
 
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	cin >> n >> m >> q;
	for (int i = 1; i < n; i++){
		int u, v; cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs(1, -1);
	build_sparse();
	
	for (int i = 0; i < m; i++) cin >> a[i];
	preprocess();
	
	while (q--){
		int type; cin >> type;
		if (type == 1){
			int pos, val; cin >> pos >> val;
			update(pos-1, val);
		}
		else{
			int l, r, v; cin >> l >> r >> v;
			pii ans = query(l-1, r-1, v);
			if (ans.fi != -1){
				int cur = a[ans.fi];
				for (int i = ans.fi+1; i <= ans.se; i++) cur = lca(cur, a[i]);
				ans.fi++, ans.se++;
			}
			cout << ans.fi << ' ' << ans.se << '\n';
		}
	}
	return 0;
}

Compilation message

In file included from /usr/include/x86_64-linux-gnu/c++/10/bits/gthr.h:148,
                 from /usr/include/c++/10/ext/atomicity.h:35,
                 from /usr/include/c++/10/bits/ios_base.h:39,
                 from /usr/include/c++/10/ios:42,
                 from /usr/include/c++/10/istream:38,
                 from /usr/include/c++/10/sstream:38,
                 from /usr/include/c++/10/complex:45,
                 from /usr/include/c++/10/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:54,
                 from treearray.cpp:4:
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:102:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  102 | __gthrw(pthread_once)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:102:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:103:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  103 | __gthrw(pthread_getspecific)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:103:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:104:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  104 | __gthrw(pthread_setspecific)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:104:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:106:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  106 | __gthrw(pthread_create)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:106:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:107:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  107 | __gthrw(pthread_join)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:107:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:108:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  108 | __gthrw(pthread_equal)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:108:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:109:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  109 | __gthrw(pthread_self)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:109:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:110:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  110 | __gthrw(pthread_detach)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:110:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:112:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  112 | __gthrw(pthread_cancel)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:112:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:114:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  114 | __gthrw(sched_yield)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:114:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:116:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  116 | __gthrw(pthread_mutex_lock)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:116:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:117:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  117 | __gthrw(pthread_mutex_trylock)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:117:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:119:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  119 | __gthrw(pthread_mutex_timedlock)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:119:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:121:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  121 | __gthrw(pthread_mutex_unlock)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:121:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:122:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  122 | __gthrw(pthread_mutex_init)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:122:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:123:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  123 | __gthrw(pthread_mutex_destroy)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:123:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:125:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  125 | __gthrw(pthread_cond_init)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:125:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:126:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  126 | __gthrw(pthread_cond_broadcast)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:126:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:127:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  127 | __gthrw(pthread_cond_signal)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:127:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:128:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  128 | __gthrw(pthread_cond_wait)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:128:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:129:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  129 | __gthrw(pthread_cond_timedwait)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:129:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:130:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  130 | __gthrw(pthread_cond_destroy)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:130:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:132:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  132 | __gthrw(pthread_key_create)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:132:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:133:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  133 | __gthrw(pthread_key_delete)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:133:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:134:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  134 | __gthrw(pthread_mutexattr_init)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:134:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:135:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  135 | __gthrw(pthread_mutexattr_settype)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:135:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:136:1: error: attribute value 'tune=native' was already specified in 'target' attribute
  136 | __gthrw(pthread_mutexattr_destroy)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:136:1: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:237:10: error: attribute value 'tune=native' was already specified in 'target' attribute
  237 | __gthrw2(__gthrw_(__pthread_key_create),
      |          ^~~~~~~~
/usr/include/x86_64-linux-gnu/c++/10/bits/gthr-default.h:237:10: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/c++/10/shared_mutex:73:3: error: attribute value 'tune=native' was already specified in 'target' attribute
   73 |   _GLIBCXX_GTHRW(rwlock_rdlock)
      |   ^~~~~~~~~~~~~~
/usr/include/c++/10/shared_mutex:73:3: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/c++/10/shared_mutex:74:3: error: attribute value 'tune=native' was already specified in 'target' attribute
   74 |   _GLIBCXX_GTHRW(rwlock_tryrdlock)
      |   ^~~~~~~~~~~~~~
/usr/include/c++/10/shared_mutex:74:3: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/c++/10/shared_mutex:75:3: error: attribute value 'tune=native' was already specified in 'target' attribute
   75 |   _GLIBCXX_GTHRW(rwlock_wrlock)
      |   ^~~~~~~~~~~~~~
/usr/include/c++/10/shared_mutex:75:3: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/c++/10/shared_mutex:76:3: error: attribute value 'tune=native' was already specified in 'target' attribute
   76 |   _GLIBCXX_GTHRW(rwlock_trywrlock)
      |   ^~~~~~~~~~~~~~
/usr/include/c++/10/shared_mutex:76:3: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/c++/10/shared_mutex:77:3: error: attribute value 'tune=native' was already specified in 'target' attribute
   77 |   _GLIBCXX_GTHRW(rwlock_unlock)
      |   ^~~~~~~~~~~~~~
/usr/include/c++/10/shared_mutex:77:3: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/c++/10/shared_mutex:91:4: error: attribute value 'tune=native' was already specified in 'target' attribute
   91 |    __gthrw(pthread_rwlock_timedrdlock);
      |    ^~~~~~~
/usr/include/c++/10/shared_mutex:91:4: error: attribute value 'tune=native' was already specified in 'target' attribute
/usr/include/c++/10/shared_mutex:101:4: error: attribute value 'tune=native' was already specified in 'target' attribute
  101 |    __gthrw(pthread_rwlock_timedwrlock);
      |    ^~~~~~~
/usr/include/c++/10/shared_mutex:101:4: error: attribute value 'tune=native' was already specified in 'target' attribute