Submission #793746

# Submission time Handle Problem Language Result Execution time Memory
793746 2023-07-26T06:13:45 Z vjudge1 Tourism (JOI23_tourism) C++17
100 / 100
472 ms 33704 KB
#include <bits/stdc++.h>

#define fi first
#define se second

const int N = 100100;
const int mod = 1e9 + 7;

using namespace std;

int n;
int m;
int q;
int s[N];
int st[N];
int en[N];
int inv[N];
int tin[N];
int tout[N];
int tim;
int a[N];
int f[N][20];
int res[N];
vector<int> v[N];
vector<pair<int, int>> qu[N];

void dfs(int x, int p)
{
    s[x] = 1;
    f[x][0] = p;
    for (int i = 1; i < 20; i++) {
        f[x][i] = f[f[x][i - 1]][i - 1];
    }
    for (int y: v[x]) {
        if (y == p) {
            continue;
        }
        dfs(y, x);
        s[x] += s[y];
    }
}

bool isp(int x, int y)
{
    return tin[x] <= tin[y] && tout[x] >= tout[y];
}

int lca(int x, int y)
{
    if (isp(x, y)) {
        return x;
    } else if (isp(y, x)) {
        return y;
    }
    for (int i = 19; i >= 0; i--) {
        if (!isp(f[x][i], y)) {
            x = f[x][i];
        }
    }
    return f[x][0];
}

void trace(int x, int p)
{
    tin[x] = ++tim;
    inv[tim] = x;
    if (!st[x]) {
        st[x] = tim;
    }

    int big = -1;
    for (int y: v[x]) {
        if(y == p) {
            continue;
        }
        if (big == -1 || s[y] > s[big]) {
            big = y;
        }
    }
    if (big != -1) {
        st[big] = st[x];
        trace(big, x);
        en[x] = en[big];
    } else {
        en[x] = tin[x];
    }
    for (int y: v[x]) {
        if (y == p || y == big) {
            continue;
        }
        trace(y, x);
    }
    tout[x] = tim;
}

int F[N];

void upd(int x, int y)
{
    x = N - x - 10;
    while (x < N) {
        F[x] += y;
        x += x & -x;
    }
}

int get(int x)
{
    x = N - x - 10;
    int res = 0;
    while (x > 0) {
        res += F[x];
        x -= x & -x;
    }
    return res;
}

int t[4 * N];

void push(int x)
{
    if(t[x] != -1) {
        t[x * 2] = t[x * 2 + 1] = t[x];
        t[x] = -1;
    }
}

void cler(int x, int l, int r)
{
    if (t[x] == -2) {
        return;
    } else if (t[x] != -1) {
        if (t[x] > 0) {
            upd(t[x], - (r - l + 1));
            //cout << "- " << t[x] << ' ' << r - l + 1 << "\n";
        }
        t[x] = -2;
        return;
    }
    push(x);
    int m = (l + r) / 2;
    cler(x * 2, l, m);
    cler(x * 2 + 1, m + 1, r);
}

void upd(int x, int l, int r, int tl, int tr, int g)
{
    if (tl > tr) {
        return;
    } else if(l == tl && r == tr) {
        cler(x, l, r);
        t[x] = g;
        //cout << "+ " << g << " " << r - l + 1 << "\n";
        upd(g, r - l + 1);
        return;
    }
    push(x);
    int m = (l + r) / 2;
    upd(x * 2, l, m, tl, min(m, tr), g);
    upd(x * 2 + 1, m + 1, r, max(m + 1, tl), tr, g);
}

void upd(int x, int y, int g)
{
    //cout << x << " " << y << endl;
    while (true) {
        if (st[x] != st[y]) {
            // st[x], tin[x]
            upd(1, 1, n, st[x], tin[x], g);
            x = f[inv[st[x]]][0];
        } else {
            // tin[y], tin[x]
            upd(1, 1, n, tin[y], tin[x], g);
            break;
        }
    }
}

int main() {
    #ifdef zxc
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0);

    cin >> n >> m >> q;
    for (int i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }

    for (int i = 1; i <= m; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= q; i++) {
        int l, r;
        cin >> l >> r;
        qu[r].push_back({l, i});
    }

    dfs(1, 1);
    trace(1, 1);

    for (int i = 1; i <= m; i++) {
        if (i > 1) {
            int p = lca(a[i - 1], a[i]);
            upd(a[i - 1], p, i - 1);
            upd(a[i], p, i - 1);
        }
        upd(a[i], a[i], i);
        for (auto p: qu[i]) {
            res[p.se] = get(p.fi);
        }
    }

    for (int i = 1; i <= q; i++) {
        cout << res[i] << "\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 4 ms 5076 KB Output is correct
5 Correct 4 ms 5076 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 4 ms 5076 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 4 ms 5076 KB Output is correct
12 Correct 4 ms 5076 KB Output is correct
13 Correct 4 ms 5076 KB Output is correct
14 Correct 5 ms 5076 KB Output is correct
15 Correct 4 ms 5108 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 4 ms 5116 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 2 ms 5076 KB Output is correct
22 Correct 4 ms 5076 KB Output is correct
23 Correct 4 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 4 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5084 KB Output is correct
29 Correct 3 ms 5060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 4 ms 5076 KB Output is correct
5 Correct 4 ms 5076 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 4 ms 5076 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 4 ms 5076 KB Output is correct
12 Correct 4 ms 5076 KB Output is correct
13 Correct 4 ms 5076 KB Output is correct
14 Correct 5 ms 5076 KB Output is correct
15 Correct 4 ms 5108 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 4 ms 5116 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 2 ms 5076 KB Output is correct
22 Correct 4 ms 5076 KB Output is correct
23 Correct 4 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 4 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5084 KB Output is correct
29 Correct 3 ms 5060 KB Output is correct
30 Correct 5 ms 5332 KB Output is correct
31 Correct 5 ms 5332 KB Output is correct
32 Correct 6 ms 5332 KB Output is correct
33 Correct 6 ms 5332 KB Output is correct
34 Correct 6 ms 5420 KB Output is correct
35 Correct 6 ms 5332 KB Output is correct
36 Correct 6 ms 5416 KB Output is correct
37 Correct 8 ms 5332 KB Output is correct
38 Correct 6 ms 5460 KB Output is correct
39 Correct 6 ms 5480 KB Output is correct
40 Correct 6 ms 5456 KB Output is correct
41 Correct 5 ms 5460 KB Output is correct
42 Correct 5 ms 5460 KB Output is correct
43 Correct 6 ms 5460 KB Output is correct
44 Correct 7 ms 5484 KB Output is correct
45 Correct 7 ms 5460 KB Output is correct
46 Correct 6 ms 5460 KB Output is correct
47 Correct 7 ms 5460 KB Output is correct
48 Correct 6 ms 5424 KB Output is correct
49 Correct 6 ms 5460 KB Output is correct
50 Correct 7 ms 5332 KB Output is correct
51 Correct 5 ms 5332 KB Output is correct
52 Correct 5 ms 5332 KB Output is correct
53 Correct 5 ms 5332 KB Output is correct
54 Correct 5 ms 5436 KB Output is correct
55 Correct 5 ms 5320 KB Output is correct
56 Correct 4 ms 5076 KB Output is correct
57 Correct 3 ms 5332 KB Output is correct
58 Correct 8 ms 5424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 160 ms 20208 KB Output is correct
5 Correct 138 ms 25432 KB Output is correct
6 Correct 171 ms 27892 KB Output is correct
7 Correct 197 ms 30876 KB Output is correct
8 Correct 197 ms 30844 KB Output is correct
9 Correct 210 ms 30932 KB Output is correct
10 Correct 194 ms 30868 KB Output is correct
11 Correct 198 ms 30836 KB Output is correct
12 Correct 136 ms 30192 KB Output is correct
13 Correct 133 ms 30184 KB Output is correct
14 Correct 152 ms 30172 KB Output is correct
15 Correct 55 ms 27688 KB Output is correct
16 Correct 214 ms 30620 KB Output is correct
17 Correct 39 ms 8396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 142 ms 13356 KB Output is correct
3 Correct 214 ms 15388 KB Output is correct
4 Correct 181 ms 16796 KB Output is correct
5 Correct 371 ms 21888 KB Output is correct
6 Correct 332 ms 21960 KB Output is correct
7 Correct 343 ms 21960 KB Output is correct
8 Correct 331 ms 21900 KB Output is correct
9 Correct 293 ms 21936 KB Output is correct
10 Correct 301 ms 21964 KB Output is correct
11 Correct 326 ms 22008 KB Output is correct
12 Correct 319 ms 22116 KB Output is correct
13 Correct 344 ms 22516 KB Output is correct
14 Correct 322 ms 23364 KB Output is correct
15 Correct 323 ms 26852 KB Output is correct
16 Correct 294 ms 22448 KB Output is correct
17 Correct 300 ms 22452 KB Output is correct
18 Correct 298 ms 22356 KB Output is correct
19 Correct 426 ms 21916 KB Output is correct
20 Correct 380 ms 22044 KB Output is correct
21 Correct 409 ms 21984 KB Output is correct
22 Correct 367 ms 21932 KB Output is correct
23 Correct 376 ms 21892 KB Output is correct
24 Correct 396 ms 22008 KB Output is correct
25 Correct 360 ms 21964 KB Output is correct
26 Correct 390 ms 22100 KB Output is correct
27 Correct 400 ms 22044 KB Output is correct
28 Correct 383 ms 21940 KB Output is correct
29 Correct 372 ms 22024 KB Output is correct
30 Correct 431 ms 22256 KB Output is correct
31 Correct 356 ms 22528 KB Output is correct
32 Correct 386 ms 22944 KB Output is correct
33 Correct 430 ms 24396 KB Output is correct
34 Correct 390 ms 26864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 326 ms 16516 KB Output is correct
5 Correct 324 ms 18876 KB Output is correct
6 Correct 388 ms 23736 KB Output is correct
7 Correct 405 ms 25872 KB Output is correct
8 Correct 415 ms 25976 KB Output is correct
9 Correct 414 ms 25868 KB Output is correct
10 Correct 420 ms 25940 KB Output is correct
11 Correct 407 ms 25900 KB Output is correct
12 Correct 404 ms 25888 KB Output is correct
13 Correct 472 ms 25980 KB Output is correct
14 Correct 44 ms 9668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 4 ms 5076 KB Output is correct
5 Correct 4 ms 5076 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 4 ms 5076 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 4 ms 5076 KB Output is correct
12 Correct 4 ms 5076 KB Output is correct
13 Correct 4 ms 5076 KB Output is correct
14 Correct 5 ms 5076 KB Output is correct
15 Correct 4 ms 5108 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 4 ms 5116 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 2 ms 5076 KB Output is correct
22 Correct 4 ms 5076 KB Output is correct
23 Correct 4 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 4 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5084 KB Output is correct
29 Correct 3 ms 5060 KB Output is correct
30 Correct 5 ms 5332 KB Output is correct
31 Correct 5 ms 5332 KB Output is correct
32 Correct 6 ms 5332 KB Output is correct
33 Correct 6 ms 5332 KB Output is correct
34 Correct 6 ms 5420 KB Output is correct
35 Correct 6 ms 5332 KB Output is correct
36 Correct 6 ms 5416 KB Output is correct
37 Correct 8 ms 5332 KB Output is correct
38 Correct 6 ms 5460 KB Output is correct
39 Correct 6 ms 5480 KB Output is correct
40 Correct 6 ms 5456 KB Output is correct
41 Correct 5 ms 5460 KB Output is correct
42 Correct 5 ms 5460 KB Output is correct
43 Correct 6 ms 5460 KB Output is correct
44 Correct 7 ms 5484 KB Output is correct
45 Correct 7 ms 5460 KB Output is correct
46 Correct 6 ms 5460 KB Output is correct
47 Correct 7 ms 5460 KB Output is correct
48 Correct 6 ms 5424 KB Output is correct
49 Correct 6 ms 5460 KB Output is correct
50 Correct 7 ms 5332 KB Output is correct
51 Correct 5 ms 5332 KB Output is correct
52 Correct 5 ms 5332 KB Output is correct
53 Correct 5 ms 5332 KB Output is correct
54 Correct 5 ms 5436 KB Output is correct
55 Correct 5 ms 5320 KB Output is correct
56 Correct 4 ms 5076 KB Output is correct
57 Correct 3 ms 5332 KB Output is correct
58 Correct 8 ms 5424 KB Output is correct
59 Correct 2 ms 4948 KB Output is correct
60 Correct 2 ms 5076 KB Output is correct
61 Correct 3 ms 5076 KB Output is correct
62 Correct 160 ms 20208 KB Output is correct
63 Correct 138 ms 25432 KB Output is correct
64 Correct 171 ms 27892 KB Output is correct
65 Correct 197 ms 30876 KB Output is correct
66 Correct 197 ms 30844 KB Output is correct
67 Correct 210 ms 30932 KB Output is correct
68 Correct 194 ms 30868 KB Output is correct
69 Correct 198 ms 30836 KB Output is correct
70 Correct 136 ms 30192 KB Output is correct
71 Correct 133 ms 30184 KB Output is correct
72 Correct 152 ms 30172 KB Output is correct
73 Correct 55 ms 27688 KB Output is correct
74 Correct 214 ms 30620 KB Output is correct
75 Correct 39 ms 8396 KB Output is correct
76 Correct 3 ms 5076 KB Output is correct
77 Correct 142 ms 13356 KB Output is correct
78 Correct 214 ms 15388 KB Output is correct
79 Correct 181 ms 16796 KB Output is correct
80 Correct 371 ms 21888 KB Output is correct
81 Correct 332 ms 21960 KB Output is correct
82 Correct 343 ms 21960 KB Output is correct
83 Correct 331 ms 21900 KB Output is correct
84 Correct 293 ms 21936 KB Output is correct
85 Correct 301 ms 21964 KB Output is correct
86 Correct 326 ms 22008 KB Output is correct
87 Correct 319 ms 22116 KB Output is correct
88 Correct 344 ms 22516 KB Output is correct
89 Correct 322 ms 23364 KB Output is correct
90 Correct 323 ms 26852 KB Output is correct
91 Correct 294 ms 22448 KB Output is correct
92 Correct 300 ms 22452 KB Output is correct
93 Correct 298 ms 22356 KB Output is correct
94 Correct 426 ms 21916 KB Output is correct
95 Correct 380 ms 22044 KB Output is correct
96 Correct 409 ms 21984 KB Output is correct
97 Correct 367 ms 21932 KB Output is correct
98 Correct 376 ms 21892 KB Output is correct
99 Correct 396 ms 22008 KB Output is correct
100 Correct 360 ms 21964 KB Output is correct
101 Correct 390 ms 22100 KB Output is correct
102 Correct 400 ms 22044 KB Output is correct
103 Correct 383 ms 21940 KB Output is correct
104 Correct 372 ms 22024 KB Output is correct
105 Correct 431 ms 22256 KB Output is correct
106 Correct 356 ms 22528 KB Output is correct
107 Correct 386 ms 22944 KB Output is correct
108 Correct 430 ms 24396 KB Output is correct
109 Correct 390 ms 26864 KB Output is correct
110 Correct 2 ms 4948 KB Output is correct
111 Correct 2 ms 5076 KB Output is correct
112 Correct 3 ms 5076 KB Output is correct
113 Correct 326 ms 16516 KB Output is correct
114 Correct 324 ms 18876 KB Output is correct
115 Correct 388 ms 23736 KB Output is correct
116 Correct 405 ms 25872 KB Output is correct
117 Correct 415 ms 25976 KB Output is correct
118 Correct 414 ms 25868 KB Output is correct
119 Correct 420 ms 25940 KB Output is correct
120 Correct 407 ms 25900 KB Output is correct
121 Correct 404 ms 25888 KB Output is correct
122 Correct 472 ms 25980 KB Output is correct
123 Correct 44 ms 9668 KB Output is correct
124 Correct 292 ms 25556 KB Output is correct
125 Correct 245 ms 23432 KB Output is correct
126 Correct 390 ms 26044 KB Output is correct
127 Correct 369 ms 26100 KB Output is correct
128 Correct 374 ms 26088 KB Output is correct
129 Correct 360 ms 26076 KB Output is correct
130 Correct 322 ms 26020 KB Output is correct
131 Correct 246 ms 32540 KB Output is correct
132 Correct 244 ms 33704 KB Output is correct
133 Correct 259 ms 29908 KB Output is correct
134 Correct 400 ms 26144 KB Output is correct
135 Correct 399 ms 26060 KB Output is correct
136 Correct 406 ms 26080 KB Output is correct
137 Correct 177 ms 26284 KB Output is correct
138 Correct 172 ms 26296 KB Output is correct
139 Correct 173 ms 26460 KB Output is correct
140 Correct 181 ms 26348 KB Output is correct
141 Correct 173 ms 26280 KB Output is correct
142 Correct 177 ms 26452 KB Output is correct
143 Correct 63 ms 21440 KB Output is correct
144 Correct 322 ms 25740 KB Output is correct