답안 #1037648

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1037648 2024-07-29T06:26:21 Z 정희우(#10983) Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 23004 KB
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>

using namespace std;
using lint = long long;
using intp = pair<int,int>;
using vint = vector<int>;

const int MAX_N=100010;
const int RN=317;
const int BN=20;

struct Qry
{
    int i,s,e;
    bool operator < (const Qry &qq) const
    {
        return (s/RN==qq.s/RN) ? e<qq.e : s<qq.s;
    }
};

int n,m,q;
vint edge[MAX_N];
int idx[MAX_N];
int ridx[MAX_N];
int depth[MAX_N];
int ac[MAX_N][BN];
int vert[MAX_N];
int seg[MAX_N<<2];
Qry qry[MAX_N];
int ans[MAX_N];

void dfs(int v,int p,int &c)
{
    ridx[c]=v;
    idx[v]=c++;
    depth[v]=depth[p]+1;
    ac[v][0]=p;
    for(int i=1;i<BN;i++)
        ac[v][i]=ac[ac[v][i-1]][i-1];
    for(auto v0 : edge[v])
        if(v0!=p)
            dfs(v0,v,c);
}

int lca(int u,int v)
{
    if(depth[u]>depth[v])swap(u,v);
    for(int i=BN-1;i>=0;i--)
        if(depth[ac[v][i]]>=depth[u])v=ac[v][i];
    if(u==v)
        return v;
    for(int i=BN-1;i>=0;i--)
        if(ac[v][i]!=ac[u][i])
        {
            v=ac[v][i];
            u=ac[u][i];
        }
    return ac[v][0];    
}

void init_seg(int i,int s,int e)
{
    if(s+1==e)
    {
        seg[i]=vert[s];
        return;
    }
    init_seg(i<<1,s,(s+e)>>1);
    init_seg(i<<1|1,(s+e)>>1,e);
    seg[i]=lca(seg[i<<1],seg[i<<1|1]);
}

int find_(int i,int s,int e,int l,int r)
{
    if(s>=r || e<=l)return -1;
    if(l<=s && e<=r)return seg[i];
    int lv=find_(i<<1,s,(s+e)>>1,l,r);
    int rv=find_(i<<1|1,(s+e)>>1,e,l,r);
    if(lv==-1 || rv==-1)return max(lv,rv);
    return lca(lv,rv);
}

int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin >> n >> m >> q;
    for(int i=1;i<n;i++)
    {
        int u,v;
        cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    int id=0;
    dfs(1,0,id);
    for(int i=0;i<m;i++)
        cin >> vert[i];
    init_seg(1,0,m);
    for(int i=0;i<q;i++)
    {
        cin >> qry[i].s >> qry[i].e;qry[i].s--;
        qry[i].i=i;
    }
    sort(qry,qry+q);
    multiset<int> st;
    for(int i=0,c=0,s=0,e=0;i<q;i++)
    {
        while(s>qry[i].s)
        {
            s--;
            int v=vert[s];
            auto it=st.lower_bound(idx[v]);
            int d=0;
            if(it!=st.end())d=max(d,depth[lca(v,ridx[*it])]);
            if(it!=st.begin())d=max(d,depth[lca(v,ridx[*prev(it)])]);
            c+=depth[v]-d;
            st.insert(idx[v]);
        }
        while(e<qry[i].e)
        {
            int v=vert[e];
            auto it=st.lower_bound(idx[v]);
            int d=0;
            if(it!=st.end())d=max(d,depth[lca(v,ridx[*it])]);
            if(it!=st.begin())d=max(d,depth[lca(v,ridx[*prev(it)])]);
            c+=depth[v]-d;
            st.insert(idx[v]);
            e++;
        }
        while(s<qry[i].s)
        {
            int v=vert[s];
            auto it=st.lower_bound(idx[v]);
            st.erase(it);
            it=st.lower_bound(idx[v]);
            int d=0;
            if(it!=st.end())d=max(d,depth[lca(v,ridx[*it])]);
            if(it!=st.begin())d=max(d,depth[lca(v,ridx[*prev(it)])]);
            c-=depth[v]-d;
            s++;
        }
        while(e>qry[i].e)
        {
            e--;
            int v=vert[e];
            auto it=st.lower_bound(idx[v]);
            st.erase(it);
            it=st.lower_bound(idx[v]);
            int d=0;
            if(it!=st.end())d=max(d,depth[lca(v,ridx[*it])]);
            if(it!=st.begin())d=max(d,depth[lca(v,ridx[*prev(it)])]);
            c-=depth[v]-d;
        }
        ans[qry[i].i]=c-depth[find_(1,0,m,qry[i].s,qry[i].e)]+1;
    }
    for(int i=0;i<q;i++)
        cout << ans[i] << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 1 ms 8796 KB Output is correct
3 Correct 1 ms 8796 KB Output is correct
4 Correct 5 ms 8796 KB Output is correct
5 Correct 4 ms 8796 KB Output is correct
6 Correct 4 ms 8796 KB Output is correct
7 Correct 5 ms 8796 KB Output is correct
8 Correct 4 ms 8796 KB Output is correct
9 Correct 8 ms 9028 KB Output is correct
10 Correct 9 ms 8796 KB Output is correct
11 Correct 7 ms 9036 KB Output is correct
12 Correct 2 ms 8796 KB Output is correct
13 Correct 2 ms 8796 KB Output is correct
14 Correct 2 ms 8796 KB Output is correct
15 Correct 7 ms 8988 KB Output is correct
16 Correct 6 ms 8792 KB Output is correct
17 Correct 7 ms 8792 KB Output is correct
18 Correct 9 ms 8796 KB Output is correct
19 Correct 6 ms 8796 KB Output is correct
20 Correct 6 ms 9032 KB Output is correct
21 Correct 8 ms 8796 KB Output is correct
22 Correct 8 ms 8796 KB Output is correct
23 Correct 7 ms 8796 KB Output is correct
24 Correct 10 ms 8796 KB Output is correct
25 Correct 9 ms 8792 KB Output is correct
26 Correct 9 ms 9032 KB Output is correct
27 Correct 4 ms 8796 KB Output is correct
28 Correct 1 ms 8792 KB Output is correct
29 Correct 1 ms 8796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 1 ms 8796 KB Output is correct
3 Correct 1 ms 8796 KB Output is correct
4 Correct 5 ms 8796 KB Output is correct
5 Correct 4 ms 8796 KB Output is correct
6 Correct 4 ms 8796 KB Output is correct
7 Correct 5 ms 8796 KB Output is correct
8 Correct 4 ms 8796 KB Output is correct
9 Correct 8 ms 9028 KB Output is correct
10 Correct 9 ms 8796 KB Output is correct
11 Correct 7 ms 9036 KB Output is correct
12 Correct 2 ms 8796 KB Output is correct
13 Correct 2 ms 8796 KB Output is correct
14 Correct 2 ms 8796 KB Output is correct
15 Correct 7 ms 8988 KB Output is correct
16 Correct 6 ms 8792 KB Output is correct
17 Correct 7 ms 8792 KB Output is correct
18 Correct 9 ms 8796 KB Output is correct
19 Correct 6 ms 8796 KB Output is correct
20 Correct 6 ms 9032 KB Output is correct
21 Correct 8 ms 8796 KB Output is correct
22 Correct 8 ms 8796 KB Output is correct
23 Correct 7 ms 8796 KB Output is correct
24 Correct 10 ms 8796 KB Output is correct
25 Correct 9 ms 8792 KB Output is correct
26 Correct 9 ms 9032 KB Output is correct
27 Correct 4 ms 8796 KB Output is correct
28 Correct 1 ms 8792 KB Output is correct
29 Correct 1 ms 8796 KB Output is correct
30 Correct 47 ms 9052 KB Output is correct
31 Correct 62 ms 9092 KB Output is correct
32 Correct 71 ms 9112 KB Output is correct
33 Correct 75 ms 9052 KB Output is correct
34 Correct 70 ms 9048 KB Output is correct
35 Correct 12 ms 9052 KB Output is correct
36 Correct 12 ms 9052 KB Output is correct
37 Correct 12 ms 9304 KB Output is correct
38 Correct 54 ms 9048 KB Output is correct
39 Correct 54 ms 9212 KB Output is correct
40 Correct 55 ms 9224 KB Output is correct
41 Correct 11 ms 9052 KB Output is correct
42 Correct 13 ms 9260 KB Output is correct
43 Correct 13 ms 9052 KB Output is correct
44 Correct 58 ms 9184 KB Output is correct
45 Correct 56 ms 9168 KB Output is correct
46 Correct 56 ms 9052 KB Output is correct
47 Correct 11 ms 9192 KB Output is correct
48 Correct 11 ms 9188 KB Output is correct
49 Correct 11 ms 9048 KB Output is correct
50 Correct 73 ms 9168 KB Output is correct
51 Correct 74 ms 9052 KB Output is correct
52 Correct 72 ms 9048 KB Output is correct
53 Correct 72 ms 9048 KB Output is correct
54 Correct 75 ms 9052 KB Output is correct
55 Correct 71 ms 9052 KB Output is correct
56 Correct 31 ms 9052 KB Output is correct
57 Correct 2 ms 9052 KB Output is correct
58 Correct 3 ms 9052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 3 ms 8796 KB Output is correct
3 Correct 29 ms 9048 KB Output is correct
4 Execution timed out 5069 ms 21500 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 74 ms 14884 KB Output is correct
3 Correct 112 ms 15020 KB Output is correct
4 Correct 85 ms 17244 KB Output is correct
5 Correct 108 ms 23004 KB Output is correct
6 Correct 157 ms 21460 KB Output is correct
7 Correct 161 ms 19328 KB Output is correct
8 Correct 160 ms 18516 KB Output is correct
9 Correct 154 ms 18380 KB Output is correct
10 Correct 151 ms 18396 KB Output is correct
11 Correct 149 ms 18512 KB Output is correct
12 Correct 135 ms 18256 KB Output is correct
13 Correct 132 ms 18392 KB Output is correct
14 Correct 140 ms 18516 KB Output is correct
15 Correct 106 ms 18680 KB Output is correct
16 Correct 141 ms 20496 KB Output is correct
17 Correct 145 ms 20568 KB Output is correct
18 Correct 146 ms 20412 KB Output is correct
19 Correct 109 ms 22872 KB Output is correct
20 Correct 138 ms 22968 KB Output is correct
21 Correct 147 ms 20152 KB Output is correct
22 Correct 152 ms 19296 KB Output is correct
23 Correct 158 ms 18768 KB Output is correct
24 Correct 163 ms 18372 KB Output is correct
25 Correct 160 ms 18448 KB Output is correct
26 Correct 182 ms 18636 KB Output is correct
27 Correct 185 ms 18260 KB Output is correct
28 Correct 183 ms 18260 KB Output is correct
29 Correct 186 ms 18184 KB Output is correct
30 Correct 187 ms 18264 KB Output is correct
31 Correct 169 ms 18516 KB Output is correct
32 Correct 149 ms 18512 KB Output is correct
33 Correct 149 ms 18516 KB Output is correct
34 Correct 136 ms 18772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 4 ms 9024 KB Output is correct
3 Correct 31 ms 9052 KB Output is correct
4 Execution timed out 5060 ms 19028 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 1 ms 8796 KB Output is correct
3 Correct 1 ms 8796 KB Output is correct
4 Correct 5 ms 8796 KB Output is correct
5 Correct 4 ms 8796 KB Output is correct
6 Correct 4 ms 8796 KB Output is correct
7 Correct 5 ms 8796 KB Output is correct
8 Correct 4 ms 8796 KB Output is correct
9 Correct 8 ms 9028 KB Output is correct
10 Correct 9 ms 8796 KB Output is correct
11 Correct 7 ms 9036 KB Output is correct
12 Correct 2 ms 8796 KB Output is correct
13 Correct 2 ms 8796 KB Output is correct
14 Correct 2 ms 8796 KB Output is correct
15 Correct 7 ms 8988 KB Output is correct
16 Correct 6 ms 8792 KB Output is correct
17 Correct 7 ms 8792 KB Output is correct
18 Correct 9 ms 8796 KB Output is correct
19 Correct 6 ms 8796 KB Output is correct
20 Correct 6 ms 9032 KB Output is correct
21 Correct 8 ms 8796 KB Output is correct
22 Correct 8 ms 8796 KB Output is correct
23 Correct 7 ms 8796 KB Output is correct
24 Correct 10 ms 8796 KB Output is correct
25 Correct 9 ms 8792 KB Output is correct
26 Correct 9 ms 9032 KB Output is correct
27 Correct 4 ms 8796 KB Output is correct
28 Correct 1 ms 8792 KB Output is correct
29 Correct 1 ms 8796 KB Output is correct
30 Correct 47 ms 9052 KB Output is correct
31 Correct 62 ms 9092 KB Output is correct
32 Correct 71 ms 9112 KB Output is correct
33 Correct 75 ms 9052 KB Output is correct
34 Correct 70 ms 9048 KB Output is correct
35 Correct 12 ms 9052 KB Output is correct
36 Correct 12 ms 9052 KB Output is correct
37 Correct 12 ms 9304 KB Output is correct
38 Correct 54 ms 9048 KB Output is correct
39 Correct 54 ms 9212 KB Output is correct
40 Correct 55 ms 9224 KB Output is correct
41 Correct 11 ms 9052 KB Output is correct
42 Correct 13 ms 9260 KB Output is correct
43 Correct 13 ms 9052 KB Output is correct
44 Correct 58 ms 9184 KB Output is correct
45 Correct 56 ms 9168 KB Output is correct
46 Correct 56 ms 9052 KB Output is correct
47 Correct 11 ms 9192 KB Output is correct
48 Correct 11 ms 9188 KB Output is correct
49 Correct 11 ms 9048 KB Output is correct
50 Correct 73 ms 9168 KB Output is correct
51 Correct 74 ms 9052 KB Output is correct
52 Correct 72 ms 9048 KB Output is correct
53 Correct 72 ms 9048 KB Output is correct
54 Correct 75 ms 9052 KB Output is correct
55 Correct 71 ms 9052 KB Output is correct
56 Correct 31 ms 9052 KB Output is correct
57 Correct 2 ms 9052 KB Output is correct
58 Correct 3 ms 9052 KB Output is correct
59 Correct 1 ms 8796 KB Output is correct
60 Correct 3 ms 8796 KB Output is correct
61 Correct 29 ms 9048 KB Output is correct
62 Execution timed out 5069 ms 21500 KB Time limit exceeded
63 Halted 0 ms 0 KB -