Submission #949816

# Submission time Handle Problem Language Result Execution time Memory
949816 2024-03-19T18:09:30 Z andrei_boaca Tourism (JOI23_tourism) C++17
100 / 100
672 ms 112192 KB
#include <bits/stdc++.h>

using namespace std;
typedef pair<int,int> pii;
int n,m,q,v[100005],niv[100005],par[100005],timp,in[100005],out[100005],sol[100005],lin[100005];
int dp[21][100005];
vector<int> muchii[100005];
vector<pii> qr[100005];
int rmax[21][100005],rmin[21][100005];
int loga[100005];
struct date
{
    int l,r,add;
};
vector<date> events[100005];
vector<int> poz[100005];
set<pii> s[100005];
set<pair<pii,int>> s1[100005];
void dfs(int nod)
{
    timp++;
    lin[timp]=nod;
    in[nod]=timp;
    dp[0][nod]=par[nod];
    for(int i=1;i<=20;i++)
        dp[i][nod]=dp[i-1][dp[i-1][nod]];
    int who=0,maxim=0;
    for(int i:muchii[nod])
        if(i!=par[nod])
        {
            par[i]=nod;
            niv[i]=niv[nod]+1;
            dfs(i);
            if(s[i].size()>=maxim)
            {
                maxim=s[i].size();
                who=i;
            }
        }
    out[nod]=timp;
    if(who==0)
    {
        s1[nod].insert({{1,m},niv[nod]});
        for(int i:poz[nod])
        {
            s[nod].insert({i,i});
            auto it=prev(s1[nod].lower_bound({{i+1,0},0}));
            int l=(*it).first.first;
            int r=(*it).first.second;
            int l1=l;
            int r1=i-1;
            int l2=i+1;
            int r2=r;
            s1[nod].erase(it);
            if(l1<=r1)
                s1[nod].insert({{l1,r1},niv[nod]});
            if(l2<=r2)
                s1[nod].insert({{l2,r2},niv[nod]});
        }
        return;
    }
    swap(s[nod],s[who]);
    swap(s1[nod],s1[who]);
    for(int i:muchii[nod])
        if(i!=par[nod])
            if(i!=who)
            {
                for(pii j:s[i])
                {
                    s[nod].insert(j);
                    int st=j.first;
                    int dr=j.second;
                    auto it=prev(s1[nod].lower_bound({{st+1,0},0}));
                    int l=(*it).first.first;
                    int r=(*it).first.second;
                    int x=(*it).second;
                    int l1=l;
                    int r1=st-1;
                    int l2=dr+1;
                    int r2=r;
                    s1[nod].erase(it);
                    events[l].push_back({l,r,x-niv[nod]});
                    events[r+1].push_back({l,r,niv[nod]-x});
                    if(l1<=r1)
                        s1[nod].insert({{l1,r1},niv[nod]});
                    if(l2<=r2)
                        s1[nod].insert({{l2,r2},niv[nod]});
                }
                for(auto j:s1[i])
                {
                    int l=j.first.first;
                    int r=j.first.second;
                    int x=j.second;
                    events[l].push_back({l,r,x-niv[nod]});
                    events[r+1].push_back({l,r,niv[nod]-x});
                }
                s1[i].clear();
                s[i].clear();
            }
    for(int i:poz[nod])
    {
        s[nod].insert({i,i});
        auto it=prev(s1[nod].lower_bound({{i+1,0},0}));
        int l=(*it).first.first;
        int r=(*it).first.second;
        int x=(*it).second;
        int l1=l;
        int r1=i-1;
        int l2=i+1;
        int r2=r;
        s1[nod].erase(it);
        events[l].push_back({l,r,x-niv[nod]});
        events[r+1].push_back({l,r,niv[nod]-x});
        if(l1<=r1)
            s1[nod].insert({{l1,r1},niv[nod]});
        if(l2<=r2)
            s1[nod].insert({{l2,r2},niv[nod]});
    }
}
int aib[100005];
int lsb(int x)
{
    return x&(-x);
}
void update(int poz,int val)
{
    for(int i=poz;i<=m;i+=lsb(i))
        aib[i]+=val;
}
int suma(int poz)
{
    int rez=0;
    for(int i=poz;i>=1;i-=lsb(i))
        rez+=aib[i];
    return rez;
}
int getmin(int l,int r)
{
    int lg=loga[r-l+1];
    return min(rmin[lg][l],rmin[lg][r-(1<<lg)+1]);
}
int getmax(int l,int r)
{
    int lg=loga[r-l+1];
    return max(rmax[lg][l],rmax[lg][r-(1<<lg)+1]);
}
bool isancestor(int a,int b)
{
    return in[a]<=in[b]&&out[a]>=out[b];
}
int LCA(int a,int b)
{
    if(niv[a]>niv[b])
        swap(a,b);
    if(isancestor(a,b))
        return a;
    for(int i=17;i>=0;i--)
        if(dp[i][a]!=0&&!isancestor(dp[i][a],b))
            a=dp[i][a];
    return par[a];
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m>>q;
    for(int i=1;i<n;i++)
    {
        int a,b;
        cin>>a>>b;
        muchii[a].push_back(b);
        muchii[b].push_back(a);
    }
    for(int i=1;i<=m;i++)
    {
        cin>>v[i];
        poz[v[i]].push_back(i);
    }
    dfs(1);
    for(auto i:s1[1])
    {
        int l=i.first.first;
        int r=i.first.second;
        int x=i.second+1;
        events[l].push_back({l,r,x});
        events[r+1].push_back({l,r,x});
    }
    for(int i=1;i<=q;i++)
    {
        int l,r;
        cin>>l>>r;
        qr[l].push_back({r,i});
    }
    for(int i=1;i<=m;i++)
    {
        for(int bit=0;bit<20;bit++)
            if((1<<bit)<=i)
                loga[i]=bit;
    }
    for(int i=1;i<=m;i++)
    {
        v[i]=in[v[i]];
        rmax[0][i]=v[i];
        rmin[0][i]=v[i];
    }
    for(int j=1;j<=17;j++)
        for(int i=1;i<=m;i++)
        {
            rmax[j][i]=rmax[j-1][i];
            rmin[j][i]=rmin[j-1][i];
            int poz=i+(1<<(j-1));
            if(poz<=m)
            {
                rmax[j][i]=max(rmax[j][i],rmax[j-1][poz]);
                rmin[j][i]=min(rmin[j][i],rmin[j-1][poz]);
            }
        }
    for(int i=1;i<=m;i++)
    {
        for(date j:events[i])
        {
            update(j.l,j.add);
            update(j.r+1,-j.add);
        }
        for(pii p:qr[i])
        {
            int poz=p.first,ind=p.second;
            int val=suma(poz);
            val=n-val;
            int a=getmin(i,poz);
            int b=getmax(i,poz);
            a=lin[a];
            b=lin[b];
            int lca=LCA(a,b);
            val-=niv[lca];
            sol[ind]=val;
        }
    }
    for(int i=1;i<=q;i++)
        cout<<sol[i]<<'\n';
    return 0;
}

Compilation message

tourism.cpp: In function 'void dfs(int)':
tourism.cpp:34:27: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   34 |             if(s[i].size()>=maxim)
      |                ~~~~~~~~~~~^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 45564 KB Output is correct
2 Correct 8 ms 45404 KB Output is correct
3 Correct 8 ms 45404 KB Output is correct
4 Correct 9 ms 45512 KB Output is correct
5 Correct 9 ms 45660 KB Output is correct
6 Correct 8 ms 45660 KB Output is correct
7 Correct 9 ms 45684 KB Output is correct
8 Correct 8 ms 45660 KB Output is correct
9 Correct 8 ms 45660 KB Output is correct
10 Correct 9 ms 45660 KB Output is correct
11 Correct 8 ms 45660 KB Output is correct
12 Correct 9 ms 45496 KB Output is correct
13 Correct 8 ms 45656 KB Output is correct
14 Correct 9 ms 45660 KB Output is correct
15 Correct 8 ms 45660 KB Output is correct
16 Correct 8 ms 45660 KB Output is correct
17 Correct 8 ms 45660 KB Output is correct
18 Correct 8 ms 45660 KB Output is correct
19 Correct 8 ms 45660 KB Output is correct
20 Correct 9 ms 45568 KB Output is correct
21 Correct 8 ms 45600 KB Output is correct
22 Correct 9 ms 45656 KB Output is correct
23 Correct 8 ms 45660 KB Output is correct
24 Correct 8 ms 45656 KB Output is correct
25 Correct 8 ms 45732 KB Output is correct
26 Correct 8 ms 45656 KB Output is correct
27 Correct 8 ms 45404 KB Output is correct
28 Correct 7 ms 45912 KB Output is correct
29 Correct 9 ms 45600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 45564 KB Output is correct
2 Correct 8 ms 45404 KB Output is correct
3 Correct 8 ms 45404 KB Output is correct
4 Correct 9 ms 45512 KB Output is correct
5 Correct 9 ms 45660 KB Output is correct
6 Correct 8 ms 45660 KB Output is correct
7 Correct 9 ms 45684 KB Output is correct
8 Correct 8 ms 45660 KB Output is correct
9 Correct 8 ms 45660 KB Output is correct
10 Correct 9 ms 45660 KB Output is correct
11 Correct 8 ms 45660 KB Output is correct
12 Correct 9 ms 45496 KB Output is correct
13 Correct 8 ms 45656 KB Output is correct
14 Correct 9 ms 45660 KB Output is correct
15 Correct 8 ms 45660 KB Output is correct
16 Correct 8 ms 45660 KB Output is correct
17 Correct 8 ms 45660 KB Output is correct
18 Correct 8 ms 45660 KB Output is correct
19 Correct 8 ms 45660 KB Output is correct
20 Correct 9 ms 45568 KB Output is correct
21 Correct 8 ms 45600 KB Output is correct
22 Correct 9 ms 45656 KB Output is correct
23 Correct 8 ms 45660 KB Output is correct
24 Correct 8 ms 45656 KB Output is correct
25 Correct 8 ms 45732 KB Output is correct
26 Correct 8 ms 45656 KB Output is correct
27 Correct 8 ms 45404 KB Output is correct
28 Correct 7 ms 45912 KB Output is correct
29 Correct 9 ms 45600 KB Output is correct
30 Correct 12 ms 46184 KB Output is correct
31 Correct 12 ms 46048 KB Output is correct
32 Correct 14 ms 46428 KB Output is correct
33 Correct 14 ms 46340 KB Output is correct
34 Correct 14 ms 46372 KB Output is correct
35 Correct 15 ms 46392 KB Output is correct
36 Correct 13 ms 46168 KB Output is correct
37 Correct 12 ms 46428 KB Output is correct
38 Correct 11 ms 46464 KB Output is correct
39 Correct 14 ms 46464 KB Output is correct
40 Correct 11 ms 46428 KB Output is correct
41 Correct 10 ms 46424 KB Output is correct
42 Correct 11 ms 46424 KB Output is correct
43 Correct 10 ms 46428 KB Output is correct
44 Correct 11 ms 46172 KB Output is correct
45 Correct 12 ms 46172 KB Output is correct
46 Correct 11 ms 46172 KB Output is correct
47 Correct 12 ms 46168 KB Output is correct
48 Correct 12 ms 46172 KB Output is correct
49 Correct 11 ms 46172 KB Output is correct
50 Correct 11 ms 46172 KB Output is correct
51 Correct 11 ms 46172 KB Output is correct
52 Correct 11 ms 46172 KB Output is correct
53 Correct 11 ms 46172 KB Output is correct
54 Correct 11 ms 46172 KB Output is correct
55 Correct 13 ms 46172 KB Output is correct
56 Correct 9 ms 45764 KB Output is correct
57 Correct 9 ms 45660 KB Output is correct
58 Correct 13 ms 46440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 45408 KB Output is correct
2 Correct 7 ms 45652 KB Output is correct
3 Correct 9 ms 45656 KB Output is correct
4 Correct 191 ms 75660 KB Output is correct
5 Correct 129 ms 79656 KB Output is correct
6 Correct 202 ms 86112 KB Output is correct
7 Correct 229 ms 92132 KB Output is correct
8 Correct 229 ms 91984 KB Output is correct
9 Correct 224 ms 91984 KB Output is correct
10 Correct 229 ms 92048 KB Output is correct
11 Correct 237 ms 92116 KB Output is correct
12 Correct 141 ms 90932 KB Output is correct
13 Correct 144 ms 90960 KB Output is correct
14 Correct 145 ms 90892 KB Output is correct
15 Correct 62 ms 77608 KB Output is correct
16 Correct 237 ms 91612 KB Output is correct
17 Correct 64 ms 54484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 45404 KB Output is correct
2 Correct 261 ms 69976 KB Output is correct
3 Correct 384 ms 82704 KB Output is correct
4 Correct 323 ms 74992 KB Output is correct
5 Correct 551 ms 94028 KB Output is correct
6 Correct 514 ms 91356 KB Output is correct
7 Correct 554 ms 91744 KB Output is correct
8 Correct 535 ms 91380 KB Output is correct
9 Correct 500 ms 93576 KB Output is correct
10 Correct 548 ms 92192 KB Output is correct
11 Correct 578 ms 91420 KB Output is correct
12 Correct 512 ms 91684 KB Output is correct
13 Correct 495 ms 91376 KB Output is correct
14 Correct 578 ms 92640 KB Output is correct
15 Correct 549 ms 95152 KB Output is correct
16 Correct 506 ms 89264 KB Output is correct
17 Correct 594 ms 91580 KB Output is correct
18 Correct 489 ms 91484 KB Output is correct
19 Correct 413 ms 82004 KB Output is correct
20 Correct 461 ms 82604 KB Output is correct
21 Correct 429 ms 84312 KB Output is correct
22 Correct 406 ms 82208 KB Output is correct
23 Correct 388 ms 80980 KB Output is correct
24 Correct 402 ms 81560 KB Output is correct
25 Correct 406 ms 82372 KB Output is correct
26 Correct 414 ms 82540 KB Output is correct
27 Correct 457 ms 81932 KB Output is correct
28 Correct 398 ms 82160 KB Output is correct
29 Correct 406 ms 82780 KB Output is correct
30 Correct 472 ms 83044 KB Output is correct
31 Correct 422 ms 82520 KB Output is correct
32 Correct 454 ms 83208 KB Output is correct
33 Correct 473 ms 84996 KB Output is correct
34 Correct 417 ms 83252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 45404 KB Output is correct
2 Correct 9 ms 45660 KB Output is correct
3 Correct 9 ms 45784 KB Output is correct
4 Correct 528 ms 101136 KB Output is correct
5 Correct 592 ms 102984 KB Output is correct
6 Correct 606 ms 110064 KB Output is correct
7 Correct 672 ms 112024 KB Output is correct
8 Correct 625 ms 112108 KB Output is correct
9 Correct 641 ms 112048 KB Output is correct
10 Correct 630 ms 112192 KB Output is correct
11 Correct 646 ms 112008 KB Output is correct
12 Correct 645 ms 111984 KB Output is correct
13 Correct 635 ms 112004 KB Output is correct
14 Correct 68 ms 54484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 45564 KB Output is correct
2 Correct 8 ms 45404 KB Output is correct
3 Correct 8 ms 45404 KB Output is correct
4 Correct 9 ms 45512 KB Output is correct
5 Correct 9 ms 45660 KB Output is correct
6 Correct 8 ms 45660 KB Output is correct
7 Correct 9 ms 45684 KB Output is correct
8 Correct 8 ms 45660 KB Output is correct
9 Correct 8 ms 45660 KB Output is correct
10 Correct 9 ms 45660 KB Output is correct
11 Correct 8 ms 45660 KB Output is correct
12 Correct 9 ms 45496 KB Output is correct
13 Correct 8 ms 45656 KB Output is correct
14 Correct 9 ms 45660 KB Output is correct
15 Correct 8 ms 45660 KB Output is correct
16 Correct 8 ms 45660 KB Output is correct
17 Correct 8 ms 45660 KB Output is correct
18 Correct 8 ms 45660 KB Output is correct
19 Correct 8 ms 45660 KB Output is correct
20 Correct 9 ms 45568 KB Output is correct
21 Correct 8 ms 45600 KB Output is correct
22 Correct 9 ms 45656 KB Output is correct
23 Correct 8 ms 45660 KB Output is correct
24 Correct 8 ms 45656 KB Output is correct
25 Correct 8 ms 45732 KB Output is correct
26 Correct 8 ms 45656 KB Output is correct
27 Correct 8 ms 45404 KB Output is correct
28 Correct 7 ms 45912 KB Output is correct
29 Correct 9 ms 45600 KB Output is correct
30 Correct 12 ms 46184 KB Output is correct
31 Correct 12 ms 46048 KB Output is correct
32 Correct 14 ms 46428 KB Output is correct
33 Correct 14 ms 46340 KB Output is correct
34 Correct 14 ms 46372 KB Output is correct
35 Correct 15 ms 46392 KB Output is correct
36 Correct 13 ms 46168 KB Output is correct
37 Correct 12 ms 46428 KB Output is correct
38 Correct 11 ms 46464 KB Output is correct
39 Correct 14 ms 46464 KB Output is correct
40 Correct 11 ms 46428 KB Output is correct
41 Correct 10 ms 46424 KB Output is correct
42 Correct 11 ms 46424 KB Output is correct
43 Correct 10 ms 46428 KB Output is correct
44 Correct 11 ms 46172 KB Output is correct
45 Correct 12 ms 46172 KB Output is correct
46 Correct 11 ms 46172 KB Output is correct
47 Correct 12 ms 46168 KB Output is correct
48 Correct 12 ms 46172 KB Output is correct
49 Correct 11 ms 46172 KB Output is correct
50 Correct 11 ms 46172 KB Output is correct
51 Correct 11 ms 46172 KB Output is correct
52 Correct 11 ms 46172 KB Output is correct
53 Correct 11 ms 46172 KB Output is correct
54 Correct 11 ms 46172 KB Output is correct
55 Correct 13 ms 46172 KB Output is correct
56 Correct 9 ms 45764 KB Output is correct
57 Correct 9 ms 45660 KB Output is correct
58 Correct 13 ms 46440 KB Output is correct
59 Correct 8 ms 45408 KB Output is correct
60 Correct 7 ms 45652 KB Output is correct
61 Correct 9 ms 45656 KB Output is correct
62 Correct 191 ms 75660 KB Output is correct
63 Correct 129 ms 79656 KB Output is correct
64 Correct 202 ms 86112 KB Output is correct
65 Correct 229 ms 92132 KB Output is correct
66 Correct 229 ms 91984 KB Output is correct
67 Correct 224 ms 91984 KB Output is correct
68 Correct 229 ms 92048 KB Output is correct
69 Correct 237 ms 92116 KB Output is correct
70 Correct 141 ms 90932 KB Output is correct
71 Correct 144 ms 90960 KB Output is correct
72 Correct 145 ms 90892 KB Output is correct
73 Correct 62 ms 77608 KB Output is correct
74 Correct 237 ms 91612 KB Output is correct
75 Correct 64 ms 54484 KB Output is correct
76 Correct 7 ms 45404 KB Output is correct
77 Correct 261 ms 69976 KB Output is correct
78 Correct 384 ms 82704 KB Output is correct
79 Correct 323 ms 74992 KB Output is correct
80 Correct 551 ms 94028 KB Output is correct
81 Correct 514 ms 91356 KB Output is correct
82 Correct 554 ms 91744 KB Output is correct
83 Correct 535 ms 91380 KB Output is correct
84 Correct 500 ms 93576 KB Output is correct
85 Correct 548 ms 92192 KB Output is correct
86 Correct 578 ms 91420 KB Output is correct
87 Correct 512 ms 91684 KB Output is correct
88 Correct 495 ms 91376 KB Output is correct
89 Correct 578 ms 92640 KB Output is correct
90 Correct 549 ms 95152 KB Output is correct
91 Correct 506 ms 89264 KB Output is correct
92 Correct 594 ms 91580 KB Output is correct
93 Correct 489 ms 91484 KB Output is correct
94 Correct 413 ms 82004 KB Output is correct
95 Correct 461 ms 82604 KB Output is correct
96 Correct 429 ms 84312 KB Output is correct
97 Correct 406 ms 82208 KB Output is correct
98 Correct 388 ms 80980 KB Output is correct
99 Correct 402 ms 81560 KB Output is correct
100 Correct 406 ms 82372 KB Output is correct
101 Correct 414 ms 82540 KB Output is correct
102 Correct 457 ms 81932 KB Output is correct
103 Correct 398 ms 82160 KB Output is correct
104 Correct 406 ms 82780 KB Output is correct
105 Correct 472 ms 83044 KB Output is correct
106 Correct 422 ms 82520 KB Output is correct
107 Correct 454 ms 83208 KB Output is correct
108 Correct 473 ms 84996 KB Output is correct
109 Correct 417 ms 83252 KB Output is correct
110 Correct 8 ms 45404 KB Output is correct
111 Correct 9 ms 45660 KB Output is correct
112 Correct 9 ms 45784 KB Output is correct
113 Correct 528 ms 101136 KB Output is correct
114 Correct 592 ms 102984 KB Output is correct
115 Correct 606 ms 110064 KB Output is correct
116 Correct 672 ms 112024 KB Output is correct
117 Correct 625 ms 112108 KB Output is correct
118 Correct 641 ms 112048 KB Output is correct
119 Correct 630 ms 112192 KB Output is correct
120 Correct 646 ms 112008 KB Output is correct
121 Correct 645 ms 111984 KB Output is correct
122 Correct 635 ms 112004 KB Output is correct
123 Correct 68 ms 54484 KB Output is correct
124 Correct 467 ms 86964 KB Output is correct
125 Correct 344 ms 77488 KB Output is correct
126 Correct 540 ms 94980 KB Output is correct
127 Correct 548 ms 97088 KB Output is correct
128 Correct 538 ms 95444 KB Output is correct
129 Correct 542 ms 94868 KB Output is correct
130 Correct 544 ms 93620 KB Output is correct
131 Correct 269 ms 89168 KB Output is correct
132 Correct 270 ms 91712 KB Output is correct
133 Correct 283 ms 84308 KB Output is correct
134 Correct 437 ms 83052 KB Output is correct
135 Correct 477 ms 84728 KB Output is correct
136 Correct 460 ms 84716 KB Output is correct
137 Correct 346 ms 76900 KB Output is correct
138 Correct 337 ms 76912 KB Output is correct
139 Correct 340 ms 76724 KB Output is correct
140 Correct 326 ms 77076 KB Output is correct
141 Correct 347 ms 76732 KB Output is correct
142 Correct 345 ms 77164 KB Output is correct
143 Correct 85 ms 54328 KB Output is correct
144 Correct 559 ms 92992 KB Output is correct