Submission #643087

#TimeUsernameProblemLanguageResultExecution timeMemory
643087ghostwriterBirthday gift (IZhO18_treearray)C++14
100 / 100
938 ms82340 KiB
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#endif
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
    Tran The Bao
    CTL - Da Lat
    Cay ngay cay dem nhung deo duoc cong nhan
*/
const int N = 2e5 + 5;
int n, m, q, a[N], h[N], p[N][18];
vi adj[N];
multiset<int> s[N], s1[N];
void dfs(int u, int p) {
	::p[u][0] = p;
	h[u] = h[p] + 1;
	EACH(v, adj[u]) {
		if (v == p) continue;
		dfs(v, u);
	}
}
void build() {
	FOR(j, 1, 17)
	FOR(i, 1, n)
		p[i][j] = p[p[i][j - 1]][j - 1];
}
int lca(int a, int b) {
	if (h[a] > h[b]) swap(a, b);
	int diff = h[b] - h[a];
	FOR(i, 0, 17)
		if (diff & (1 << i)) b = p[b][i];
	if (a == b) return a;
	FOS(i, 17, 0)
		if (p[a][i] != p[b][i]) {
			a = p[a][i];
			b = p[b][i];
		}
	return p[a][0];
}
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);
    cin >> n >> m >> q;
    FOR(i, 1, n - 1) {
    	int u, v;
    	cin >> u >> v;
    	adj[u].pb(v);
    	adj[v].pb(u);
    }
    FOR(i, 1, m) cin >> a[i];
    dfs(1, 0);
    build();
    a[m + 1] = 1;
    FOR(i, 1, m) {
    	s[a[i]].insert(i);
    	s1[lca(a[i], a[i + 1])].insert(i);
    }
    WHILE(q--) {
    	int t;
    	cin >> t;
    	if (t == 2) {
    		int l, r, v;
    		cin >> l >> r >> v;
    		if (!s[v].empty() && *--s[v].end() >= l) {
    			int pos = *s[v].lb(l);
    			if (pos <= r) {
    				cout << pos << ' ' << pos << '\n';
    				continue;
    			}
    		}
    		if (!s1[v].empty() && *--s1[v].end() >= l) {
    			int pos = *s1[v].lb(l);
    			if (pos + 1 <= r) {
    				cout << pos << ' ' << pos + 1 << '\n';
    				continue;
    			}
    		}
    		cout << -1 << ' ' << -1 << '\n';
    		continue;
    	}
    	int pos, v;
    	cin >> pos >> v;
    	s[a[pos]].erase(pos);
    	if (pos > 1) s1[lca(a[pos - 1], a[pos])].erase(pos - 1);
    	s1[lca(a[pos], a[pos + 1])].erase(pos);
    	a[pos] = v;
    	s[a[pos]].insert(pos);
    	if (pos > 1) s1[lca(a[pos - 1], a[pos])].insert(pos - 1);
    	s1[lca(a[pos], a[pos + 1])].insert(pos);
    }
    return 0;
}
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5 
2 3 4 5
2 1 3 1
*/ 

Compilation message (stderr)

treearray.cpp: In function 'void dfs(int, int)':
treearray.cpp:26:31: warning: unnecessary parentheses in declaration of 'v' [-Wparentheses]
   26 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
treearray.cpp:43:2: note: in expansion of macro 'EACH'
   43 |  EACH(v, adj[u]) {
      |  ^~~~
treearray.cpp: In function 'void build()':
treearray.cpp:24:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
treearray.cpp:49:2: note: in expansion of macro 'FOR'
   49 |  FOR(j, 1, 17)
      |  ^~~
treearray.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
treearray.cpp:50:2: note: in expansion of macro 'FOR'
   50 |  FOR(i, 1, n)
      |  ^~~
treearray.cpp: In function 'int lca(int, int)':
treearray.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
treearray.cpp:56:2: note: in expansion of macro 'FOR'
   56 |  FOR(i, 0, 17)
      |  ^~~
treearray.cpp:25:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   25 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
      |                               ^
treearray.cpp:59:2: note: in expansion of macro 'FOS'
   59 |  FOS(i, 17, 0)
      |  ^~~
treearray.cpp: In function 'int main()':
treearray.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
treearray.cpp:71:5: note: in expansion of macro 'FOR'
   71 |     FOR(i, 1, n - 1) {
      |     ^~~
treearray.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
treearray.cpp:77:5: note: in expansion of macro 'FOR'
   77 |     FOR(i, 1, m) cin >> a[i];
      |     ^~~
treearray.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
treearray.cpp:81:5: note: in expansion of macro 'FOR'
   81 |     FOR(i, 1, m) {
      |     ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...