답안 #441957

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
441957 2021-07-06T15:20:40 Z parsabahrami Birthday gift (IZhO18_treearray) C++17
100 / 100
2865 ms 201236 KB
/* I do it for the glory */
#include <bits/stdc++.h>
 
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
 
#define SZ(x)                       (int) x.size()
#define F                           first
#define S                           second

const int N = 2e5 + 10, MOD = 1e9 + 7, SQ = 2000;
int H[N], P[19][N], A[N], B[N], M[N / SQ + 1][N], M2[N / SQ + 1][N], n, m, q;
vector<int> adj[N];

void preDFS(int v, int p = -1) {
    if (!~p) P[0][v] = v;
    for (int i = 1; i < 19; i++)
        P[i][v] = P[i - 1][P[i - 1][v]];
    for (int &u : adj[v]) 
        if (u != p) 
            H[u] = H[v] + 1, P[0][u] = v, preDFS(u, v);
}

int LCA(int u, int v) {
    if (H[u] < H[v]) swap(u, v);
    for (int i = H[u] - H[v]; i; i -= i & -i) 
        u = P[__builtin_ctz(i)][u];
    for (int i = 18; ~i; i--) 
        if (P[i][u] != P[i][v]) 
            u = P[i][u], v = P[i][v];
    return u == v ? v : P[0][v];
}

void upd(int id, int v) {
    M2[id / SQ][A[id]]--;
    A[id] = v;
    M2[id / SQ][A[id]]++;
    if (id) {
        M[(id - 1) / SQ][B[id - 1]]--;
        B[id - 1] = LCA(A[id], A[id - 1]);
        M[(id - 1) / SQ][B[id - 1]]++; 
    }
    if (id + 1 < m) { 
        M[id / SQ][B[id]]--;
        B[id] = LCA(A[id], A[id + 1]);
        M[id / SQ][B[id]]++;
    }
}

void timee() { 
    while (1) {}
}

int main() {
    scanf("%d%d%d", &n, &m, &q);
    for (int i = 1; i < n; i++) {
        int u, v; scanf("%d%d", &u, &v);
        adj[u].push_back(v), adj[v].push_back(u);
    }
    preDFS(1);
    for (int i = 0; i < m; i++) {
        scanf("%d", &A[i]); 
        if (i / SQ > N / SQ + 3) timee();
        if (A[i] >= N) timee();
         M2[i / SQ][A[i]]++;
        if (i) {
            B[i - 1] = LCA(A[i - 1], A[i]);
            if (B[i - 1] < 0 || B[i - 1] >= N) timee();
            M[(i - 1) / SQ][B[i - 1]]++;
        }
    }
    for (; q; q--) {
        int t; scanf("%d", &t);
        if (t < 2) {
            int p, v; scanf("%d%d", &p, &v);
            upd(p - 1, v);
        } else {
            int l, r, v; scanf("%d%d%d", &l, &r, &v);
            pii ret = {-1, -1}; int f = 0; l--, r--; r--;
            for (int i = l; i <= r && !f; ) {
                if (i % SQ == 0 && i + SQ - 1 <= r) {
                    if (M[i / SQ][v]) {
                        for (int j = i; j < i + SQ && !f; j++) 
                            if (B[j] == v) { f = 1, ret = {j + 1, j + 2}; }
                    } else i += SQ;
                } else {
                    if (B[i] == v) { f = 1, ret = {i + 1, i + 2}; }
                    else i++;
                }
            }
            for (int i = l; i <= r + 1 && !f; ) {
                if (i % SQ == 0 && i + SQ <= r) {
                    if (M2[i / SQ][v]) {
                        for (int j = i; j < i + SQ && !f; j++) {
                            if (A[j] == v) { f = 1; ret = {j + 1, j + 1}; }
                        }
                    } else i += SQ;
                } else {
                    if (A[i] == v) { f = 1; ret = {i + 1, i + 1}; }
                    else i++;
                }
            }
            printf("%d %d\n", ret.F, ret.S);
        }
    }
    return 0;
}

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:57:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |     scanf("%d%d%d", &n, &m, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:59:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |         int u, v; scanf("%d%d", &u, &v);
      |                   ~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:64:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |         scanf("%d", &A[i]);
      |         ~~~~~^~~~~~~~~~~~~
treearray.cpp:75:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |         int t; scanf("%d", &t);
      |                ~~~~~^~~~~~~~~~
treearray.cpp:77:28: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |             int p, v; scanf("%d%d", &p, &v);
      |                       ~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:80:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |             int l, r, v; scanf("%d%d%d", &l, &r, &v);
      |                          ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5068 KB n=5
2 Correct 4 ms 5068 KB n=100
3 Correct 4 ms 5068 KB n=100
4 Correct 3 ms 5068 KB n=100
5 Correct 3 ms 5068 KB n=100
6 Correct 4 ms 5068 KB n=100
7 Correct 4 ms 5068 KB n=100
8 Correct 4 ms 5132 KB n=100
9 Correct 4 ms 5068 KB n=100
10 Correct 4 ms 5100 KB n=100
11 Correct 4 ms 5068 KB n=100
12 Correct 4 ms 5068 KB n=100
13 Correct 4 ms 5068 KB n=100
14 Correct 3 ms 5068 KB n=100
15 Correct 3 ms 5068 KB n=100
16 Correct 4 ms 5068 KB n=100
17 Correct 4 ms 5068 KB n=100
18 Correct 3 ms 5068 KB n=100
19 Correct 3 ms 5068 KB n=100
20 Correct 4 ms 5068 KB n=100
21 Correct 4 ms 5068 KB n=100
22 Correct 3 ms 5068 KB n=100
23 Correct 4 ms 5068 KB n=100
24 Correct 4 ms 5068 KB n=100
25 Correct 3 ms 5068 KB n=100
26 Correct 3 ms 5068 KB n=12
27 Correct 3 ms 5140 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5068 KB n=5
2 Correct 4 ms 5068 KB n=100
3 Correct 4 ms 5068 KB n=100
4 Correct 3 ms 5068 KB n=100
5 Correct 3 ms 5068 KB n=100
6 Correct 4 ms 5068 KB n=100
7 Correct 4 ms 5068 KB n=100
8 Correct 4 ms 5132 KB n=100
9 Correct 4 ms 5068 KB n=100
10 Correct 4 ms 5100 KB n=100
11 Correct 4 ms 5068 KB n=100
12 Correct 4 ms 5068 KB n=100
13 Correct 4 ms 5068 KB n=100
14 Correct 3 ms 5068 KB n=100
15 Correct 3 ms 5068 KB n=100
16 Correct 4 ms 5068 KB n=100
17 Correct 4 ms 5068 KB n=100
18 Correct 3 ms 5068 KB n=100
19 Correct 3 ms 5068 KB n=100
20 Correct 4 ms 5068 KB n=100
21 Correct 4 ms 5068 KB n=100
22 Correct 3 ms 5068 KB n=100
23 Correct 4 ms 5068 KB n=100
24 Correct 4 ms 5068 KB n=100
25 Correct 3 ms 5068 KB n=100
26 Correct 3 ms 5068 KB n=12
27 Correct 3 ms 5140 KB n=100
28 Correct 4 ms 5192 KB n=500
29 Correct 5 ms 5196 KB n=500
30 Correct 4 ms 5196 KB n=500
31 Correct 4 ms 5196 KB n=500
32 Correct 4 ms 5196 KB n=500
33 Correct 4 ms 5196 KB n=500
34 Correct 4 ms 5204 KB n=500
35 Correct 5 ms 5196 KB n=500
36 Correct 4 ms 5068 KB n=500
37 Correct 4 ms 5068 KB n=500
38 Correct 4 ms 5068 KB n=500
39 Correct 4 ms 5068 KB n=500
40 Correct 5 ms 5068 KB n=500
41 Correct 4 ms 5196 KB n=500
42 Correct 4 ms 5196 KB n=500
43 Correct 4 ms 5164 KB n=500
44 Correct 5 ms 5068 KB n=500
45 Correct 4 ms 5180 KB n=500
46 Correct 4 ms 5196 KB n=500
47 Correct 4 ms 5196 KB n=500
48 Correct 4 ms 5196 KB n=500
49 Correct 4 ms 5196 KB n=500
50 Correct 4 ms 5196 KB n=500
51 Correct 4 ms 5196 KB n=500
52 Correct 4 ms 5196 KB n=500
53 Correct 4 ms 5196 KB n=500
54 Correct 4 ms 5196 KB n=500
55 Correct 4 ms 5068 KB n=278
56 Correct 4 ms 5224 KB n=500
57 Correct 4 ms 5196 KB n=500
58 Correct 4 ms 5196 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5068 KB n=5
2 Correct 4 ms 5068 KB n=100
3 Correct 4 ms 5068 KB n=100
4 Correct 3 ms 5068 KB n=100
5 Correct 3 ms 5068 KB n=100
6 Correct 4 ms 5068 KB n=100
7 Correct 4 ms 5068 KB n=100
8 Correct 4 ms 5132 KB n=100
9 Correct 4 ms 5068 KB n=100
10 Correct 4 ms 5100 KB n=100
11 Correct 4 ms 5068 KB n=100
12 Correct 4 ms 5068 KB n=100
13 Correct 4 ms 5068 KB n=100
14 Correct 3 ms 5068 KB n=100
15 Correct 3 ms 5068 KB n=100
16 Correct 4 ms 5068 KB n=100
17 Correct 4 ms 5068 KB n=100
18 Correct 3 ms 5068 KB n=100
19 Correct 3 ms 5068 KB n=100
20 Correct 4 ms 5068 KB n=100
21 Correct 4 ms 5068 KB n=100
22 Correct 3 ms 5068 KB n=100
23 Correct 4 ms 5068 KB n=100
24 Correct 4 ms 5068 KB n=100
25 Correct 3 ms 5068 KB n=100
26 Correct 3 ms 5068 KB n=12
27 Correct 3 ms 5140 KB n=100
28 Correct 4 ms 5192 KB n=500
29 Correct 5 ms 5196 KB n=500
30 Correct 4 ms 5196 KB n=500
31 Correct 4 ms 5196 KB n=500
32 Correct 4 ms 5196 KB n=500
33 Correct 4 ms 5196 KB n=500
34 Correct 4 ms 5204 KB n=500
35 Correct 5 ms 5196 KB n=500
36 Correct 4 ms 5068 KB n=500
37 Correct 4 ms 5068 KB n=500
38 Correct 4 ms 5068 KB n=500
39 Correct 4 ms 5068 KB n=500
40 Correct 5 ms 5068 KB n=500
41 Correct 4 ms 5196 KB n=500
42 Correct 4 ms 5196 KB n=500
43 Correct 4 ms 5164 KB n=500
44 Correct 5 ms 5068 KB n=500
45 Correct 4 ms 5180 KB n=500
46 Correct 4 ms 5196 KB n=500
47 Correct 4 ms 5196 KB n=500
48 Correct 4 ms 5196 KB n=500
49 Correct 4 ms 5196 KB n=500
50 Correct 4 ms 5196 KB n=500
51 Correct 4 ms 5196 KB n=500
52 Correct 4 ms 5196 KB n=500
53 Correct 4 ms 5196 KB n=500
54 Correct 4 ms 5196 KB n=500
55 Correct 4 ms 5068 KB n=278
56 Correct 4 ms 5224 KB n=500
57 Correct 4 ms 5196 KB n=500
58 Correct 4 ms 5196 KB n=500
59 Correct 6 ms 5324 KB n=2000
60 Correct 7 ms 5452 KB n=2000
61 Correct 7 ms 5436 KB n=2000
62 Correct 7 ms 5324 KB n=2000
63 Correct 6 ms 5324 KB n=2000
64 Correct 7 ms 5428 KB n=2000
65 Correct 7 ms 5324 KB n=2000
66 Correct 9 ms 5468 KB n=2000
67 Correct 6 ms 5324 KB n=2000
68 Correct 7 ms 5436 KB n=2000
69 Correct 10 ms 5384 KB n=2000
70 Correct 10 ms 5376 KB n=2000
71 Correct 13 ms 5324 KB n=2000
72 Correct 10 ms 5324 KB n=2000
73 Correct 12 ms 5364 KB n=2000
74 Correct 7 ms 5264 KB n=1844
75 Correct 10 ms 5376 KB n=2000
76 Correct 8 ms 5324 KB n=2000
77 Correct 8 ms 5324 KB n=2000
78 Correct 9 ms 5384 KB n=2000
79 Correct 8 ms 5324 KB n=2000
80 Correct 8 ms 5480 KB n=2000
81 Correct 7 ms 5424 KB n=2000
82 Correct 6 ms 5400 KB n=2000
83 Correct 7 ms 5452 KB n=2000
84 Correct 8 ms 5324 KB n=2000
85 Correct 8 ms 5324 KB n=2000
86 Correct 8 ms 5324 KB n=2000
87 Correct 11 ms 5392 KB n=2000
88 Correct 13 ms 5500 KB n=2000
89 Correct 13 ms 5452 KB n=2000
90 Correct 8 ms 5452 KB n=2000
91 Correct 10 ms 5396 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5068 KB n=5
2 Correct 4 ms 5068 KB n=100
3 Correct 4 ms 5068 KB n=100
4 Correct 3 ms 5068 KB n=100
5 Correct 3 ms 5068 KB n=100
6 Correct 4 ms 5068 KB n=100
7 Correct 4 ms 5068 KB n=100
8 Correct 4 ms 5132 KB n=100
9 Correct 4 ms 5068 KB n=100
10 Correct 4 ms 5100 KB n=100
11 Correct 4 ms 5068 KB n=100
12 Correct 4 ms 5068 KB n=100
13 Correct 4 ms 5068 KB n=100
14 Correct 3 ms 5068 KB n=100
15 Correct 3 ms 5068 KB n=100
16 Correct 4 ms 5068 KB n=100
17 Correct 4 ms 5068 KB n=100
18 Correct 3 ms 5068 KB n=100
19 Correct 3 ms 5068 KB n=100
20 Correct 4 ms 5068 KB n=100
21 Correct 4 ms 5068 KB n=100
22 Correct 3 ms 5068 KB n=100
23 Correct 4 ms 5068 KB n=100
24 Correct 4 ms 5068 KB n=100
25 Correct 3 ms 5068 KB n=100
26 Correct 3 ms 5068 KB n=12
27 Correct 3 ms 5140 KB n=100
28 Correct 4 ms 5192 KB n=500
29 Correct 5 ms 5196 KB n=500
30 Correct 4 ms 5196 KB n=500
31 Correct 4 ms 5196 KB n=500
32 Correct 4 ms 5196 KB n=500
33 Correct 4 ms 5196 KB n=500
34 Correct 4 ms 5204 KB n=500
35 Correct 5 ms 5196 KB n=500
36 Correct 4 ms 5068 KB n=500
37 Correct 4 ms 5068 KB n=500
38 Correct 4 ms 5068 KB n=500
39 Correct 4 ms 5068 KB n=500
40 Correct 5 ms 5068 KB n=500
41 Correct 4 ms 5196 KB n=500
42 Correct 4 ms 5196 KB n=500
43 Correct 4 ms 5164 KB n=500
44 Correct 5 ms 5068 KB n=500
45 Correct 4 ms 5180 KB n=500
46 Correct 4 ms 5196 KB n=500
47 Correct 4 ms 5196 KB n=500
48 Correct 4 ms 5196 KB n=500
49 Correct 4 ms 5196 KB n=500
50 Correct 4 ms 5196 KB n=500
51 Correct 4 ms 5196 KB n=500
52 Correct 4 ms 5196 KB n=500
53 Correct 4 ms 5196 KB n=500
54 Correct 4 ms 5196 KB n=500
55 Correct 4 ms 5068 KB n=278
56 Correct 4 ms 5224 KB n=500
57 Correct 4 ms 5196 KB n=500
58 Correct 4 ms 5196 KB n=500
59 Correct 6 ms 5324 KB n=2000
60 Correct 7 ms 5452 KB n=2000
61 Correct 7 ms 5436 KB n=2000
62 Correct 7 ms 5324 KB n=2000
63 Correct 6 ms 5324 KB n=2000
64 Correct 7 ms 5428 KB n=2000
65 Correct 7 ms 5324 KB n=2000
66 Correct 9 ms 5468 KB n=2000
67 Correct 6 ms 5324 KB n=2000
68 Correct 7 ms 5436 KB n=2000
69 Correct 10 ms 5384 KB n=2000
70 Correct 10 ms 5376 KB n=2000
71 Correct 13 ms 5324 KB n=2000
72 Correct 10 ms 5324 KB n=2000
73 Correct 12 ms 5364 KB n=2000
74 Correct 7 ms 5264 KB n=1844
75 Correct 10 ms 5376 KB n=2000
76 Correct 8 ms 5324 KB n=2000
77 Correct 8 ms 5324 KB n=2000
78 Correct 9 ms 5384 KB n=2000
79 Correct 8 ms 5324 KB n=2000
80 Correct 8 ms 5480 KB n=2000
81 Correct 7 ms 5424 KB n=2000
82 Correct 6 ms 5400 KB n=2000
83 Correct 7 ms 5452 KB n=2000
84 Correct 8 ms 5324 KB n=2000
85 Correct 8 ms 5324 KB n=2000
86 Correct 8 ms 5324 KB n=2000
87 Correct 11 ms 5392 KB n=2000
88 Correct 13 ms 5500 KB n=2000
89 Correct 13 ms 5452 KB n=2000
90 Correct 8 ms 5452 KB n=2000
91 Correct 10 ms 5396 KB n=2000
92 Correct 1152 ms 109192 KB n=200000
93 Correct 1717 ms 194992 KB n=200000
94 Correct 1842 ms 199500 KB n=200000
95 Correct 1167 ms 113880 KB n=200000
96 Correct 1169 ms 113896 KB n=200000
97 Correct 1649 ms 193908 KB n=200000
98 Correct 1054 ms 113884 KB n=200000
99 Correct 1199 ms 114500 KB n=200000
100 Correct 1131 ms 113896 KB n=200000
101 Correct 1755 ms 201236 KB n=200000
102 Correct 1028 ms 114844 KB n=200000
103 Correct 944 ms 114744 KB n=200000
104 Correct 992 ms 114892 KB n=200000
105 Correct 1341 ms 114492 KB n=200000
106 Correct 1379 ms 114644 KB n=200000
107 Correct 1349 ms 114620 KB n=200000
108 Correct 1311 ms 114552 KB n=200000
109 Correct 1297 ms 114516 KB n=200000
110 Correct 1309 ms 114700 KB n=200000
111 Correct 1027 ms 113748 KB n=200000
112 Correct 1722 ms 199840 KB n=200000
113 Correct 1627 ms 193788 KB n=200000
114 Correct 908 ms 113776 KB n=200000
115 Correct 1506 ms 190708 KB n=200000
116 Correct 1866 ms 113684 KB n=200000
117 Correct 2072 ms 200516 KB n=200000
118 Correct 2150 ms 192348 KB n=200000
119 Correct 1952 ms 113556 KB n=200000
120 Correct 2779 ms 201088 KB n=200000
121 Correct 2865 ms 201088 KB n=200000
122 Correct 1629 ms 200804 KB n=200000
123 Correct 1175 ms 114972 KB n=200000
124 Correct 217 ms 21580 KB n=25264