Submission #1083945

# Submission time Handle Problem Language Result Execution time Memory
1083945 2024-09-04T15:23:14 Z dwuy Tourism (JOI23_tourism) C++14
100 / 100
660 ms 29268 KB
/**         - dwuy -

      />    フ
      |  _  _|
      /`ミ _x ノ
     /      |
    /   ヽ   ?
 / ̄|   | | |
 | ( ̄ヽ__ヽ_)_)
 \二つ

**/
#include <bits/stdc++.h>

#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL)
#define file(a) freopen(a".inp","r",stdin); freopen(a".out", "w",stdout)
#define fi first
#define se second
#define endl "\n"
#define len(s) (int)((s).size())
#define MASK(k)(1LL<<(k))
#define TASK "test"
#define int long long

using namespace std;

typedef tuple<int, int, int> tpiii;
typedef pair<double, double> pdd;
typedef pair<int, int> pii;
typedef long long ll;

const long long OO = 1e18;
const int MOD = 1e9 + 7;
const int INF = 1e9;

struct BIT{
    int n;
    vector<int> tree;

    BIT(int n = 0){
        this-> n = n;
        this->tree.assign(n + 5, 0);
    }

    void update(int idx, int val){
        for(; idx; idx-=-idx&idx) tree[idx] += val;
    }

    int get(int idx){
        int res = 0;
        for(; idx<=n; idx+=-idx&idx) res += tree[idx];
        return res;
    }
};

const int MX = 100005;
int n, m, q;
int c[MX];
int ans[MX];
vector<int> G[MX];
vector<pii> Q[MX];

void nhap(){
    cin >> n >> m >> q;
    for(int i=1; i<n; i++){
        int u, v;
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    for(int i=1; i<=m; i++) cin >> c[i];
    for(int i=1; i<=q; i++){
        int l, r;
        cin >> l >> r;
        Q[r].push_back({l, i});
    }
}

namespace yingbao{
    int dfsID = 0;
    int p[MX];
    int h[MX];
    int num[MX];
    int head[MX];
    int numC[MX];
    int heavy[MX];
    
    int nxt[MX];
    int value[MX];
    BIT bit(MX);
    set<int> pos;

    void dfs(int u){
        h[u] = h[p[u]] + 1;
        numC[u] = 1;
        for(int v: G[u]) if(v != p[u]){
            p[v] = u;
            dfs(v);
            numC[u] += numC[v];
            if(numC[heavy[u]] < numC[v]) heavy[u] = v;
        }
    }

    void decompose(int u, int head){
        yingbao::head[u] = head;
        num[u] = ++dfsID;
        if(heavy[u]) decompose(heavy[u], head);
        for(int v: G[u]) if(v != heavy[u] && v != p[u]){
            decompose(v, v);
        }
    }

    void build(){
        dfs(1);
        decompose(1, 1);
    }

    void azzign(int l, int r, int id){
        for(auto itr = pos.lower_bound(l); itr != pos.end() && nxt[*itr] <= r;){
            int u = *itr, v = nxt[*itr];   
            bit.update(value[u], -(v - u + 1));
            nxt[u] = value[u] = 0;
            itr++;
            pos.erase(prev(itr));
        }
        
        auto itr = pos.lower_bound(l);
        if(itr != pos.begin() && nxt[*prev(itr)] >= l){
            itr--;
            bit.update(value[*itr], -(nxt[*itr] - l + 1));
            nxt[*itr] = l - 1; 
        }

        itr = pos.lower_bound(l);
        if(itr != pos.end() && *itr <= r){
            bit.update(value[*itr], -(r - *itr + 1));
            nxt[r + 1] = nxt[*itr];
            value[r + 1] = value[*itr];
            pos.erase(itr);
            pos.insert(r + 1);
        }
        bit.update(id, r - l + 1);
        pos.insert(l);
        nxt[l] = r;
        value[l] = id;
    }

    void update(int u, int v, int id){
        while(head[u] != head[v]){
            if(h[head[u]] < h[head[v]]) swap(u, v);
            azzign(num[head[u]], num[u], id);
            u = p[head[u]];
        }
        if(num[u] > num[v]) swap(u, v);
        azzign(num[u], num[v], id);
    }

    int get(int x){
        return bit.get(x + 1);
    }
}

void solve(){
    yingbao::build();
    c[0] = c[1];
    for(int i=1; i<=m; i++){
        yingbao::update(c[i-1], c[i], i);
        for(pii &qr: Q[i]){
            int l, id;
            tie(l, id) = qr;
            ans[id] = l != i? yingbao::get(l) : 1;
        }
    }
    for(int i=1; i<=q; i++) cout << ans[i] << endl;
}

int32_t main(){
    fastIO;
    //file(TASK);

    nhap();
    solve();

    return 0;
}




# Verdict Execution time Memory Grader output
1 Correct 2 ms 5980 KB Output is correct
2 Correct 3 ms 5976 KB Output is correct
3 Correct 3 ms 5992 KB Output is correct
4 Correct 3 ms 5964 KB Output is correct
5 Correct 3 ms 5980 KB Output is correct
6 Correct 4 ms 5980 KB Output is correct
7 Correct 3 ms 5980 KB Output is correct
8 Correct 3 ms 5980 KB Output is correct
9 Correct 4 ms 5980 KB Output is correct
10 Correct 3 ms 5980 KB Output is correct
11 Correct 3 ms 5980 KB Output is correct
12 Correct 3 ms 5852 KB Output is correct
13 Correct 3 ms 5980 KB Output is correct
14 Correct 3 ms 5980 KB Output is correct
15 Correct 3 ms 5980 KB Output is correct
16 Correct 2 ms 5980 KB Output is correct
17 Correct 3 ms 5980 KB Output is correct
18 Correct 3 ms 5980 KB Output is correct
19 Correct 3 ms 5980 KB Output is correct
20 Correct 4 ms 5980 KB Output is correct
21 Correct 3 ms 5980 KB Output is correct
22 Correct 3 ms 5980 KB Output is correct
23 Correct 3 ms 5980 KB Output is correct
24 Correct 3 ms 5980 KB Output is correct
25 Correct 4 ms 5980 KB Output is correct
26 Correct 3 ms 5980 KB Output is correct
27 Correct 3 ms 5980 KB Output is correct
28 Correct 3 ms 5980 KB Output is correct
29 Correct 4 ms 5980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5980 KB Output is correct
2 Correct 3 ms 5976 KB Output is correct
3 Correct 3 ms 5992 KB Output is correct
4 Correct 3 ms 5964 KB Output is correct
5 Correct 3 ms 5980 KB Output is correct
6 Correct 4 ms 5980 KB Output is correct
7 Correct 3 ms 5980 KB Output is correct
8 Correct 3 ms 5980 KB Output is correct
9 Correct 4 ms 5980 KB Output is correct
10 Correct 3 ms 5980 KB Output is correct
11 Correct 3 ms 5980 KB Output is correct
12 Correct 3 ms 5852 KB Output is correct
13 Correct 3 ms 5980 KB Output is correct
14 Correct 3 ms 5980 KB Output is correct
15 Correct 3 ms 5980 KB Output is correct
16 Correct 2 ms 5980 KB Output is correct
17 Correct 3 ms 5980 KB Output is correct
18 Correct 3 ms 5980 KB Output is correct
19 Correct 3 ms 5980 KB Output is correct
20 Correct 4 ms 5980 KB Output is correct
21 Correct 3 ms 5980 KB Output is correct
22 Correct 3 ms 5980 KB Output is correct
23 Correct 3 ms 5980 KB Output is correct
24 Correct 3 ms 5980 KB Output is correct
25 Correct 4 ms 5980 KB Output is correct
26 Correct 3 ms 5980 KB Output is correct
27 Correct 3 ms 5980 KB Output is correct
28 Correct 3 ms 5980 KB Output is correct
29 Correct 4 ms 5980 KB Output is correct
30 Correct 6 ms 6236 KB Output is correct
31 Correct 5 ms 6236 KB Output is correct
32 Correct 7 ms 6236 KB Output is correct
33 Correct 6 ms 6236 KB Output is correct
34 Correct 7 ms 6232 KB Output is correct
35 Correct 7 ms 6232 KB Output is correct
36 Correct 7 ms 6236 KB Output is correct
37 Correct 6 ms 6236 KB Output is correct
38 Correct 4 ms 6236 KB Output is correct
39 Correct 5 ms 6236 KB Output is correct
40 Correct 6 ms 6236 KB Output is correct
41 Correct 6 ms 6236 KB Output is correct
42 Correct 4 ms 6232 KB Output is correct
43 Correct 4 ms 6232 KB Output is correct
44 Correct 5 ms 6232 KB Output is correct
45 Correct 5 ms 6232 KB Output is correct
46 Correct 4 ms 6236 KB Output is correct
47 Correct 4 ms 6236 KB Output is correct
48 Correct 5 ms 6236 KB Output is correct
49 Correct 5 ms 6236 KB Output is correct
50 Correct 5 ms 6232 KB Output is correct
51 Correct 7 ms 6232 KB Output is correct
52 Correct 5 ms 6232 KB Output is correct
53 Correct 5 ms 6228 KB Output is correct
54 Correct 6 ms 6236 KB Output is correct
55 Correct 5 ms 6328 KB Output is correct
56 Correct 3 ms 5980 KB Output is correct
57 Correct 3 ms 6236 KB Output is correct
58 Correct 6 ms 6236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5980 KB Output is correct
2 Correct 2 ms 5972 KB Output is correct
3 Correct 4 ms 5976 KB Output is correct
4 Correct 63 ms 20240 KB Output is correct
5 Correct 60 ms 23536 KB Output is correct
6 Correct 67 ms 25172 KB Output is correct
7 Correct 90 ms 29008 KB Output is correct
8 Correct 81 ms 29268 KB Output is correct
9 Correct 84 ms 28996 KB Output is correct
10 Correct 91 ms 29176 KB Output is correct
11 Correct 83 ms 29012 KB Output is correct
12 Correct 88 ms 29004 KB Output is correct
13 Correct 80 ms 28900 KB Output is correct
14 Correct 81 ms 29012 KB Output is correct
15 Correct 39 ms 24004 KB Output is correct
16 Correct 79 ms 28836 KB Output is correct
17 Correct 35 ms 11604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5980 KB Output is correct
2 Correct 190 ms 14056 KB Output is correct
3 Correct 328 ms 15516 KB Output is correct
4 Correct 239 ms 16116 KB Output is correct
5 Correct 389 ms 20816 KB Output is correct
6 Correct 383 ms 20816 KB Output is correct
7 Correct 380 ms 20816 KB Output is correct
8 Correct 382 ms 20816 KB Output is correct
9 Correct 378 ms 20820 KB Output is correct
10 Correct 393 ms 20888 KB Output is correct
11 Correct 416 ms 20820 KB Output is correct
12 Correct 376 ms 21072 KB Output is correct
13 Correct 370 ms 21404 KB Output is correct
14 Correct 385 ms 22480 KB Output is correct
15 Correct 409 ms 26180 KB Output is correct
16 Correct 385 ms 21336 KB Output is correct
17 Correct 407 ms 21584 KB Output is correct
18 Correct 379 ms 21332 KB Output is correct
19 Correct 218 ms 18256 KB Output is correct
20 Correct 216 ms 18000 KB Output is correct
21 Correct 219 ms 18072 KB Output is correct
22 Correct 232 ms 18000 KB Output is correct
23 Correct 206 ms 18060 KB Output is correct
24 Correct 221 ms 18004 KB Output is correct
25 Correct 218 ms 18004 KB Output is correct
26 Correct 215 ms 18004 KB Output is correct
27 Correct 223 ms 18088 KB Output is correct
28 Correct 222 ms 18000 KB Output is correct
29 Correct 252 ms 18260 KB Output is correct
30 Correct 222 ms 18456 KB Output is correct
31 Correct 222 ms 18692 KB Output is correct
32 Correct 220 ms 19208 KB Output is correct
33 Correct 260 ms 20820 KB Output is correct
34 Correct 254 ms 23348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5976 KB Output is correct
2 Correct 3 ms 5980 KB Output is correct
3 Correct 3 ms 5980 KB Output is correct
4 Correct 508 ms 19868 KB Output is correct
5 Correct 583 ms 20048 KB Output is correct
6 Correct 610 ms 23888 KB Output is correct
7 Correct 656 ms 26192 KB Output is correct
8 Correct 660 ms 26060 KB Output is correct
9 Correct 637 ms 26156 KB Output is correct
10 Correct 655 ms 26148 KB Output is correct
11 Correct 655 ms 26156 KB Output is correct
12 Correct 644 ms 26196 KB Output is correct
13 Correct 621 ms 26108 KB Output is correct
14 Correct 36 ms 11600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5980 KB Output is correct
2 Correct 3 ms 5976 KB Output is correct
3 Correct 3 ms 5992 KB Output is correct
4 Correct 3 ms 5964 KB Output is correct
5 Correct 3 ms 5980 KB Output is correct
6 Correct 4 ms 5980 KB Output is correct
7 Correct 3 ms 5980 KB Output is correct
8 Correct 3 ms 5980 KB Output is correct
9 Correct 4 ms 5980 KB Output is correct
10 Correct 3 ms 5980 KB Output is correct
11 Correct 3 ms 5980 KB Output is correct
12 Correct 3 ms 5852 KB Output is correct
13 Correct 3 ms 5980 KB Output is correct
14 Correct 3 ms 5980 KB Output is correct
15 Correct 3 ms 5980 KB Output is correct
16 Correct 2 ms 5980 KB Output is correct
17 Correct 3 ms 5980 KB Output is correct
18 Correct 3 ms 5980 KB Output is correct
19 Correct 3 ms 5980 KB Output is correct
20 Correct 4 ms 5980 KB Output is correct
21 Correct 3 ms 5980 KB Output is correct
22 Correct 3 ms 5980 KB Output is correct
23 Correct 3 ms 5980 KB Output is correct
24 Correct 3 ms 5980 KB Output is correct
25 Correct 4 ms 5980 KB Output is correct
26 Correct 3 ms 5980 KB Output is correct
27 Correct 3 ms 5980 KB Output is correct
28 Correct 3 ms 5980 KB Output is correct
29 Correct 4 ms 5980 KB Output is correct
30 Correct 6 ms 6236 KB Output is correct
31 Correct 5 ms 6236 KB Output is correct
32 Correct 7 ms 6236 KB Output is correct
33 Correct 6 ms 6236 KB Output is correct
34 Correct 7 ms 6232 KB Output is correct
35 Correct 7 ms 6232 KB Output is correct
36 Correct 7 ms 6236 KB Output is correct
37 Correct 6 ms 6236 KB Output is correct
38 Correct 4 ms 6236 KB Output is correct
39 Correct 5 ms 6236 KB Output is correct
40 Correct 6 ms 6236 KB Output is correct
41 Correct 6 ms 6236 KB Output is correct
42 Correct 4 ms 6232 KB Output is correct
43 Correct 4 ms 6232 KB Output is correct
44 Correct 5 ms 6232 KB Output is correct
45 Correct 5 ms 6232 KB Output is correct
46 Correct 4 ms 6236 KB Output is correct
47 Correct 4 ms 6236 KB Output is correct
48 Correct 5 ms 6236 KB Output is correct
49 Correct 5 ms 6236 KB Output is correct
50 Correct 5 ms 6232 KB Output is correct
51 Correct 7 ms 6232 KB Output is correct
52 Correct 5 ms 6232 KB Output is correct
53 Correct 5 ms 6228 KB Output is correct
54 Correct 6 ms 6236 KB Output is correct
55 Correct 5 ms 6328 KB Output is correct
56 Correct 3 ms 5980 KB Output is correct
57 Correct 3 ms 6236 KB Output is correct
58 Correct 6 ms 6236 KB Output is correct
59 Correct 4 ms 5980 KB Output is correct
60 Correct 2 ms 5972 KB Output is correct
61 Correct 4 ms 5976 KB Output is correct
62 Correct 63 ms 20240 KB Output is correct
63 Correct 60 ms 23536 KB Output is correct
64 Correct 67 ms 25172 KB Output is correct
65 Correct 90 ms 29008 KB Output is correct
66 Correct 81 ms 29268 KB Output is correct
67 Correct 84 ms 28996 KB Output is correct
68 Correct 91 ms 29176 KB Output is correct
69 Correct 83 ms 29012 KB Output is correct
70 Correct 88 ms 29004 KB Output is correct
71 Correct 80 ms 28900 KB Output is correct
72 Correct 81 ms 29012 KB Output is correct
73 Correct 39 ms 24004 KB Output is correct
74 Correct 79 ms 28836 KB Output is correct
75 Correct 35 ms 11604 KB Output is correct
76 Correct 3 ms 5980 KB Output is correct
77 Correct 190 ms 14056 KB Output is correct
78 Correct 328 ms 15516 KB Output is correct
79 Correct 239 ms 16116 KB Output is correct
80 Correct 389 ms 20816 KB Output is correct
81 Correct 383 ms 20816 KB Output is correct
82 Correct 380 ms 20816 KB Output is correct
83 Correct 382 ms 20816 KB Output is correct
84 Correct 378 ms 20820 KB Output is correct
85 Correct 393 ms 20888 KB Output is correct
86 Correct 416 ms 20820 KB Output is correct
87 Correct 376 ms 21072 KB Output is correct
88 Correct 370 ms 21404 KB Output is correct
89 Correct 385 ms 22480 KB Output is correct
90 Correct 409 ms 26180 KB Output is correct
91 Correct 385 ms 21336 KB Output is correct
92 Correct 407 ms 21584 KB Output is correct
93 Correct 379 ms 21332 KB Output is correct
94 Correct 218 ms 18256 KB Output is correct
95 Correct 216 ms 18000 KB Output is correct
96 Correct 219 ms 18072 KB Output is correct
97 Correct 232 ms 18000 KB Output is correct
98 Correct 206 ms 18060 KB Output is correct
99 Correct 221 ms 18004 KB Output is correct
100 Correct 218 ms 18004 KB Output is correct
101 Correct 215 ms 18004 KB Output is correct
102 Correct 223 ms 18088 KB Output is correct
103 Correct 222 ms 18000 KB Output is correct
104 Correct 252 ms 18260 KB Output is correct
105 Correct 222 ms 18456 KB Output is correct
106 Correct 222 ms 18692 KB Output is correct
107 Correct 220 ms 19208 KB Output is correct
108 Correct 260 ms 20820 KB Output is correct
109 Correct 254 ms 23348 KB Output is correct
110 Correct 4 ms 5976 KB Output is correct
111 Correct 3 ms 5980 KB Output is correct
112 Correct 3 ms 5980 KB Output is correct
113 Correct 508 ms 19868 KB Output is correct
114 Correct 583 ms 20048 KB Output is correct
115 Correct 610 ms 23888 KB Output is correct
116 Correct 656 ms 26192 KB Output is correct
117 Correct 660 ms 26060 KB Output is correct
118 Correct 637 ms 26156 KB Output is correct
119 Correct 655 ms 26148 KB Output is correct
120 Correct 655 ms 26156 KB Output is correct
121 Correct 644 ms 26196 KB Output is correct
122 Correct 621 ms 26108 KB Output is correct
123 Correct 36 ms 11600 KB Output is correct
124 Correct 383 ms 25424 KB Output is correct
125 Correct 265 ms 22352 KB Output is correct
126 Correct 447 ms 25936 KB Output is correct
127 Correct 420 ms 25936 KB Output is correct
128 Correct 419 ms 25940 KB Output is correct
129 Correct 435 ms 25936 KB Output is correct
130 Correct 413 ms 25940 KB Output is correct
131 Correct 95 ms 28244 KB Output is correct
132 Correct 127 ms 29008 KB Output is correct
133 Correct 101 ms 25936 KB Output is correct
134 Correct 255 ms 23332 KB Output is correct
135 Correct 245 ms 23124 KB Output is correct
136 Correct 239 ms 23200 KB Output is correct
137 Correct 174 ms 25756 KB Output is correct
138 Correct 161 ms 25800 KB Output is correct
139 Correct 160 ms 25800 KB Output is correct
140 Correct 164 ms 26516 KB Output is correct
141 Correct 163 ms 26564 KB Output is correct
142 Correct 163 ms 26704 KB Output is correct
143 Correct 45 ms 18392 KB Output is correct
144 Correct 416 ms 25684 KB Output is correct