Submission #334889

# Submission time Handle Problem Language Result Execution time Memory
334889 2020-12-10T07:04:07 Z talant117408 Birthday gift (IZhO18_treearray) C++17
100 / 100
2423 ms 60396 KB
/*
    Code written by Talant I.D.
*/
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>

//using namespace __gnu_pbds;
using namespace std;

//typedef tree <int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
  
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2*acos(0.0)
#define endl "\n"
#define sz(v) int((v).size())
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define OK cout << "OK" << endl;
  
inline bool isvowel(char ch){
    ch = tolower(ch);
    return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}
  
inline bool isprime(int n){
    if(n < 2 || (n%2 == 0 && n != 2)) return false;
    for(int i = 3; i*i <= n; i++)
        if(n%i == 0) return false;
    return true;
}
 
class Union{
    private:
        vector <int> saizu, link;
    public:
        Union(int n){
            saizu.assign(n, 1); link.resize(n); 
            iota(all(link), 0);
        }
        int find(int n){
            if(link[n] == n) return n;
            return link[n] = find(link[n]);
        }
        int same(int a, int b){
            return find(a) == find(b);
        }
        void unite(int a, int b){
            if(same(a, b)) return;
             
            a = find(a); b = find(b);
            if(saizu[a] < saizu[b]) swap(a, b);
             
            saizu[a] += saizu[b];
            link[b] = a;
        }
        int getsize(int a){
            return saizu[find(a)];
        }
};
 
const int mod = 1e9+7;
 
ll mode(ll a){
    a %= mod;
    if(a < 0) a += mod;
    return a;
}
 
ll subt(ll a, ll b){
    return mode(mode(a)-mode(b));
}
 
ll add(ll a, ll b){
    return mode(mode(a)+mode(b));
}
 
ll mult(ll a, ll b){
    return mode(mode(a)*mode(b));
}
 
ll binpow(ll a, ll b){
    ll res = 1;
    while(b){
        if(b&1) res = mult(res, a);
        a = mult(a, a);
        b >>= 1;
    }
    return res;
}

const int N = 2e5+7;
vector <vector <int>> graph(N);
set <pii> st, t;
int tree[N*4], vec[N], tin[N], tout[N], timer, up[N][20];

void dfs(int v, int p = 1){
    tin[v] = ++timer;
    up[v][0] = p;
    for(int i = 1; i < 20; i++) up[v][i] = up[up[v][i-1]][i-1];
    
    for(auto to : graph[v]){
        if(to != p){
            dfs(to, v);
        }
    }
    tout[v] = ++timer;
}

bool upper(int a, int b){
    return tin[a] <= tin[b] && tout[a] >= tout[b];
}

int lca(int a, int b){
    if(upper(a, b)) return a;
    if(upper(b, a)) return b;
    for(int i = 19; i >= 0; i--){
        if(!upper(up[a][i], b)) a = up[a][i];
    }
    return up[a][0];
}

int main(){
   // do_not_disturb
    
    int n, m, q;
    cin >> n >> m >> q;
    for(int i = 0; i < n-1; i++){
        int x, y;
        cin >> x >> y;
        graph[x].pb(y);
        graph[y].pb(x);
    }
    
    dfs(1);
    
    for(int i = 1; i <= m; i++){
        cin >> vec[i];
    }
    for(int i = 1; i <= m; i++){
        t.insert(mp(vec[i], i));
        if(i < m) st.insert(mp(lca(vec[i], vec[i+1]), i));
    }
    
    while(q--){
        int type;
        cin >> type;
        if(type == 1){
            int pos, x;
            cin >> pos >> x;
            t.erase(mp(vec[pos], pos));
            if(pos > 1) st.erase(mp(lca(vec[pos], vec[pos-1]), pos-1));
            if(pos < m) st.erase(mp(lca(vec[pos], vec[pos+1]), pos));
            vec[pos] = x;
            t.insert(mp(vec[pos], pos));
            if(pos > 1) st.insert(mp(lca(vec[pos], vec[pos-1]), pos-1));
            if(pos < m) st.insert(mp(lca(vec[pos], vec[pos+1]), pos));
        }
        else{
            int l, r, x;
            cin >> l >> r >> x;
            auto to = t.lb(mp(x, l));
            if(to != t.end() && to -> first == x && to -> second <= r){
                cout << (*to).second << ' ' << (*to).second << endl;
            }
            else{
                auto it = st.lb(mp(x, l));
                if(it != st.end() && it -> first == x && it -> second < r){
                    cout << (*it).second << ' ' << (*it).second+1 << endl;
                }
                else{
                    cout << "-1 -1" << endl;
                }
            }
        }
    }
    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4972 KB n=5
2 Correct 4 ms 5100 KB n=100
3 Correct 4 ms 5100 KB n=100
4 Correct 4 ms 5100 KB n=100
5 Correct 5 ms 5100 KB n=100
6 Correct 4 ms 5100 KB n=100
7 Correct 4 ms 5100 KB n=100
8 Correct 4 ms 5100 KB n=100
9 Correct 4 ms 5004 KB n=100
10 Correct 4 ms 5100 KB n=100
11 Correct 5 ms 5100 KB n=100
12 Correct 4 ms 5100 KB n=100
13 Correct 4 ms 5100 KB n=100
14 Correct 4 ms 5100 KB n=100
15 Correct 4 ms 5100 KB n=100
16 Correct 4 ms 5100 KB n=100
17 Correct 4 ms 5100 KB n=100
18 Correct 4 ms 5100 KB n=100
19 Correct 4 ms 5100 KB n=100
20 Correct 4 ms 5100 KB n=100
21 Correct 4 ms 5100 KB n=100
22 Correct 4 ms 5100 KB n=100
23 Correct 4 ms 5100 KB n=100
24 Correct 4 ms 5100 KB n=100
25 Correct 4 ms 5100 KB n=100
26 Correct 4 ms 5100 KB n=12
27 Correct 4 ms 5100 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4972 KB n=5
2 Correct 4 ms 5100 KB n=100
3 Correct 4 ms 5100 KB n=100
4 Correct 4 ms 5100 KB n=100
5 Correct 5 ms 5100 KB n=100
6 Correct 4 ms 5100 KB n=100
7 Correct 4 ms 5100 KB n=100
8 Correct 4 ms 5100 KB n=100
9 Correct 4 ms 5004 KB n=100
10 Correct 4 ms 5100 KB n=100
11 Correct 5 ms 5100 KB n=100
12 Correct 4 ms 5100 KB n=100
13 Correct 4 ms 5100 KB n=100
14 Correct 4 ms 5100 KB n=100
15 Correct 4 ms 5100 KB n=100
16 Correct 4 ms 5100 KB n=100
17 Correct 4 ms 5100 KB n=100
18 Correct 4 ms 5100 KB n=100
19 Correct 4 ms 5100 KB n=100
20 Correct 4 ms 5100 KB n=100
21 Correct 4 ms 5100 KB n=100
22 Correct 4 ms 5100 KB n=100
23 Correct 4 ms 5100 KB n=100
24 Correct 4 ms 5100 KB n=100
25 Correct 4 ms 5100 KB n=100
26 Correct 4 ms 5100 KB n=12
27 Correct 4 ms 5100 KB n=100
28 Correct 6 ms 5100 KB n=500
29 Correct 6 ms 5100 KB n=500
30 Correct 6 ms 5100 KB n=500
31 Correct 7 ms 5100 KB n=500
32 Correct 6 ms 5100 KB n=500
33 Correct 6 ms 5100 KB n=500
34 Correct 6 ms 5100 KB n=500
35 Correct 6 ms 5100 KB n=500
36 Correct 6 ms 5100 KB n=500
37 Correct 6 ms 5100 KB n=500
38 Correct 6 ms 5100 KB n=500
39 Correct 6 ms 5100 KB n=500
40 Correct 6 ms 5100 KB n=500
41 Correct 6 ms 5100 KB n=500
42 Correct 6 ms 5100 KB n=500
43 Correct 6 ms 5100 KB n=500
44 Correct 7 ms 5100 KB n=500
45 Correct 6 ms 5100 KB n=500
46 Correct 6 ms 5100 KB n=500
47 Correct 6 ms 5100 KB n=500
48 Correct 6 ms 5100 KB n=500
49 Correct 6 ms 5100 KB n=500
50 Correct 6 ms 5100 KB n=500
51 Correct 6 ms 5120 KB n=500
52 Correct 6 ms 5100 KB n=500
53 Correct 8 ms 5100 KB n=500
54 Correct 6 ms 5100 KB n=500
55 Correct 5 ms 5100 KB n=278
56 Correct 6 ms 5248 KB n=500
57 Correct 6 ms 5100 KB n=500
58 Correct 6 ms 5100 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4972 KB n=5
2 Correct 4 ms 5100 KB n=100
3 Correct 4 ms 5100 KB n=100
4 Correct 4 ms 5100 KB n=100
5 Correct 5 ms 5100 KB n=100
6 Correct 4 ms 5100 KB n=100
7 Correct 4 ms 5100 KB n=100
8 Correct 4 ms 5100 KB n=100
9 Correct 4 ms 5004 KB n=100
10 Correct 4 ms 5100 KB n=100
11 Correct 5 ms 5100 KB n=100
12 Correct 4 ms 5100 KB n=100
13 Correct 4 ms 5100 KB n=100
14 Correct 4 ms 5100 KB n=100
15 Correct 4 ms 5100 KB n=100
16 Correct 4 ms 5100 KB n=100
17 Correct 4 ms 5100 KB n=100
18 Correct 4 ms 5100 KB n=100
19 Correct 4 ms 5100 KB n=100
20 Correct 4 ms 5100 KB n=100
21 Correct 4 ms 5100 KB n=100
22 Correct 4 ms 5100 KB n=100
23 Correct 4 ms 5100 KB n=100
24 Correct 4 ms 5100 KB n=100
25 Correct 4 ms 5100 KB n=100
26 Correct 4 ms 5100 KB n=12
27 Correct 4 ms 5100 KB n=100
28 Correct 6 ms 5100 KB n=500
29 Correct 6 ms 5100 KB n=500
30 Correct 6 ms 5100 KB n=500
31 Correct 7 ms 5100 KB n=500
32 Correct 6 ms 5100 KB n=500
33 Correct 6 ms 5100 KB n=500
34 Correct 6 ms 5100 KB n=500
35 Correct 6 ms 5100 KB n=500
36 Correct 6 ms 5100 KB n=500
37 Correct 6 ms 5100 KB n=500
38 Correct 6 ms 5100 KB n=500
39 Correct 6 ms 5100 KB n=500
40 Correct 6 ms 5100 KB n=500
41 Correct 6 ms 5100 KB n=500
42 Correct 6 ms 5100 KB n=500
43 Correct 6 ms 5100 KB n=500
44 Correct 7 ms 5100 KB n=500
45 Correct 6 ms 5100 KB n=500
46 Correct 6 ms 5100 KB n=500
47 Correct 6 ms 5100 KB n=500
48 Correct 6 ms 5100 KB n=500
49 Correct 6 ms 5100 KB n=500
50 Correct 6 ms 5100 KB n=500
51 Correct 6 ms 5120 KB n=500
52 Correct 6 ms 5100 KB n=500
53 Correct 8 ms 5100 KB n=500
54 Correct 6 ms 5100 KB n=500
55 Correct 5 ms 5100 KB n=278
56 Correct 6 ms 5248 KB n=500
57 Correct 6 ms 5100 KB n=500
58 Correct 6 ms 5100 KB n=500
59 Correct 12 ms 5484 KB n=2000
60 Correct 14 ms 5612 KB n=2000
61 Correct 13 ms 5484 KB n=2000
62 Correct 13 ms 5484 KB n=2000
63 Correct 12 ms 5484 KB n=2000
64 Correct 15 ms 5484 KB n=2000
65 Correct 12 ms 5484 KB n=2000
66 Correct 12 ms 5484 KB n=2000
67 Correct 13 ms 5484 KB n=2000
68 Correct 13 ms 5484 KB n=2000
69 Correct 18 ms 5484 KB n=2000
70 Correct 19 ms 5484 KB n=2000
71 Correct 14 ms 5484 KB n=2000
72 Correct 13 ms 5484 KB n=2000
73 Correct 13 ms 5484 KB n=2000
74 Correct 11 ms 5484 KB n=1844
75 Correct 14 ms 5484 KB n=2000
76 Correct 13 ms 5484 KB n=2000
77 Correct 13 ms 5484 KB n=2000
78 Correct 14 ms 5484 KB n=2000
79 Correct 12 ms 5516 KB n=2000
80 Correct 12 ms 5484 KB n=2000
81 Correct 15 ms 5484 KB n=2000
82 Correct 13 ms 5484 KB n=2000
83 Correct 13 ms 5484 KB n=2000
84 Correct 12 ms 5484 KB n=2000
85 Correct 14 ms 5484 KB n=2000
86 Correct 15 ms 5484 KB n=2000
87 Correct 14 ms 5612 KB n=2000
88 Correct 12 ms 5504 KB n=2000
89 Correct 12 ms 5612 KB n=2000
90 Correct 12 ms 5612 KB n=2000
91 Correct 15 ms 5484 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4972 KB n=5
2 Correct 4 ms 5100 KB n=100
3 Correct 4 ms 5100 KB n=100
4 Correct 4 ms 5100 KB n=100
5 Correct 5 ms 5100 KB n=100
6 Correct 4 ms 5100 KB n=100
7 Correct 4 ms 5100 KB n=100
8 Correct 4 ms 5100 KB n=100
9 Correct 4 ms 5004 KB n=100
10 Correct 4 ms 5100 KB n=100
11 Correct 5 ms 5100 KB n=100
12 Correct 4 ms 5100 KB n=100
13 Correct 4 ms 5100 KB n=100
14 Correct 4 ms 5100 KB n=100
15 Correct 4 ms 5100 KB n=100
16 Correct 4 ms 5100 KB n=100
17 Correct 4 ms 5100 KB n=100
18 Correct 4 ms 5100 KB n=100
19 Correct 4 ms 5100 KB n=100
20 Correct 4 ms 5100 KB n=100
21 Correct 4 ms 5100 KB n=100
22 Correct 4 ms 5100 KB n=100
23 Correct 4 ms 5100 KB n=100
24 Correct 4 ms 5100 KB n=100
25 Correct 4 ms 5100 KB n=100
26 Correct 4 ms 5100 KB n=12
27 Correct 4 ms 5100 KB n=100
28 Correct 6 ms 5100 KB n=500
29 Correct 6 ms 5100 KB n=500
30 Correct 6 ms 5100 KB n=500
31 Correct 7 ms 5100 KB n=500
32 Correct 6 ms 5100 KB n=500
33 Correct 6 ms 5100 KB n=500
34 Correct 6 ms 5100 KB n=500
35 Correct 6 ms 5100 KB n=500
36 Correct 6 ms 5100 KB n=500
37 Correct 6 ms 5100 KB n=500
38 Correct 6 ms 5100 KB n=500
39 Correct 6 ms 5100 KB n=500
40 Correct 6 ms 5100 KB n=500
41 Correct 6 ms 5100 KB n=500
42 Correct 6 ms 5100 KB n=500
43 Correct 6 ms 5100 KB n=500
44 Correct 7 ms 5100 KB n=500
45 Correct 6 ms 5100 KB n=500
46 Correct 6 ms 5100 KB n=500
47 Correct 6 ms 5100 KB n=500
48 Correct 6 ms 5100 KB n=500
49 Correct 6 ms 5100 KB n=500
50 Correct 6 ms 5100 KB n=500
51 Correct 6 ms 5120 KB n=500
52 Correct 6 ms 5100 KB n=500
53 Correct 8 ms 5100 KB n=500
54 Correct 6 ms 5100 KB n=500
55 Correct 5 ms 5100 KB n=278
56 Correct 6 ms 5248 KB n=500
57 Correct 6 ms 5100 KB n=500
58 Correct 6 ms 5100 KB n=500
59 Correct 12 ms 5484 KB n=2000
60 Correct 14 ms 5612 KB n=2000
61 Correct 13 ms 5484 KB n=2000
62 Correct 13 ms 5484 KB n=2000
63 Correct 12 ms 5484 KB n=2000
64 Correct 15 ms 5484 KB n=2000
65 Correct 12 ms 5484 KB n=2000
66 Correct 12 ms 5484 KB n=2000
67 Correct 13 ms 5484 KB n=2000
68 Correct 13 ms 5484 KB n=2000
69 Correct 18 ms 5484 KB n=2000
70 Correct 19 ms 5484 KB n=2000
71 Correct 14 ms 5484 KB n=2000
72 Correct 13 ms 5484 KB n=2000
73 Correct 13 ms 5484 KB n=2000
74 Correct 11 ms 5484 KB n=1844
75 Correct 14 ms 5484 KB n=2000
76 Correct 13 ms 5484 KB n=2000
77 Correct 13 ms 5484 KB n=2000
78 Correct 14 ms 5484 KB n=2000
79 Correct 12 ms 5516 KB n=2000
80 Correct 12 ms 5484 KB n=2000
81 Correct 15 ms 5484 KB n=2000
82 Correct 13 ms 5484 KB n=2000
83 Correct 13 ms 5484 KB n=2000
84 Correct 12 ms 5484 KB n=2000
85 Correct 14 ms 5484 KB n=2000
86 Correct 15 ms 5484 KB n=2000
87 Correct 14 ms 5612 KB n=2000
88 Correct 12 ms 5504 KB n=2000
89 Correct 12 ms 5612 KB n=2000
90 Correct 12 ms 5612 KB n=2000
91 Correct 15 ms 5484 KB n=2000
92 Correct 1736 ms 51948 KB n=200000
93 Correct 2140 ms 55788 KB n=200000
94 Correct 1826 ms 59372 KB n=200000
95 Correct 1638 ms 52196 KB n=200000
96 Correct 1584 ms 52064 KB n=200000
97 Correct 2196 ms 54764 KB n=200000
98 Correct 1622 ms 52192 KB n=200000
99 Correct 1872 ms 51428 KB n=200000
100 Correct 1612 ms 52168 KB n=200000
101 Correct 1730 ms 60396 KB n=200000
102 Correct 1412 ms 53152 KB n=200000
103 Correct 1374 ms 52992 KB n=200000
104 Correct 1371 ms 53228 KB n=200000
105 Correct 1398 ms 52588 KB n=200000
106 Correct 1394 ms 52616 KB n=200000
107 Correct 1397 ms 52620 KB n=200000
108 Correct 1797 ms 51564 KB n=200000
109 Correct 1735 ms 51052 KB n=200000
110 Correct 1752 ms 51180 KB n=200000
111 Correct 1526 ms 51552 KB n=200000
112 Correct 1780 ms 58732 KB n=200000
113 Correct 2181 ms 54380 KB n=200000
114 Correct 1509 ms 51296 KB n=200000
115 Correct 2423 ms 52156 KB n=200000
116 Correct 1624 ms 51044 KB n=200000
117 Correct 1782 ms 58828 KB n=200000
118 Correct 2296 ms 52972 KB n=200000
119 Correct 1633 ms 50784 KB n=200000
120 Correct 1559 ms 58804 KB n=200000
121 Correct 1557 ms 58860 KB n=200000
122 Correct 1593 ms 59116 KB n=200000
123 Correct 1422 ms 51052 KB n=200000
124 Correct 472 ms 18796 KB n=25264