Submission #748956

# Submission time Handle Problem Language Result Execution time Memory
748956 2023-05-27T08:02:51 Z WongChun1234 Tourism (JOI23_tourism) C++14
100 / 100
1112 ms 32332 KB
#include<bits/stdc++.h>
using namespace std;
const int N=100050;
int n,m,q,u,v,c[N],sz[N],ch[N],pre[N],post[N],lift[20][N],pt;
int hd[N],rpar[N],bit[N],lcaa,ans[N];
vector<int> adj[N];
vector<pair<int,int>> qs[N];
set<pair<int,int>> s;
void calcsize(int nde,int par){
    sz[nde]=1;
    for (int i:adj[nde]) if (i!=par){
        calcsize(i,nde);
        sz[nde]+=sz[i];
        if (sz[i]>sz[ch[nde]]) ch[nde]=i;
    }
}
void prehld(int nde,int par){
    lift[0][nde]=rpar[nde]=par;
    for (int i=1;i<19;i++) lift[i][nde]=lift[i-1][lift[i-1][nde]];
    pre[nde]=++pt;
    if (sz[nde]>1){
        hd[ch[nde]]=hd[nde];
        prehld(ch[nde],nde);
    }
    for (int i:adj[nde]) if (i!=par&&i!=ch[nde]){
        hd[i]=i;
        prehld(i,nde);
    }
    post[nde]=pt;
}
bool isa(int a,int b){
    return pre[a]<=pre[b]&&post[a]>=post[b];
}
int lca(int a,int b){
    if (isa(a,b)) return a;
    if (isa(b,a)) return b;
    for (int i=18;~i;i--) if (!isa(lift[i][a],b)) a=lift[i][a];
    return rpar[a];
}
void modify(int q,int v){
    for (;q<N;q+=q&-q) bit[q]+=v;
}
int query(int q){
    int ret=0;
    for (;q;q-=q&-q) ret+=bit[q];
    return ret;
}
vector<pair<pair<int,int>,int>> del;
int curr[N];
void add(int l,int r,int v){
	//cout<<"add "<<l<<" "<<r<<" "<<v<<"\n";
	//for (auto i:s) cout<<(i).first<<" "<<(i).second<<" ins\n";
    set<pair<int,int>>::iterator it;
    del.clear();
    it=s.upper_bound({l,INT_MIN});
    if ((*it).second>0) it--;
    while ((*it).second&&(*it).first<=r){
    	//cout<<(*it).first<<" "<<(*it).second<<"!\n";
    	int st=(*it).first;
    	it++;
    	s.erase(prev(it));
    	del.push_back({{st,(*it).first},(*it).second});
        modify((*it).second,-((*it).first-st+1));
        it++;
        s.erase(prev(it));
    }
    for (auto i:del){
    	if (i.first.first<l) s.insert({i.first.first,-i.second}),s.insert({l-1,i.second}),modify(i.second,l-i.first.first);
    	if (i.first.second>r) s.insert({r+1,-i.second}),s.insert({i.first.second,i.second}),modify(i.second,i.first.second-r);
	}
    s.insert({l,-v});
    s.insert({r,v});
    modify(v,r-l+1);
    //cout<<"end\n";
}
int main(){
    cin.tie(0)->sync_with_stdio(0);
    cin>>n>>m>>q;
    for (int i=1;i<n;i++){
        cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    calcsize(1,1);
    hd[1]=1;
    prehld(1,1);
    for (int i=1;i<=m;i++) cin>>c[i];
    for (int i=1;i<=q;i++){
        cin>>u>>v;
        qs[v].push_back({u,i});
    }
    //for (int i=1;i<=n;i++) cout<<i<<" "<<hd[i]<<" "<<rpar[i]<<"\n";
    s.insert({0,0});
    s.insert({INT_MAX,0});
    for (int i=1;i<=m;i++){
        if (i>1){
            u=c[i-1];
            v=c[i];
            lcaa=lca(u,v);
        	//cout<<u<<"-"<<v<<"\n";
            while (hd[u]!=hd[lcaa]){
            	//cout<<u<<"!\n";
                add(pre[hd[u]],pre[u],i-1);
                u=rpar[hd[u]];
            }
            while (hd[v]!=hd[lcaa]){
            	//cout<<v<<"?\n";
                add(pre[hd[v]],pre[v],i-1);
                v=rpar[hd[v]];
            }
            add(min(pre[u],pre[v]),max(pre[u],pre[v]),i-1);
        }
        //cout<<i<<":\n";
        //for (auto j:s) cout<<j.first<<" "<<j.second<<"\n";
        for (auto j:qs[i]){
            ans[j.second]=(j.first==i?1:query(m)-query(j.first-1));
        }
    }
    for (int i=1;i<=q;i++) cout<<ans[i]<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 5 ms 5160 KB Output is correct
4 Correct 3 ms 5204 KB Output is correct
5 Correct 3 ms 5164 KB Output is correct
6 Correct 4 ms 5204 KB Output is correct
7 Correct 3 ms 5156 KB Output is correct
8 Correct 4 ms 5204 KB Output is correct
9 Correct 4 ms 5204 KB Output is correct
10 Correct 4 ms 5164 KB Output is correct
11 Correct 4 ms 5204 KB Output is correct
12 Correct 3 ms 5204 KB Output is correct
13 Correct 4 ms 5204 KB Output is correct
14 Correct 4 ms 5164 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 4 ms 5204 KB Output is correct
17 Correct 4 ms 5164 KB Output is correct
18 Correct 4 ms 5204 KB Output is correct
19 Correct 3 ms 5204 KB Output is correct
20 Correct 3 ms 5204 KB Output is correct
21 Correct 3 ms 5204 KB Output is correct
22 Correct 3 ms 5204 KB Output is correct
23 Correct 3 ms 5204 KB Output is correct
24 Correct 3 ms 5204 KB Output is correct
25 Correct 3 ms 5204 KB Output is correct
26 Correct 3 ms 5168 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5160 KB Output is correct
29 Correct 3 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 5 ms 5160 KB Output is correct
4 Correct 3 ms 5204 KB Output is correct
5 Correct 3 ms 5164 KB Output is correct
6 Correct 4 ms 5204 KB Output is correct
7 Correct 3 ms 5156 KB Output is correct
8 Correct 4 ms 5204 KB Output is correct
9 Correct 4 ms 5204 KB Output is correct
10 Correct 4 ms 5164 KB Output is correct
11 Correct 4 ms 5204 KB Output is correct
12 Correct 3 ms 5204 KB Output is correct
13 Correct 4 ms 5204 KB Output is correct
14 Correct 4 ms 5164 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 4 ms 5204 KB Output is correct
17 Correct 4 ms 5164 KB Output is correct
18 Correct 4 ms 5204 KB Output is correct
19 Correct 3 ms 5204 KB Output is correct
20 Correct 3 ms 5204 KB Output is correct
21 Correct 3 ms 5204 KB Output is correct
22 Correct 3 ms 5204 KB Output is correct
23 Correct 3 ms 5204 KB Output is correct
24 Correct 3 ms 5204 KB Output is correct
25 Correct 3 ms 5204 KB Output is correct
26 Correct 3 ms 5168 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5160 KB Output is correct
29 Correct 3 ms 5204 KB Output is correct
30 Correct 8 ms 5428 KB Output is correct
31 Correct 9 ms 5460 KB Output is correct
32 Correct 10 ms 5552 KB Output is correct
33 Correct 9 ms 5620 KB Output is correct
34 Correct 9 ms 5604 KB Output is correct
35 Correct 10 ms 5556 KB Output is correct
36 Correct 10 ms 5588 KB Output is correct
37 Correct 10 ms 5548 KB Output is correct
38 Correct 6 ms 5588 KB Output is correct
39 Correct 6 ms 5588 KB Output is correct
40 Correct 6 ms 5560 KB Output is correct
41 Correct 5 ms 5552 KB Output is correct
42 Correct 5 ms 5588 KB Output is correct
43 Correct 6 ms 5552 KB Output is correct
44 Correct 7 ms 5612 KB Output is correct
45 Correct 7 ms 5520 KB Output is correct
46 Correct 7 ms 5612 KB Output is correct
47 Correct 7 ms 5556 KB Output is correct
48 Correct 7 ms 5552 KB Output is correct
49 Correct 9 ms 5588 KB Output is correct
50 Correct 7 ms 5588 KB Output is correct
51 Correct 7 ms 5620 KB Output is correct
52 Correct 7 ms 5588 KB Output is correct
53 Correct 7 ms 5676 KB Output is correct
54 Correct 6 ms 5588 KB Output is correct
55 Correct 6 ms 5588 KB Output is correct
56 Correct 4 ms 5192 KB Output is correct
57 Correct 4 ms 5460 KB Output is correct
58 Correct 10 ms 5588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5156 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 4 ms 5204 KB Output is correct
4 Correct 107 ms 21504 KB Output is correct
5 Correct 96 ms 26144 KB Output is correct
6 Correct 108 ms 28648 KB Output is correct
7 Correct 148 ms 32284 KB Output is correct
8 Correct 142 ms 32332 KB Output is correct
9 Correct 138 ms 32308 KB Output is correct
10 Correct 139 ms 32180 KB Output is correct
11 Correct 139 ms 32024 KB Output is correct
12 Correct 139 ms 31692 KB Output is correct
13 Correct 139 ms 31820 KB Output is correct
14 Correct 141 ms 31460 KB Output is correct
15 Correct 55 ms 28716 KB Output is correct
16 Correct 155 ms 32084 KB Output is correct
17 Correct 58 ms 9824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 357 ms 16476 KB Output is correct
3 Correct 552 ms 18044 KB Output is correct
4 Correct 445 ms 19024 KB Output is correct
5 Correct 761 ms 25136 KB Output is correct
6 Correct 756 ms 25492 KB Output is correct
7 Correct 757 ms 25384 KB Output is correct
8 Correct 751 ms 25304 KB Output is correct
9 Correct 770 ms 25504 KB Output is correct
10 Correct 757 ms 25416 KB Output is correct
11 Correct 784 ms 25232 KB Output is correct
12 Correct 769 ms 25476 KB Output is correct
13 Correct 769 ms 25588 KB Output is correct
14 Correct 773 ms 26912 KB Output is correct
15 Correct 818 ms 30236 KB Output is correct
16 Correct 794 ms 25984 KB Output is correct
17 Correct 773 ms 26136 KB Output is correct
18 Correct 755 ms 26120 KB Output is correct
19 Correct 501 ms 20980 KB Output is correct
20 Correct 495 ms 20968 KB Output is correct
21 Correct 491 ms 21012 KB Output is correct
22 Correct 475 ms 20736 KB Output is correct
23 Correct 458 ms 20652 KB Output is correct
24 Correct 458 ms 20996 KB Output is correct
25 Correct 460 ms 20940 KB Output is correct
26 Correct 517 ms 20852 KB Output is correct
27 Correct 480 ms 20916 KB Output is correct
28 Correct 480 ms 21068 KB Output is correct
29 Correct 493 ms 21196 KB Output is correct
30 Correct 497 ms 21240 KB Output is correct
31 Correct 502 ms 21504 KB Output is correct
32 Correct 494 ms 22088 KB Output is correct
33 Correct 504 ms 23412 KB Output is correct
34 Correct 514 ms 26052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5204 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 4 ms 5204 KB Output is correct
4 Correct 860 ms 21316 KB Output is correct
5 Correct 907 ms 21852 KB Output is correct
6 Correct 1036 ms 27248 KB Output is correct
7 Correct 1079 ms 29988 KB Output is correct
8 Correct 1088 ms 30012 KB Output is correct
9 Correct 1100 ms 29988 KB Output is correct
10 Correct 1084 ms 29844 KB Output is correct
11 Correct 1112 ms 30108 KB Output is correct
12 Correct 1077 ms 30188 KB Output is correct
13 Correct 1098 ms 30076 KB Output is correct
14 Correct 75 ms 9748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 5 ms 5160 KB Output is correct
4 Correct 3 ms 5204 KB Output is correct
5 Correct 3 ms 5164 KB Output is correct
6 Correct 4 ms 5204 KB Output is correct
7 Correct 3 ms 5156 KB Output is correct
8 Correct 4 ms 5204 KB Output is correct
9 Correct 4 ms 5204 KB Output is correct
10 Correct 4 ms 5164 KB Output is correct
11 Correct 4 ms 5204 KB Output is correct
12 Correct 3 ms 5204 KB Output is correct
13 Correct 4 ms 5204 KB Output is correct
14 Correct 4 ms 5164 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 4 ms 5204 KB Output is correct
17 Correct 4 ms 5164 KB Output is correct
18 Correct 4 ms 5204 KB Output is correct
19 Correct 3 ms 5204 KB Output is correct
20 Correct 3 ms 5204 KB Output is correct
21 Correct 3 ms 5204 KB Output is correct
22 Correct 3 ms 5204 KB Output is correct
23 Correct 3 ms 5204 KB Output is correct
24 Correct 3 ms 5204 KB Output is correct
25 Correct 3 ms 5204 KB Output is correct
26 Correct 3 ms 5168 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5160 KB Output is correct
29 Correct 3 ms 5204 KB Output is correct
30 Correct 8 ms 5428 KB Output is correct
31 Correct 9 ms 5460 KB Output is correct
32 Correct 10 ms 5552 KB Output is correct
33 Correct 9 ms 5620 KB Output is correct
34 Correct 9 ms 5604 KB Output is correct
35 Correct 10 ms 5556 KB Output is correct
36 Correct 10 ms 5588 KB Output is correct
37 Correct 10 ms 5548 KB Output is correct
38 Correct 6 ms 5588 KB Output is correct
39 Correct 6 ms 5588 KB Output is correct
40 Correct 6 ms 5560 KB Output is correct
41 Correct 5 ms 5552 KB Output is correct
42 Correct 5 ms 5588 KB Output is correct
43 Correct 6 ms 5552 KB Output is correct
44 Correct 7 ms 5612 KB Output is correct
45 Correct 7 ms 5520 KB Output is correct
46 Correct 7 ms 5612 KB Output is correct
47 Correct 7 ms 5556 KB Output is correct
48 Correct 7 ms 5552 KB Output is correct
49 Correct 9 ms 5588 KB Output is correct
50 Correct 7 ms 5588 KB Output is correct
51 Correct 7 ms 5620 KB Output is correct
52 Correct 7 ms 5588 KB Output is correct
53 Correct 7 ms 5676 KB Output is correct
54 Correct 6 ms 5588 KB Output is correct
55 Correct 6 ms 5588 KB Output is correct
56 Correct 4 ms 5192 KB Output is correct
57 Correct 4 ms 5460 KB Output is correct
58 Correct 10 ms 5588 KB Output is correct
59 Correct 3 ms 5156 KB Output is correct
60 Correct 3 ms 5076 KB Output is correct
61 Correct 4 ms 5204 KB Output is correct
62 Correct 107 ms 21504 KB Output is correct
63 Correct 96 ms 26144 KB Output is correct
64 Correct 108 ms 28648 KB Output is correct
65 Correct 148 ms 32284 KB Output is correct
66 Correct 142 ms 32332 KB Output is correct
67 Correct 138 ms 32308 KB Output is correct
68 Correct 139 ms 32180 KB Output is correct
69 Correct 139 ms 32024 KB Output is correct
70 Correct 139 ms 31692 KB Output is correct
71 Correct 139 ms 31820 KB Output is correct
72 Correct 141 ms 31460 KB Output is correct
73 Correct 55 ms 28716 KB Output is correct
74 Correct 155 ms 32084 KB Output is correct
75 Correct 58 ms 9824 KB Output is correct
76 Correct 3 ms 5076 KB Output is correct
77 Correct 357 ms 16476 KB Output is correct
78 Correct 552 ms 18044 KB Output is correct
79 Correct 445 ms 19024 KB Output is correct
80 Correct 761 ms 25136 KB Output is correct
81 Correct 756 ms 25492 KB Output is correct
82 Correct 757 ms 25384 KB Output is correct
83 Correct 751 ms 25304 KB Output is correct
84 Correct 770 ms 25504 KB Output is correct
85 Correct 757 ms 25416 KB Output is correct
86 Correct 784 ms 25232 KB Output is correct
87 Correct 769 ms 25476 KB Output is correct
88 Correct 769 ms 25588 KB Output is correct
89 Correct 773 ms 26912 KB Output is correct
90 Correct 818 ms 30236 KB Output is correct
91 Correct 794 ms 25984 KB Output is correct
92 Correct 773 ms 26136 KB Output is correct
93 Correct 755 ms 26120 KB Output is correct
94 Correct 501 ms 20980 KB Output is correct
95 Correct 495 ms 20968 KB Output is correct
96 Correct 491 ms 21012 KB Output is correct
97 Correct 475 ms 20736 KB Output is correct
98 Correct 458 ms 20652 KB Output is correct
99 Correct 458 ms 20996 KB Output is correct
100 Correct 460 ms 20940 KB Output is correct
101 Correct 517 ms 20852 KB Output is correct
102 Correct 480 ms 20916 KB Output is correct
103 Correct 480 ms 21068 KB Output is correct
104 Correct 493 ms 21196 KB Output is correct
105 Correct 497 ms 21240 KB Output is correct
106 Correct 502 ms 21504 KB Output is correct
107 Correct 494 ms 22088 KB Output is correct
108 Correct 504 ms 23412 KB Output is correct
109 Correct 514 ms 26052 KB Output is correct
110 Correct 3 ms 5204 KB Output is correct
111 Correct 3 ms 5076 KB Output is correct
112 Correct 4 ms 5204 KB Output is correct
113 Correct 860 ms 21316 KB Output is correct
114 Correct 907 ms 21852 KB Output is correct
115 Correct 1036 ms 27248 KB Output is correct
116 Correct 1079 ms 29988 KB Output is correct
117 Correct 1088 ms 30012 KB Output is correct
118 Correct 1100 ms 29988 KB Output is correct
119 Correct 1084 ms 29844 KB Output is correct
120 Correct 1112 ms 30108 KB Output is correct
121 Correct 1077 ms 30188 KB Output is correct
122 Correct 1098 ms 30076 KB Output is correct
123 Correct 75 ms 9748 KB Output is correct
124 Correct 683 ms 28920 KB Output is correct
125 Correct 512 ms 26132 KB Output is correct
126 Correct 813 ms 29860 KB Output is correct
127 Correct 812 ms 29548 KB Output is correct
128 Correct 817 ms 29552 KB Output is correct
129 Correct 843 ms 29716 KB Output is correct
130 Correct 814 ms 29616 KB Output is correct
131 Correct 197 ms 31108 KB Output is correct
132 Correct 167 ms 32208 KB Output is correct
133 Correct 220 ms 28436 KB Output is correct
134 Correct 514 ms 25128 KB Output is correct
135 Correct 544 ms 25028 KB Output is correct
136 Correct 618 ms 25072 KB Output is correct
137 Correct 445 ms 30516 KB Output is correct
138 Correct 445 ms 30484 KB Output is correct
139 Correct 409 ms 30132 KB Output is correct
140 Correct 445 ms 30936 KB Output is correct
141 Correct 422 ms 30880 KB Output is correct
142 Correct 389 ms 30880 KB Output is correct
143 Correct 80 ms 21000 KB Output is correct
144 Correct 875 ms 29168 KB Output is correct