Submission #834508

# Submission time Handle Problem Language Result Execution time Memory
834508 2023-08-22T15:06:00 Z alvingogo Tourism (JOI23_tourism) C++14
100 / 100
232 ms 56496 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
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-1;
}
int lca(int l,int r){
    int a=lin.query(l,r);
    int b=-lout.query(l,r);
    return pd.query(a,b);
}
int 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:178:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  178 |         for(auto [h,l]:vq[i]){
      |                  ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 324 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 328 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 324 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 320 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 324 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 328 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 324 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 320 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 3 ms 852 KB Output is correct
31 Correct 2 ms 852 KB Output is correct
32 Correct 3 ms 1108 KB Output is correct
33 Correct 3 ms 1108 KB Output is correct
34 Correct 4 ms 1108 KB Output is correct
35 Correct 4 ms 1108 KB Output is correct
36 Correct 4 ms 980 KB Output is correct
37 Correct 3 ms 1108 KB Output is correct
38 Correct 3 ms 1108 KB Output is correct
39 Correct 3 ms 1108 KB Output is correct
40 Correct 2 ms 1108 KB Output is correct
41 Correct 3 ms 1096 KB Output is correct
42 Correct 2 ms 1108 KB Output is correct
43 Correct 2 ms 1108 KB Output is correct
44 Correct 3 ms 1132 KB Output is correct
45 Correct 3 ms 1140 KB Output is correct
46 Correct 3 ms 1108 KB Output is correct
47 Correct 3 ms 1136 KB Output is correct
48 Correct 3 ms 1092 KB Output is correct
49 Correct 3 ms 1108 KB Output is correct
50 Correct 2 ms 1108 KB Output is correct
51 Correct 2 ms 1108 KB Output is correct
52 Correct 2 ms 1012 KB Output is correct
53 Correct 2 ms 1108 KB Output is correct
54 Correct 3 ms 1100 KB Output is correct
55 Correct 2 ms 1100 KB Output is correct
56 Correct 1 ms 596 KB Output is correct
57 Correct 2 ms 724 KB Output is correct
58 Correct 3 ms 1096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 316 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 616 KB Output is correct
4 Correct 109 ms 37924 KB Output is correct
5 Correct 95 ms 39900 KB Output is correct
6 Correct 120 ms 48708 KB Output is correct
7 Correct 192 ms 56364 KB Output is correct
8 Correct 159 ms 56364 KB Output is correct
9 Correct 158 ms 56364 KB Output is correct
10 Correct 170 ms 56496 KB Output is correct
11 Correct 150 ms 56404 KB Output is correct
12 Correct 117 ms 55520 KB Output is correct
13 Correct 116 ms 55580 KB Output is correct
14 Correct 109 ms 55596 KB Output is correct
15 Correct 48 ms 36748 KB Output is correct
16 Correct 157 ms 56108 KB Output is correct
17 Correct 44 ms 21852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 87 ms 24580 KB Output is correct
3 Correct 141 ms 32952 KB Output is correct
4 Correct 126 ms 31524 KB Output is correct
5 Correct 176 ms 46524 KB Output is correct
6 Correct 200 ms 46512 KB Output is correct
7 Correct 180 ms 46512 KB Output is correct
8 Correct 185 ms 46520 KB Output is correct
9 Correct 180 ms 46520 KB Output is correct
10 Correct 178 ms 46524 KB Output is correct
11 Correct 183 ms 46640 KB Output is correct
12 Correct 176 ms 46552 KB Output is correct
13 Correct 180 ms 47016 KB Output is correct
14 Correct 186 ms 47576 KB Output is correct
15 Correct 199 ms 50972 KB Output is correct
16 Correct 170 ms 47024 KB Output is correct
17 Correct 175 ms 47020 KB Output is correct
18 Correct 187 ms 47024 KB Output is correct
19 Correct 163 ms 46436 KB Output is correct
20 Correct 177 ms 46416 KB Output is correct
21 Correct 169 ms 46516 KB Output is correct
22 Correct 166 ms 46492 KB Output is correct
23 Correct 166 ms 46528 KB Output is correct
24 Correct 171 ms 46512 KB Output is correct
25 Correct 165 ms 46380 KB Output is correct
26 Correct 175 ms 46492 KB Output is correct
27 Correct 181 ms 46536 KB Output is correct
28 Correct 184 ms 46388 KB Output is correct
29 Correct 188 ms 46548 KB Output is correct
30 Correct 167 ms 46640 KB Output is correct
31 Correct 177 ms 47032 KB Output is correct
32 Correct 180 ms 47120 KB Output is correct
33 Correct 211 ms 48636 KB Output is correct
34 Correct 198 ms 50992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 316 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 620 KB Output is correct
4 Correct 169 ms 35600 KB Output is correct
5 Correct 177 ms 36472 KB Output is correct
6 Correct 207 ms 45704 KB Output is correct
7 Correct 229 ms 50104 KB Output is correct
8 Correct 228 ms 50088 KB Output is correct
9 Correct 219 ms 50160 KB Output is correct
10 Correct 232 ms 50096 KB Output is correct
11 Correct 226 ms 50088 KB Output is correct
12 Correct 223 ms 50096 KB Output is correct
13 Correct 231 ms 50156 KB Output is correct
14 Correct 44 ms 21808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 324 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 328 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 324 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 320 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 3 ms 852 KB Output is correct
31 Correct 2 ms 852 KB Output is correct
32 Correct 3 ms 1108 KB Output is correct
33 Correct 3 ms 1108 KB Output is correct
34 Correct 4 ms 1108 KB Output is correct
35 Correct 4 ms 1108 KB Output is correct
36 Correct 4 ms 980 KB Output is correct
37 Correct 3 ms 1108 KB Output is correct
38 Correct 3 ms 1108 KB Output is correct
39 Correct 3 ms 1108 KB Output is correct
40 Correct 2 ms 1108 KB Output is correct
41 Correct 3 ms 1096 KB Output is correct
42 Correct 2 ms 1108 KB Output is correct
43 Correct 2 ms 1108 KB Output is correct
44 Correct 3 ms 1132 KB Output is correct
45 Correct 3 ms 1140 KB Output is correct
46 Correct 3 ms 1108 KB Output is correct
47 Correct 3 ms 1136 KB Output is correct
48 Correct 3 ms 1092 KB Output is correct
49 Correct 3 ms 1108 KB Output is correct
50 Correct 2 ms 1108 KB Output is correct
51 Correct 2 ms 1108 KB Output is correct
52 Correct 2 ms 1012 KB Output is correct
53 Correct 2 ms 1108 KB Output is correct
54 Correct 3 ms 1100 KB Output is correct
55 Correct 2 ms 1100 KB Output is correct
56 Correct 1 ms 596 KB Output is correct
57 Correct 2 ms 724 KB Output is correct
58 Correct 3 ms 1096 KB Output is correct
59 Correct 0 ms 316 KB Output is correct
60 Correct 1 ms 316 KB Output is correct
61 Correct 1 ms 616 KB Output is correct
62 Correct 109 ms 37924 KB Output is correct
63 Correct 95 ms 39900 KB Output is correct
64 Correct 120 ms 48708 KB Output is correct
65 Correct 192 ms 56364 KB Output is correct
66 Correct 159 ms 56364 KB Output is correct
67 Correct 158 ms 56364 KB Output is correct
68 Correct 170 ms 56496 KB Output is correct
69 Correct 150 ms 56404 KB Output is correct
70 Correct 117 ms 55520 KB Output is correct
71 Correct 116 ms 55580 KB Output is correct
72 Correct 109 ms 55596 KB Output is correct
73 Correct 48 ms 36748 KB Output is correct
74 Correct 157 ms 56108 KB Output is correct
75 Correct 44 ms 21852 KB Output is correct
76 Correct 0 ms 212 KB Output is correct
77 Correct 87 ms 24580 KB Output is correct
78 Correct 141 ms 32952 KB Output is correct
79 Correct 126 ms 31524 KB Output is correct
80 Correct 176 ms 46524 KB Output is correct
81 Correct 200 ms 46512 KB Output is correct
82 Correct 180 ms 46512 KB Output is correct
83 Correct 185 ms 46520 KB Output is correct
84 Correct 180 ms 46520 KB Output is correct
85 Correct 178 ms 46524 KB Output is correct
86 Correct 183 ms 46640 KB Output is correct
87 Correct 176 ms 46552 KB Output is correct
88 Correct 180 ms 47016 KB Output is correct
89 Correct 186 ms 47576 KB Output is correct
90 Correct 199 ms 50972 KB Output is correct
91 Correct 170 ms 47024 KB Output is correct
92 Correct 175 ms 47020 KB Output is correct
93 Correct 187 ms 47024 KB Output is correct
94 Correct 163 ms 46436 KB Output is correct
95 Correct 177 ms 46416 KB Output is correct
96 Correct 169 ms 46516 KB Output is correct
97 Correct 166 ms 46492 KB Output is correct
98 Correct 166 ms 46528 KB Output is correct
99 Correct 171 ms 46512 KB Output is correct
100 Correct 165 ms 46380 KB Output is correct
101 Correct 175 ms 46492 KB Output is correct
102 Correct 181 ms 46536 KB Output is correct
103 Correct 184 ms 46388 KB Output is correct
104 Correct 188 ms 46548 KB Output is correct
105 Correct 167 ms 46640 KB Output is correct
106 Correct 177 ms 47032 KB Output is correct
107 Correct 180 ms 47120 KB Output is correct
108 Correct 211 ms 48636 KB Output is correct
109 Correct 198 ms 50992 KB Output is correct
110 Correct 0 ms 316 KB Output is correct
111 Correct 1 ms 340 KB Output is correct
112 Correct 1 ms 620 KB Output is correct
113 Correct 169 ms 35600 KB Output is correct
114 Correct 177 ms 36472 KB Output is correct
115 Correct 207 ms 45704 KB Output is correct
116 Correct 229 ms 50104 KB Output is correct
117 Correct 228 ms 50088 KB Output is correct
118 Correct 219 ms 50160 KB Output is correct
119 Correct 232 ms 50096 KB Output is correct
120 Correct 226 ms 50088 KB Output is correct
121 Correct 223 ms 50096 KB Output is correct
122 Correct 231 ms 50156 KB Output is correct
123 Correct 44 ms 21808 KB Output is correct
124 Correct 187 ms 47060 KB Output is correct
125 Correct 143 ms 40420 KB Output is correct
126 Correct 223 ms 50208 KB Output is correct
127 Correct 204 ms 50224 KB Output is correct
128 Correct 210 ms 50204 KB Output is correct
129 Correct 218 ms 50228 KB Output is correct
130 Correct 216 ms 50212 KB Output is correct
131 Correct 155 ms 55416 KB Output is correct
132 Correct 172 ms 56368 KB Output is correct
133 Correct 158 ms 53236 KB Output is correct
134 Correct 206 ms 50352 KB Output is correct
135 Correct 218 ms 50236 KB Output is correct
136 Correct 209 ms 50232 KB Output is correct
137 Correct 127 ms 50560 KB Output is correct
138 Correct 129 ms 50468 KB Output is correct
139 Correct 126 ms 50480 KB Output is correct
140 Correct 130 ms 50576 KB Output is correct
141 Correct 126 ms 50528 KB Output is correct
142 Correct 123 ms 50512 KB Output is correct
143 Correct 59 ms 30624 KB Output is correct
144 Correct 206 ms 49932 KB Output is correct