Submission #1000018

# Submission time Handle Problem Language Result Execution time Memory
1000018 2024-06-16T13:13:21 Z NintsiChkhaidze Tourism (JOI23_tourism) C++17
100 / 100
832 ms 79528 KB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define s second
#define f first
#define pii pair <int,int>
#define left h*2,l,(l + r)/2
#define right h*2+1,(l + r)/2 + 1,r
#define int ll
using namespace std;

const int N = 3e5 + 5;
vector <int> v[N];
int ans[N],ord[N],sz[N],heavy[N],head[N],tin[N];
int timer,d[25][N],dep[N],arr[N],n,m,fen[N];
vector <pii> qr[N];
set <pair<pii,int> > st;

void dfs(int x,int par){
    d[0][x] = par;
    dep[x] = dep[par] + 1;
    sz[x] = 1;

    for (int to: v[x]){
        if (to == par) continue;
        dfs(to,x);
        sz[x] += sz[to];    
        if (sz[to] > sz[heavy[x]]) heavy[x] = to;
    }
}

int lca(int x,int y){
    if (dep[x] < dep[y]) swap(x,y);
    for (int i = 20; i >= 0; i--)
        if (dep[d[i][x]] >= dep[y]) x = d[i][x];

    if (x == y) return x;
    for (int i = 20; i >= 0; i--)
        if (d[i][x] != d[i][y]) 
            x = d[i][x],y = d[i][y];
    
    return d[0][x];
} 
void dfs2(int x,int par,int h){
    tin[x] = ++timer;
    head[x] = h;
    if (heavy[x]) dfs2(heavy[x],x,h);

    for (int to: v[x]){
        if (to == par || to == heavy[x]) continue;
        dfs2(to,x,to);
    }
}

void upd(int idx,int s){
    if (!idx) return;
    while (idx <= 100000){
        fen[idx] += s;
        idx += (idx & (-idx));
    }
}

void addseg(int l,int r,int col){
    upd(col,(r - l + 1));

    set <pair<pii,int> > :: iterator it = st.lower_bound({{l,0},0});
    while (it != st.end()){
        int R = (it -> f).s,L = (it -> f).f,c = (it->s);
        if (l <= L && R <= r) {
            upd(c,-(R - L + 1));
            st.erase(it++);
        } else break;
    }

    st.lower_bound({{l,0},0});
    int R = (it -> f).s,L = (it -> f).f,c = (it->s);
    if (L >= l && L <= r && R >= r) {
        upd(c, -(R - L + 1));
        st.erase(it);

        L = r + 1;
        if (L <= R) upd(c, R - L + 1),st.insert({{L,R},c});
    }

    st.insert({{l,r},col});
    it = st.find({{l,r},col});
    if (it != st.begin()){
        --it;
        int R = (it -> f).s,L = (it -> f).f,c = (it->s);
        if (L <= l && r <= R){
            upd(c, -(r - l + 1));
            st.erase(it);
            
            int rr = l - 1;
            if (L <= rr) st.insert({{L,rr},c});
            int l1 = r + 1;
            if (l1 <= R) st.insert({{l1,R},c});
        }
    }

    it = st.find({{l,r},col});
    if (it != st.begin()){
        --it;
        int R = (it -> f).s,L = (it -> f).f,c = (it->s);
        if (L <= l && R >= l && R <= r){
            int cm = (R - l + 1);
            upd(c,-cm);

            st.erase(it);
            int rr = l - 1;
            if (L <= rr) st.insert({{L,rr},c});
        }
    }
}

int gett(int x){
    int s=0;
    while (x>0){
        s+=fen[x];
        x-=(x&(-x));
    }
    return s;
}

int get(int x){
    return gett(m) - gett(x - 1);
}

void upd(int x,int y,int col){
    int c = lca(x,y),cnt=0;
    while (head[x] != head[y]){
        if (dep[head[x]] < dep[head[y]]) swap(x,y);
        addseg(tin[head[x]],tin[x],col);
        x = d[0][head[x]];
    }
    if (dep[x] < dep[y]) swap(x,y);
    addseg(tin[y],tin[x],col);
}

signed main (){
    ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);

    int q;
    cin>>n>>m>>q;

    for (int i = 1; i < n; i++){
        int a,b;
        cin>>a>>b;
        v[a].pb(b);
        v[b].pb(a);
    }

    dfs(1,1);
    dfs2(1,1,1);
    for (int i=1;i<=20;i++)
        for (int j=1;j<=n;j++)
            d[i][j] = d[i - 1][d[i - 1][j]];
        
    for (int i = 1; i <= m; i++){
        cin >> ord[i];
    }

    for (int i = 1; i <= q; i++){
        int l,r;
        cin>>l>>r;
        qr[r].pb({l,i});
    }

    for (int i = 1; i <= m; i++){
        if (i > 1) upd(ord[i - 1],ord[i],i-1);
        upd(ord[i],ord[i],i);

        for (auto [x,id]: qr[i]){
            ans[id] = get(x);
        }
    }

    for (int i = 1; i <= q; i++)
        cout<<ans[i]<<endl;
}

Compilation message

tourism.cpp: In function 'void upd(long long int, long long int, long long int)':
tourism.cpp:130:9: warning: unused variable 'c' [-Wunused-variable]
  130 |     int c = lca(x,y),cnt=0;
      |         ^
tourism.cpp:130:22: warning: unused variable 'cnt' [-Wunused-variable]
  130 |     int c = lca(x,y),cnt=0;
      |                      ^~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 49240 KB Output is correct
2 Correct 6 ms 47196 KB Output is correct
3 Correct 6 ms 45148 KB Output is correct
4 Correct 6 ms 47196 KB Output is correct
5 Correct 6 ms 47196 KB Output is correct
6 Correct 7 ms 53340 KB Output is correct
7 Correct 7 ms 53376 KB Output is correct
8 Correct 7 ms 51292 KB Output is correct
9 Correct 9 ms 53340 KB Output is correct
10 Correct 7 ms 45308 KB Output is correct
11 Correct 7 ms 47192 KB Output is correct
12 Correct 6 ms 47192 KB Output is correct
13 Correct 7 ms 45148 KB Output is correct
14 Correct 7 ms 45148 KB Output is correct
15 Correct 6 ms 45148 KB Output is correct
16 Correct 7 ms 47196 KB Output is correct
17 Correct 6 ms 47196 KB Output is correct
18 Correct 7 ms 47196 KB Output is correct
19 Correct 6 ms 47196 KB Output is correct
20 Correct 7 ms 47196 KB Output is correct
21 Correct 7 ms 49244 KB Output is correct
22 Correct 6 ms 47196 KB Output is correct
23 Correct 7 ms 47196 KB Output is correct
24 Correct 6 ms 47196 KB Output is correct
25 Correct 6 ms 45144 KB Output is correct
26 Correct 6 ms 47196 KB Output is correct
27 Correct 6 ms 45148 KB Output is correct
28 Correct 6 ms 45148 KB Output is correct
29 Correct 7 ms 47192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 49240 KB Output is correct
2 Correct 6 ms 47196 KB Output is correct
3 Correct 6 ms 45148 KB Output is correct
4 Correct 6 ms 47196 KB Output is correct
5 Correct 6 ms 47196 KB Output is correct
6 Correct 7 ms 53340 KB Output is correct
7 Correct 7 ms 53376 KB Output is correct
8 Correct 7 ms 51292 KB Output is correct
9 Correct 9 ms 53340 KB Output is correct
10 Correct 7 ms 45308 KB Output is correct
11 Correct 7 ms 47192 KB Output is correct
12 Correct 6 ms 47192 KB Output is correct
13 Correct 7 ms 45148 KB Output is correct
14 Correct 7 ms 45148 KB Output is correct
15 Correct 6 ms 45148 KB Output is correct
16 Correct 7 ms 47196 KB Output is correct
17 Correct 6 ms 47196 KB Output is correct
18 Correct 7 ms 47196 KB Output is correct
19 Correct 6 ms 47196 KB Output is correct
20 Correct 7 ms 47196 KB Output is correct
21 Correct 7 ms 49244 KB Output is correct
22 Correct 6 ms 47196 KB Output is correct
23 Correct 7 ms 47196 KB Output is correct
24 Correct 6 ms 47196 KB Output is correct
25 Correct 6 ms 45144 KB Output is correct
26 Correct 6 ms 47196 KB Output is correct
27 Correct 6 ms 45148 KB Output is correct
28 Correct 6 ms 45148 KB Output is correct
29 Correct 7 ms 47192 KB Output is correct
30 Correct 11 ms 45404 KB Output is correct
31 Correct 12 ms 47452 KB Output is correct
32 Correct 13 ms 47608 KB Output is correct
33 Correct 13 ms 45660 KB Output is correct
34 Correct 14 ms 49756 KB Output is correct
35 Correct 14 ms 53592 KB Output is correct
36 Correct 13 ms 51548 KB Output is correct
37 Correct 13 ms 51548 KB Output is correct
38 Correct 11 ms 53596 KB Output is correct
39 Correct 11 ms 51548 KB Output is correct
40 Correct 10 ms 53596 KB Output is correct
41 Correct 10 ms 53760 KB Output is correct
42 Correct 10 ms 51804 KB Output is correct
43 Correct 10 ms 51548 KB Output is correct
44 Correct 13 ms 53596 KB Output is correct
45 Correct 11 ms 53512 KB Output is correct
46 Correct 12 ms 53596 KB Output is correct
47 Correct 11 ms 51548 KB Output is correct
48 Correct 11 ms 53596 KB Output is correct
49 Correct 11 ms 53596 KB Output is correct
50 Correct 11 ms 51544 KB Output is correct
51 Correct 11 ms 51548 KB Output is correct
52 Correct 12 ms 53592 KB Output is correct
53 Correct 11 ms 53720 KB Output is correct
54 Correct 11 ms 53592 KB Output is correct
55 Correct 11 ms 53596 KB Output is correct
56 Correct 9 ms 51292 KB Output is correct
57 Correct 9 ms 53732 KB Output is correct
58 Correct 16 ms 53596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 53336 KB Output is correct
2 Correct 7 ms 53340 KB Output is correct
3 Correct 9 ms 53340 KB Output is correct
4 Correct 155 ms 65876 KB Output is correct
5 Correct 136 ms 71472 KB Output is correct
6 Correct 149 ms 73300 KB Output is correct
7 Correct 200 ms 76628 KB Output is correct
8 Correct 205 ms 78164 KB Output is correct
9 Correct 209 ms 79444 KB Output is correct
10 Correct 213 ms 79528 KB Output is correct
11 Correct 197 ms 78160 KB Output is correct
12 Correct 213 ms 79444 KB Output is correct
13 Correct 204 ms 79332 KB Output is correct
14 Correct 204 ms 79444 KB Output is correct
15 Correct 138 ms 76224 KB Output is correct
16 Correct 205 ms 77904 KB Output is correct
17 Correct 137 ms 58960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 53340 KB Output is correct
2 Correct 236 ms 62672 KB Output is correct
3 Correct 362 ms 63996 KB Output is correct
4 Correct 294 ms 63572 KB Output is correct
5 Correct 415 ms 70224 KB Output is correct
6 Correct 451 ms 68944 KB Output is correct
7 Correct 441 ms 70424 KB Output is correct
8 Correct 442 ms 69036 KB Output is correct
9 Correct 426 ms 69020 KB Output is correct
10 Correct 448 ms 70532 KB Output is correct
11 Correct 433 ms 70592 KB Output is correct
12 Correct 447 ms 70516 KB Output is correct
13 Correct 432 ms 70632 KB Output is correct
14 Correct 475 ms 70224 KB Output is correct
15 Correct 576 ms 74324 KB Output is correct
16 Correct 420 ms 71248 KB Output is correct
17 Correct 450 ms 71448 KB Output is correct
18 Correct 427 ms 71248 KB Output is correct
19 Correct 288 ms 68424 KB Output is correct
20 Correct 261 ms 68436 KB Output is correct
21 Correct 306 ms 68692 KB Output is correct
22 Correct 282 ms 67152 KB Output is correct
23 Correct 272 ms 69716 KB Output is correct
24 Correct 278 ms 68440 KB Output is correct
25 Correct 279 ms 67260 KB Output is correct
26 Correct 289 ms 69456 KB Output is correct
27 Correct 292 ms 69972 KB Output is correct
28 Correct 298 ms 68596 KB Output is correct
29 Correct 278 ms 68712 KB Output is correct
30 Correct 288 ms 67412 KB Output is correct
31 Correct 291 ms 69028 KB Output is correct
32 Correct 298 ms 69456 KB Output is correct
33 Correct 360 ms 71172 KB Output is correct
34 Correct 370 ms 75096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 53340 KB Output is correct
2 Correct 6 ms 51272 KB Output is correct
3 Correct 9 ms 53504 KB Output is correct
4 Correct 659 ms 65796 KB Output is correct
5 Correct 639 ms 66128 KB Output is correct
6 Correct 743 ms 70740 KB Output is correct
7 Correct 773 ms 76372 KB Output is correct
8 Correct 803 ms 77648 KB Output is correct
9 Correct 761 ms 77460 KB Output is correct
10 Correct 821 ms 76368 KB Output is correct
11 Correct 832 ms 76396 KB Output is correct
12 Correct 830 ms 77712 KB Output is correct
13 Correct 814 ms 76368 KB Output is correct
14 Correct 145 ms 59116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 49240 KB Output is correct
2 Correct 6 ms 47196 KB Output is correct
3 Correct 6 ms 45148 KB Output is correct
4 Correct 6 ms 47196 KB Output is correct
5 Correct 6 ms 47196 KB Output is correct
6 Correct 7 ms 53340 KB Output is correct
7 Correct 7 ms 53376 KB Output is correct
8 Correct 7 ms 51292 KB Output is correct
9 Correct 9 ms 53340 KB Output is correct
10 Correct 7 ms 45308 KB Output is correct
11 Correct 7 ms 47192 KB Output is correct
12 Correct 6 ms 47192 KB Output is correct
13 Correct 7 ms 45148 KB Output is correct
14 Correct 7 ms 45148 KB Output is correct
15 Correct 6 ms 45148 KB Output is correct
16 Correct 7 ms 47196 KB Output is correct
17 Correct 6 ms 47196 KB Output is correct
18 Correct 7 ms 47196 KB Output is correct
19 Correct 6 ms 47196 KB Output is correct
20 Correct 7 ms 47196 KB Output is correct
21 Correct 7 ms 49244 KB Output is correct
22 Correct 6 ms 47196 KB Output is correct
23 Correct 7 ms 47196 KB Output is correct
24 Correct 6 ms 47196 KB Output is correct
25 Correct 6 ms 45144 KB Output is correct
26 Correct 6 ms 47196 KB Output is correct
27 Correct 6 ms 45148 KB Output is correct
28 Correct 6 ms 45148 KB Output is correct
29 Correct 7 ms 47192 KB Output is correct
30 Correct 11 ms 45404 KB Output is correct
31 Correct 12 ms 47452 KB Output is correct
32 Correct 13 ms 47608 KB Output is correct
33 Correct 13 ms 45660 KB Output is correct
34 Correct 14 ms 49756 KB Output is correct
35 Correct 14 ms 53592 KB Output is correct
36 Correct 13 ms 51548 KB Output is correct
37 Correct 13 ms 51548 KB Output is correct
38 Correct 11 ms 53596 KB Output is correct
39 Correct 11 ms 51548 KB Output is correct
40 Correct 10 ms 53596 KB Output is correct
41 Correct 10 ms 53760 KB Output is correct
42 Correct 10 ms 51804 KB Output is correct
43 Correct 10 ms 51548 KB Output is correct
44 Correct 13 ms 53596 KB Output is correct
45 Correct 11 ms 53512 KB Output is correct
46 Correct 12 ms 53596 KB Output is correct
47 Correct 11 ms 51548 KB Output is correct
48 Correct 11 ms 53596 KB Output is correct
49 Correct 11 ms 53596 KB Output is correct
50 Correct 11 ms 51544 KB Output is correct
51 Correct 11 ms 51548 KB Output is correct
52 Correct 12 ms 53592 KB Output is correct
53 Correct 11 ms 53720 KB Output is correct
54 Correct 11 ms 53592 KB Output is correct
55 Correct 11 ms 53596 KB Output is correct
56 Correct 9 ms 51292 KB Output is correct
57 Correct 9 ms 53732 KB Output is correct
58 Correct 16 ms 53596 KB Output is correct
59 Correct 6 ms 53336 KB Output is correct
60 Correct 7 ms 53340 KB Output is correct
61 Correct 9 ms 53340 KB Output is correct
62 Correct 155 ms 65876 KB Output is correct
63 Correct 136 ms 71472 KB Output is correct
64 Correct 149 ms 73300 KB Output is correct
65 Correct 200 ms 76628 KB Output is correct
66 Correct 205 ms 78164 KB Output is correct
67 Correct 209 ms 79444 KB Output is correct
68 Correct 213 ms 79528 KB Output is correct
69 Correct 197 ms 78160 KB Output is correct
70 Correct 213 ms 79444 KB Output is correct
71 Correct 204 ms 79332 KB Output is correct
72 Correct 204 ms 79444 KB Output is correct
73 Correct 138 ms 76224 KB Output is correct
74 Correct 205 ms 77904 KB Output is correct
75 Correct 137 ms 58960 KB Output is correct
76 Correct 7 ms 53340 KB Output is correct
77 Correct 236 ms 62672 KB Output is correct
78 Correct 362 ms 63996 KB Output is correct
79 Correct 294 ms 63572 KB Output is correct
80 Correct 415 ms 70224 KB Output is correct
81 Correct 451 ms 68944 KB Output is correct
82 Correct 441 ms 70424 KB Output is correct
83 Correct 442 ms 69036 KB Output is correct
84 Correct 426 ms 69020 KB Output is correct
85 Correct 448 ms 70532 KB Output is correct
86 Correct 433 ms 70592 KB Output is correct
87 Correct 447 ms 70516 KB Output is correct
88 Correct 432 ms 70632 KB Output is correct
89 Correct 475 ms 70224 KB Output is correct
90 Correct 576 ms 74324 KB Output is correct
91 Correct 420 ms 71248 KB Output is correct
92 Correct 450 ms 71448 KB Output is correct
93 Correct 427 ms 71248 KB Output is correct
94 Correct 288 ms 68424 KB Output is correct
95 Correct 261 ms 68436 KB Output is correct
96 Correct 306 ms 68692 KB Output is correct
97 Correct 282 ms 67152 KB Output is correct
98 Correct 272 ms 69716 KB Output is correct
99 Correct 278 ms 68440 KB Output is correct
100 Correct 279 ms 67260 KB Output is correct
101 Correct 289 ms 69456 KB Output is correct
102 Correct 292 ms 69972 KB Output is correct
103 Correct 298 ms 68596 KB Output is correct
104 Correct 278 ms 68712 KB Output is correct
105 Correct 288 ms 67412 KB Output is correct
106 Correct 291 ms 69028 KB Output is correct
107 Correct 298 ms 69456 KB Output is correct
108 Correct 360 ms 71172 KB Output is correct
109 Correct 370 ms 75096 KB Output is correct
110 Correct 7 ms 53340 KB Output is correct
111 Correct 6 ms 51272 KB Output is correct
112 Correct 9 ms 53504 KB Output is correct
113 Correct 659 ms 65796 KB Output is correct
114 Correct 639 ms 66128 KB Output is correct
115 Correct 743 ms 70740 KB Output is correct
116 Correct 773 ms 76372 KB Output is correct
117 Correct 803 ms 77648 KB Output is correct
118 Correct 761 ms 77460 KB Output is correct
119 Correct 821 ms 76368 KB Output is correct
120 Correct 832 ms 76396 KB Output is correct
121 Correct 830 ms 77712 KB Output is correct
122 Correct 814 ms 76368 KB Output is correct
123 Correct 145 ms 59116 KB Output is correct
124 Correct 520 ms 75612 KB Output is correct
125 Correct 368 ms 72184 KB Output is correct
126 Correct 607 ms 75968 KB Output is correct
127 Correct 602 ms 77140 KB Output is correct
128 Correct 578 ms 77136 KB Output is correct
129 Correct 571 ms 75856 KB Output is correct
130 Correct 545 ms 77140 KB Output is correct
131 Correct 212 ms 78676 KB Output is correct
132 Correct 215 ms 79372 KB Output is correct
133 Correct 215 ms 75088 KB Output is correct
134 Correct 402 ms 73552 KB Output is correct
135 Correct 426 ms 73704 KB Output is correct
136 Correct 410 ms 72276 KB Output is correct
137 Correct 288 ms 77252 KB Output is correct
138 Correct 287 ms 77256 KB Output is correct
139 Correct 282 ms 77508 KB Output is correct
140 Correct 287 ms 76744 KB Output is correct
141 Correct 297 ms 78020 KB Output is correct
142 Correct 288 ms 77968 KB Output is correct
143 Correct 138 ms 69312 KB Output is correct
144 Correct 558 ms 76880 KB Output is correct