답안 #1016786

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1016786 2024-07-08T12:23:39 Z alexdd Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 27488 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 || depth[x.first] < depth[y.first])) aux.first = x.first;
    else aux.first = y.first;
    if(x.second!=-1 && (y.second==-1 || depth[x.second] > depth[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)
{
    int lc = v[le];
    for(int i=le;i<=ri;i++)
        lc = lca(lc, v[i]);
    return lc;
    pair<int,int> x = qry(1,1,n,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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9816 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 3 ms 9976 KB Output is correct
4 Correct 4 ms 9820 KB Output is correct
5 Correct 2 ms 9820 KB Output is correct
6 Correct 2 ms 9820 KB Output is correct
7 Correct 2 ms 9820 KB Output is correct
8 Correct 2 ms 9652 KB Output is correct
9 Correct 3 ms 9820 KB Output is correct
10 Correct 3 ms 9820 KB Output is correct
11 Correct 3 ms 9820 KB Output is correct
12 Correct 3 ms 9804 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 9824 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 9820 KB Output is correct
21 Correct 2 ms 9872 KB Output is correct
22 Correct 2 ms 9820 KB Output is correct
23 Correct 3 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 9784 KB Output is correct
29 Correct 2 ms 9820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9816 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 3 ms 9976 KB Output is correct
4 Correct 4 ms 9820 KB Output is correct
5 Correct 2 ms 9820 KB Output is correct
6 Correct 2 ms 9820 KB Output is correct
7 Correct 2 ms 9820 KB Output is correct
8 Correct 2 ms 9652 KB Output is correct
9 Correct 3 ms 9820 KB Output is correct
10 Correct 3 ms 9820 KB Output is correct
11 Correct 3 ms 9820 KB Output is correct
12 Correct 3 ms 9804 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 9824 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 9820 KB Output is correct
21 Correct 2 ms 9872 KB Output is correct
22 Correct 2 ms 9820 KB Output is correct
23 Correct 3 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 9784 KB Output is correct
29 Correct 2 ms 9820 KB Output is correct
30 Correct 5 ms 9820 KB Output is correct
31 Correct 7 ms 9820 KB Output is correct
32 Correct 8 ms 9820 KB Output is correct
33 Correct 8 ms 9820 KB Output is correct
34 Correct 11 ms 9900 KB Output is correct
35 Correct 15 ms 10008 KB Output is correct
36 Correct 19 ms 10072 KB Output is correct
37 Correct 21 ms 10076 KB Output is correct
38 Correct 9 ms 10076 KB Output is correct
39 Correct 9 ms 10076 KB Output is correct
40 Correct 9 ms 10132 KB Output is correct
41 Correct 16 ms 10144 KB Output is correct
42 Correct 15 ms 10076 KB Output is correct
43 Correct 15 ms 9876 KB Output is correct
44 Correct 9 ms 10076 KB Output is correct
45 Correct 8 ms 9820 KB Output is correct
46 Correct 9 ms 10076 KB Output is correct
47 Correct 15 ms 10044 KB Output is correct
48 Correct 15 ms 10032 KB Output is correct
49 Correct 16 ms 9816 KB Output is correct
50 Correct 8 ms 10036 KB Output is correct
51 Correct 8 ms 9820 KB Output is correct
52 Correct 8 ms 9820 KB Output is correct
53 Correct 8 ms 9816 KB Output is correct
54 Correct 8 ms 9820 KB Output is correct
55 Correct 8 ms 10020 KB Output is correct
56 Correct 7 ms 9820 KB Output is correct
57 Correct 3 ms 9816 KB Output is correct
58 Correct 5 ms 9976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9820 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 7 ms 9908 KB Output is correct
4 Execution timed out 5076 ms 25680 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9820 KB Output is correct
2 Correct 63 ms 16856 KB Output is correct
3 Correct 97 ms 19024 KB Output is correct
4 Correct 82 ms 19496 KB Output is correct
5 Correct 82 ms 23020 KB Output is correct
6 Correct 83 ms 22976 KB Output is correct
7 Correct 80 ms 22864 KB Output is correct
8 Correct 101 ms 23020 KB Output is correct
9 Correct 82 ms 23072 KB Output is correct
10 Correct 93 ms 23312 KB Output is correct
11 Correct 102 ms 22972 KB Output is correct
12 Correct 143 ms 23224 KB Output is correct
13 Correct 292 ms 23524 KB Output is correct
14 Correct 740 ms 24404 KB Output is correct
15 Correct 2134 ms 27384 KB Output is correct
16 Correct 204 ms 23332 KB Output is correct
17 Correct 191 ms 23404 KB Output is correct
18 Correct 203 ms 23380 KB Output is correct
19 Correct 151 ms 23072 KB Output is correct
20 Correct 171 ms 23128 KB Output is correct
21 Correct 136 ms 22992 KB Output is correct
22 Correct 139 ms 23120 KB Output is correct
23 Correct 150 ms 23048 KB Output is correct
24 Correct 133 ms 23120 KB Output is correct
25 Correct 142 ms 22896 KB Output is correct
26 Correct 175 ms 23088 KB Output is correct
27 Correct 170 ms 23076 KB Output is correct
28 Correct 143 ms 23124 KB Output is correct
29 Correct 197 ms 23120 KB Output is correct
30 Correct 276 ms 23288 KB Output is correct
31 Correct 356 ms 23376 KB Output is correct
32 Correct 556 ms 23948 KB Output is correct
33 Correct 1171 ms 25276 KB Output is correct
34 Correct 2231 ms 27488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9820 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 7 ms 9820 KB Output is correct
4 Execution timed out 5087 ms 21776 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9816 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 3 ms 9976 KB Output is correct
4 Correct 4 ms 9820 KB Output is correct
5 Correct 2 ms 9820 KB Output is correct
6 Correct 2 ms 9820 KB Output is correct
7 Correct 2 ms 9820 KB Output is correct
8 Correct 2 ms 9652 KB Output is correct
9 Correct 3 ms 9820 KB Output is correct
10 Correct 3 ms 9820 KB Output is correct
11 Correct 3 ms 9820 KB Output is correct
12 Correct 3 ms 9804 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 9824 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 9820 KB Output is correct
21 Correct 2 ms 9872 KB Output is correct
22 Correct 2 ms 9820 KB Output is correct
23 Correct 3 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 9784 KB Output is correct
29 Correct 2 ms 9820 KB Output is correct
30 Correct 5 ms 9820 KB Output is correct
31 Correct 7 ms 9820 KB Output is correct
32 Correct 8 ms 9820 KB Output is correct
33 Correct 8 ms 9820 KB Output is correct
34 Correct 11 ms 9900 KB Output is correct
35 Correct 15 ms 10008 KB Output is correct
36 Correct 19 ms 10072 KB Output is correct
37 Correct 21 ms 10076 KB Output is correct
38 Correct 9 ms 10076 KB Output is correct
39 Correct 9 ms 10076 KB Output is correct
40 Correct 9 ms 10132 KB Output is correct
41 Correct 16 ms 10144 KB Output is correct
42 Correct 15 ms 10076 KB Output is correct
43 Correct 15 ms 9876 KB Output is correct
44 Correct 9 ms 10076 KB Output is correct
45 Correct 8 ms 9820 KB Output is correct
46 Correct 9 ms 10076 KB Output is correct
47 Correct 15 ms 10044 KB Output is correct
48 Correct 15 ms 10032 KB Output is correct
49 Correct 16 ms 9816 KB Output is correct
50 Correct 8 ms 10036 KB Output is correct
51 Correct 8 ms 9820 KB Output is correct
52 Correct 8 ms 9820 KB Output is correct
53 Correct 8 ms 9816 KB Output is correct
54 Correct 8 ms 9820 KB Output is correct
55 Correct 8 ms 10020 KB Output is correct
56 Correct 7 ms 9820 KB Output is correct
57 Correct 3 ms 9816 KB Output is correct
58 Correct 5 ms 9976 KB Output is correct
59 Correct 2 ms 9820 KB Output is correct
60 Correct 2 ms 9820 KB Output is correct
61 Correct 7 ms 9908 KB Output is correct
62 Execution timed out 5076 ms 25680 KB Time limit exceeded
63 Halted 0 ms 0 KB -