Submission #1016788

# Submission time Handle Problem Language Result Execution time Memory
1016788 2024-07-08T12:25:08 Z alexdd Tourism (JOI23_tourism) C++17
52 / 100
5000 ms 33872 KB
#include<bits/stdc++.h>
using namespace std;
int n,m,q;
vector<int> con[100005];
int v[100005];
int rez[100005];
int anc[100005][17];
int tin[100005],tout[100005],timer;
vector<pair<int,int>> qrys[100005];

int fr[100005];
int val[100005];
void baga(int le, int ri, int t)
{
    for(int i=le;i<=ri;i++)
    {
        fr[val[i]]--;
        val[i]=t;
    }
    fr[t] += ri-le+1;
}
int numara(int le)
{
    int cnt=0;
    for(int i=le;i<=m;i++)
        cnt += fr[i];
    return cnt;
}

int depth[100005], siz[100005], head[100005], parent[100005], pos[100005], curpos;
void dfs(int nod)
{
    siz[nod]=1;
    tin[nod]=++timer;
    for(auto adj:con[nod])
    {
        if(adj!=parent[nod])
        {
            parent[adj]=nod;
            depth[adj]=depth[nod]+1;
            dfs(adj);
            siz[nod]+=siz[adj];
        }
    }
    tout[nod]=++timer;
}
void decompose(int nod, int chead)
{
    pos[nod]=++curpos;
    head[nod]=chead;
    int heavy=-1,maxc=-1;
    for(auto adj:con[nod])
        if(adj!=parent[nod] && siz[adj]>maxc)
            maxc=siz[adj], heavy=adj;
    if(heavy!=-1)
        decompose(heavy,chead);
    for(auto adj:con[nod])
        if(adj!=parent[nod] && adj!=heavy)
            decompose(adj,adj);
}
void upd_hld(int b, int t)
{
    for(;head[1]!=head[b];b=parent[head[b]])
    {
        baga(pos[head[b]], pos[b], t);
    }
    baga(pos[1],pos[b],t);
}

pair<int,int> aint[270000];
pair<int,int> combine(pair<int,int> x, pair<int,int> y)
{
    pair<int,int> aux;
    if(x.first!=-1 && (y.first==-1 || tin[x.first] < tin[y.first])) aux.first = x.first;
    else aux.first = y.first;
    if(x.second!=-1 && (y.second==-1 || tin[x.second] > tin[y.second])) aux.second = x.second;
    else aux.second = y.second;
    return aux;
}
void build(int nod, int st, int dr)
{
    if(st==dr)
    {
        aint[nod] = {v[st],v[st]};
        return;
    }
    int mij=(st+dr)/2;
    build(nod*2,st,mij);
    build(nod*2+1,mij+1,dr);
    aint[nod] = combine(aint[nod*2], aint[nod*2+1]);
}
pair<int,int> qry(int nod, int st, int dr, int le, int ri)
{
    if(le>ri)
        return {-1,-1};
    if(le==st && dr==ri)
        return aint[nod];
    int mij=(st+dr)/2;
    return combine(qry(nod*2,st,mij,le,min(mij,ri)),
                   qry(nod*2+1,mij+1,dr,max(mij+1,le),ri));

}
bool isanc(int x, int y)
{
    return (tin[x]<=tin[y] && tout[x]>=tout[y]);
}
int lca(int x, int y)
{
    if(isanc(x,y))
        return x;
    if(isanc(y,x))
        return y;
    for(int p=16;p>=0;p--)
        if(!isanc(anc[x][p],y))
            x = anc[x][p];
    return anc[x][0];
}
int qry_lca(int le, int ri)
{
    pair<int,int> x = qry(1,1,m,le,ri);
    return lca(x.first, x.second);
}
signed main()
{
    cin>>n>>m>>q;
    int a,b;
    for(int i=1;i<n;i++)
    {
        cin>>a>>b;
        con[a].push_back(b);
        con[b].push_back(a);
    }
    dfs(1);
    decompose(1,1);

    for(int i=1;i<=n;i++)
        anc[i][0] = parent[i];
    anc[1][0]=1;
    for(int p=1;p<17;p++)
        for(int i=1;i<=n;i++)
            anc[i][p] = anc[anc[i][p-1]][p-1];

    for(int i=1;i<=m;i++)
        cin>>v[i];
    build(1,1,m);
    for(int i=1;i<=q;i++)
    {
        cin>>a>>b;
        qrys[b].push_back({a,i});
    }
    fr[0]=n;
    for(int i=1;i<=m;i++)
    {
        upd_hld(v[i],i);
        for(auto [le,id]:qrys[i])
        {
            int lc = qry_lca(le,i);
            rez[id] = numara(le) - depth[lc];
        }
    }
    for(int i=1;i<=q;i++)
        cout<<rez[i]<<"\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9816 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 2 ms 9820 KB Output is correct
4 Correct 2 ms 9820 KB Output is correct
5 Correct 2 ms 9820 KB Output is correct
6 Correct 2 ms 9832 KB Output is correct
7 Correct 2 ms 9820 KB Output is correct
8 Correct 2 ms 9820 KB Output is correct
9 Correct 2 ms 9820 KB Output is correct
10 Correct 2 ms 9820 KB Output is correct
11 Correct 2 ms 9820 KB Output is correct
12 Correct 2 ms 9820 KB Output is correct
13 Correct 2 ms 9820 KB Output is correct
14 Correct 2 ms 9820 KB Output is correct
15 Correct 2 ms 9820 KB Output is correct
16 Correct 2 ms 9820 KB Output is correct
17 Correct 2 ms 9820 KB Output is correct
18 Correct 2 ms 9820 KB Output is correct
19 Correct 2 ms 9820 KB Output is correct
20 Correct 2 ms 9668 KB Output is correct
21 Correct 2 ms 9820 KB Output is correct
22 Correct 2 ms 9820 KB Output is correct
23 Correct 2 ms 9820 KB Output is correct
24 Correct 2 ms 9820 KB Output is correct
25 Correct 2 ms 9820 KB Output is correct
26 Correct 2 ms 9820 KB Output is correct
27 Correct 2 ms 9820 KB Output is correct
28 Correct 2 ms 9820 KB Output is correct
29 Correct 2 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9816 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 2 ms 9820 KB Output is correct
4 Correct 2 ms 9820 KB Output is correct
5 Correct 2 ms 9820 KB Output is correct
6 Correct 2 ms 9832 KB Output is correct
7 Correct 2 ms 9820 KB Output is correct
8 Correct 2 ms 9820 KB Output is correct
9 Correct 2 ms 9820 KB Output is correct
10 Correct 2 ms 9820 KB Output is correct
11 Correct 2 ms 9820 KB Output is correct
12 Correct 2 ms 9820 KB Output is correct
13 Correct 2 ms 9820 KB Output is correct
14 Correct 2 ms 9820 KB Output is correct
15 Correct 2 ms 9820 KB Output is correct
16 Correct 2 ms 9820 KB Output is correct
17 Correct 2 ms 9820 KB Output is correct
18 Correct 2 ms 9820 KB Output is correct
19 Correct 2 ms 9820 KB Output is correct
20 Correct 2 ms 9668 KB Output is correct
21 Correct 2 ms 9820 KB Output is correct
22 Correct 2 ms 9820 KB Output is correct
23 Correct 2 ms 9820 KB Output is correct
24 Correct 2 ms 9820 KB Output is correct
25 Correct 2 ms 9820 KB Output is correct
26 Correct 2 ms 9820 KB Output is correct
27 Correct 2 ms 9820 KB Output is correct
28 Correct 2 ms 9820 KB Output is correct
29 Correct 2 ms 9820 KB Output is correct
30 Correct 4 ms 9820 KB Output is correct
31 Correct 4 ms 9820 KB Output is correct
32 Correct 5 ms 9888 KB Output is correct
33 Correct 6 ms 9816 KB Output is correct
34 Correct 6 ms 9820 KB Output is correct
35 Correct 5 ms 9944 KB Output is correct
36 Correct 6 ms 9820 KB Output is correct
37 Correct 6 ms 9820 KB Output is correct
38 Correct 6 ms 9832 KB Output is correct
39 Correct 6 ms 10076 KB Output is correct
40 Correct 6 ms 10076 KB Output is correct
41 Correct 6 ms 10076 KB Output is correct
42 Correct 6 ms 10076 KB Output is correct
43 Correct 6 ms 9820 KB Output is correct
44 Correct 6 ms 9820 KB Output is correct
45 Correct 5 ms 9820 KB Output is correct
46 Correct 6 ms 9820 KB Output is correct
47 Correct 6 ms 9820 KB Output is correct
48 Correct 5 ms 9820 KB Output is correct
49 Correct 6 ms 9816 KB Output is correct
50 Correct 5 ms 9820 KB Output is correct
51 Correct 5 ms 9988 KB Output is correct
52 Correct 5 ms 9964 KB Output is correct
53 Correct 5 ms 9820 KB Output is correct
54 Correct 7 ms 9980 KB Output is correct
55 Correct 5 ms 9984 KB Output is correct
56 Correct 4 ms 9820 KB Output is correct
57 Correct 3 ms 9820 KB Output is correct
58 Correct 4 ms 9816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9820 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 4 ms 9896 KB Output is correct
4 Correct 3560 ms 23952 KB Output is correct
5 Correct 2684 ms 29440 KB Output is correct
6 Correct 3890 ms 31824 KB Output is correct
7 Execution timed out 5083 ms 33872 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9820 KB Output is correct
2 Correct 71 ms 16876 KB Output is correct
3 Correct 94 ms 18000 KB Output is correct
4 Correct 79 ms 18180 KB Output is correct
5 Correct 84 ms 21332 KB Output is correct
6 Correct 80 ms 21368 KB Output is correct
7 Correct 92 ms 21328 KB Output is correct
8 Correct 79 ms 21332 KB Output is correct
9 Correct 84 ms 21340 KB Output is correct
10 Correct 90 ms 21308 KB Output is correct
11 Correct 110 ms 21368 KB Output is correct
12 Correct 193 ms 21392 KB Output is correct
13 Correct 291 ms 21712 KB Output is correct
14 Correct 729 ms 22200 KB Output is correct
15 Correct 2287 ms 24500 KB Output is correct
16 Correct 195 ms 21588 KB Output is correct
17 Correct 192 ms 21584 KB Output is correct
18 Correct 196 ms 21584 KB Output is correct
19 Correct 162 ms 21332 KB Output is correct
20 Correct 152 ms 21392 KB Output is correct
21 Correct 131 ms 21148 KB Output is correct
22 Correct 151 ms 21296 KB Output is correct
23 Correct 144 ms 21328 KB Output is correct
24 Correct 132 ms 21336 KB Output is correct
25 Correct 142 ms 21332 KB Output is correct
26 Correct 177 ms 21328 KB Output is correct
27 Correct 147 ms 21328 KB Output is correct
28 Correct 155 ms 21328 KB Output is correct
29 Correct 192 ms 21328 KB Output is correct
30 Correct 293 ms 21588 KB Output is correct
31 Correct 359 ms 21708 KB Output is correct
32 Correct 593 ms 22020 KB Output is correct
33 Correct 1092 ms 23120 KB Output is correct
34 Correct 2156 ms 24604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9820 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 5 ms 9820 KB Output is correct
4 Correct 2336 ms 20196 KB Output is correct
5 Correct 2328 ms 22424 KB Output is correct
6 Correct 2520 ms 25872 KB Output is correct
7 Correct 2765 ms 26628 KB Output is correct
8 Correct 2758 ms 26620 KB Output is correct
9 Correct 2633 ms 26616 KB Output is correct
10 Correct 2848 ms 26628 KB Output is correct
11 Correct 2880 ms 26708 KB Output is correct
12 Correct 2913 ms 26624 KB Output is correct
13 Correct 2853 ms 26520 KB Output is correct
14 Correct 2394 ms 15184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9816 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 2 ms 9820 KB Output is correct
4 Correct 2 ms 9820 KB Output is correct
5 Correct 2 ms 9820 KB Output is correct
6 Correct 2 ms 9832 KB Output is correct
7 Correct 2 ms 9820 KB Output is correct
8 Correct 2 ms 9820 KB Output is correct
9 Correct 2 ms 9820 KB Output is correct
10 Correct 2 ms 9820 KB Output is correct
11 Correct 2 ms 9820 KB Output is correct
12 Correct 2 ms 9820 KB Output is correct
13 Correct 2 ms 9820 KB Output is correct
14 Correct 2 ms 9820 KB Output is correct
15 Correct 2 ms 9820 KB Output is correct
16 Correct 2 ms 9820 KB Output is correct
17 Correct 2 ms 9820 KB Output is correct
18 Correct 2 ms 9820 KB Output is correct
19 Correct 2 ms 9820 KB Output is correct
20 Correct 2 ms 9668 KB Output is correct
21 Correct 2 ms 9820 KB Output is correct
22 Correct 2 ms 9820 KB Output is correct
23 Correct 2 ms 9820 KB Output is correct
24 Correct 2 ms 9820 KB Output is correct
25 Correct 2 ms 9820 KB Output is correct
26 Correct 2 ms 9820 KB Output is correct
27 Correct 2 ms 9820 KB Output is correct
28 Correct 2 ms 9820 KB Output is correct
29 Correct 2 ms 9820 KB Output is correct
30 Correct 4 ms 9820 KB Output is correct
31 Correct 4 ms 9820 KB Output is correct
32 Correct 5 ms 9888 KB Output is correct
33 Correct 6 ms 9816 KB Output is correct
34 Correct 6 ms 9820 KB Output is correct
35 Correct 5 ms 9944 KB Output is correct
36 Correct 6 ms 9820 KB Output is correct
37 Correct 6 ms 9820 KB Output is correct
38 Correct 6 ms 9832 KB Output is correct
39 Correct 6 ms 10076 KB Output is correct
40 Correct 6 ms 10076 KB Output is correct
41 Correct 6 ms 10076 KB Output is correct
42 Correct 6 ms 10076 KB Output is correct
43 Correct 6 ms 9820 KB Output is correct
44 Correct 6 ms 9820 KB Output is correct
45 Correct 5 ms 9820 KB Output is correct
46 Correct 6 ms 9820 KB Output is correct
47 Correct 6 ms 9820 KB Output is correct
48 Correct 5 ms 9820 KB Output is correct
49 Correct 6 ms 9816 KB Output is correct
50 Correct 5 ms 9820 KB Output is correct
51 Correct 5 ms 9988 KB Output is correct
52 Correct 5 ms 9964 KB Output is correct
53 Correct 5 ms 9820 KB Output is correct
54 Correct 7 ms 9980 KB Output is correct
55 Correct 5 ms 9984 KB Output is correct
56 Correct 4 ms 9820 KB Output is correct
57 Correct 3 ms 9820 KB Output is correct
58 Correct 4 ms 9816 KB Output is correct
59 Correct 2 ms 9820 KB Output is correct
60 Correct 2 ms 9820 KB Output is correct
61 Correct 4 ms 9896 KB Output is correct
62 Correct 3560 ms 23952 KB Output is correct
63 Correct 2684 ms 29440 KB Output is correct
64 Correct 3890 ms 31824 KB Output is correct
65 Execution timed out 5083 ms 33872 KB Time limit exceeded
66 Halted 0 ms 0 KB -