Submission #343837

# Submission time Handle Problem Language Result Execution time Memory
343837 2021-01-04T14:26:47 Z mat_v Birthday gift (IZhO18_treearray) C++14
100 / 100
1211 ms 84588 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define mod 998244353
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int,int>
#define maxn 200005

using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;/// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());


int n,m,q;

vector<int> graf[maxn];
int cale[maxn][20];
int in[maxn], out[maxn];
int t;
int niz[maxn];

void root(int x, int y){
    cale[x][0] = y;
    in[x] = t;
    ff(i,1,19)cale[x][i] = cale[cale[x][i - 1]][i - 1];
    for(auto c:graf[x]){
        if(c == y)continue;
        t++;
        root(c, x);
    }
    out[x] = t;
}

bool subtr(int x, int y){
    if(x == 0)return true;
    if(in[y] >= in[x] && in[y] <= out[x])return true;
    return false;
}

int lca(int x, int y){
    if(subtr(x,y))return x;
    if(subtr(y,x))return y;
    fb(i,19,0){
        if(subtr(cale[x][i],y))continue;
        x = cale[x][i];
    }
    return cale[x][0];
}

set<int> op[maxn], p[maxn];

void obrisi(int pos){
    op[niz[pos]].erase(pos);
    if(pos > 1)p[lca(niz[pos - 1], niz[pos])].erase(pos - 1);
    if(pos < m)p[lca(niz[pos], niz[pos + 1])].erase(pos);
}

void dodaj(int pos, int v){
    niz[pos] = v;
    op[v].insert(pos);
    if(pos > 1)p[lca(niz[pos - 1], niz[pos])].insert(pos - 1);
    if(pos < m)p[lca(niz[pos + 1], niz[pos])].insert(pos);
}

int main()
{

    ios_base::sync_with_stdio(false); cin.tie(0);

    cin >> n >> m >> q;
    ff(i,1,n - 1){
        int a,b;
        cin >> a >> b;
        graf[a].pb(b);
        graf[b].pb(a);
    }
    root(1, 0);
    ff(i,1,m){
        cin >> niz[i];
        op[niz[i]].insert(i);
    }
    ff(i,1,m - 1){
        //cout << niz[i] << " " << niz[i + 1] << lca(niz[i], niz[i + 1]) << "\n";
        p[lca(niz[i], niz[i + 1])].insert(i);
    }



    while(q--){
        int idx;
        cin >> idx;
        if(idx == 1){
            int pos,v;
            cin >> pos >> v;
            obrisi(pos);
            dodaj(pos, v);
            continue;
        }
        int l,r,v;
        cin >> l >> r >> v;
        auto it1 = op[v].lower_bound(l);
        int v1 = m+5;
        auto it2 = p[v].lower_bound(l);
        int v2 = m+5;
        if(it1 != op[v].end())v1 = *it1;
        if(it2 != p[v].end())v2 = *it2;
        if(v1 <= r){
            cout << v1 << " " << v1 << "\n";
        }
        else if(v2 <= r-1){
            cout << v2 << " " << v2 + 1 << "\n";
        }
        else cout << "-1 -1\n";
    }

    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

treearray.cpp: In function 'void root(int, int)':
treearray.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
treearray.cpp:34:5: note: in expansion of macro 'ff'
   34 |     ff(i,1,19)cale[x][i] = cale[cale[x][i - 1]][i - 1];
      |     ^~
treearray.cpp: In function 'int lca(int, int)':
treearray.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    7 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
treearray.cpp:52:5: note: in expansion of macro 'fb'
   52 |     fb(i,19,0){
      |     ^~
treearray.cpp: In function 'int main()':
treearray.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
treearray.cpp:80:5: note: in expansion of macro 'ff'
   80 |     ff(i,1,n - 1){
      |     ^~
treearray.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
treearray.cpp:87:5: note: in expansion of macro 'ff'
   87 |     ff(i,1,m){
      |     ^~
treearray.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
treearray.cpp:91:5: note: in expansion of macro 'ff'
   91 |     ff(i,1,m - 1){
      |     ^~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 24064 KB n=5
2 Correct 14 ms 23916 KB n=100
3 Correct 14 ms 23916 KB n=100
4 Correct 15 ms 23916 KB n=100
5 Correct 14 ms 23916 KB n=100
6 Correct 16 ms 23916 KB n=100
7 Correct 15 ms 23916 KB n=100
8 Correct 16 ms 23916 KB n=100
9 Correct 15 ms 23916 KB n=100
10 Correct 14 ms 23916 KB n=100
11 Correct 15 ms 23916 KB n=100
12 Correct 15 ms 23916 KB n=100
13 Correct 14 ms 23916 KB n=100
14 Correct 14 ms 23916 KB n=100
15 Correct 14 ms 23916 KB n=100
16 Correct 15 ms 23916 KB n=100
17 Correct 15 ms 23916 KB n=100
18 Correct 14 ms 23916 KB n=100
19 Correct 14 ms 23916 KB n=100
20 Correct 15 ms 23916 KB n=100
21 Correct 15 ms 23916 KB n=100
22 Correct 14 ms 23916 KB n=100
23 Correct 14 ms 23916 KB n=100
24 Correct 14 ms 23916 KB n=100
25 Correct 15 ms 23916 KB n=100
26 Correct 15 ms 23916 KB n=12
27 Correct 17 ms 23896 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 14 ms 24064 KB n=5
2 Correct 14 ms 23916 KB n=100
3 Correct 14 ms 23916 KB n=100
4 Correct 15 ms 23916 KB n=100
5 Correct 14 ms 23916 KB n=100
6 Correct 16 ms 23916 KB n=100
7 Correct 15 ms 23916 KB n=100
8 Correct 16 ms 23916 KB n=100
9 Correct 15 ms 23916 KB n=100
10 Correct 14 ms 23916 KB n=100
11 Correct 15 ms 23916 KB n=100
12 Correct 15 ms 23916 KB n=100
13 Correct 14 ms 23916 KB n=100
14 Correct 14 ms 23916 KB n=100
15 Correct 14 ms 23916 KB n=100
16 Correct 15 ms 23916 KB n=100
17 Correct 15 ms 23916 KB n=100
18 Correct 14 ms 23916 KB n=100
19 Correct 14 ms 23916 KB n=100
20 Correct 15 ms 23916 KB n=100
21 Correct 15 ms 23916 KB n=100
22 Correct 14 ms 23916 KB n=100
23 Correct 14 ms 23916 KB n=100
24 Correct 14 ms 23916 KB n=100
25 Correct 15 ms 23916 KB n=100
26 Correct 15 ms 23916 KB n=12
27 Correct 17 ms 23896 KB n=100
28 Correct 16 ms 23916 KB n=500
29 Correct 15 ms 24044 KB n=500
30 Correct 15 ms 23916 KB n=500
31 Correct 16 ms 24044 KB n=500
32 Correct 15 ms 23916 KB n=500
33 Correct 15 ms 23916 KB n=500
34 Correct 16 ms 24044 KB n=500
35 Correct 15 ms 24044 KB n=500
36 Correct 16 ms 23916 KB n=500
37 Correct 15 ms 23916 KB n=500
38 Correct 15 ms 23916 KB n=500
39 Correct 15 ms 23916 KB n=500
40 Correct 17 ms 24044 KB n=500
41 Correct 15 ms 23916 KB n=500
42 Correct 15 ms 23916 KB n=500
43 Correct 15 ms 23916 KB n=500
44 Correct 15 ms 23916 KB n=500
45 Correct 15 ms 23916 KB n=500
46 Correct 15 ms 24044 KB n=500
47 Correct 15 ms 24044 KB n=500
48 Correct 15 ms 23916 KB n=500
49 Correct 15 ms 24044 KB n=500
50 Correct 15 ms 23916 KB n=500
51 Correct 15 ms 24192 KB n=500
52 Correct 15 ms 24044 KB n=500
53 Correct 19 ms 23916 KB n=500
54 Correct 16 ms 24044 KB n=500
55 Correct 15 ms 23916 KB n=278
56 Correct 15 ms 24044 KB n=500
57 Correct 15 ms 24044 KB n=500
58 Correct 16 ms 23916 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 14 ms 24064 KB n=5
2 Correct 14 ms 23916 KB n=100
3 Correct 14 ms 23916 KB n=100
4 Correct 15 ms 23916 KB n=100
5 Correct 14 ms 23916 KB n=100
6 Correct 16 ms 23916 KB n=100
7 Correct 15 ms 23916 KB n=100
8 Correct 16 ms 23916 KB n=100
9 Correct 15 ms 23916 KB n=100
10 Correct 14 ms 23916 KB n=100
11 Correct 15 ms 23916 KB n=100
12 Correct 15 ms 23916 KB n=100
13 Correct 14 ms 23916 KB n=100
14 Correct 14 ms 23916 KB n=100
15 Correct 14 ms 23916 KB n=100
16 Correct 15 ms 23916 KB n=100
17 Correct 15 ms 23916 KB n=100
18 Correct 14 ms 23916 KB n=100
19 Correct 14 ms 23916 KB n=100
20 Correct 15 ms 23916 KB n=100
21 Correct 15 ms 23916 KB n=100
22 Correct 14 ms 23916 KB n=100
23 Correct 14 ms 23916 KB n=100
24 Correct 14 ms 23916 KB n=100
25 Correct 15 ms 23916 KB n=100
26 Correct 15 ms 23916 KB n=12
27 Correct 17 ms 23896 KB n=100
28 Correct 16 ms 23916 KB n=500
29 Correct 15 ms 24044 KB n=500
30 Correct 15 ms 23916 KB n=500
31 Correct 16 ms 24044 KB n=500
32 Correct 15 ms 23916 KB n=500
33 Correct 15 ms 23916 KB n=500
34 Correct 16 ms 24044 KB n=500
35 Correct 15 ms 24044 KB n=500
36 Correct 16 ms 23916 KB n=500
37 Correct 15 ms 23916 KB n=500
38 Correct 15 ms 23916 KB n=500
39 Correct 15 ms 23916 KB n=500
40 Correct 17 ms 24044 KB n=500
41 Correct 15 ms 23916 KB n=500
42 Correct 15 ms 23916 KB n=500
43 Correct 15 ms 23916 KB n=500
44 Correct 15 ms 23916 KB n=500
45 Correct 15 ms 23916 KB n=500
46 Correct 15 ms 24044 KB n=500
47 Correct 15 ms 24044 KB n=500
48 Correct 15 ms 23916 KB n=500
49 Correct 15 ms 24044 KB n=500
50 Correct 15 ms 23916 KB n=500
51 Correct 15 ms 24192 KB n=500
52 Correct 15 ms 24044 KB n=500
53 Correct 19 ms 23916 KB n=500
54 Correct 16 ms 24044 KB n=500
55 Correct 15 ms 23916 KB n=278
56 Correct 15 ms 24044 KB n=500
57 Correct 15 ms 24044 KB n=500
58 Correct 16 ms 23916 KB n=500
59 Correct 18 ms 24300 KB n=2000
60 Correct 18 ms 24428 KB n=2000
61 Correct 18 ms 24428 KB n=2000
62 Correct 18 ms 24428 KB n=2000
63 Correct 19 ms 24300 KB n=2000
64 Correct 18 ms 24428 KB n=2000
65 Correct 19 ms 24320 KB n=2000
66 Correct 18 ms 24428 KB n=2000
67 Correct 19 ms 24300 KB n=2000
68 Correct 19 ms 24420 KB n=2000
69 Correct 17 ms 24300 KB n=2000
70 Correct 18 ms 24300 KB n=2000
71 Correct 18 ms 24300 KB n=2000
72 Correct 17 ms 24300 KB n=2000
73 Correct 17 ms 24300 KB n=2000
74 Correct 18 ms 24300 KB n=1844
75 Correct 17 ms 24300 KB n=2000
76 Correct 19 ms 24300 KB n=2000
77 Correct 18 ms 24300 KB n=2000
78 Correct 18 ms 24300 KB n=2000
79 Correct 18 ms 24300 KB n=2000
80 Correct 18 ms 24428 KB n=2000
81 Correct 18 ms 24428 KB n=2000
82 Correct 18 ms 24300 KB n=2000
83 Correct 19 ms 24428 KB n=2000
84 Correct 18 ms 24300 KB n=2000
85 Correct 18 ms 24428 KB n=2000
86 Correct 18 ms 24428 KB n=2000
87 Correct 18 ms 24300 KB n=2000
88 Correct 18 ms 24428 KB n=2000
89 Correct 18 ms 24556 KB n=2000
90 Correct 17 ms 24428 KB n=2000
91 Correct 17 ms 24300 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 14 ms 24064 KB n=5
2 Correct 14 ms 23916 KB n=100
3 Correct 14 ms 23916 KB n=100
4 Correct 15 ms 23916 KB n=100
5 Correct 14 ms 23916 KB n=100
6 Correct 16 ms 23916 KB n=100
7 Correct 15 ms 23916 KB n=100
8 Correct 16 ms 23916 KB n=100
9 Correct 15 ms 23916 KB n=100
10 Correct 14 ms 23916 KB n=100
11 Correct 15 ms 23916 KB n=100
12 Correct 15 ms 23916 KB n=100
13 Correct 14 ms 23916 KB n=100
14 Correct 14 ms 23916 KB n=100
15 Correct 14 ms 23916 KB n=100
16 Correct 15 ms 23916 KB n=100
17 Correct 15 ms 23916 KB n=100
18 Correct 14 ms 23916 KB n=100
19 Correct 14 ms 23916 KB n=100
20 Correct 15 ms 23916 KB n=100
21 Correct 15 ms 23916 KB n=100
22 Correct 14 ms 23916 KB n=100
23 Correct 14 ms 23916 KB n=100
24 Correct 14 ms 23916 KB n=100
25 Correct 15 ms 23916 KB n=100
26 Correct 15 ms 23916 KB n=12
27 Correct 17 ms 23896 KB n=100
28 Correct 16 ms 23916 KB n=500
29 Correct 15 ms 24044 KB n=500
30 Correct 15 ms 23916 KB n=500
31 Correct 16 ms 24044 KB n=500
32 Correct 15 ms 23916 KB n=500
33 Correct 15 ms 23916 KB n=500
34 Correct 16 ms 24044 KB n=500
35 Correct 15 ms 24044 KB n=500
36 Correct 16 ms 23916 KB n=500
37 Correct 15 ms 23916 KB n=500
38 Correct 15 ms 23916 KB n=500
39 Correct 15 ms 23916 KB n=500
40 Correct 17 ms 24044 KB n=500
41 Correct 15 ms 23916 KB n=500
42 Correct 15 ms 23916 KB n=500
43 Correct 15 ms 23916 KB n=500
44 Correct 15 ms 23916 KB n=500
45 Correct 15 ms 23916 KB n=500
46 Correct 15 ms 24044 KB n=500
47 Correct 15 ms 24044 KB n=500
48 Correct 15 ms 23916 KB n=500
49 Correct 15 ms 24044 KB n=500
50 Correct 15 ms 23916 KB n=500
51 Correct 15 ms 24192 KB n=500
52 Correct 15 ms 24044 KB n=500
53 Correct 19 ms 23916 KB n=500
54 Correct 16 ms 24044 KB n=500
55 Correct 15 ms 23916 KB n=278
56 Correct 15 ms 24044 KB n=500
57 Correct 15 ms 24044 KB n=500
58 Correct 16 ms 23916 KB n=500
59 Correct 18 ms 24300 KB n=2000
60 Correct 18 ms 24428 KB n=2000
61 Correct 18 ms 24428 KB n=2000
62 Correct 18 ms 24428 KB n=2000
63 Correct 19 ms 24300 KB n=2000
64 Correct 18 ms 24428 KB n=2000
65 Correct 19 ms 24320 KB n=2000
66 Correct 18 ms 24428 KB n=2000
67 Correct 19 ms 24300 KB n=2000
68 Correct 19 ms 24420 KB n=2000
69 Correct 17 ms 24300 KB n=2000
70 Correct 18 ms 24300 KB n=2000
71 Correct 18 ms 24300 KB n=2000
72 Correct 17 ms 24300 KB n=2000
73 Correct 17 ms 24300 KB n=2000
74 Correct 18 ms 24300 KB n=1844
75 Correct 17 ms 24300 KB n=2000
76 Correct 19 ms 24300 KB n=2000
77 Correct 18 ms 24300 KB n=2000
78 Correct 18 ms 24300 KB n=2000
79 Correct 18 ms 24300 KB n=2000
80 Correct 18 ms 24428 KB n=2000
81 Correct 18 ms 24428 KB n=2000
82 Correct 18 ms 24300 KB n=2000
83 Correct 19 ms 24428 KB n=2000
84 Correct 18 ms 24300 KB n=2000
85 Correct 18 ms 24428 KB n=2000
86 Correct 18 ms 24428 KB n=2000
87 Correct 18 ms 24300 KB n=2000
88 Correct 18 ms 24428 KB n=2000
89 Correct 18 ms 24556 KB n=2000
90 Correct 17 ms 24428 KB n=2000
91 Correct 17 ms 24300 KB n=2000
92 Correct 827 ms 75468 KB n=200000
93 Correct 942 ms 80064 KB n=200000
94 Correct 703 ms 83308 KB n=200000
95 Correct 802 ms 75360 KB n=200000
96 Correct 805 ms 75488 KB n=200000
97 Correct 1024 ms 79264 KB n=200000
98 Correct 820 ms 75360 KB n=200000
99 Correct 967 ms 75756 KB n=200000
100 Correct 823 ms 75272 KB n=200000
101 Correct 633 ms 84588 KB n=200000
102 Correct 477 ms 76524 KB n=200000
103 Correct 484 ms 76652 KB n=200000
104 Correct 482 ms 76524 KB n=200000
105 Correct 477 ms 76780 KB n=200000
106 Correct 482 ms 77036 KB n=200000
107 Correct 477 ms 76780 KB n=200000
108 Correct 868 ms 75372 KB n=200000
109 Correct 870 ms 75500 KB n=200000
110 Correct 863 ms 75376 KB n=200000
111 Correct 818 ms 74724 KB n=200000
112 Correct 664 ms 83564 KB n=200000
113 Correct 981 ms 78960 KB n=200000
114 Correct 843 ms 74736 KB n=200000
115 Correct 1211 ms 77116 KB n=200000
116 Correct 755 ms 74932 KB n=200000
117 Correct 638 ms 83716 KB n=200000
118 Correct 1088 ms 77884 KB n=200000
119 Correct 741 ms 75488 KB n=200000
120 Correct 572 ms 83940 KB n=200000
121 Correct 570 ms 83820 KB n=200000
122 Correct 589 ms 83948 KB n=200000
123 Correct 485 ms 76140 KB n=200000
124 Correct 214 ms 38892 KB n=25264