답안 #249310

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
249310 2020-07-14T15:55:45 Z aloo123 Birthday gift (IZhO18_treearray) C++14
100 / 100
1308 ms 100476 KB
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <climits>
#define ll int
#define ld long double
#define mp make_pair
#define pb push_back
#define in insert
#define vll vector<ll>
#define endl "\n"
#define pll pair<ll,ll>
#define f first
#define s second
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
// #define int ll
#define sz(x) (ll)x.size()
#define all(x) (x.begin(),x.end())
using namespace std;

 
// const ll INF = 1e12;
const ll N =(2e5+5); // TODO : change value as per problem
const ll MOD = 1e9+7;
ll up[N][40];
ll n,m,q;
vll adj[N];
ll a[N];
int k;
set<int> wow[N];
set<int> pt[N];
int timer;
ll tin[N];
ll tout[N];

void dfs(int v, int p)
{
    tin[v] = ++timer;
    up[v][0] = p;
    for (int i = 1; i <= k; ++i)
        up[v][i] = up[up[v][i-1]][i-1];

    for (int u : adj[v]) {
        if (u != p)
            dfs(u, v);
    }

    tout[v] = ++timer;
}

bool is_ancestor(int u, int v)
{
    return tin[u] <= tin[v] && tout[u] >= tout[v];
}

int lca(int u, int v)
{
    if (is_ancestor(u, v))
        return u;
    if (is_ancestor(v, u))
        return v;
    for (int i = k; i >= 0; --i) {
        if (!is_ancestor(up[u][i], v))
            u = up[u][i];
    }
    return up[u][0];
}

void preprocess(int root) {
    timer = 0;
    k = 25;
    dfs(root, root);
}


void solve(){
	cin >> n >> m >> q;
	for(int i = 2;i <= n;i++){
		ll u,v;
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(int i =1;i<=m;i++) cin >> a[i],pt[a[i]].in(i);
	preprocess(1);
	for(int i = 1;i <= m-1;i++){
		ll u = lca(a[i],a[i+1]);
		wow[u].in(i);
	}
	while(q--){
		ll t;
		cin >> t;
		if(t == 1){
			ll pos,v;
			cin >> pos >> v;
			// ll old = lca(a[pos],a[pos-1]);
			// ll old2 = lca(a[pos],a[pos+1]);
			pt[a[pos]].erase(pt[a[pos]].find(pos));
			if(pos != 1){
				ll old = lca(a[pos],a[pos-1]);
				wow[old].erase(wow[old].find(pos-1));
			}
			if(pos != m){
				ll old = lca(a[pos],a[pos+1]);
				wow[old].erase(wow[old].find(pos));
			}
			a[pos] = v;
			if(pos != 1){
				ll ne = lca(a[pos],a[pos-1]);
				wow[ne].in(pos-1);
			}
			if(pos != m){
				ll ne = lca(a[pos],a[pos+1]);
				wow[ne].in(pos);
			}
			pt[a[pos]].in(pos);
		}
		else{
			ll l,r,v;
			cin >> l >> r >> v;
			bool yay = false;
			auto it = pt[v].lower_bound(l);
			if(it != pt[v].end()){
				// cout << "HERE " << l << ' ' << r << endl;
				ll nd = *it;
				if(nd <= r){
					cout << nd << " " << nd << endl;
					yay = true;
				}
			}
			if(yay) continue;
			it = wow[v].lower_bound(l);
			if(it != wow[v].end()){
				int nd = *it;
				if((nd + 1) <= r){
					cout << nd << " " << nd+1 << endl;
					yay = true;
				}
			}
			if(!yay)
				cout << "-1 -1\n";
		}
	}
}
signed main(){
 
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
     // freopen(".in","r",stdin);freopen(".out","w",stdout);
    
     ll tt=1;   
     // cin >> tt;
    while(tt--){    
        solve();
    }    
}
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23808 KB n=5
2 Correct 14 ms 23808 KB n=100
3 Correct 13 ms 23808 KB n=100
4 Correct 13 ms 23808 KB n=100
5 Correct 13 ms 23808 KB n=100
6 Correct 14 ms 23808 KB n=100
7 Correct 14 ms 23808 KB n=100
8 Correct 14 ms 23860 KB n=100
9 Correct 13 ms 23936 KB n=100
10 Correct 13 ms 23808 KB n=100
11 Correct 14 ms 23808 KB n=100
12 Correct 13 ms 23808 KB n=100
13 Correct 14 ms 23808 KB n=100
14 Correct 14 ms 23808 KB n=100
15 Correct 15 ms 23936 KB n=100
16 Correct 13 ms 23808 KB n=100
17 Correct 14 ms 23808 KB n=100
18 Correct 13 ms 23936 KB n=100
19 Correct 14 ms 23808 KB n=100
20 Correct 14 ms 23808 KB n=100
21 Correct 14 ms 23808 KB n=100
22 Correct 15 ms 23808 KB n=100
23 Correct 14 ms 23808 KB n=100
24 Correct 16 ms 23808 KB n=100
25 Correct 13 ms 23808 KB n=100
26 Correct 14 ms 23808 KB n=12
27 Correct 14 ms 23808 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23808 KB n=5
2 Correct 14 ms 23808 KB n=100
3 Correct 13 ms 23808 KB n=100
4 Correct 13 ms 23808 KB n=100
5 Correct 13 ms 23808 KB n=100
6 Correct 14 ms 23808 KB n=100
7 Correct 14 ms 23808 KB n=100
8 Correct 14 ms 23860 KB n=100
9 Correct 13 ms 23936 KB n=100
10 Correct 13 ms 23808 KB n=100
11 Correct 14 ms 23808 KB n=100
12 Correct 13 ms 23808 KB n=100
13 Correct 14 ms 23808 KB n=100
14 Correct 14 ms 23808 KB n=100
15 Correct 15 ms 23936 KB n=100
16 Correct 13 ms 23808 KB n=100
17 Correct 14 ms 23808 KB n=100
18 Correct 13 ms 23936 KB n=100
19 Correct 14 ms 23808 KB n=100
20 Correct 14 ms 23808 KB n=100
21 Correct 14 ms 23808 KB n=100
22 Correct 15 ms 23808 KB n=100
23 Correct 14 ms 23808 KB n=100
24 Correct 16 ms 23808 KB n=100
25 Correct 13 ms 23808 KB n=100
26 Correct 14 ms 23808 KB n=12
27 Correct 14 ms 23808 KB n=100
28 Correct 20 ms 23936 KB n=500
29 Correct 19 ms 24064 KB n=500
30 Correct 19 ms 24064 KB n=500
31 Correct 17 ms 24064 KB n=500
32 Correct 15 ms 23936 KB n=500
33 Correct 15 ms 24064 KB n=500
34 Correct 15 ms 23936 KB n=500
35 Correct 14 ms 24064 KB n=500
36 Correct 14 ms 23936 KB n=500
37 Correct 14 ms 23936 KB n=500
38 Correct 16 ms 23936 KB n=500
39 Correct 15 ms 24064 KB n=500
40 Correct 14 ms 24064 KB n=500
41 Correct 14 ms 24064 KB n=500
42 Correct 15 ms 23936 KB n=500
43 Correct 14 ms 23936 KB n=500
44 Correct 15 ms 23936 KB n=500
45 Correct 14 ms 23936 KB n=500
46 Correct 14 ms 24064 KB n=500
47 Correct 14 ms 24064 KB n=500
48 Correct 14 ms 23936 KB n=500
49 Correct 15 ms 24064 KB n=500
50 Correct 14 ms 23936 KB n=500
51 Correct 14 ms 24064 KB n=500
52 Correct 14 ms 24064 KB n=500
53 Correct 15 ms 23936 KB n=500
54 Correct 14 ms 24064 KB n=500
55 Correct 14 ms 23936 KB n=278
56 Correct 14 ms 24064 KB n=500
57 Correct 14 ms 24064 KB n=500
58 Correct 15 ms 24064 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23808 KB n=5
2 Correct 14 ms 23808 KB n=100
3 Correct 13 ms 23808 KB n=100
4 Correct 13 ms 23808 KB n=100
5 Correct 13 ms 23808 KB n=100
6 Correct 14 ms 23808 KB n=100
7 Correct 14 ms 23808 KB n=100
8 Correct 14 ms 23860 KB n=100
9 Correct 13 ms 23936 KB n=100
10 Correct 13 ms 23808 KB n=100
11 Correct 14 ms 23808 KB n=100
12 Correct 13 ms 23808 KB n=100
13 Correct 14 ms 23808 KB n=100
14 Correct 14 ms 23808 KB n=100
15 Correct 15 ms 23936 KB n=100
16 Correct 13 ms 23808 KB n=100
17 Correct 14 ms 23808 KB n=100
18 Correct 13 ms 23936 KB n=100
19 Correct 14 ms 23808 KB n=100
20 Correct 14 ms 23808 KB n=100
21 Correct 14 ms 23808 KB n=100
22 Correct 15 ms 23808 KB n=100
23 Correct 14 ms 23808 KB n=100
24 Correct 16 ms 23808 KB n=100
25 Correct 13 ms 23808 KB n=100
26 Correct 14 ms 23808 KB n=12
27 Correct 14 ms 23808 KB n=100
28 Correct 20 ms 23936 KB n=500
29 Correct 19 ms 24064 KB n=500
30 Correct 19 ms 24064 KB n=500
31 Correct 17 ms 24064 KB n=500
32 Correct 15 ms 23936 KB n=500
33 Correct 15 ms 24064 KB n=500
34 Correct 15 ms 23936 KB n=500
35 Correct 14 ms 24064 KB n=500
36 Correct 14 ms 23936 KB n=500
37 Correct 14 ms 23936 KB n=500
38 Correct 16 ms 23936 KB n=500
39 Correct 15 ms 24064 KB n=500
40 Correct 14 ms 24064 KB n=500
41 Correct 14 ms 24064 KB n=500
42 Correct 15 ms 23936 KB n=500
43 Correct 14 ms 23936 KB n=500
44 Correct 15 ms 23936 KB n=500
45 Correct 14 ms 23936 KB n=500
46 Correct 14 ms 24064 KB n=500
47 Correct 14 ms 24064 KB n=500
48 Correct 14 ms 23936 KB n=500
49 Correct 15 ms 24064 KB n=500
50 Correct 14 ms 23936 KB n=500
51 Correct 14 ms 24064 KB n=500
52 Correct 14 ms 24064 KB n=500
53 Correct 15 ms 23936 KB n=500
54 Correct 14 ms 24064 KB n=500
55 Correct 14 ms 23936 KB n=278
56 Correct 14 ms 24064 KB n=500
57 Correct 14 ms 24064 KB n=500
58 Correct 15 ms 24064 KB n=500
59 Correct 18 ms 24576 KB n=2000
60 Correct 16 ms 24576 KB n=2000
61 Correct 17 ms 24576 KB n=2000
62 Correct 19 ms 24576 KB n=2000
63 Correct 18 ms 24576 KB n=2000
64 Correct 18 ms 24576 KB n=2000
65 Correct 18 ms 24568 KB n=2000
66 Correct 17 ms 24576 KB n=2000
67 Correct 18 ms 24448 KB n=2000
68 Correct 17 ms 24576 KB n=2000
69 Correct 16 ms 24576 KB n=2000
70 Correct 17 ms 24572 KB n=2000
71 Correct 17 ms 24576 KB n=2000
72 Correct 16 ms 24448 KB n=2000
73 Correct 16 ms 24576 KB n=2000
74 Correct 17 ms 24448 KB n=1844
75 Correct 16 ms 24576 KB n=2000
76 Correct 17 ms 24448 KB n=2000
77 Correct 19 ms 24448 KB n=2000
78 Correct 18 ms 24448 KB n=2000
79 Correct 19 ms 24568 KB n=2000
80 Correct 17 ms 24576 KB n=2000
81 Correct 17 ms 24576 KB n=2000
82 Correct 18 ms 24576 KB n=2000
83 Correct 17 ms 24576 KB n=2000
84 Correct 18 ms 24576 KB n=2000
85 Correct 18 ms 24568 KB n=2000
86 Correct 17 ms 24576 KB n=2000
87 Correct 18 ms 24576 KB n=2000
88 Correct 16 ms 24576 KB n=2000
89 Correct 16 ms 24576 KB n=2000
90 Correct 16 ms 24576 KB n=2000
91 Correct 17 ms 24576 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23808 KB n=5
2 Correct 14 ms 23808 KB n=100
3 Correct 13 ms 23808 KB n=100
4 Correct 13 ms 23808 KB n=100
5 Correct 13 ms 23808 KB n=100
6 Correct 14 ms 23808 KB n=100
7 Correct 14 ms 23808 KB n=100
8 Correct 14 ms 23860 KB n=100
9 Correct 13 ms 23936 KB n=100
10 Correct 13 ms 23808 KB n=100
11 Correct 14 ms 23808 KB n=100
12 Correct 13 ms 23808 KB n=100
13 Correct 14 ms 23808 KB n=100
14 Correct 14 ms 23808 KB n=100
15 Correct 15 ms 23936 KB n=100
16 Correct 13 ms 23808 KB n=100
17 Correct 14 ms 23808 KB n=100
18 Correct 13 ms 23936 KB n=100
19 Correct 14 ms 23808 KB n=100
20 Correct 14 ms 23808 KB n=100
21 Correct 14 ms 23808 KB n=100
22 Correct 15 ms 23808 KB n=100
23 Correct 14 ms 23808 KB n=100
24 Correct 16 ms 23808 KB n=100
25 Correct 13 ms 23808 KB n=100
26 Correct 14 ms 23808 KB n=12
27 Correct 14 ms 23808 KB n=100
28 Correct 20 ms 23936 KB n=500
29 Correct 19 ms 24064 KB n=500
30 Correct 19 ms 24064 KB n=500
31 Correct 17 ms 24064 KB n=500
32 Correct 15 ms 23936 KB n=500
33 Correct 15 ms 24064 KB n=500
34 Correct 15 ms 23936 KB n=500
35 Correct 14 ms 24064 KB n=500
36 Correct 14 ms 23936 KB n=500
37 Correct 14 ms 23936 KB n=500
38 Correct 16 ms 23936 KB n=500
39 Correct 15 ms 24064 KB n=500
40 Correct 14 ms 24064 KB n=500
41 Correct 14 ms 24064 KB n=500
42 Correct 15 ms 23936 KB n=500
43 Correct 14 ms 23936 KB n=500
44 Correct 15 ms 23936 KB n=500
45 Correct 14 ms 23936 KB n=500
46 Correct 14 ms 24064 KB n=500
47 Correct 14 ms 24064 KB n=500
48 Correct 14 ms 23936 KB n=500
49 Correct 15 ms 24064 KB n=500
50 Correct 14 ms 23936 KB n=500
51 Correct 14 ms 24064 KB n=500
52 Correct 14 ms 24064 KB n=500
53 Correct 15 ms 23936 KB n=500
54 Correct 14 ms 24064 KB n=500
55 Correct 14 ms 23936 KB n=278
56 Correct 14 ms 24064 KB n=500
57 Correct 14 ms 24064 KB n=500
58 Correct 15 ms 24064 KB n=500
59 Correct 18 ms 24576 KB n=2000
60 Correct 16 ms 24576 KB n=2000
61 Correct 17 ms 24576 KB n=2000
62 Correct 19 ms 24576 KB n=2000
63 Correct 18 ms 24576 KB n=2000
64 Correct 18 ms 24576 KB n=2000
65 Correct 18 ms 24568 KB n=2000
66 Correct 17 ms 24576 KB n=2000
67 Correct 18 ms 24448 KB n=2000
68 Correct 17 ms 24576 KB n=2000
69 Correct 16 ms 24576 KB n=2000
70 Correct 17 ms 24572 KB n=2000
71 Correct 17 ms 24576 KB n=2000
72 Correct 16 ms 24448 KB n=2000
73 Correct 16 ms 24576 KB n=2000
74 Correct 17 ms 24448 KB n=1844
75 Correct 16 ms 24576 KB n=2000
76 Correct 17 ms 24448 KB n=2000
77 Correct 19 ms 24448 KB n=2000
78 Correct 18 ms 24448 KB n=2000
79 Correct 19 ms 24568 KB n=2000
80 Correct 17 ms 24576 KB n=2000
81 Correct 17 ms 24576 KB n=2000
82 Correct 18 ms 24576 KB n=2000
83 Correct 17 ms 24576 KB n=2000
84 Correct 18 ms 24576 KB n=2000
85 Correct 18 ms 24568 KB n=2000
86 Correct 17 ms 24576 KB n=2000
87 Correct 18 ms 24576 KB n=2000
88 Correct 16 ms 24576 KB n=2000
89 Correct 16 ms 24576 KB n=2000
90 Correct 16 ms 24576 KB n=2000
91 Correct 17 ms 24576 KB n=2000
92 Correct 882 ms 91000 KB n=200000
93 Correct 937 ms 95968 KB n=200000
94 Correct 706 ms 99320 KB n=200000
95 Correct 813 ms 90984 KB n=200000
96 Correct 773 ms 90860 KB n=200000
97 Correct 1043 ms 95104 KB n=200000
98 Correct 829 ms 90960 KB n=200000
99 Correct 1063 ms 91000 KB n=200000
100 Correct 828 ms 90868 KB n=200000
101 Correct 658 ms 100476 KB n=200000
102 Correct 512 ms 92024 KB n=200000
103 Correct 512 ms 92076 KB n=200000
104 Correct 510 ms 92072 KB n=200000
105 Correct 500 ms 92408 KB n=200000
106 Correct 502 ms 92536 KB n=200000
107 Correct 505 ms 92536 KB n=200000
108 Correct 894 ms 90964 KB n=200000
109 Correct 933 ms 91060 KB n=200000
110 Correct 952 ms 90872 KB n=200000
111 Correct 912 ms 90352 KB n=200000
112 Correct 704 ms 99468 KB n=200000
113 Correct 1056 ms 95000 KB n=200000
114 Correct 860 ms 90400 KB n=200000
115 Correct 1308 ms 92956 KB n=200000
116 Correct 826 ms 90988 KB n=200000
117 Correct 689 ms 99916 KB n=200000
118 Correct 1070 ms 93816 KB n=200000
119 Correct 782 ms 90988 KB n=200000
120 Correct 591 ms 99452 KB n=200000
121 Correct 573 ms 99540 KB n=200000
122 Correct 594 ms 100088 KB n=200000
123 Correct 526 ms 92280 KB n=200000
124 Correct 218 ms 40772 KB n=25264