답안 #934628

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
934628 2024-02-27T17:56:33 Z velislavgarkov Tourism (JOI23_tourism) C++14
100 / 100
3994 ms 43428 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
#define endl '\n'
const int MAXN=1e5+10;
const int BUCK=450;
struct Query {
    int l, r;
    int ind;
    bool friend operator < (Query a, Query b) {
        return a.r>b.r;
    }
};
struct Sight {
    int x;
    int ind;
    bool friend operator < (Sight a, Sight b) {
        return a.x<b.x;
    }
};
Sight c[MAXN];
Query que[MAXN];
vector <int> v[MAXN];
vector <int> arr, isl;
int fe[MAXN], le[MAXN], in[MAXN], dep[MAXN];
int sp[2*MAXN][20], stepen[2*MAXN];
stack <pair <int*,int> > st;
int pos[MAXN], lef[MAXN], ri[MAXN], res;
vector <int> sq[MAXN/BUCK+10];
int ans[MAXN];
int n, m, q, cnt;
bool cmpi(int a, int b) {
    return in[a]<in[b];
}
bool cmpc(Sight a, Sight b) {
    return in[a.x]<in[b.x];
}
void dfs(int x, int p) {
    in[x]=cnt;
    cnt++;
    fe[x]=arr.size();
    for (auto i:v[x]) {
        if (i==p) continue;
        arr.push_back(x);
        dep[i]=dep[x]+1;
        dfs(i,x);
    }
    le[x]=arr.size();
    arr.push_back(x);
}
void sparce_table() {
    int kn=arr.size();
    for (int i=0;i<kn;i++) sp[i][0]=arr[i];
    for (int i=1;(1<<i)<=kn;i++) {
        for (int j=0;j+(1<<i)-1<kn;j++) {
            int i1, i2;
            i1=sp[j][i-1];
            i2=sp[j + (1 << (i-1))][i-1];
            if (dep[i1]<dep[i2]) sp[j][i]=i1;
            else sp[j][i]=i2;
        }
    }
    int st=0;
    for (int i=1;i<=kn;i++) {
        if ((1 << (st+1))<i) st++;
        stepen[i]=st;
    }
}
int lca(int l, int r) {
    int st=stepen[r-l+1];
    int i1, i2;
    i1=sp[l][st];
    i2=sp[r - (1<<st)+1][st];
    if (dep[i1]<dep[i2]) return i1;
    return i2;
}
int getdist(int a, int b) {
    if (in[a]>in[b]) swap(a,b);
    return dep[a]+dep[b]-2*dep[lca(fe[a],fe[b])];
}
void undo() {
    for (int times=0;times<3;times++) {
        if (st.empty()) st.pop();
        (*st.top().first)=st.top().second;
        st.pop();
    }
}
void remove(int i) {
    i=pos[i];
    st.push({&res,res});
    st.push({ri+lef[i],ri[lef[i]]});
    st.push({lef+ri[i],lef[ri[i]]});

    res-=getdist(c[lef[i]].x,c[i].x);
    res-=getdist(c[i].x,c[ri[i]].x);
    res+=getdist(c[lef[i]].x,c[ri[i]].x);
    ri[lef[i]]=ri[i];
    lef[ri[i]]=lef[i];
}
int main () {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int a, b;
    cin >> n >> m >> q;
    for (int i=0;i<n-1;i++) {
        cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    cnt=1;
    dfs(1,-1);
    sparce_table();
    for (int i=0;i<m;i++) {
        cin >> c[i].x;
        c[i].ind=i;
    }
    sort(c,c+m,cmpc);
    for (int i=0;i<m;i++) {
        pos[c[i].ind]=i;
        lef[i]=i-1;
        ri[i]=i+1;
        res+=getdist(c[i].x,c[(i+1)%m].x);
    }
    lef[0]=m-1;
    ri[m-1]=0;
    for (int i=0;i<q;i++) {
        cin >> que[i].l >> que[i].r;
        que[i].l--; que[i].r--;
        que[i].ind=i;
    }
    sort(que,que+q);
    for (int i=0;i<q;i++) {
        if (que[i].l==que[i].r) ans[que[i].ind]=1;
        else sq[que[i].l/BUCK].push_back(i);
    }
    int curl, curr;
    for (int i=0;i*BUCK<m;i++) {
        curr=m-1;
        for (auto j:sq[i]) {
            while (curr>que[j].r) {
                remove(curr);
                curr--;
            }
            curl=i*BUCK;
            while (curl<que[j].l) {
                remove(curl);
                curl++;
            }
            ans[que[j].ind]=res/2+1;
            while (curl>i*BUCK) {
                undo();
                curl--;
            }
        }
        while (curr<m-1) {
            curr++;
            undo();
        }
        if ((i+1)*BUCK<m) {
            for (int j=i*BUCK;j<(i+1)*BUCK;j++) remove(j);
        }
    }
    for (int i=0;i<q;i++) cout << ans[i] << endl;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10600 KB Output is correct
4 Correct 3 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 3 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 4 ms 10588 KB Output is correct
10 Correct 3 ms 10584 KB Output is correct
11 Correct 3 ms 10588 KB Output is correct
12 Correct 2 ms 10588 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 3 ms 10588 KB Output is correct
15 Correct 4 ms 10588 KB Output is correct
16 Correct 3 ms 10588 KB Output is correct
17 Correct 4 ms 10588 KB Output is correct
18 Correct 3 ms 10588 KB Output is correct
19 Correct 3 ms 10588 KB Output is correct
20 Correct 3 ms 10588 KB Output is correct
21 Correct 3 ms 10588 KB Output is correct
22 Correct 3 ms 10588 KB Output is correct
23 Correct 3 ms 10588 KB Output is correct
24 Correct 4 ms 10764 KB Output is correct
25 Correct 3 ms 10588 KB Output is correct
26 Correct 3 ms 10588 KB Output is correct
27 Correct 4 ms 10588 KB Output is correct
28 Correct 2 ms 10588 KB Output is correct
29 Correct 2 ms 10588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10600 KB Output is correct
4 Correct 3 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 3 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 4 ms 10588 KB Output is correct
10 Correct 3 ms 10584 KB Output is correct
11 Correct 3 ms 10588 KB Output is correct
12 Correct 2 ms 10588 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 3 ms 10588 KB Output is correct
15 Correct 4 ms 10588 KB Output is correct
16 Correct 3 ms 10588 KB Output is correct
17 Correct 4 ms 10588 KB Output is correct
18 Correct 3 ms 10588 KB Output is correct
19 Correct 3 ms 10588 KB Output is correct
20 Correct 3 ms 10588 KB Output is correct
21 Correct 3 ms 10588 KB Output is correct
22 Correct 3 ms 10588 KB Output is correct
23 Correct 3 ms 10588 KB Output is correct
24 Correct 4 ms 10764 KB Output is correct
25 Correct 3 ms 10588 KB Output is correct
26 Correct 3 ms 10588 KB Output is correct
27 Correct 4 ms 10588 KB Output is correct
28 Correct 2 ms 10588 KB Output is correct
29 Correct 2 ms 10588 KB Output is correct
30 Correct 17 ms 10888 KB Output is correct
31 Correct 23 ms 10876 KB Output is correct
32 Correct 25 ms 10844 KB Output is correct
33 Correct 26 ms 10840 KB Output is correct
34 Correct 26 ms 10844 KB Output is correct
35 Correct 5 ms 10840 KB Output is correct
36 Correct 5 ms 10844 KB Output is correct
37 Correct 5 ms 10928 KB Output is correct
38 Correct 25 ms 11028 KB Output is correct
39 Correct 26 ms 11032 KB Output is correct
40 Correct 34 ms 11184 KB Output is correct
41 Correct 5 ms 10844 KB Output is correct
42 Correct 5 ms 10960 KB Output is correct
43 Correct 5 ms 10844 KB Output is correct
44 Correct 28 ms 10952 KB Output is correct
45 Correct 27 ms 10844 KB Output is correct
46 Correct 26 ms 10992 KB Output is correct
47 Correct 5 ms 10844 KB Output is correct
48 Correct 5 ms 10844 KB Output is correct
49 Correct 6 ms 10844 KB Output is correct
50 Correct 28 ms 10928 KB Output is correct
51 Correct 27 ms 10960 KB Output is correct
52 Correct 27 ms 10840 KB Output is correct
53 Correct 25 ms 10840 KB Output is correct
54 Correct 31 ms 10840 KB Output is correct
55 Correct 27 ms 10844 KB Output is correct
56 Correct 25 ms 10840 KB Output is correct
57 Correct 2 ms 10584 KB Output is correct
58 Correct 3 ms 11096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 3 ms 10588 KB Output is correct
3 Correct 24 ms 10860 KB Output is correct
4 Correct 1986 ms 30600 KB Output is correct
5 Correct 1341 ms 35296 KB Output is correct
6 Correct 1682 ms 37844 KB Output is correct
7 Correct 2924 ms 40560 KB Output is correct
8 Correct 2785 ms 43424 KB Output is correct
9 Correct 2942 ms 43428 KB Output is correct
10 Correct 2889 ms 43416 KB Output is correct
11 Correct 2829 ms 43412 KB Output is correct
12 Correct 846 ms 42936 KB Output is correct
13 Correct 827 ms 42936 KB Output is correct
14 Correct 828 ms 42940 KB Output is correct
15 Correct 50 ms 35480 KB Output is correct
16 Correct 92 ms 42280 KB Output is correct
17 Correct 1710 ms 18892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 374 ms 24252 KB Output is correct
3 Correct 1045 ms 26428 KB Output is correct
4 Correct 582 ms 27588 KB Output is correct
5 Correct 76 ms 32708 KB Output is correct
6 Correct 93 ms 34484 KB Output is correct
7 Correct 149 ms 34292 KB Output is correct
8 Correct 306 ms 34244 KB Output is correct
9 Correct 874 ms 34436 KB Output is correct
10 Correct 1669 ms 34500 KB Output is correct
11 Correct 1587 ms 34572 KB Output is correct
12 Correct 1597 ms 34784 KB Output is correct
13 Correct 1879 ms 34500 KB Output is correct
14 Correct 2168 ms 35100 KB Output is correct
15 Correct 91 ms 36292 KB Output is correct
16 Correct 689 ms 34712 KB Output is correct
17 Correct 694 ms 34760 KB Output is correct
18 Correct 752 ms 34504 KB Output is correct
19 Correct 74 ms 34364 KB Output is correct
20 Correct 71 ms 34244 KB Output is correct
21 Correct 91 ms 34284 KB Output is correct
22 Correct 126 ms 34264 KB Output is correct
23 Correct 191 ms 34380 KB Output is correct
24 Correct 344 ms 34500 KB Output is correct
25 Correct 628 ms 34452 KB Output is correct
26 Correct 1211 ms 34504 KB Output is correct
27 Correct 1270 ms 34512 KB Output is correct
28 Correct 1452 ms 34480 KB Output is correct
29 Correct 1319 ms 34500 KB Output is correct
30 Correct 1474 ms 34616 KB Output is correct
31 Correct 1559 ms 34940 KB Output is correct
32 Correct 1699 ms 34912 KB Output is correct
33 Correct 1888 ms 35432 KB Output is correct
34 Correct 89 ms 36296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 3 ms 10588 KB Output is correct
3 Correct 24 ms 10840 KB Output is correct
4 Correct 2701 ms 27968 KB Output is correct
5 Correct 2830 ms 27952 KB Output is correct
6 Correct 3503 ms 33592 KB Output is correct
7 Correct 3810 ms 34292 KB Output is correct
8 Correct 3887 ms 37160 KB Output is correct
9 Correct 3769 ms 37152 KB Output is correct
10 Correct 3712 ms 37164 KB Output is correct
11 Correct 3727 ms 37152 KB Output is correct
12 Correct 3720 ms 37180 KB Output is correct
13 Correct 3801 ms 37152 KB Output is correct
14 Correct 1724 ms 19144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10600 KB Output is correct
4 Correct 3 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 3 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 4 ms 10588 KB Output is correct
10 Correct 3 ms 10584 KB Output is correct
11 Correct 3 ms 10588 KB Output is correct
12 Correct 2 ms 10588 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 3 ms 10588 KB Output is correct
15 Correct 4 ms 10588 KB Output is correct
16 Correct 3 ms 10588 KB Output is correct
17 Correct 4 ms 10588 KB Output is correct
18 Correct 3 ms 10588 KB Output is correct
19 Correct 3 ms 10588 KB Output is correct
20 Correct 3 ms 10588 KB Output is correct
21 Correct 3 ms 10588 KB Output is correct
22 Correct 3 ms 10588 KB Output is correct
23 Correct 3 ms 10588 KB Output is correct
24 Correct 4 ms 10764 KB Output is correct
25 Correct 3 ms 10588 KB Output is correct
26 Correct 3 ms 10588 KB Output is correct
27 Correct 4 ms 10588 KB Output is correct
28 Correct 2 ms 10588 KB Output is correct
29 Correct 2 ms 10588 KB Output is correct
30 Correct 17 ms 10888 KB Output is correct
31 Correct 23 ms 10876 KB Output is correct
32 Correct 25 ms 10844 KB Output is correct
33 Correct 26 ms 10840 KB Output is correct
34 Correct 26 ms 10844 KB Output is correct
35 Correct 5 ms 10840 KB Output is correct
36 Correct 5 ms 10844 KB Output is correct
37 Correct 5 ms 10928 KB Output is correct
38 Correct 25 ms 11028 KB Output is correct
39 Correct 26 ms 11032 KB Output is correct
40 Correct 34 ms 11184 KB Output is correct
41 Correct 5 ms 10844 KB Output is correct
42 Correct 5 ms 10960 KB Output is correct
43 Correct 5 ms 10844 KB Output is correct
44 Correct 28 ms 10952 KB Output is correct
45 Correct 27 ms 10844 KB Output is correct
46 Correct 26 ms 10992 KB Output is correct
47 Correct 5 ms 10844 KB Output is correct
48 Correct 5 ms 10844 KB Output is correct
49 Correct 6 ms 10844 KB Output is correct
50 Correct 28 ms 10928 KB Output is correct
51 Correct 27 ms 10960 KB Output is correct
52 Correct 27 ms 10840 KB Output is correct
53 Correct 25 ms 10840 KB Output is correct
54 Correct 31 ms 10840 KB Output is correct
55 Correct 27 ms 10844 KB Output is correct
56 Correct 25 ms 10840 KB Output is correct
57 Correct 2 ms 10584 KB Output is correct
58 Correct 3 ms 11096 KB Output is correct
59 Correct 2 ms 10588 KB Output is correct
60 Correct 3 ms 10588 KB Output is correct
61 Correct 24 ms 10860 KB Output is correct
62 Correct 1986 ms 30600 KB Output is correct
63 Correct 1341 ms 35296 KB Output is correct
64 Correct 1682 ms 37844 KB Output is correct
65 Correct 2924 ms 40560 KB Output is correct
66 Correct 2785 ms 43424 KB Output is correct
67 Correct 2942 ms 43428 KB Output is correct
68 Correct 2889 ms 43416 KB Output is correct
69 Correct 2829 ms 43412 KB Output is correct
70 Correct 846 ms 42936 KB Output is correct
71 Correct 827 ms 42936 KB Output is correct
72 Correct 828 ms 42940 KB Output is correct
73 Correct 50 ms 35480 KB Output is correct
74 Correct 92 ms 42280 KB Output is correct
75 Correct 1710 ms 18892 KB Output is correct
76 Correct 2 ms 10584 KB Output is correct
77 Correct 374 ms 24252 KB Output is correct
78 Correct 1045 ms 26428 KB Output is correct
79 Correct 582 ms 27588 KB Output is correct
80 Correct 76 ms 32708 KB Output is correct
81 Correct 93 ms 34484 KB Output is correct
82 Correct 149 ms 34292 KB Output is correct
83 Correct 306 ms 34244 KB Output is correct
84 Correct 874 ms 34436 KB Output is correct
85 Correct 1669 ms 34500 KB Output is correct
86 Correct 1587 ms 34572 KB Output is correct
87 Correct 1597 ms 34784 KB Output is correct
88 Correct 1879 ms 34500 KB Output is correct
89 Correct 2168 ms 35100 KB Output is correct
90 Correct 91 ms 36292 KB Output is correct
91 Correct 689 ms 34712 KB Output is correct
92 Correct 694 ms 34760 KB Output is correct
93 Correct 752 ms 34504 KB Output is correct
94 Correct 74 ms 34364 KB Output is correct
95 Correct 71 ms 34244 KB Output is correct
96 Correct 91 ms 34284 KB Output is correct
97 Correct 126 ms 34264 KB Output is correct
98 Correct 191 ms 34380 KB Output is correct
99 Correct 344 ms 34500 KB Output is correct
100 Correct 628 ms 34452 KB Output is correct
101 Correct 1211 ms 34504 KB Output is correct
102 Correct 1270 ms 34512 KB Output is correct
103 Correct 1452 ms 34480 KB Output is correct
104 Correct 1319 ms 34500 KB Output is correct
105 Correct 1474 ms 34616 KB Output is correct
106 Correct 1559 ms 34940 KB Output is correct
107 Correct 1699 ms 34912 KB Output is correct
108 Correct 1888 ms 35432 KB Output is correct
109 Correct 89 ms 36296 KB Output is correct
110 Correct 1 ms 10588 KB Output is correct
111 Correct 3 ms 10588 KB Output is correct
112 Correct 24 ms 10840 KB Output is correct
113 Correct 2701 ms 27968 KB Output is correct
114 Correct 2830 ms 27952 KB Output is correct
115 Correct 3503 ms 33592 KB Output is correct
116 Correct 3810 ms 34292 KB Output is correct
117 Correct 3887 ms 37160 KB Output is correct
118 Correct 3769 ms 37152 KB Output is correct
119 Correct 3712 ms 37164 KB Output is correct
120 Correct 3727 ms 37152 KB Output is correct
121 Correct 3720 ms 37180 KB Output is correct
122 Correct 3801 ms 37152 KB Output is correct
123 Correct 1724 ms 19144 KB Output is correct
124 Correct 3319 ms 36176 KB Output is correct
125 Correct 1905 ms 33740 KB Output is correct
126 Correct 3927 ms 37268 KB Output is correct
127 Correct 3994 ms 37344 KB Output is correct
128 Correct 3955 ms 37292 KB Output is correct
129 Correct 3934 ms 37280 KB Output is correct
130 Correct 3862 ms 37320 KB Output is correct
131 Correct 3215 ms 42472 KB Output is correct
132 Correct 3397 ms 43332 KB Output is correct
133 Correct 3353 ms 40388 KB Output is correct
134 Correct 3368 ms 37592 KB Output is correct
135 Correct 3479 ms 37292 KB Output is correct
136 Correct 3507 ms 37288 KB Output is correct
137 Correct 3677 ms 37448 KB Output is correct
138 Correct 3619 ms 37448 KB Output is correct
139 Correct 3587 ms 37568 KB Output is correct
140 Correct 3551 ms 37448 KB Output is correct
141 Correct 3666 ms 37564 KB Output is correct
142 Correct 3725 ms 37560 KB Output is correct
143 Correct 57 ms 29384 KB Output is correct
144 Correct 106 ms 36292 KB Output is correct