Submission #834218

# Submission time Handle Problem Language Result Execution time Memory
834218 2023-08-22T12:01:05 Z alvingogo Tourism (JOI23_tourism) C++14
100 / 100
299 ms 113064 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
#define int long long
using namespace std;

struct BIT{
    int n;
    vector<int> bt;
    void init(int x){
        n=x;
        bt.resize(n+1);
    }
    void update(int x,int y){
        x++;
        for(;x<=n;x+=(x&-x)){
            bt[x]+=y;
        }
    }
    int query(int x){
        int ans=0;
        x++;
        for(;x>0;x-=(x&-x)){
            ans+=bt[x];
        }
        return ans;
    }
    int sum(int l,int r){
        return query(r)-(l==0?0:query(l-1));
    }
}bt;
struct SZT{
    vector<vector<int> > dg;
    void init(vector<int>& x){
        int f=x.size();
        int r=__lg(f)+1;
        dg.resize(r,vector<int>(f));
        dg[0]=x;
        for(int i=1;i<r;i++){
            for(int j=0;j+(1<<(i))-1<f;j++){
                dg[i][j]=min(dg[i-1][j],dg[i-1][j+(1<<(i-1))]);
            }
        }
    }
    int query(int l,int r){
        int t=__lg(r-l+1);
        return min(dg[t][l],dg[t][r-(1<<(t))+1]);
    }
}pd,lin,lout;
struct ST{
    vector<int> st;
    void init(int x){
        st.resize(4*x);
    }
    void update(int v,int L,int R,int p,int x){
        if(L==R){
            st[v]=x;
            return;
        }  
        int m=(L+R)/2;
        if(p<=m){
            update(2*v+1,L,m,p,x);
        }
        else{
            update(2*v+2,m+1,R,p,x);
        }
        st[v]=max(st[2*v+1],st[2*v+2]);
    }
    int query(int v,int L,int R,int l,int r){
        if(L==l && r==R){
            return st[v];
        }
        int m=(L+R)/2;
        if(r<=m){
            return query(2*v+1,L,m,l,r);
        }
        else if(l>m){
            return query(2*v+2,m+1,R,l,r);
        }
        else{
            return max(query(2*v+1,L,m,l,m),query(2*v+2,m+1,R,m+1,r));
        }
    }
}po;
struct no{
    vector<int> ch;
    int dep=-1;
    int fa=-1;
    int vis=0;
    int in=-1,out=-1;
};
vector<no> v;
vector<int> z;
int cnt=0;
void dfs(int r,int f){
    v[r].fa=f;
    v[r].dep=v[f].dep+1;
    v[r].in=cnt;
    z.push_back(v[r].dep);
    cnt++;
    for(auto h:v[r].ch){
        if(h!=f){
            dfs(h,r);
            z.push_back(v[r].dep);
            cnt++;
        }
    }
    v[r].out=cnt;
    z.push_back(v[r].dep);
    cnt++;
}
int lca(int l,int r){
    int a=lin.query(l,r);
    int b=-lout.query(l,r);
    return pd.query(a,b);
}
signed main(){
    AquA;
    int n,m,q;
    cin >> n >> m >> q;
    v.resize(n);
    vector<int> c(n+1);
    c[0]=0;
    for(int i=1;i<n;i++){
        int a,b;
        cin >> a >> b;
        a--;
        b--;
        v[a].ch.push_back(b);
        v[b].ch.push_back(a);
    }
    dfs(0,0);
    pd.init(z);
    int g=z.size();
    vector<int> ii,oo;
    vector<int> w(m);
    for(int i=0;i<m;i++){
        int a;
        cin >> a;
        a--;
        w[i]=a;
        ii.push_back(v[a].in);
        oo.push_back(-v[a].out);
    }
    lin.init(ii);
    lout.init(oo);
    vector<vector<pair<int,int> > > vq(m);
    vector<int> ans(q);
    for(int i=0;i<q;i++){
        int a,b;
        cin >> a >> b;
        a--;
        b--;
        vq[b].push_back({i,a});
    }
    po.init(g);
    bt.init(m);
    vector<int> co(m);
    for(int i=0;i<m;i++){
        bt.update(i,v[w[i]].dep);
        int x=w[i];
        while(!v[x].vis){
            v[x].vis=1;
            if(x==0){
                break;
            }
            x=v[x].fa;
        }
        while(x!=0){
            int s=po.query(0,0,g-1,v[x].in,v[x].out);
            int z=co[s];
            bt.update(s,v[co[s]].dep-v[x].dep);
            co[s]=x;
            x=z;
        }
        co[i]=0;
        po.update(0,0,g-1,v[w[i]].in,i);
        for(auto [h,l]:vq[i]){
            ans[h]=bt.sum(l,i)-lca(l,i)+1;
        }
    }
    for(int i=0;i<q;i++){
        cout << ans[i] << "\n";
    }
    return 0;
}

Compilation message

tourism.cpp: In function 'int main()':
tourism.cpp:181:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  181 |         for(auto [h,l]:vq[i]){
      |                  ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 488 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 452 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 496 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 452 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 488 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 452 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 496 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 452 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 456 KB Output is correct
30 Correct 3 ms 1492 KB Output is correct
31 Correct 3 ms 1492 KB Output is correct
32 Correct 4 ms 2004 KB Output is correct
33 Correct 4 ms 2004 KB Output is correct
34 Correct 4 ms 1876 KB Output is correct
35 Correct 3 ms 1876 KB Output is correct
36 Correct 4 ms 2004 KB Output is correct
37 Correct 4 ms 2004 KB Output is correct
38 Correct 4 ms 2004 KB Output is correct
39 Correct 3 ms 2004 KB Output is correct
40 Correct 4 ms 2004 KB Output is correct
41 Correct 3 ms 2004 KB Output is correct
42 Correct 3 ms 2004 KB Output is correct
43 Correct 3 ms 2060 KB Output is correct
44 Correct 3 ms 2004 KB Output is correct
45 Correct 4 ms 2004 KB Output is correct
46 Correct 4 ms 2004 KB Output is correct
47 Correct 3 ms 2004 KB Output is correct
48 Correct 3 ms 2004 KB Output is correct
49 Correct 3 ms 2004 KB Output is correct
50 Correct 3 ms 1876 KB Output is correct
51 Correct 3 ms 1876 KB Output is correct
52 Correct 3 ms 1876 KB Output is correct
53 Correct 3 ms 1876 KB Output is correct
54 Correct 4 ms 1876 KB Output is correct
55 Correct 2 ms 1868 KB Output is correct
56 Correct 1 ms 852 KB Output is correct
57 Correct 3 ms 1492 KB Output is correct
58 Correct 3 ms 1876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 852 KB Output is correct
4 Correct 143 ms 73508 KB Output is correct
5 Correct 131 ms 79672 KB Output is correct
6 Correct 174 ms 98584 KB Output is correct
7 Correct 209 ms 112832 KB Output is correct
8 Correct 214 ms 112812 KB Output is correct
9 Correct 242 ms 112808 KB Output is correct
10 Correct 224 ms 113064 KB Output is correct
11 Correct 215 ms 112780 KB Output is correct
12 Correct 154 ms 112660 KB Output is correct
13 Correct 157 ms 112508 KB Output is correct
14 Correct 147 ms 112552 KB Output is correct
15 Correct 69 ms 79168 KB Output is correct
16 Correct 192 ms 112432 KB Output is correct
17 Correct 60 ms 38172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 115 ms 54260 KB Output is correct
3 Correct 178 ms 69968 KB Output is correct
4 Correct 135 ms 67724 KB Output is correct
5 Correct 241 ms 102684 KB Output is correct
6 Correct 299 ms 102700 KB Output is correct
7 Correct 248 ms 102568 KB Output is correct
8 Correct 259 ms 102664 KB Output is correct
9 Correct 234 ms 102688 KB Output is correct
10 Correct 222 ms 102580 KB Output is correct
11 Correct 242 ms 102708 KB Output is correct
12 Correct 247 ms 102728 KB Output is correct
13 Correct 241 ms 102820 KB Output is correct
14 Correct 253 ms 103832 KB Output is correct
15 Correct 276 ms 107284 KB Output is correct
16 Correct 257 ms 102800 KB Output is correct
17 Correct 265 ms 102720 KB Output is correct
18 Correct 225 ms 102804 KB Output is correct
19 Correct 230 ms 102220 KB Output is correct
20 Correct 224 ms 102108 KB Output is correct
21 Correct 220 ms 102284 KB Output is correct
22 Correct 208 ms 102164 KB Output is correct
23 Correct 210 ms 102204 KB Output is correct
24 Correct 215 ms 102196 KB Output is correct
25 Correct 223 ms 102184 KB Output is correct
26 Correct 210 ms 102164 KB Output is correct
27 Correct 211 ms 102152 KB Output is correct
28 Correct 233 ms 102164 KB Output is correct
29 Correct 215 ms 102232 KB Output is correct
30 Correct 230 ms 102284 KB Output is correct
31 Correct 219 ms 102316 KB Output is correct
32 Correct 242 ms 103312 KB Output is correct
33 Correct 240 ms 104200 KB Output is correct
34 Correct 245 ms 107056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 316 KB Output is correct
3 Correct 2 ms 852 KB Output is correct
4 Correct 204 ms 72232 KB Output is correct
5 Correct 223 ms 74352 KB Output is correct
6 Correct 263 ms 94580 KB Output is correct
7 Correct 269 ms 107316 KB Output is correct
8 Correct 282 ms 107332 KB Output is correct
9 Correct 279 ms 107324 KB Output is correct
10 Correct 278 ms 107368 KB Output is correct
11 Correct 281 ms 107320 KB Output is correct
12 Correct 273 ms 107400 KB Output is correct
13 Correct 291 ms 107400 KB Output is correct
14 Correct 56 ms 38204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 488 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 452 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 496 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 452 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 456 KB Output is correct
30 Correct 3 ms 1492 KB Output is correct
31 Correct 3 ms 1492 KB Output is correct
32 Correct 4 ms 2004 KB Output is correct
33 Correct 4 ms 2004 KB Output is correct
34 Correct 4 ms 1876 KB Output is correct
35 Correct 3 ms 1876 KB Output is correct
36 Correct 4 ms 2004 KB Output is correct
37 Correct 4 ms 2004 KB Output is correct
38 Correct 4 ms 2004 KB Output is correct
39 Correct 3 ms 2004 KB Output is correct
40 Correct 4 ms 2004 KB Output is correct
41 Correct 3 ms 2004 KB Output is correct
42 Correct 3 ms 2004 KB Output is correct
43 Correct 3 ms 2060 KB Output is correct
44 Correct 3 ms 2004 KB Output is correct
45 Correct 4 ms 2004 KB Output is correct
46 Correct 4 ms 2004 KB Output is correct
47 Correct 3 ms 2004 KB Output is correct
48 Correct 3 ms 2004 KB Output is correct
49 Correct 3 ms 2004 KB Output is correct
50 Correct 3 ms 1876 KB Output is correct
51 Correct 3 ms 1876 KB Output is correct
52 Correct 3 ms 1876 KB Output is correct
53 Correct 3 ms 1876 KB Output is correct
54 Correct 4 ms 1876 KB Output is correct
55 Correct 2 ms 1868 KB Output is correct
56 Correct 1 ms 852 KB Output is correct
57 Correct 3 ms 1492 KB Output is correct
58 Correct 3 ms 1876 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 1 ms 852 KB Output is correct
62 Correct 143 ms 73508 KB Output is correct
63 Correct 131 ms 79672 KB Output is correct
64 Correct 174 ms 98584 KB Output is correct
65 Correct 209 ms 112832 KB Output is correct
66 Correct 214 ms 112812 KB Output is correct
67 Correct 242 ms 112808 KB Output is correct
68 Correct 224 ms 113064 KB Output is correct
69 Correct 215 ms 112780 KB Output is correct
70 Correct 154 ms 112660 KB Output is correct
71 Correct 157 ms 112508 KB Output is correct
72 Correct 147 ms 112552 KB Output is correct
73 Correct 69 ms 79168 KB Output is correct
74 Correct 192 ms 112432 KB Output is correct
75 Correct 60 ms 38172 KB Output is correct
76 Correct 1 ms 212 KB Output is correct
77 Correct 115 ms 54260 KB Output is correct
78 Correct 178 ms 69968 KB Output is correct
79 Correct 135 ms 67724 KB Output is correct
80 Correct 241 ms 102684 KB Output is correct
81 Correct 299 ms 102700 KB Output is correct
82 Correct 248 ms 102568 KB Output is correct
83 Correct 259 ms 102664 KB Output is correct
84 Correct 234 ms 102688 KB Output is correct
85 Correct 222 ms 102580 KB Output is correct
86 Correct 242 ms 102708 KB Output is correct
87 Correct 247 ms 102728 KB Output is correct
88 Correct 241 ms 102820 KB Output is correct
89 Correct 253 ms 103832 KB Output is correct
90 Correct 276 ms 107284 KB Output is correct
91 Correct 257 ms 102800 KB Output is correct
92 Correct 265 ms 102720 KB Output is correct
93 Correct 225 ms 102804 KB Output is correct
94 Correct 230 ms 102220 KB Output is correct
95 Correct 224 ms 102108 KB Output is correct
96 Correct 220 ms 102284 KB Output is correct
97 Correct 208 ms 102164 KB Output is correct
98 Correct 210 ms 102204 KB Output is correct
99 Correct 215 ms 102196 KB Output is correct
100 Correct 223 ms 102184 KB Output is correct
101 Correct 210 ms 102164 KB Output is correct
102 Correct 211 ms 102152 KB Output is correct
103 Correct 233 ms 102164 KB Output is correct
104 Correct 215 ms 102232 KB Output is correct
105 Correct 230 ms 102284 KB Output is correct
106 Correct 219 ms 102316 KB Output is correct
107 Correct 242 ms 103312 KB Output is correct
108 Correct 240 ms 104200 KB Output is correct
109 Correct 245 ms 107056 KB Output is correct
110 Correct 1 ms 212 KB Output is correct
111 Correct 0 ms 316 KB Output is correct
112 Correct 2 ms 852 KB Output is correct
113 Correct 204 ms 72232 KB Output is correct
114 Correct 223 ms 74352 KB Output is correct
115 Correct 263 ms 94580 KB Output is correct
116 Correct 269 ms 107316 KB Output is correct
117 Correct 282 ms 107332 KB Output is correct
118 Correct 279 ms 107324 KB Output is correct
119 Correct 278 ms 107368 KB Output is correct
120 Correct 281 ms 107320 KB Output is correct
121 Correct 273 ms 107400 KB Output is correct
122 Correct 291 ms 107400 KB Output is correct
123 Correct 56 ms 38204 KB Output is correct
124 Correct 247 ms 101476 KB Output is correct
125 Correct 184 ms 89904 KB Output is correct
126 Correct 292 ms 107276 KB Output is correct
127 Correct 267 ms 107144 KB Output is correct
128 Correct 264 ms 107168 KB Output is correct
129 Correct 285 ms 107256 KB Output is correct
130 Correct 269 ms 107160 KB Output is correct
131 Correct 224 ms 111880 KB Output is correct
132 Correct 202 ms 112696 KB Output is correct
133 Correct 203 ms 109712 KB Output is correct
134 Correct 248 ms 106768 KB Output is correct
135 Correct 257 ms 106720 KB Output is correct
136 Correct 255 ms 106768 KB Output is correct
137 Correct 179 ms 107356 KB Output is correct
138 Correct 168 ms 107352 KB Output is correct
139 Correct 164 ms 107412 KB Output is correct
140 Correct 163 ms 107336 KB Output is correct
141 Correct 176 ms 107408 KB Output is correct
142 Correct 163 ms 107344 KB Output is correct
143 Correct 75 ms 73508 KB Output is correct
144 Correct 266 ms 106832 KB Output is correct