Submission #834226

# Submission time Handle Problem Language Result Execution time Memory
834226 2023-08-22T12:03:52 Z alvingogo Tourism (JOI23_tourism) C++14
100 / 100
286 ms 89696 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-1;
}
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:179:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  179 |         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 212 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 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 512 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 468 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 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 468 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 212 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 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 512 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 468 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 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 3 ms 1272 KB Output is correct
31 Correct 2 ms 1236 KB Output is correct
32 Correct 3 ms 1620 KB Output is correct
33 Correct 3 ms 1620 KB Output is correct
34 Correct 4 ms 1620 KB Output is correct
35 Correct 3 ms 1620 KB Output is correct
36 Correct 5 ms 1620 KB Output is correct
37 Correct 4 ms 1620 KB Output is correct
38 Correct 4 ms 1620 KB Output is correct
39 Correct 4 ms 1620 KB Output is correct
40 Correct 3 ms 1620 KB Output is correct
41 Correct 3 ms 1620 KB Output is correct
42 Correct 3 ms 1692 KB Output is correct
43 Correct 3 ms 1620 KB Output is correct
44 Correct 3 ms 1620 KB Output is correct
45 Correct 4 ms 1620 KB Output is correct
46 Correct 4 ms 1620 KB Output is correct
47 Correct 4 ms 1620 KB Output is correct
48 Correct 4 ms 1620 KB Output is correct
49 Correct 3 ms 1620 KB Output is correct
50 Correct 2 ms 1620 KB Output is correct
51 Correct 2 ms 1592 KB Output is correct
52 Correct 2 ms 1616 KB Output is correct
53 Correct 2 ms 1592 KB Output is correct
54 Correct 2 ms 1620 KB Output is correct
55 Correct 2 ms 1592 KB Output is correct
56 Correct 1 ms 852 KB Output is correct
57 Correct 2 ms 1152 KB Output is correct
58 Correct 4 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 852 KB Output is correct
4 Correct 151 ms 60676 KB Output is correct
5 Correct 132 ms 63116 KB Output is correct
6 Correct 151 ms 77752 KB Output is correct
7 Correct 190 ms 89664 KB Output is correct
8 Correct 200 ms 89676 KB Output is correct
9 Correct 227 ms 89696 KB Output is correct
10 Correct 187 ms 89604 KB Output is correct
11 Correct 192 ms 89600 KB Output is correct
12 Correct 141 ms 89536 KB Output is correct
13 Correct 125 ms 89536 KB Output is correct
14 Correct 137 ms 89604 KB Output is correct
15 Correct 68 ms 57344 KB Output is correct
16 Correct 203 ms 89296 KB Output is correct
17 Correct 60 ms 36788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 121 ms 42600 KB Output is correct
3 Correct 169 ms 57264 KB Output is correct
4 Correct 148 ms 54384 KB Output is correct
5 Correct 237 ms 80620 KB Output is correct
6 Correct 271 ms 80624 KB Output is correct
7 Correct 227 ms 80560 KB Output is correct
8 Correct 211 ms 80620 KB Output is correct
9 Correct 205 ms 80632 KB Output is correct
10 Correct 227 ms 80624 KB Output is correct
11 Correct 208 ms 80628 KB Output is correct
12 Correct 222 ms 80536 KB Output is correct
13 Correct 227 ms 80624 KB Output is correct
14 Correct 232 ms 81420 KB Output is correct
15 Correct 245 ms 84096 KB Output is correct
16 Correct 223 ms 80620 KB Output is correct
17 Correct 234 ms 80732 KB Output is correct
18 Correct 218 ms 80632 KB Output is correct
19 Correct 219 ms 80156 KB Output is correct
20 Correct 225 ms 80236 KB Output is correct
21 Correct 228 ms 80128 KB Output is correct
22 Correct 211 ms 80148 KB Output is correct
23 Correct 221 ms 80168 KB Output is correct
24 Correct 230 ms 80136 KB Output is correct
25 Correct 194 ms 80064 KB Output is correct
26 Correct 223 ms 80100 KB Output is correct
27 Correct 198 ms 80052 KB Output is correct
28 Correct 210 ms 80080 KB Output is correct
29 Correct 220 ms 80140 KB Output is correct
30 Correct 206 ms 80168 KB Output is correct
31 Correct 204 ms 80196 KB Output is correct
32 Correct 220 ms 81024 KB Output is correct
33 Correct 231 ms 81608 KB Output is correct
34 Correct 230 ms 83740 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 198 ms 59052 KB Output is correct
5 Correct 204 ms 60752 KB Output is correct
6 Correct 249 ms 76648 KB Output is correct
7 Correct 277 ms 84172 KB Output is correct
8 Correct 266 ms 84128 KB Output is correct
9 Correct 286 ms 84124 KB Output is correct
10 Correct 261 ms 84168 KB Output is correct
11 Correct 271 ms 84120 KB Output is correct
12 Correct 269 ms 84236 KB Output is correct
13 Correct 275 ms 84136 KB Output is correct
14 Correct 66 ms 36812 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 212 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 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 512 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 468 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 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 3 ms 1272 KB Output is correct
31 Correct 2 ms 1236 KB Output is correct
32 Correct 3 ms 1620 KB Output is correct
33 Correct 3 ms 1620 KB Output is correct
34 Correct 4 ms 1620 KB Output is correct
35 Correct 3 ms 1620 KB Output is correct
36 Correct 5 ms 1620 KB Output is correct
37 Correct 4 ms 1620 KB Output is correct
38 Correct 4 ms 1620 KB Output is correct
39 Correct 4 ms 1620 KB Output is correct
40 Correct 3 ms 1620 KB Output is correct
41 Correct 3 ms 1620 KB Output is correct
42 Correct 3 ms 1692 KB Output is correct
43 Correct 3 ms 1620 KB Output is correct
44 Correct 3 ms 1620 KB Output is correct
45 Correct 4 ms 1620 KB Output is correct
46 Correct 4 ms 1620 KB Output is correct
47 Correct 4 ms 1620 KB Output is correct
48 Correct 4 ms 1620 KB Output is correct
49 Correct 3 ms 1620 KB Output is correct
50 Correct 2 ms 1620 KB Output is correct
51 Correct 2 ms 1592 KB Output is correct
52 Correct 2 ms 1616 KB Output is correct
53 Correct 2 ms 1592 KB Output is correct
54 Correct 2 ms 1620 KB Output is correct
55 Correct 2 ms 1592 KB Output is correct
56 Correct 1 ms 852 KB Output is correct
57 Correct 2 ms 1152 KB Output is correct
58 Correct 4 ms 1492 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 0 ms 340 KB Output is correct
61 Correct 1 ms 852 KB Output is correct
62 Correct 151 ms 60676 KB Output is correct
63 Correct 132 ms 63116 KB Output is correct
64 Correct 151 ms 77752 KB Output is correct
65 Correct 190 ms 89664 KB Output is correct
66 Correct 200 ms 89676 KB Output is correct
67 Correct 227 ms 89696 KB Output is correct
68 Correct 187 ms 89604 KB Output is correct
69 Correct 192 ms 89600 KB Output is correct
70 Correct 141 ms 89536 KB Output is correct
71 Correct 125 ms 89536 KB Output is correct
72 Correct 137 ms 89604 KB Output is correct
73 Correct 68 ms 57344 KB Output is correct
74 Correct 203 ms 89296 KB Output is correct
75 Correct 60 ms 36788 KB Output is correct
76 Correct 0 ms 212 KB Output is correct
77 Correct 121 ms 42600 KB Output is correct
78 Correct 169 ms 57264 KB Output is correct
79 Correct 148 ms 54384 KB Output is correct
80 Correct 237 ms 80620 KB Output is correct
81 Correct 271 ms 80624 KB Output is correct
82 Correct 227 ms 80560 KB Output is correct
83 Correct 211 ms 80620 KB Output is correct
84 Correct 205 ms 80632 KB Output is correct
85 Correct 227 ms 80624 KB Output is correct
86 Correct 208 ms 80628 KB Output is correct
87 Correct 222 ms 80536 KB Output is correct
88 Correct 227 ms 80624 KB Output is correct
89 Correct 232 ms 81420 KB Output is correct
90 Correct 245 ms 84096 KB Output is correct
91 Correct 223 ms 80620 KB Output is correct
92 Correct 234 ms 80732 KB Output is correct
93 Correct 218 ms 80632 KB Output is correct
94 Correct 219 ms 80156 KB Output is correct
95 Correct 225 ms 80236 KB Output is correct
96 Correct 228 ms 80128 KB Output is correct
97 Correct 211 ms 80148 KB Output is correct
98 Correct 221 ms 80168 KB Output is correct
99 Correct 230 ms 80136 KB Output is correct
100 Correct 194 ms 80064 KB Output is correct
101 Correct 223 ms 80100 KB Output is correct
102 Correct 198 ms 80052 KB Output is correct
103 Correct 210 ms 80080 KB Output is correct
104 Correct 220 ms 80140 KB Output is correct
105 Correct 206 ms 80168 KB Output is correct
106 Correct 204 ms 80196 KB Output is correct
107 Correct 220 ms 81024 KB Output is correct
108 Correct 231 ms 81608 KB Output is correct
109 Correct 230 ms 83740 KB Output is correct
110 Correct 0 ms 212 KB Output is correct
111 Correct 1 ms 340 KB Output is correct
112 Correct 1 ms 852 KB Output is correct
113 Correct 198 ms 59052 KB Output is correct
114 Correct 204 ms 60752 KB Output is correct
115 Correct 249 ms 76648 KB Output is correct
116 Correct 277 ms 84172 KB Output is correct
117 Correct 266 ms 84128 KB Output is correct
118 Correct 286 ms 84124 KB Output is correct
119 Correct 261 ms 84168 KB Output is correct
120 Correct 271 ms 84120 KB Output is correct
121 Correct 269 ms 84236 KB Output is correct
122 Correct 275 ms 84136 KB Output is correct
123 Correct 66 ms 36812 KB Output is correct
124 Correct 250 ms 78400 KB Output is correct
125 Correct 177 ms 67956 KB Output is correct
126 Correct 268 ms 83980 KB Output is correct
127 Correct 266 ms 83960 KB Output is correct
128 Correct 272 ms 84020 KB Output is correct
129 Correct 266 ms 83996 KB Output is correct
130 Correct 265 ms 83968 KB Output is correct
131 Correct 199 ms 88652 KB Output is correct
132 Correct 207 ms 89596 KB Output is correct
133 Correct 201 ms 86520 KB Output is correct
134 Correct 252 ms 83512 KB Output is correct
135 Correct 256 ms 83484 KB Output is correct
136 Correct 250 ms 83472 KB Output is correct
137 Correct 163 ms 84172 KB Output is correct
138 Correct 174 ms 84128 KB Output is correct
139 Correct 169 ms 84140 KB Output is correct
140 Correct 168 ms 84152 KB Output is correct
141 Correct 167 ms 84140 KB Output is correct
142 Correct 169 ms 84144 KB Output is correct
143 Correct 81 ms 51704 KB Output is correct
144 Correct 259 ms 83680 KB Output is correct