Submission #755330

# Submission time Handle Problem Language Result Execution time Memory
755330 2023-06-09T22:46:39 Z bachhoangxuan Tourism (JOI23_tourism) C++17
100 / 100
466 ms 30452 KB
// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define fi first
#define se second
//const int inf=1e18;
const int mod=998244353;
const int mod2=1e9+7;
const int maxn=200005;
const int bl=650;
const int maxs=650;
const int maxm=200005;
const int maxq=500005;
const int maxl=20;
const int maxa=1000005;
namespace BIT{
    int n;
    vector<int> bit;
    void init(int _n=0){n=_n;bit.assign(n+1,0);}
    void update(int x,int val){
        x=n+1-x;
        for(int i=x;i<=n;i+=(i&(-i))) bit[i]+=val;
    }
    int query(int x){
        int res=0;x=n+1-x;
        for(int i=x;i>=1;i-=(i&(-i))) res+=bit[i];
        return res;
    }
}
namespace Segtree{
    int n=0;
    vector<int> tree;
    void init(int _n=0){
        n=_n;
        tree.assign(4*n,0);
    }
    void pushdown(int id){
        if(tree[id]==-1) return;
        tree[id<<1]=tree[id<<1|1]=tree[id];
    }
    void update(int l,int r,int id,int tl,int tr,int val){
        if(tr<l || r<tl) return;
        if(tl<=l && r<=tr && tree[id]>=0){
            if(tree[id]) BIT::update(tree[id],-(r-l+1));
            BIT::update(tree[id]=val,r-l+1);
            return;
        }
        pushdown(id);
        int mid=(l+r)>>1;
        update(l,mid,id<<1,tl,tr,val);update(mid+1,r,id<<1|1,tl,tr,val);
        tree[id]=(tree[id<<1]==tree[id<<1|1]?tree[id<<1]:-1);
    }
};
void solve(){
    int n,m,q;cin >> n >> m >> q;
    vector<vector<int>> edge(n+1);
    vector<int> L(n+1),head(n+1,0),child(n+1,1),son(n+1,0),par(n+1,0);
    vector<int> ans(q+1,1),c(m+1);
    vector<vector<pii>> v(m+1);
    int pos=0;
    BIT::init(m);Segtree::init(n);
    for(int i=1;i<n;i++){
        int u,v;cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    function<void(int,int)> dfs = [&](int u,int p){
        par[u]=p;
        for(int v:edge[u]){
            if(v==p) continue;
            dfs(v,u);child[u]+=child[v];
            son[u]=(child[son[u]]<=child[v]?v:son[u]);
        }
    };
    dfs(1,0);
    function<void(int,int)> hld_dfs = [&](int u,int p){
        L[u]=++pos;head[u]+=(!head[u])*u;
        if(son[u]){
            head[son[u]]=head[u];
            hld_dfs(son[u],u);
        }
        for(int v:edge[u]){
            if(v==p || v==son[u]) continue;
            hld_dfs(v,u);
        }
    };
    hld_dfs(1,0);
    for(int i=1;i<=m;i++) cin >> c[i];
    for(int i=1;i<=q;i++){
        int l,r;cin >> l >> r;
        if(l<r) v[r].push_back({l,i});
    }
    auto hld_update = [=](int u,int v,int val){
        while(head[u]!=head[v]){
            if(L[head[u]]<L[head[v]]) swap(u,v);
            Segtree::update(1,n,1,L[head[u]],L[u],val);
            u=par[head[u]];
        }
        Segtree::update(1,n,1,min(L[u],L[v]),max(L[u],L[v]),val);
    };
    for(int i=2;i<=m;i++){
        hld_update(c[i],c[i-1],i-1);
        for(auto [l,id]:v[i]) ans[id]=BIT::query(l);
    }
    for(int i=1;i<=q;i++) cout << ans[i] << '\n';
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int test=1;//cin >> test;
    while(test--) solve();
}
# 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 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 340 KB Output is correct
9 Correct 1 ms 324 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 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 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 340 KB Output is correct
28 Correct 1 ms 324 KB Output is correct
29 Correct 1 ms 340 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 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 340 KB Output is correct
9 Correct 1 ms 324 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 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 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 340 KB Output is correct
28 Correct 1 ms 324 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 3 ms 588 KB Output is correct
31 Correct 3 ms 596 KB Output is correct
32 Correct 4 ms 596 KB Output is correct
33 Correct 4 ms 596 KB Output is correct
34 Correct 4 ms 596 KB Output is correct
35 Correct 5 ms 596 KB Output is correct
36 Correct 4 ms 592 KB Output is correct
37 Correct 4 ms 596 KB Output is correct
38 Correct 4 ms 724 KB Output is correct
39 Correct 4 ms 852 KB Output is correct
40 Correct 4 ms 848 KB Output is correct
41 Correct 4 ms 852 KB Output is correct
42 Correct 4 ms 852 KB Output is correct
43 Correct 4 ms 724 KB Output is correct
44 Correct 5 ms 724 KB Output is correct
45 Correct 5 ms 724 KB Output is correct
46 Correct 4 ms 724 KB Output is correct
47 Correct 4 ms 720 KB Output is correct
48 Correct 4 ms 724 KB Output is correct
49 Correct 4 ms 720 KB Output is correct
50 Correct 3 ms 596 KB Output is correct
51 Correct 3 ms 596 KB Output is correct
52 Correct 3 ms 596 KB Output is correct
53 Correct 3 ms 596 KB Output is correct
54 Correct 3 ms 596 KB Output is correct
55 Correct 3 ms 724 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 2 ms 468 KB Output is correct
58 Correct 4 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 188 ms 19076 KB Output is correct
5 Correct 135 ms 23056 KB Output is correct
6 Correct 192 ms 26188 KB Output is correct
7 Correct 245 ms 30448 KB Output is correct
8 Correct 234 ms 30404 KB Output is correct
9 Correct 239 ms 30452 KB Output is correct
10 Correct 250 ms 30408 KB Output is correct
11 Correct 233 ms 30368 KB Output is correct
12 Correct 137 ms 29460 KB Output is correct
13 Correct 132 ms 29548 KB Output is correct
14 Correct 134 ms 29520 KB Output is correct
15 Correct 52 ms 23504 KB Output is correct
16 Correct 218 ms 28124 KB Output is correct
17 Correct 48 ms 7372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 148 ms 8752 KB Output is correct
3 Correct 228 ms 10444 KB Output is correct
4 Correct 185 ms 10660 KB Output is correct
5 Correct 292 ms 15508 KB Output is correct
6 Correct 310 ms 15500 KB Output is correct
7 Correct 288 ms 15436 KB Output is correct
8 Correct 283 ms 15484 KB Output is correct
9 Correct 292 ms 15528 KB Output is correct
10 Correct 304 ms 15500 KB Output is correct
11 Correct 285 ms 15520 KB Output is correct
12 Correct 293 ms 15636 KB Output is correct
13 Correct 292 ms 15948 KB Output is correct
14 Correct 317 ms 16844 KB Output is correct
15 Correct 299 ms 17104 KB Output is correct
16 Correct 290 ms 16076 KB Output is correct
17 Correct 290 ms 15944 KB Output is correct
18 Correct 297 ms 16056 KB Output is correct
19 Correct 384 ms 15564 KB Output is correct
20 Correct 403 ms 15600 KB Output is correct
21 Correct 411 ms 15556 KB Output is correct
22 Correct 394 ms 15704 KB Output is correct
23 Correct 389 ms 15672 KB Output is correct
24 Correct 390 ms 15644 KB Output is correct
25 Correct 397 ms 15540 KB Output is correct
26 Correct 390 ms 15516 KB Output is correct
27 Correct 386 ms 15616 KB Output is correct
28 Correct 397 ms 15596 KB Output is correct
29 Correct 416 ms 15704 KB Output is correct
30 Correct 415 ms 15828 KB Output is correct
31 Correct 403 ms 16076 KB Output is correct
32 Correct 416 ms 16564 KB Output is correct
33 Correct 412 ms 17376 KB Output is correct
34 Correct 433 ms 17308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 373 ms 13816 KB Output is correct
5 Correct 343 ms 13992 KB Output is correct
6 Correct 409 ms 17480 KB Output is correct
7 Correct 434 ms 19404 KB Output is correct
8 Correct 425 ms 19404 KB Output is correct
9 Correct 447 ms 19396 KB Output is correct
10 Correct 429 ms 19452 KB Output is correct
11 Correct 438 ms 19404 KB Output is correct
12 Correct 437 ms 19492 KB Output is correct
13 Correct 445 ms 19360 KB Output is correct
14 Correct 44 ms 7360 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 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 340 KB Output is correct
9 Correct 1 ms 324 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 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 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 340 KB Output is correct
28 Correct 1 ms 324 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 3 ms 588 KB Output is correct
31 Correct 3 ms 596 KB Output is correct
32 Correct 4 ms 596 KB Output is correct
33 Correct 4 ms 596 KB Output is correct
34 Correct 4 ms 596 KB Output is correct
35 Correct 5 ms 596 KB Output is correct
36 Correct 4 ms 592 KB Output is correct
37 Correct 4 ms 596 KB Output is correct
38 Correct 4 ms 724 KB Output is correct
39 Correct 4 ms 852 KB Output is correct
40 Correct 4 ms 848 KB Output is correct
41 Correct 4 ms 852 KB Output is correct
42 Correct 4 ms 852 KB Output is correct
43 Correct 4 ms 724 KB Output is correct
44 Correct 5 ms 724 KB Output is correct
45 Correct 5 ms 724 KB Output is correct
46 Correct 4 ms 724 KB Output is correct
47 Correct 4 ms 720 KB Output is correct
48 Correct 4 ms 724 KB Output is correct
49 Correct 4 ms 720 KB Output is correct
50 Correct 3 ms 596 KB Output is correct
51 Correct 3 ms 596 KB Output is correct
52 Correct 3 ms 596 KB Output is correct
53 Correct 3 ms 596 KB Output is correct
54 Correct 3 ms 596 KB Output is correct
55 Correct 3 ms 724 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 2 ms 468 KB Output is correct
58 Correct 4 ms 596 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 2 ms 340 KB Output is correct
62 Correct 188 ms 19076 KB Output is correct
63 Correct 135 ms 23056 KB Output is correct
64 Correct 192 ms 26188 KB Output is correct
65 Correct 245 ms 30448 KB Output is correct
66 Correct 234 ms 30404 KB Output is correct
67 Correct 239 ms 30452 KB Output is correct
68 Correct 250 ms 30408 KB Output is correct
69 Correct 233 ms 30368 KB Output is correct
70 Correct 137 ms 29460 KB Output is correct
71 Correct 132 ms 29548 KB Output is correct
72 Correct 134 ms 29520 KB Output is correct
73 Correct 52 ms 23504 KB Output is correct
74 Correct 218 ms 28124 KB Output is correct
75 Correct 48 ms 7372 KB Output is correct
76 Correct 1 ms 212 KB Output is correct
77 Correct 148 ms 8752 KB Output is correct
78 Correct 228 ms 10444 KB Output is correct
79 Correct 185 ms 10660 KB Output is correct
80 Correct 292 ms 15508 KB Output is correct
81 Correct 310 ms 15500 KB Output is correct
82 Correct 288 ms 15436 KB Output is correct
83 Correct 283 ms 15484 KB Output is correct
84 Correct 292 ms 15528 KB Output is correct
85 Correct 304 ms 15500 KB Output is correct
86 Correct 285 ms 15520 KB Output is correct
87 Correct 293 ms 15636 KB Output is correct
88 Correct 292 ms 15948 KB Output is correct
89 Correct 317 ms 16844 KB Output is correct
90 Correct 299 ms 17104 KB Output is correct
91 Correct 290 ms 16076 KB Output is correct
92 Correct 290 ms 15944 KB Output is correct
93 Correct 297 ms 16056 KB Output is correct
94 Correct 384 ms 15564 KB Output is correct
95 Correct 403 ms 15600 KB Output is correct
96 Correct 411 ms 15556 KB Output is correct
97 Correct 394 ms 15704 KB Output is correct
98 Correct 389 ms 15672 KB Output is correct
99 Correct 390 ms 15644 KB Output is correct
100 Correct 397 ms 15540 KB Output is correct
101 Correct 390 ms 15516 KB Output is correct
102 Correct 386 ms 15616 KB Output is correct
103 Correct 397 ms 15596 KB Output is correct
104 Correct 416 ms 15704 KB Output is correct
105 Correct 415 ms 15828 KB Output is correct
106 Correct 403 ms 16076 KB Output is correct
107 Correct 416 ms 16564 KB Output is correct
108 Correct 412 ms 17376 KB Output is correct
109 Correct 433 ms 17308 KB Output is correct
110 Correct 1 ms 212 KB Output is correct
111 Correct 1 ms 320 KB Output is correct
112 Correct 1 ms 468 KB Output is correct
113 Correct 373 ms 13816 KB Output is correct
114 Correct 343 ms 13992 KB Output is correct
115 Correct 409 ms 17480 KB Output is correct
116 Correct 434 ms 19404 KB Output is correct
117 Correct 425 ms 19404 KB Output is correct
118 Correct 447 ms 19396 KB Output is correct
119 Correct 429 ms 19452 KB Output is correct
120 Correct 438 ms 19404 KB Output is correct
121 Correct 437 ms 19492 KB Output is correct
122 Correct 445 ms 19360 KB Output is correct
123 Correct 44 ms 7360 KB Output is correct
124 Correct 283 ms 18740 KB Output is correct
125 Correct 219 ms 16120 KB Output is correct
126 Correct 344 ms 19516 KB Output is correct
127 Correct 365 ms 19528 KB Output is correct
128 Correct 342 ms 19532 KB Output is correct
129 Correct 335 ms 19520 KB Output is correct
130 Correct 334 ms 19580 KB Output is correct
131 Correct 272 ms 28800 KB Output is correct
132 Correct 250 ms 30244 KB Output is correct
133 Correct 283 ms 24976 KB Output is correct
134 Correct 428 ms 19776 KB Output is correct
135 Correct 466 ms 19636 KB Output is correct
136 Correct 450 ms 19696 KB Output is correct
137 Correct 172 ms 19916 KB Output is correct
138 Correct 156 ms 19784 KB Output is correct
139 Correct 162 ms 19868 KB Output is correct
140 Correct 162 ms 19764 KB Output is correct
141 Correct 158 ms 19780 KB Output is correct
142 Correct 156 ms 19804 KB Output is correct
143 Correct 66 ms 12632 KB Output is correct
144 Correct 325 ms 17144 KB Output is correct