Submission #892772

# Submission time Handle Problem Language Result Execution time Memory
892772 2023-12-25T23:03:51 Z alexander707070 Tourism (JOI23_tourism) C++14
100 / 100
4524 ms 48668 KB
#include<bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#define MAXN 100007
using namespace std;

const int bucket_sz=500;

struct qr{
    int l,r,id;
}qs[MAXN];

struct node{
    int l,r;
}w[MAXN];

struct event{
    int type,ind,val,tim;
};

stack<event> st;

int n,m,q,a,b,tt,dist[MAXN],bucket[MAXN],l,r,cnt[MAXN],tim,sum,last,ans[MAXN];
int c[MAXN],num[MAXN],ret[MAXN],fr[MAXN];
vector<int> v[MAXN],euler;

void dfs(int x,int p,int d){
    tt++; num[x]=tt; ret[tt]=x;
    dist[x]=d;

    fr[x]=int(euler.size());
    euler.push_back(num[x]);

    for(int i=0;i<v[x].size();i++){
        if(v[x][i]==p)continue;
        dfs(v[x][i],x,d+1);

        euler.push_back(num[x]);
    }
}

bool li[4*MAXN][20];
int dp[4*MAXN][20];

int lg[4*MAXN],power[20];

void calc(){
    power[0]=1;
    for(int i=1;i<20;i++)power[i]=power[i-1]*2;
    lg[1]=0;
    for(int i=2;i<=4*n;i++)lg[i]=lg[i/2]+1;
}

int rmq(int i,int j){
    if(j==0)return euler[i];

    if(li[i][j])return dp[i][j];
    li[i][j]=true;

    dp[i][j]=min( rmq(i,j-1) , rmq(i+power[j-1],j-1) );
    return dp[i][j];
}

int lca(int x,int y){
    if(fr[x]>fr[y])swap(x,y);
    x=fr[x]; y=fr[y];

    return ret[ min( rmq(x,lg[y-x+1]) , rmq(y-power[lg[y-x+1]]+1,lg[y-x+1]) ) ];
}

int distance(int x,int y){
    if(x==0 or y==n+1)return 0;

    return dist[x]+dist[y]-2*dist[lca(x,y)];
}

bool cmp(qr fr,qr sc){
    if(bucket[fr.l]!=bucket[sc.l])return bucket[fr.l]<bucket[sc.l];
    if(fr.r!=sc.r)return fr.r>sc.r;   
    return fr.l<sc.l;
}

void insertv(int x,int y,int z){
    sum-=distance(ret[y],ret[z]);
    sum+=distance(ret[y],ret[x]);
    sum+=distance(ret[x],ret[z]);

    w[x].l=y; w[x].r=z;
    w[y].r=x; w[z].l=x;
}

void erasev(int x){
    st.push({1,w[x].l,x,tim});
    st.push({2,w[x].r,x,tim});

    w[w[x].l].r=w[x].r;
    w[w[x].r].l=w[x].l;
}

void rem(int x){
    if(x==0){
        tim++; return;
    }

    tim++;
    x=num[c[x]];
    cnt[x]--;

    if(cnt[x]!=0)return;

    st.push({3,0,sum,tim});

    sum-=distance(ret[w[x].l],ret[x]);
    sum-=distance(ret[x],ret[w[x].r]);
    sum+=distance(ret[w[x].l],ret[w[x].r]);

    erasev(x);
}

void undo(){
    while(!st.empty() and st.top().tim==tim){
        if(st.top().type==1){
            w[st.top().ind].r=st.top().val;
        }
        if(st.top().type==2){
            w[st.top().ind].l=st.top().val;
        }
        if(st.top().type==3){
            sum=st.top().val;
        }

        st.pop();
    }
    tim--;
}

void reset(){
    tim=sum=0;
    while(!st.empty())st.pop();
    for(int i=1;i<=n;i++)cnt[i]=0;
}

int edge(){
    int lx=w[0].r,rx=w[n+1].l;
    return distance(ret[lx],ret[rx]);
}

int main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin>>n>>m>>q;
    calc();

    for(int i=1;i<=n-1;i++){
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    dfs(1,0,0);

    w[0].l=n+1;
    w[n+1].r=0;
    ret[n+1]=n+1;

    for(int i=1;i<=m;i++){
        cin>>c[i];
        bucket[i]=i/bucket_sz;
    }

    for(int i=1;i<=q;i++){
        cin>>qs[i].l>>qs[i].r;
        qs[i].id=i;
    }

    sort(qs+1,qs+q+1,cmp);

    for(int i=1;i<=q;i++){
        if(i==1 or bucket[qs[i].l]!=bucket[qs[i-1].l]){
            reset();
            for(int f=bucket[qs[i].l]*bucket_sz;f<=m;f++){
                cnt[num[c[f]]]++;
            }

            last=0;
            for(int f=1;f<=n;f++){
                if(cnt[f]==0)continue;
                insertv(f,last,n+1);
                last=f;
            }
            l=bucket[qs[i].l]*bucket_sz;
            r=m;
        }

        while(r>qs[i].r){
            rem(r); r--;
        }

        while(l<qs[i].l){
            rem(l); l++;
        }

        ans[qs[i].id]=(sum+edge())/2;

        if(qs[i+1].r==qs[i].r and qs[i+1].l>=qs[i].l and bucket[qs[i+1].l]==bucket[qs[i].l]){
            continue;
        }

        while(l>bucket[qs[i].l]*bucket_sz){
            undo(); l--; cnt[num[c[l]]]++;
        }

    }

    for(int i=1;i<=q;i++){
        cout<<ans[i]+1<<"\n";
    }

    return 0;
}

Compilation message

tourism.cpp: In function 'void dfs(int, int, int)':
tourism.cpp:34:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |     for(int i=0;i<v[x].size();i++){
      |                 ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10680 KB Output is correct
10 Correct 3 ms 10588 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 2 ms 10588 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 2 ms 10588 KB Output is correct
15 Correct 2 ms 10588 KB Output is correct
16 Correct 3 ms 10588 KB Output is correct
17 Correct 2 ms 10588 KB Output is correct
18 Correct 3 ms 10588 KB Output is correct
19 Correct 2 ms 10588 KB Output is correct
20 Correct 2 ms 10588 KB Output is correct
21 Correct 3 ms 10588 KB Output is correct
22 Correct 3 ms 10588 KB Output is correct
23 Correct 3 ms 10588 KB Output is correct
24 Correct 3 ms 10588 KB Output is correct
25 Correct 3 ms 10584 KB Output is correct
26 Correct 3 ms 10840 KB Output is correct
27 Correct 2 ms 8540 KB Output is correct
28 Correct 1 ms 8536 KB Output is correct
29 Correct 3 ms 10588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10680 KB Output is correct
10 Correct 3 ms 10588 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 2 ms 10588 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 2 ms 10588 KB Output is correct
15 Correct 2 ms 10588 KB Output is correct
16 Correct 3 ms 10588 KB Output is correct
17 Correct 2 ms 10588 KB Output is correct
18 Correct 3 ms 10588 KB Output is correct
19 Correct 2 ms 10588 KB Output is correct
20 Correct 2 ms 10588 KB Output is correct
21 Correct 3 ms 10588 KB Output is correct
22 Correct 3 ms 10588 KB Output is correct
23 Correct 3 ms 10588 KB Output is correct
24 Correct 3 ms 10588 KB Output is correct
25 Correct 3 ms 10584 KB Output is correct
26 Correct 3 ms 10840 KB Output is correct
27 Correct 2 ms 8540 KB Output is correct
28 Correct 1 ms 8536 KB Output is correct
29 Correct 3 ms 10588 KB Output is correct
30 Correct 18 ms 10840 KB Output is correct
31 Correct 19 ms 10840 KB Output is correct
32 Correct 25 ms 10940 KB Output is correct
33 Correct 23 ms 10844 KB Output is correct
34 Correct 23 ms 10844 KB Output is correct
35 Correct 3 ms 10844 KB Output is correct
36 Correct 3 ms 10844 KB Output is correct
37 Correct 3 ms 10844 KB Output is correct
38 Correct 22 ms 10844 KB Output is correct
39 Correct 23 ms 10840 KB Output is correct
40 Correct 23 ms 11100 KB Output is correct
41 Correct 3 ms 10844 KB Output is correct
42 Correct 3 ms 10844 KB Output is correct
43 Correct 3 ms 10844 KB Output is correct
44 Correct 23 ms 10844 KB Output is correct
45 Correct 25 ms 10844 KB Output is correct
46 Correct 23 ms 11044 KB Output is correct
47 Correct 3 ms 10840 KB Output is correct
48 Correct 3 ms 10844 KB Output is correct
49 Correct 3 ms 10844 KB Output is correct
50 Correct 27 ms 11192 KB Output is correct
51 Correct 29 ms 10840 KB Output is correct
52 Correct 23 ms 10844 KB Output is correct
53 Correct 23 ms 10840 KB Output is correct
54 Correct 23 ms 10840 KB Output is correct
55 Correct 23 ms 10844 KB Output is correct
56 Correct 6 ms 8540 KB Output is correct
57 Correct 2 ms 8796 KB Output is correct
58 Correct 24 ms 10992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 2 ms 8536 KB Output is correct
3 Correct 6 ms 8540 KB Output is correct
4 Correct 1752 ms 25500 KB Output is correct
5 Correct 1764 ms 30748 KB Output is correct
6 Correct 1903 ms 34876 KB Output is correct
7 Correct 3225 ms 36856 KB Output is correct
8 Correct 3251 ms 36856 KB Output is correct
9 Correct 3384 ms 36900 KB Output is correct
10 Correct 3238 ms 36852 KB Output is correct
11 Correct 3223 ms 36864 KB Output is correct
12 Correct 71 ms 34248 KB Output is correct
13 Correct 68 ms 34240 KB Output is correct
14 Correct 84 ms 34212 KB Output is correct
15 Correct 44 ms 23512 KB Output is correct
16 Correct 2688 ms 36872 KB Output is correct
17 Correct 351 ms 9556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10584 KB Output is correct
2 Correct 442 ms 25064 KB Output is correct
3 Correct 976 ms 28356 KB Output is correct
4 Correct 905 ms 28624 KB Output is correct
5 Correct 43 ms 35272 KB Output is correct
6 Correct 71 ms 36804 KB Output is correct
7 Correct 166 ms 37836 KB Output is correct
8 Correct 350 ms 38080 KB Output is correct
9 Correct 1033 ms 38124 KB Output is correct
10 Correct 1927 ms 38516 KB Output is correct
11 Correct 2001 ms 38340 KB Output is correct
12 Correct 2165 ms 38344 KB Output is correct
13 Correct 2284 ms 38596 KB Output is correct
14 Correct 2661 ms 38356 KB Output is correct
15 Correct 4238 ms 39144 KB Output is correct
16 Correct 955 ms 37340 KB Output is correct
17 Correct 922 ms 37336 KB Output is correct
18 Correct 1046 ms 37336 KB Output is correct
19 Correct 50 ms 35268 KB Output is correct
20 Correct 52 ms 36292 KB Output is correct
21 Correct 78 ms 37316 KB Output is correct
22 Correct 117 ms 38084 KB Output is correct
23 Correct 218 ms 38080 KB Output is correct
24 Correct 384 ms 38116 KB Output is correct
25 Correct 833 ms 38500 KB Output is correct
26 Correct 1248 ms 38792 KB Output is correct
27 Correct 1411 ms 38344 KB Output is correct
28 Correct 1473 ms 38420 KB Output is correct
29 Correct 1482 ms 38328 KB Output is correct
30 Correct 1397 ms 38384 KB Output is correct
31 Correct 1857 ms 38344 KB Output is correct
32 Correct 1793 ms 38384 KB Output is correct
33 Correct 2673 ms 38548 KB Output is correct
34 Correct 3768 ms 39160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 8672 KB Output is correct
3 Correct 7 ms 8540 KB Output is correct
4 Correct 2571 ms 26876 KB Output is correct
5 Correct 2514 ms 29112 KB Output is correct
6 Correct 3638 ms 35988 KB Output is correct
7 Correct 4221 ms 39200 KB Output is correct
8 Correct 4229 ms 39204 KB Output is correct
9 Correct 4326 ms 39276 KB Output is correct
10 Correct 4368 ms 39272 KB Output is correct
11 Correct 4276 ms 39404 KB Output is correct
12 Correct 4181 ms 39260 KB Output is correct
13 Correct 4126 ms 40928 KB Output is correct
14 Correct 350 ms 10444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10680 KB Output is correct
10 Correct 3 ms 10588 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 2 ms 10588 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 2 ms 10588 KB Output is correct
15 Correct 2 ms 10588 KB Output is correct
16 Correct 3 ms 10588 KB Output is correct
17 Correct 2 ms 10588 KB Output is correct
18 Correct 3 ms 10588 KB Output is correct
19 Correct 2 ms 10588 KB Output is correct
20 Correct 2 ms 10588 KB Output is correct
21 Correct 3 ms 10588 KB Output is correct
22 Correct 3 ms 10588 KB Output is correct
23 Correct 3 ms 10588 KB Output is correct
24 Correct 3 ms 10588 KB Output is correct
25 Correct 3 ms 10584 KB Output is correct
26 Correct 3 ms 10840 KB Output is correct
27 Correct 2 ms 8540 KB Output is correct
28 Correct 1 ms 8536 KB Output is correct
29 Correct 3 ms 10588 KB Output is correct
30 Correct 18 ms 10840 KB Output is correct
31 Correct 19 ms 10840 KB Output is correct
32 Correct 25 ms 10940 KB Output is correct
33 Correct 23 ms 10844 KB Output is correct
34 Correct 23 ms 10844 KB Output is correct
35 Correct 3 ms 10844 KB Output is correct
36 Correct 3 ms 10844 KB Output is correct
37 Correct 3 ms 10844 KB Output is correct
38 Correct 22 ms 10844 KB Output is correct
39 Correct 23 ms 10840 KB Output is correct
40 Correct 23 ms 11100 KB Output is correct
41 Correct 3 ms 10844 KB Output is correct
42 Correct 3 ms 10844 KB Output is correct
43 Correct 3 ms 10844 KB Output is correct
44 Correct 23 ms 10844 KB Output is correct
45 Correct 25 ms 10844 KB Output is correct
46 Correct 23 ms 11044 KB Output is correct
47 Correct 3 ms 10840 KB Output is correct
48 Correct 3 ms 10844 KB Output is correct
49 Correct 3 ms 10844 KB Output is correct
50 Correct 27 ms 11192 KB Output is correct
51 Correct 29 ms 10840 KB Output is correct
52 Correct 23 ms 10844 KB Output is correct
53 Correct 23 ms 10840 KB Output is correct
54 Correct 23 ms 10840 KB Output is correct
55 Correct 23 ms 10844 KB Output is correct
56 Correct 6 ms 8540 KB Output is correct
57 Correct 2 ms 8796 KB Output is correct
58 Correct 24 ms 10992 KB Output is correct
59 Correct 1 ms 10588 KB Output is correct
60 Correct 2 ms 8536 KB Output is correct
61 Correct 6 ms 8540 KB Output is correct
62 Correct 1752 ms 25500 KB Output is correct
63 Correct 1764 ms 30748 KB Output is correct
64 Correct 1903 ms 34876 KB Output is correct
65 Correct 3225 ms 36856 KB Output is correct
66 Correct 3251 ms 36856 KB Output is correct
67 Correct 3384 ms 36900 KB Output is correct
68 Correct 3238 ms 36852 KB Output is correct
69 Correct 3223 ms 36864 KB Output is correct
70 Correct 71 ms 34248 KB Output is correct
71 Correct 68 ms 34240 KB Output is correct
72 Correct 84 ms 34212 KB Output is correct
73 Correct 44 ms 23512 KB Output is correct
74 Correct 2688 ms 36872 KB Output is correct
75 Correct 351 ms 9556 KB Output is correct
76 Correct 1 ms 10584 KB Output is correct
77 Correct 442 ms 25064 KB Output is correct
78 Correct 976 ms 28356 KB Output is correct
79 Correct 905 ms 28624 KB Output is correct
80 Correct 43 ms 35272 KB Output is correct
81 Correct 71 ms 36804 KB Output is correct
82 Correct 166 ms 37836 KB Output is correct
83 Correct 350 ms 38080 KB Output is correct
84 Correct 1033 ms 38124 KB Output is correct
85 Correct 1927 ms 38516 KB Output is correct
86 Correct 2001 ms 38340 KB Output is correct
87 Correct 2165 ms 38344 KB Output is correct
88 Correct 2284 ms 38596 KB Output is correct
89 Correct 2661 ms 38356 KB Output is correct
90 Correct 4238 ms 39144 KB Output is correct
91 Correct 955 ms 37340 KB Output is correct
92 Correct 922 ms 37336 KB Output is correct
93 Correct 1046 ms 37336 KB Output is correct
94 Correct 50 ms 35268 KB Output is correct
95 Correct 52 ms 36292 KB Output is correct
96 Correct 78 ms 37316 KB Output is correct
97 Correct 117 ms 38084 KB Output is correct
98 Correct 218 ms 38080 KB Output is correct
99 Correct 384 ms 38116 KB Output is correct
100 Correct 833 ms 38500 KB Output is correct
101 Correct 1248 ms 38792 KB Output is correct
102 Correct 1411 ms 38344 KB Output is correct
103 Correct 1473 ms 38420 KB Output is correct
104 Correct 1482 ms 38328 KB Output is correct
105 Correct 1397 ms 38384 KB Output is correct
106 Correct 1857 ms 38344 KB Output is correct
107 Correct 1793 ms 38384 KB Output is correct
108 Correct 2673 ms 38548 KB Output is correct
109 Correct 3768 ms 39160 KB Output is correct
110 Correct 2 ms 10588 KB Output is correct
111 Correct 2 ms 8672 KB Output is correct
112 Correct 7 ms 8540 KB Output is correct
113 Correct 2571 ms 26876 KB Output is correct
114 Correct 2514 ms 29112 KB Output is correct
115 Correct 3638 ms 35988 KB Output is correct
116 Correct 4221 ms 39200 KB Output is correct
117 Correct 4229 ms 39204 KB Output is correct
118 Correct 4326 ms 39276 KB Output is correct
119 Correct 4368 ms 39272 KB Output is correct
120 Correct 4276 ms 39404 KB Output is correct
121 Correct 4181 ms 39260 KB Output is correct
122 Correct 4126 ms 40928 KB Output is correct
123 Correct 350 ms 10444 KB Output is correct
124 Correct 3992 ms 40644 KB Output is correct
125 Correct 2264 ms 39332 KB Output is correct
126 Correct 4451 ms 41316 KB Output is correct
127 Correct 4237 ms 40964 KB Output is correct
128 Correct 4246 ms 41316 KB Output is correct
129 Correct 4446 ms 41104 KB Output is correct
130 Correct 4524 ms 41092 KB Output is correct
131 Correct 3509 ms 45412 KB Output is correct
132 Correct 3546 ms 48668 KB Output is correct
133 Correct 3548 ms 39732 KB Output is correct
134 Correct 3617 ms 41192 KB Output is correct
135 Correct 3831 ms 41148 KB Output is correct
136 Correct 3563 ms 41148 KB Output is correct
137 Correct 4301 ms 41492 KB Output is correct
138 Correct 3958 ms 41412 KB Output is correct
139 Correct 4427 ms 41328 KB Output is correct
140 Correct 4308 ms 41384 KB Output is correct
141 Correct 4192 ms 41420 KB Output is correct
142 Correct 4085 ms 41416 KB Output is correct
143 Correct 43 ms 16792 KB Output is correct
144 Correct 3661 ms 40912 KB Output is correct