답안 #789162

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
789162 2023-07-21T06:49:55 Z 반딧불(#10041) Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 19880 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int B = 300;

int n, k, q;
vector<int> link[100002];

int inCnt, in[100002], out[100002], idx[100002];
int par[100002], LCADB[100002][20], depth[100002];
int arr[100002];

void dfs(int x, int p=-1){
    in[x] = ++inCnt;
    idx[in[x]] = x;
    for(auto y: link[x]){
        if(y==p) continue;
        par[y] = x, depth[y] = depth[x] + 1;
        dfs(y, x);
    }
    out[x] = inCnt;
}

int getLCA(int x, int y){
    if(depth[x] > depth[y]) swap(x, y);
    for(int d=19; d>=0; d--) if((depth[y] - depth[x]) & (1<<d)) y = LCADB[y][d];
    if(x==y) return x;
    for(int d=19; d>=0; d--) if(LCADB[x][d] != LCADB[y][d]) x = LCADB[x][d], y = LCADB[y][d];
    return par[x];
}

inline int getDist(int x, int y){
    return depth[x] + depth[y] - depth[getLCA(x, y)] * 2;
}

struct Query{
    int l, r, idx;
    Query(){}
    Query(int l, int r, int idx): l(l), r(r), idx(idx){}
    bool operator<(const Query &nxt)const{
        if(l/B != nxt.l/B) return l/B < nxt.l/B;
        return r < nxt.r;
    }
} query[100002];

struct segTree{
    int tree[1<<18];

    inline void add(int i, int l, int r, int x, int v){
        tree[i] += v;
        if(l==r) return;
        int m = (l+r)>>1;
        if(x<=m) add(i*2, l, m, x, v);
        else add(i*2+1, m+1, r, x, v);
    }

    inline int nxt(int i, int l, int r, int x){
        if(r<x||!tree[i]) return r+1;
        if(l==r) return l;
        int m = (l+r)>>1;
        int v = nxt(i*2,l,m,x);
        return v==m+1 ? nxt(i*2+1, m+1, r, x) : v;
    }

    inline int prv(int i, int l, int r, int x){
        if(x<l||!tree[i]) return l-1;
        if(l==r) return l;
        int m = (l+r)>>1;
        int v = prv(i*2+1, m+1, r, x);
        return v==m ? prv(i*2, l, m, x) : v;
    }
} tree;

int ans[100002];
int distSum, cnt;

inline void add(int x){
    if(cnt==0) cnt++, tree.add(1, 1, n, arr[x], 1);
    else if(cnt==1){
        cnt++;
        int tmp = arr[x]==1 ? 0 : tree.prv(1, 1, n, arr[x]);
        if(tmp==0) tmp = tree.nxt(1, 1, n, arr[x]);
        distSum += getDist(idx[arr[x]], idx[tmp]) * 2;
        tree.add(1, 1, n, arr[x], 1);
    }
    else{
        cnt++;
        int prv = arr[x]==1 ? 0 : tree.prv(1, 1, n, arr[x]);
        if(prv==0) prv = tree.prv(1, 1, n, n);
        int nxt = arr[x]==n ? n+1 : tree.nxt(1, 1, n, arr[x]);
        if(nxt==n+1) nxt = tree.nxt(1, 1, n, 1);
        distSum += getDist(idx[arr[x]], idx[prv]) + getDist(idx[arr[x]], idx[nxt]) - getDist(idx[prv], idx[nxt]);
        tree.add(1, 1, n, arr[x], 1);
    }
}

inline void del(int x){
    if(cnt<=1) cnt--, tree.add(1, 1, n, arr[x], -1), distSum = 0;
    else{
        cnt--;
        tree.add(1, 1, n, arr[x], -1);
        int prv = arr[x]==1 ? 0 : tree.prv(1, 1, n, arr[x]);
        if(prv==0) prv = tree.prv(1, 1, n, n);
        int nxt = arr[x]==n ? n+1 : tree.nxt(1, 1, n, arr[x]);
        if(nxt==n+1) nxt = tree.nxt(1, 1, n, 1);
        distSum -= getDist(idx[arr[x]], idx[prv]) + getDist(idx[arr[x]], idx[nxt]) - getDist(idx[prv], idx[nxt]);
    }
}

int main(){
    scanf("%d %d %d", &n, &k, &q);
    for(int i=1; i<n; i++){
        int x, y;
        scanf("%d %d", &x, &y);
        link[x].push_back(y);
        link[y].push_back(x);
    }
    dfs(1);
    for(int i=1; i<=n; i++) LCADB[i][0] = par[i];
    for(int d=1; d<20; d++) for(int i=1; i<=n; i++) LCADB[i][d] = LCADB[LCADB[i][d-1]][d-1];

    for(int i=1; i<=k; i++){
        scanf("%d", &arr[i]);
        arr[i] = in[arr[i]];
    }
    for(int i=1; i<=q; i++){
        scanf("%d %d", &query[i].l, &query[i].r);
        query[i].idx = i;
    }
    sort(query+1, query+q+1);

    int l = 1, r = 0;
    for(int i=1; i<=q; i++){
        while(r < query[i].r) add(++r);
        while(l > query[i].l) add(--l);
        while(r > query[i].r) del(r--);
        while(l < query[i].l) del(l++);
        ans[query[i].idx] = distSum / 2 + 1;
    }

    for(int i=1; i<=q; i++){
        printf("%d\n", ans[i]);
    }
}

Compilation message

tourism.cpp: In function 'int main()':
tourism.cpp:113:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |     scanf("%d %d %d", &n, &k, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:116:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
tourism.cpp:125:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |         scanf("%d", &arr[i]);
      |         ~~~~~^~~~~~~~~~~~~~~
tourism.cpp:129:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  129 |         scanf("%d %d", &query[i].l, &query[i].r);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2660 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 4 ms 2652 KB Output is correct
5 Correct 4 ms 2660 KB Output is correct
6 Correct 4 ms 2656 KB Output is correct
7 Correct 5 ms 2644 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 7 ms 2644 KB Output is correct
10 Correct 8 ms 2644 KB Output is correct
11 Correct 7 ms 2656 KB Output is correct
12 Correct 2 ms 2656 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 3 ms 2644 KB Output is correct
15 Correct 6 ms 2756 KB Output is correct
16 Correct 5 ms 2644 KB Output is correct
17 Correct 6 ms 2644 KB Output is correct
18 Correct 6 ms 2644 KB Output is correct
19 Correct 7 ms 2644 KB Output is correct
20 Correct 8 ms 2772 KB Output is correct
21 Correct 6 ms 2644 KB Output is correct
22 Correct 6 ms 2644 KB Output is correct
23 Correct 6 ms 2660 KB Output is correct
24 Correct 7 ms 2644 KB Output is correct
25 Correct 7 ms 2644 KB Output is correct
26 Correct 6 ms 2652 KB Output is correct
27 Correct 7 ms 2644 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2660 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 4 ms 2652 KB Output is correct
5 Correct 4 ms 2660 KB Output is correct
6 Correct 4 ms 2656 KB Output is correct
7 Correct 5 ms 2644 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 7 ms 2644 KB Output is correct
10 Correct 8 ms 2644 KB Output is correct
11 Correct 7 ms 2656 KB Output is correct
12 Correct 2 ms 2656 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 3 ms 2644 KB Output is correct
15 Correct 6 ms 2756 KB Output is correct
16 Correct 5 ms 2644 KB Output is correct
17 Correct 6 ms 2644 KB Output is correct
18 Correct 6 ms 2644 KB Output is correct
19 Correct 7 ms 2644 KB Output is correct
20 Correct 8 ms 2772 KB Output is correct
21 Correct 6 ms 2644 KB Output is correct
22 Correct 6 ms 2644 KB Output is correct
23 Correct 6 ms 2660 KB Output is correct
24 Correct 7 ms 2644 KB Output is correct
25 Correct 7 ms 2644 KB Output is correct
26 Correct 6 ms 2652 KB Output is correct
27 Correct 7 ms 2644 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 40 ms 2936 KB Output is correct
31 Correct 48 ms 2952 KB Output is correct
32 Correct 71 ms 3028 KB Output is correct
33 Correct 63 ms 3048 KB Output is correct
34 Correct 69 ms 3028 KB Output is correct
35 Correct 8 ms 3028 KB Output is correct
36 Correct 8 ms 3024 KB Output is correct
37 Correct 8 ms 2968 KB Output is correct
38 Correct 64 ms 3124 KB Output is correct
39 Correct 72 ms 3132 KB Output is correct
40 Correct 53 ms 3128 KB Output is correct
41 Correct 9 ms 3156 KB Output is correct
42 Correct 11 ms 3156 KB Output is correct
43 Correct 7 ms 3028 KB Output is correct
44 Correct 69 ms 3076 KB Output is correct
45 Correct 56 ms 3056 KB Output is correct
46 Correct 64 ms 3088 KB Output is correct
47 Correct 8 ms 3028 KB Output is correct
48 Correct 9 ms 3072 KB Output is correct
49 Correct 7 ms 3000 KB Output is correct
50 Correct 52 ms 3052 KB Output is correct
51 Correct 51 ms 3036 KB Output is correct
52 Correct 58 ms 3036 KB Output is correct
53 Correct 61 ms 3048 KB Output is correct
54 Correct 54 ms 3040 KB Output is correct
55 Correct 53 ms 3040 KB Output is correct
56 Correct 14 ms 2644 KB Output is correct
57 Correct 4 ms 2908 KB Output is correct
58 Correct 4 ms 3040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2648 KB Output is correct
2 Correct 6 ms 2700 KB Output is correct
3 Correct 13 ms 2644 KB Output is correct
4 Execution timed out 5049 ms 16844 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 90 ms 11504 KB Output is correct
3 Correct 144 ms 12304 KB Output is correct
4 Correct 126 ms 13816 KB Output is correct
5 Correct 114 ms 18076 KB Output is correct
6 Correct 139 ms 18160 KB Output is correct
7 Correct 168 ms 18172 KB Output is correct
8 Correct 189 ms 18328 KB Output is correct
9 Correct 236 ms 18228 KB Output is correct
10 Correct 182 ms 18188 KB Output is correct
11 Correct 195 ms 18252 KB Output is correct
12 Correct 182 ms 18156 KB Output is correct
13 Correct 178 ms 18252 KB Output is correct
14 Correct 188 ms 18560 KB Output is correct
15 Correct 161 ms 19832 KB Output is correct
16 Correct 181 ms 18252 KB Output is correct
17 Correct 170 ms 18176 KB Output is correct
18 Correct 188 ms 18292 KB Output is correct
19 Correct 99 ms 18020 KB Output is correct
20 Correct 126 ms 18024 KB Output is correct
21 Correct 168 ms 18096 KB Output is correct
22 Correct 169 ms 18016 KB Output is correct
23 Correct 171 ms 18072 KB Output is correct
24 Correct 195 ms 18036 KB Output is correct
25 Correct 237 ms 18188 KB Output is correct
26 Correct 229 ms 18132 KB Output is correct
27 Correct 238 ms 18104 KB Output is correct
28 Correct 247 ms 18096 KB Output is correct
29 Correct 240 ms 18124 KB Output is correct
30 Correct 252 ms 18212 KB Output is correct
31 Correct 250 ms 18276 KB Output is correct
32 Correct 255 ms 18396 KB Output is correct
33 Correct 243 ms 18952 KB Output is correct
34 Correct 187 ms 19880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2644 KB Output is correct
3 Correct 13 ms 2748 KB Output is correct
4 Execution timed out 5047 ms 14024 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2660 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 4 ms 2652 KB Output is correct
5 Correct 4 ms 2660 KB Output is correct
6 Correct 4 ms 2656 KB Output is correct
7 Correct 5 ms 2644 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 7 ms 2644 KB Output is correct
10 Correct 8 ms 2644 KB Output is correct
11 Correct 7 ms 2656 KB Output is correct
12 Correct 2 ms 2656 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 3 ms 2644 KB Output is correct
15 Correct 6 ms 2756 KB Output is correct
16 Correct 5 ms 2644 KB Output is correct
17 Correct 6 ms 2644 KB Output is correct
18 Correct 6 ms 2644 KB Output is correct
19 Correct 7 ms 2644 KB Output is correct
20 Correct 8 ms 2772 KB Output is correct
21 Correct 6 ms 2644 KB Output is correct
22 Correct 6 ms 2644 KB Output is correct
23 Correct 6 ms 2660 KB Output is correct
24 Correct 7 ms 2644 KB Output is correct
25 Correct 7 ms 2644 KB Output is correct
26 Correct 6 ms 2652 KB Output is correct
27 Correct 7 ms 2644 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 40 ms 2936 KB Output is correct
31 Correct 48 ms 2952 KB Output is correct
32 Correct 71 ms 3028 KB Output is correct
33 Correct 63 ms 3048 KB Output is correct
34 Correct 69 ms 3028 KB Output is correct
35 Correct 8 ms 3028 KB Output is correct
36 Correct 8 ms 3024 KB Output is correct
37 Correct 8 ms 2968 KB Output is correct
38 Correct 64 ms 3124 KB Output is correct
39 Correct 72 ms 3132 KB Output is correct
40 Correct 53 ms 3128 KB Output is correct
41 Correct 9 ms 3156 KB Output is correct
42 Correct 11 ms 3156 KB Output is correct
43 Correct 7 ms 3028 KB Output is correct
44 Correct 69 ms 3076 KB Output is correct
45 Correct 56 ms 3056 KB Output is correct
46 Correct 64 ms 3088 KB Output is correct
47 Correct 8 ms 3028 KB Output is correct
48 Correct 9 ms 3072 KB Output is correct
49 Correct 7 ms 3000 KB Output is correct
50 Correct 52 ms 3052 KB Output is correct
51 Correct 51 ms 3036 KB Output is correct
52 Correct 58 ms 3036 KB Output is correct
53 Correct 61 ms 3048 KB Output is correct
54 Correct 54 ms 3040 KB Output is correct
55 Correct 53 ms 3040 KB Output is correct
56 Correct 14 ms 2644 KB Output is correct
57 Correct 4 ms 2908 KB Output is correct
58 Correct 4 ms 3040 KB Output is correct
59 Correct 2 ms 2648 KB Output is correct
60 Correct 6 ms 2700 KB Output is correct
61 Correct 13 ms 2644 KB Output is correct
62 Execution timed out 5049 ms 16844 KB Time limit exceeded
63 Halted 0 ms 0 KB -