Submission #1071515

# Submission time Handle Problem Language Result Execution time Memory
1071515 2024-08-23T08:00:45 Z isaachew Tourism (JOI23_tourism) C++17
100 / 100
819 ms 26824 KB
#include <bits/stdc++.h>


/*
 HLD + Segtree + Segtree
 */
struct segtree2{//range sum segtree
    int size;
    std::vector<int> nodes;
    segtree2(){}
    void init(int n){
        size=n;
        nodes.resize(2*n-1);
    }
    void update(int p,int x,int nl,int nr,int ni){
        if(p<nl||p>=nr)return;
        if(nl+1>=nr){
            nodes[ni]+=x;
            return;
        }
        int nm=(nl+nr)/2;
        update(p,x,nl,nm,ni+1);
        update(p,x,nm,nr,ni+2*(nm-nl));
        nodes[ni]=nodes[ni+1]+nodes[ni+2*(nm-nl)];
    }
    int query(int l,int r,int nl,int nr,int ni){
        if(l<=nl&&r>=nr){
            return nodes[ni];
        }
        if(r<=nl||l>=nr)return 0;
        int nm=(nl+nr)/2;
        return query(l,r,nl,nm,ni+1)+query(l,r,nm,nr,ni+2*(nm-nl));
    }
};
segtree2 sumseg;
struct segtree{//segtree to keep track of values in the other segtree
    int size;
    std::vector<int> nodes;
    segtree(){}
    void init(int n){
        size=n;
        nodes.resize(2*n-1,-1);
    }
    void update(int l,int r,int x,int nl,int nr,int ni){//
        if(r<=nl||l>=nr)return;
        if(l<=nl&&r>=nr){
            if(nodes[ni]==-2){//recurse on a mix
                int nm=(nl+nr)/2;//reset everything below node
                update(l,r,-1,nl,nm,ni+1);
                update(l,r,-1,nm,nr,ni+2*(nm-nl));
            }else if(nodes[ni]>=0){
                //std::cout<<"["<<nl<<","<<nr<<") not at "<<nodes[ni]<<'\n';
                sumseg.update(nodes[ni],-(nr-nl),0,sumseg.size,0);
            }
            nodes[ni]=x;
            //std::cout<<"["<<nl<<","<<nr<<") at "<<nodes[ni]<<'\n';
            if(nodes[ni]!=-1)sumseg.update(nodes[ni],(nr-nl),0,sumseg.size,0);
            return;
        }
        int nm=(nl+nr)/2;
        if(nodes[ni]>=0){//pushdown
            nodes[ni+1]=nodes[ni];
            nodes[ni+2*(nm-nl)]=nodes[ni];
            nodes[ni]=-2;
        }
        update(l,r,x,nl,nm,ni+1);
        update(l,r,x,nm,nr,ni+2*(nm-nl));
        nodes[ni]=(nodes[ni+1]!=-1||nodes[ni+2*(nm-nl)]!=-1)?-2:-1;
    }
};
segtree mainseg;
int nids=0;
std::vector<int> parents;
std::vector<std::vector<int>> edges;//children
std::vector<int> depths;
std::vector<int> ssizes;
std::vector<int> ids;
std::vector<int> heavys;
std::vector<int> heavybs;
int dfs(int cur,int par){
    parents[cur]=par;
    for(int i:edges[cur]){
        if(i==par)continue;
        depths[i]=depths[cur]+1;
        ssizes[cur]+=dfs(i,cur);
        if(heavys[cur]==-1||ssizes[i]>ssizes[heavys[cur]])heavys[cur]=i;//replace heavy if necessary
    }
    return ssizes[cur];
}
void dfs2(int cur,int heavy){
    ids[cur]=nids++;
    heavybs[cur]=heavy;
    if(heavys[cur]!=-1)dfs2(heavys[cur],heavy);
    for(int i:edges[cur]){
        if(i==heavys[cur]||i==parents[cur])continue;
        dfs2(i,i);
    }
}
int main(){
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    int n,m,q;
    std::cin>>n>>m>>q;
    parents.resize(n);
    edges.resize(n);
    ids.resize(n);
    depths.resize(n);
    heavys.resize(n,-1);
    heavybs.resize(n);
    ssizes.resize(n,1);
    std::vector<int> spots;
    for(int i=0;i<n-1;i++){
        int a,b;
        std::cin>>a>>b;
        a--,b--;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }
    for(int i=0;i<m;i++){
        int x;
        std::cin>>x;
        x--;
        spots.push_back(x);
    }
    std::vector<std::pair<int,int>> queries;
    std::vector<int> results(q);
    std::vector<std::vector<int>> squeries(m);//indices per ending time
    for(int i=0;i<q;i++){
        int a,b;
        std::cin>>a>>b;
        a--,b--;
        queries.push_back({a,b});
        squeries[b].push_back(i);
    }
    dfs(0,0);
    dfs2(0,0);
    sumseg.init(m);
    mainseg.init(n);
    for(int i=0;i<m-1;i++){//go
        int src=spots[i],dst=spots[i+1];
        while(heavybs[src]!=heavybs[dst]){
            if(depths[heavybs[src]]>depths[heavybs[dst]]){
                mainseg.update(ids[heavybs[src]],ids[src]+1,i,0,n,0);
                src=parents[heavybs[src]];
            }else{
                mainseg.update(ids[heavybs[dst]],ids[dst]+1,i,0,n,0);
                dst=parents[heavybs[dst]];
            }
        }
        mainseg.update(std::min(ids[src],ids[dst]),std::max(ids[src],ids[dst])+1,i,0,n,0);
        for(int j:squeries[i+1]){
            results[j]=std::max(1,sumseg.query(queries[j].first,queries[j].second,0,m,0));//make sure at least 1 so paths like 3 3 3 visit city 3
        }
    }
    for(int j:squeries[0]){//random edge case: query ends in 1 -> only 1 city visited
        results[j]=1;
    }
    for(int i:results)std::cout<<i<<'\n';
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 456 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 456 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 456 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 456 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 5 ms 604 KB Output is correct
31 Correct 4 ms 604 KB Output is correct
32 Correct 5 ms 604 KB Output is correct
33 Correct 5 ms 604 KB Output is correct
34 Correct 5 ms 812 KB Output is correct
35 Correct 5 ms 604 KB Output is correct
36 Correct 5 ms 604 KB Output is correct
37 Correct 5 ms 728 KB Output is correct
38 Correct 5 ms 860 KB Output is correct
39 Correct 5 ms 860 KB Output is correct
40 Correct 5 ms 856 KB Output is correct
41 Correct 4 ms 860 KB Output is correct
42 Correct 4 ms 924 KB Output is correct
43 Correct 5 ms 860 KB Output is correct
44 Correct 6 ms 860 KB Output is correct
45 Correct 6 ms 600 KB Output is correct
46 Correct 6 ms 860 KB Output is correct
47 Correct 6 ms 860 KB Output is correct
48 Correct 6 ms 604 KB Output is correct
49 Correct 5 ms 604 KB Output is correct
50 Correct 3 ms 604 KB Output is correct
51 Correct 3 ms 604 KB Output is correct
52 Correct 3 ms 604 KB Output is correct
53 Correct 3 ms 604 KB Output is correct
54 Correct 3 ms 604 KB Output is correct
55 Correct 3 ms 604 KB Output is correct
56 Correct 1 ms 604 KB Output is correct
57 Correct 1 ms 604 KB Output is correct
58 Correct 5 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 310 ms 17308 KB Output is correct
5 Correct 213 ms 19792 KB Output is correct
6 Correct 314 ms 22592 KB Output is correct
7 Correct 360 ms 26824 KB Output is correct
8 Correct 361 ms 26824 KB Output is correct
9 Correct 376 ms 26824 KB Output is correct
10 Correct 390 ms 26804 KB Output is correct
11 Correct 374 ms 26820 KB Output is correct
12 Correct 182 ms 25548 KB Output is correct
13 Correct 178 ms 25540 KB Output is correct
14 Correct 176 ms 25400 KB Output is correct
15 Correct 39 ms 20676 KB Output is correct
16 Correct 372 ms 26612 KB Output is correct
17 Correct 60 ms 8396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 216 ms 8120 KB Output is correct
3 Correct 354 ms 9940 KB Output is correct
4 Correct 270 ms 10048 KB Output is correct
5 Correct 461 ms 14696 KB Output is correct
6 Correct 437 ms 14540 KB Output is correct
7 Correct 431 ms 14536 KB Output is correct
8 Correct 422 ms 14536 KB Output is correct
9 Correct 477 ms 14556 KB Output is correct
10 Correct 436 ms 14536 KB Output is correct
11 Correct 440 ms 14572 KB Output is correct
12 Correct 449 ms 14604 KB Output is correct
13 Correct 426 ms 14984 KB Output is correct
14 Correct 443 ms 16080 KB Output is correct
15 Correct 488 ms 19912 KB Output is correct
16 Correct 438 ms 14988 KB Output is correct
17 Correct 435 ms 14796 KB Output is correct
18 Correct 415 ms 14996 KB Output is correct
19 Correct 687 ms 14548 KB Output is correct
20 Correct 705 ms 14536 KB Output is correct
21 Correct 758 ms 14576 KB Output is correct
22 Correct 685 ms 14596 KB Output is correct
23 Correct 720 ms 14536 KB Output is correct
24 Correct 708 ms 14584 KB Output is correct
25 Correct 736 ms 14540 KB Output is correct
26 Correct 708 ms 14540 KB Output is correct
27 Correct 680 ms 14612 KB Output is correct
28 Correct 719 ms 14576 KB Output is correct
29 Correct 688 ms 14540 KB Output is correct
30 Correct 726 ms 14768 KB Output is correct
31 Correct 744 ms 15052 KB Output is correct
32 Correct 715 ms 15572 KB Output is correct
33 Correct 763 ms 17096 KB Output is correct
34 Correct 717 ms 20168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 556 ms 13976 KB Output is correct
5 Correct 604 ms 14280 KB Output is correct
6 Correct 641 ms 17288 KB Output is correct
7 Correct 675 ms 18956 KB Output is correct
8 Correct 681 ms 18884 KB Output is correct
9 Correct 703 ms 18888 KB Output is correct
10 Correct 701 ms 18940 KB Output is correct
11 Correct 696 ms 18948 KB Output is correct
12 Correct 722 ms 18884 KB Output is correct
13 Correct 703 ms 18888 KB Output is correct
14 Correct 63 ms 8400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 456 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 456 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 5 ms 604 KB Output is correct
31 Correct 4 ms 604 KB Output is correct
32 Correct 5 ms 604 KB Output is correct
33 Correct 5 ms 604 KB Output is correct
34 Correct 5 ms 812 KB Output is correct
35 Correct 5 ms 604 KB Output is correct
36 Correct 5 ms 604 KB Output is correct
37 Correct 5 ms 728 KB Output is correct
38 Correct 5 ms 860 KB Output is correct
39 Correct 5 ms 860 KB Output is correct
40 Correct 5 ms 856 KB Output is correct
41 Correct 4 ms 860 KB Output is correct
42 Correct 4 ms 924 KB Output is correct
43 Correct 5 ms 860 KB Output is correct
44 Correct 6 ms 860 KB Output is correct
45 Correct 6 ms 600 KB Output is correct
46 Correct 6 ms 860 KB Output is correct
47 Correct 6 ms 860 KB Output is correct
48 Correct 6 ms 604 KB Output is correct
49 Correct 5 ms 604 KB Output is correct
50 Correct 3 ms 604 KB Output is correct
51 Correct 3 ms 604 KB Output is correct
52 Correct 3 ms 604 KB Output is correct
53 Correct 3 ms 604 KB Output is correct
54 Correct 3 ms 604 KB Output is correct
55 Correct 3 ms 604 KB Output is correct
56 Correct 1 ms 604 KB Output is correct
57 Correct 1 ms 604 KB Output is correct
58 Correct 5 ms 604 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 1 ms 604 KB Output is correct
62 Correct 310 ms 17308 KB Output is correct
63 Correct 213 ms 19792 KB Output is correct
64 Correct 314 ms 22592 KB Output is correct
65 Correct 360 ms 26824 KB Output is correct
66 Correct 361 ms 26824 KB Output is correct
67 Correct 376 ms 26824 KB Output is correct
68 Correct 390 ms 26804 KB Output is correct
69 Correct 374 ms 26820 KB Output is correct
70 Correct 182 ms 25548 KB Output is correct
71 Correct 178 ms 25540 KB Output is correct
72 Correct 176 ms 25400 KB Output is correct
73 Correct 39 ms 20676 KB Output is correct
74 Correct 372 ms 26612 KB Output is correct
75 Correct 60 ms 8396 KB Output is correct
76 Correct 0 ms 344 KB Output is correct
77 Correct 216 ms 8120 KB Output is correct
78 Correct 354 ms 9940 KB Output is correct
79 Correct 270 ms 10048 KB Output is correct
80 Correct 461 ms 14696 KB Output is correct
81 Correct 437 ms 14540 KB Output is correct
82 Correct 431 ms 14536 KB Output is correct
83 Correct 422 ms 14536 KB Output is correct
84 Correct 477 ms 14556 KB Output is correct
85 Correct 436 ms 14536 KB Output is correct
86 Correct 440 ms 14572 KB Output is correct
87 Correct 449 ms 14604 KB Output is correct
88 Correct 426 ms 14984 KB Output is correct
89 Correct 443 ms 16080 KB Output is correct
90 Correct 488 ms 19912 KB Output is correct
91 Correct 438 ms 14988 KB Output is correct
92 Correct 435 ms 14796 KB Output is correct
93 Correct 415 ms 14996 KB Output is correct
94 Correct 687 ms 14548 KB Output is correct
95 Correct 705 ms 14536 KB Output is correct
96 Correct 758 ms 14576 KB Output is correct
97 Correct 685 ms 14596 KB Output is correct
98 Correct 720 ms 14536 KB Output is correct
99 Correct 708 ms 14584 KB Output is correct
100 Correct 736 ms 14540 KB Output is correct
101 Correct 708 ms 14540 KB Output is correct
102 Correct 680 ms 14612 KB Output is correct
103 Correct 719 ms 14576 KB Output is correct
104 Correct 688 ms 14540 KB Output is correct
105 Correct 726 ms 14768 KB Output is correct
106 Correct 744 ms 15052 KB Output is correct
107 Correct 715 ms 15572 KB Output is correct
108 Correct 763 ms 17096 KB Output is correct
109 Correct 717 ms 20168 KB Output is correct
110 Correct 0 ms 348 KB Output is correct
111 Correct 0 ms 348 KB Output is correct
112 Correct 1 ms 604 KB Output is correct
113 Correct 556 ms 13976 KB Output is correct
114 Correct 604 ms 14280 KB Output is correct
115 Correct 641 ms 17288 KB Output is correct
116 Correct 675 ms 18956 KB Output is correct
117 Correct 681 ms 18884 KB Output is correct
118 Correct 703 ms 18888 KB Output is correct
119 Correct 701 ms 18940 KB Output is correct
120 Correct 696 ms 18948 KB Output is correct
121 Correct 722 ms 18884 KB Output is correct
122 Correct 703 ms 18888 KB Output is correct
123 Correct 63 ms 8400 KB Output is correct
124 Correct 406 ms 18124 KB Output is correct
125 Correct 331 ms 15240 KB Output is correct
126 Correct 490 ms 19140 KB Output is correct
127 Correct 477 ms 19140 KB Output is correct
128 Correct 528 ms 19064 KB Output is correct
129 Correct 516 ms 19068 KB Output is correct
130 Correct 472 ms 19140 KB Output is correct
131 Correct 412 ms 25544 KB Output is correct
132 Correct 384 ms 26664 KB Output is correct
133 Correct 418 ms 22888 KB Output is correct
134 Correct 777 ms 19092 KB Output is correct
135 Correct 819 ms 19140 KB Output is correct
136 Correct 769 ms 19152 KB Output is correct
137 Correct 175 ms 19276 KB Output is correct
138 Correct 165 ms 19188 KB Output is correct
139 Correct 183 ms 19272 KB Output is correct
140 Correct 195 ms 19340 KB Output is correct
141 Correct 179 ms 19268 KB Output is correct
142 Correct 180 ms 19272 KB Output is correct
143 Correct 40 ms 12992 KB Output is correct
144 Correct 468 ms 18816 KB Output is correct