답안 #1016841

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1016841 2024-07-08T13:14:12 Z alexdd Tourism (JOI23_tourism) C++17
100 / 100
494 ms 34640 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 aib[100015];
int qry_aib(int poz)
{
    poz+=2;
    int aux=0;
    for(int i=poz;i<=m+5;i+=(i&(-i)))
        aux += aib[i];
    return aux;
}
void upd_aib(int poz, int newv)
{
    poz+=2;
    for(int i=poz;i>0;i-=(i&(-i)))
        aib[i] += newv;
}

int val[100005];
set<pair<pair<int,int>,int>> s;
void baga(int le, int ri, int t)
{
    auto it = prev(s.lower_bound({{le+1,-1},-1}));

    pair<pair<int,int>,int> tole,tori;
    tole = *it;
    tole.first.second = le-1;
    while(it!=s.end() && (*it).first.first <= ri)
    {
        tori = *it;
        upd_aib((*it).second,-((*it).first.second - (*it).first.first + 1));
        it = s.erase(it);
    }
    tori.first.first = ri+1;
    s.insert({{le,ri},t});
    upd_aib(t,ri-le+1);
    if(tole.first.first <= tole.first.second)
    {
        s.insert(tole);
        upd_aib(tole.second, tole.first.second-tole.first.first+1);
    }
    if(tori.first.first <= tori.first.second)
    {
        s.insert(tori);
        upd_aib(tori.second, tori.first.second-tori.first.first+1);
    }
    /*for(int i=le;i<=ri;i++)
    {
        upd_aib(val[i],-1);
        val[i]=t;
    }
    upd_aib(t,ri-le+1);*/
}

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});
    }
    s.insert({{0,n+1},0});
    upd_aib(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] = qry_aib(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 9816 KB Output is correct
3 Correct 2 ms 9652 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 9820 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 9816 KB Output is correct
15 Correct 3 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 3 ms 9816 KB Output is correct
20 Correct 2 ms 9812 KB Output is correct
21 Correct 2 ms 9888 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 3 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
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9816 KB Output is correct
2 Correct 2 ms 9816 KB Output is correct
3 Correct 2 ms 9652 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 9820 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 9816 KB Output is correct
15 Correct 3 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 3 ms 9816 KB Output is correct
20 Correct 2 ms 9812 KB Output is correct
21 Correct 2 ms 9888 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 3 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 5 ms 9820 KB Output is correct
31 Correct 5 ms 9820 KB Output is correct
32 Correct 6 ms 9904 KB Output is correct
33 Correct 5 ms 10076 KB Output is correct
34 Correct 6 ms 9908 KB Output is correct
35 Correct 5 ms 9816 KB Output is correct
36 Correct 5 ms 9820 KB Output is correct
37 Correct 5 ms 9808 KB Output is correct
38 Correct 4 ms 10076 KB Output is correct
39 Correct 4 ms 10328 KB Output is correct
40 Correct 7 ms 10076 KB Output is correct
41 Correct 4 ms 10076 KB Output is correct
42 Correct 4 ms 10076 KB Output is correct
43 Correct 5 ms 9816 KB Output is correct
44 Correct 5 ms 9960 KB Output is correct
45 Correct 4 ms 9816 KB Output is correct
46 Correct 4 ms 9820 KB Output is correct
47 Correct 4 ms 9820 KB Output is correct
48 Correct 4 ms 9820 KB Output is correct
49 Correct 4 ms 9820 KB Output is correct
50 Correct 5 ms 10076 KB Output is correct
51 Correct 5 ms 9972 KB Output is correct
52 Correct 4 ms 10076 KB Output is correct
53 Correct 4 ms 10076 KB Output is correct
54 Correct 5 ms 10076 KB Output is correct
55 Correct 6 ms 10076 KB Output is correct
56 Correct 3 ms 9820 KB Output is correct
57 Correct 3 ms 9820 KB Output is correct
58 Correct 5 ms 9860 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 9908 KB Output is correct
4 Correct 111 ms 24068 KB Output is correct
5 Correct 105 ms 27372 KB Output is correct
6 Correct 127 ms 29632 KB Output is correct
7 Correct 155 ms 31428 KB Output is correct
8 Correct 149 ms 34388 KB Output is correct
9 Correct 190 ms 34384 KB Output is correct
10 Correct 167 ms 34640 KB Output is correct
11 Correct 159 ms 34388 KB Output is correct
12 Correct 135 ms 33540 KB Output is correct
13 Correct 147 ms 33616 KB Output is correct
14 Correct 131 ms 33672 KB Output is correct
15 Correct 69 ms 29648 KB Output is correct
16 Correct 145 ms 34052 KB Output is correct
17 Correct 90 ms 15188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9816 KB Output is correct
2 Correct 130 ms 19192 KB Output is correct
3 Correct 199 ms 20488 KB Output is correct
4 Correct 173 ms 22988 KB Output is correct
5 Correct 262 ms 25428 KB Output is correct
6 Correct 266 ms 25424 KB Output is correct
7 Correct 251 ms 25424 KB Output is correct
8 Correct 255 ms 25424 KB Output is correct
9 Correct 264 ms 25456 KB Output is correct
10 Correct 251 ms 25428 KB Output is correct
11 Correct 273 ms 25596 KB Output is correct
12 Correct 261 ms 25680 KB Output is correct
13 Correct 293 ms 25848 KB Output is correct
14 Correct 280 ms 26572 KB Output is correct
15 Correct 330 ms 28788 KB Output is correct
16 Correct 257 ms 25936 KB Output is correct
17 Correct 293 ms 25680 KB Output is correct
18 Correct 264 ms 25896 KB Output is correct
19 Correct 162 ms 21468 KB Output is correct
20 Correct 159 ms 21528 KB Output is correct
21 Correct 175 ms 21668 KB Output is correct
22 Correct 171 ms 21588 KB Output is correct
23 Correct 169 ms 21656 KB Output is correct
24 Correct 166 ms 21500 KB Output is correct
25 Correct 162 ms 21636 KB Output is correct
26 Correct 164 ms 21588 KB Output is correct
27 Correct 159 ms 21584 KB Output is correct
28 Correct 180 ms 21588 KB Output is correct
29 Correct 165 ms 21584 KB Output is correct
30 Correct 172 ms 21840 KB Output is correct
31 Correct 172 ms 21856 KB Output is correct
32 Correct 181 ms 22228 KB Output is correct
33 Correct 208 ms 23380 KB Output is correct
34 Correct 227 ms 24968 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 3 ms 9820 KB Output is correct
4 Correct 372 ms 22964 KB Output is correct
5 Correct 374 ms 22864 KB Output is correct
6 Correct 437 ms 27200 KB Output is correct
7 Correct 447 ms 28500 KB Output is correct
8 Correct 494 ms 28636 KB Output is correct
9 Correct 462 ms 28496 KB Output is correct
10 Correct 435 ms 28496 KB Output is correct
11 Correct 468 ms 28504 KB Output is correct
12 Correct 484 ms 28500 KB Output is correct
13 Correct 453 ms 28556 KB Output is correct
14 Correct 115 ms 13840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9816 KB Output is correct
2 Correct 2 ms 9816 KB Output is correct
3 Correct 2 ms 9652 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 9820 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 9816 KB Output is correct
15 Correct 3 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 3 ms 9816 KB Output is correct
20 Correct 2 ms 9812 KB Output is correct
21 Correct 2 ms 9888 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 3 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 5 ms 9820 KB Output is correct
31 Correct 5 ms 9820 KB Output is correct
32 Correct 6 ms 9904 KB Output is correct
33 Correct 5 ms 10076 KB Output is correct
34 Correct 6 ms 9908 KB Output is correct
35 Correct 5 ms 9816 KB Output is correct
36 Correct 5 ms 9820 KB Output is correct
37 Correct 5 ms 9808 KB Output is correct
38 Correct 4 ms 10076 KB Output is correct
39 Correct 4 ms 10328 KB Output is correct
40 Correct 7 ms 10076 KB Output is correct
41 Correct 4 ms 10076 KB Output is correct
42 Correct 4 ms 10076 KB Output is correct
43 Correct 5 ms 9816 KB Output is correct
44 Correct 5 ms 9960 KB Output is correct
45 Correct 4 ms 9816 KB Output is correct
46 Correct 4 ms 9820 KB Output is correct
47 Correct 4 ms 9820 KB Output is correct
48 Correct 4 ms 9820 KB Output is correct
49 Correct 4 ms 9820 KB Output is correct
50 Correct 5 ms 10076 KB Output is correct
51 Correct 5 ms 9972 KB Output is correct
52 Correct 4 ms 10076 KB Output is correct
53 Correct 4 ms 10076 KB Output is correct
54 Correct 5 ms 10076 KB Output is correct
55 Correct 6 ms 10076 KB Output is correct
56 Correct 3 ms 9820 KB Output is correct
57 Correct 3 ms 9820 KB Output is correct
58 Correct 5 ms 9860 KB Output is correct
59 Correct 2 ms 9816 KB Output is correct
60 Correct 2 ms 9820 KB Output is correct
61 Correct 3 ms 9908 KB Output is correct
62 Correct 111 ms 24068 KB Output is correct
63 Correct 105 ms 27372 KB Output is correct
64 Correct 127 ms 29632 KB Output is correct
65 Correct 155 ms 31428 KB Output is correct
66 Correct 149 ms 34388 KB Output is correct
67 Correct 190 ms 34384 KB Output is correct
68 Correct 167 ms 34640 KB Output is correct
69 Correct 159 ms 34388 KB Output is correct
70 Correct 135 ms 33540 KB Output is correct
71 Correct 147 ms 33616 KB Output is correct
72 Correct 131 ms 33672 KB Output is correct
73 Correct 69 ms 29648 KB Output is correct
74 Correct 145 ms 34052 KB Output is correct
75 Correct 90 ms 15188 KB Output is correct
76 Correct 2 ms 9816 KB Output is correct
77 Correct 130 ms 19192 KB Output is correct
78 Correct 199 ms 20488 KB Output is correct
79 Correct 173 ms 22988 KB Output is correct
80 Correct 262 ms 25428 KB Output is correct
81 Correct 266 ms 25424 KB Output is correct
82 Correct 251 ms 25424 KB Output is correct
83 Correct 255 ms 25424 KB Output is correct
84 Correct 264 ms 25456 KB Output is correct
85 Correct 251 ms 25428 KB Output is correct
86 Correct 273 ms 25596 KB Output is correct
87 Correct 261 ms 25680 KB Output is correct
88 Correct 293 ms 25848 KB Output is correct
89 Correct 280 ms 26572 KB Output is correct
90 Correct 330 ms 28788 KB Output is correct
91 Correct 257 ms 25936 KB Output is correct
92 Correct 293 ms 25680 KB Output is correct
93 Correct 264 ms 25896 KB Output is correct
94 Correct 162 ms 21468 KB Output is correct
95 Correct 159 ms 21528 KB Output is correct
96 Correct 175 ms 21668 KB Output is correct
97 Correct 171 ms 21588 KB Output is correct
98 Correct 169 ms 21656 KB Output is correct
99 Correct 166 ms 21500 KB Output is correct
100 Correct 162 ms 21636 KB Output is correct
101 Correct 164 ms 21588 KB Output is correct
102 Correct 159 ms 21584 KB Output is correct
103 Correct 180 ms 21588 KB Output is correct
104 Correct 165 ms 21584 KB Output is correct
105 Correct 172 ms 21840 KB Output is correct
106 Correct 172 ms 21856 KB Output is correct
107 Correct 181 ms 22228 KB Output is correct
108 Correct 208 ms 23380 KB Output is correct
109 Correct 227 ms 24968 KB Output is correct
110 Correct 2 ms 9820 KB Output is correct
111 Correct 2 ms 9820 KB Output is correct
112 Correct 3 ms 9820 KB Output is correct
113 Correct 372 ms 22964 KB Output is correct
114 Correct 374 ms 22864 KB Output is correct
115 Correct 437 ms 27200 KB Output is correct
116 Correct 447 ms 28500 KB Output is correct
117 Correct 494 ms 28636 KB Output is correct
118 Correct 462 ms 28496 KB Output is correct
119 Correct 435 ms 28496 KB Output is correct
120 Correct 468 ms 28504 KB Output is correct
121 Correct 484 ms 28500 KB Output is correct
122 Correct 453 ms 28556 KB Output is correct
123 Correct 115 ms 13840 KB Output is correct
124 Correct 309 ms 30556 KB Output is correct
125 Correct 235 ms 27404 KB Output is correct
126 Correct 348 ms 30828 KB Output is correct
127 Correct 354 ms 30804 KB Output is correct
128 Correct 359 ms 30804 KB Output is correct
129 Correct 333 ms 30956 KB Output is correct
130 Correct 350 ms 30804 KB Output is correct
131 Correct 176 ms 33364 KB Output is correct
132 Correct 183 ms 34388 KB Output is correct
133 Correct 187 ms 30416 KB Output is correct
134 Correct 244 ms 27196 KB Output is correct
135 Correct 241 ms 26960 KB Output is correct
136 Correct 251 ms 27148 KB Output is correct
137 Correct 219 ms 32316 KB Output is correct
138 Correct 215 ms 32456 KB Output is correct
139 Correct 215 ms 32332 KB Output is correct
140 Correct 202 ms 32460 KB Output is correct
141 Correct 260 ms 32420 KB Output is correct
142 Correct 210 ms 32316 KB Output is correct
143 Correct 72 ms 21972 KB Output is correct
144 Correct 345 ms 30696 KB Output is correct