답안 #789173

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
789173 2023-07-21T07:01:26 Z 반딧불(#10041) Tourism (JOI23_tourism) C++17
10 / 100
186 ms 53484 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 eventCnt, eventIn[100002], eventOut[100002], event[200002];
int par[100002], LCADB[100002][20], depth[100002];
int arr[100002];

int spsR[200002][18];

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

int p[200002];
inline int getDist(int x, int y){
    if(in[x]>in[y]) swap(x,y);
    int td=depth[x]+depth[y];
    x=eventOut[x], y=eventIn[y];
    int d=p[y-x+1];
    return td - 2 * min(spsR[x][d], spsR[y-(1<<d)+1][d]);
}

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+n; i++) spsR[i][0] = event[i];
    for(int d=1; d<18; d++) for(int i=1; i<=n+n; i++)
        spsR[i][d] = min(spsR[i][d-1], (i+(1<<(d-1))) > n+n ? INT_MAX : spsR[i+(1<<(d-1))][d-1]);
    for(int i=1; i<=n+n+1; i++){
        int j=0;
        while((1<<(j+1))<=i) j++;
        p[i]=j;
    }

    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:117:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |     scanf("%d %d %d", &n, &k, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:120:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
tourism.cpp:135:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  135 |         scanf("%d", &arr[i]);
      |         ~~~~~^~~~~~~~~~~~~~~
tourism.cpp:139:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |         scanf("%d %d", &query[i].l, &query[i].r);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 3 ms 2660 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 2 ms 2660 KB Output is correct
8 Correct 2 ms 2788 KB Output is correct
9 Correct 4 ms 2772 KB Output is correct
10 Correct 4 ms 2772 KB Output is correct
11 Correct 4 ms 2772 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 3 ms 2772 KB Output is correct
15 Correct 4 ms 2772 KB Output is correct
16 Correct 4 ms 2772 KB Output is correct
17 Correct 4 ms 2792 KB Output is correct
18 Correct 4 ms 2800 KB Output is correct
19 Correct 4 ms 2772 KB Output is correct
20 Correct 4 ms 2772 KB Output is correct
21 Correct 4 ms 2772 KB Output is correct
22 Correct 4 ms 2772 KB Output is correct
23 Correct 4 ms 2900 KB Output is correct
24 Correct 4 ms 2788 KB Output is correct
25 Correct 4 ms 2800 KB Output is correct
26 Correct 5 ms 2772 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2784 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 3 ms 2660 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 2 ms 2660 KB Output is correct
8 Correct 2 ms 2788 KB Output is correct
9 Correct 4 ms 2772 KB Output is correct
10 Correct 4 ms 2772 KB Output is correct
11 Correct 4 ms 2772 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 3 ms 2772 KB Output is correct
15 Correct 4 ms 2772 KB Output is correct
16 Correct 4 ms 2772 KB Output is correct
17 Correct 4 ms 2792 KB Output is correct
18 Correct 4 ms 2800 KB Output is correct
19 Correct 4 ms 2772 KB Output is correct
20 Correct 4 ms 2772 KB Output is correct
21 Correct 4 ms 2772 KB Output is correct
22 Correct 4 ms 2772 KB Output is correct
23 Correct 4 ms 2900 KB Output is correct
24 Correct 4 ms 2788 KB Output is correct
25 Correct 4 ms 2800 KB Output is correct
26 Correct 5 ms 2772 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2784 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
30 Correct 22 ms 3028 KB Output is correct
31 Correct 33 ms 3028 KB Output is correct
32 Correct 34 ms 3252 KB Output is correct
33 Correct 34 ms 3244 KB Output is correct
34 Correct 33 ms 3244 KB Output is correct
35 Correct 6 ms 3248 KB Output is correct
36 Correct 6 ms 3156 KB Output is correct
37 Correct 6 ms 3156 KB Output is correct
38 Correct 34 ms 3284 KB Output is correct
39 Correct 34 ms 3352 KB Output is correct
40 Correct 39 ms 3284 KB Output is correct
41 Correct 7 ms 3380 KB Output is correct
42 Correct 6 ms 3284 KB Output is correct
43 Correct 7 ms 3348 KB Output is correct
44 Correct 35 ms 3296 KB Output is correct
45 Correct 41 ms 3276 KB Output is correct
46 Correct 34 ms 3300 KB Output is correct
47 Correct 6 ms 3284 KB Output is correct
48 Correct 6 ms 3284 KB Output is correct
49 Correct 6 ms 3184 KB Output is correct
50 Correct 34 ms 3252 KB Output is correct
51 Correct 35 ms 3136 KB Output is correct
52 Correct 33 ms 3156 KB Output is correct
53 Correct 37 ms 3184 KB Output is correct
54 Correct 39 ms 3156 KB Output is correct
55 Correct 33 ms 3244 KB Output is correct
56 Correct 7 ms 2772 KB Output is correct
57 Correct 3 ms 3156 KB Output is correct
58 Correct 3 ms 3188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2656 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 7 ms 2668 KB Output is correct
4 Runtime error 76 ms 41672 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 69 ms 15800 KB Output is correct
3 Correct 116 ms 17184 KB Output is correct
4 Correct 101 ms 19244 KB Output is correct
5 Correct 107 ms 26720 KB Output is correct
6 Correct 127 ms 26648 KB Output is correct
7 Correct 146 ms 26724 KB Output is correct
8 Correct 146 ms 26732 KB Output is correct
9 Correct 151 ms 26724 KB Output is correct
10 Correct 186 ms 26728 KB Output is correct
11 Correct 164 ms 26780 KB Output is correct
12 Correct 158 ms 26756 KB Output is correct
13 Correct 151 ms 26916 KB Output is correct
14 Correct 164 ms 27272 KB Output is correct
15 Runtime error 119 ms 53484 KB Execution killed with signal 11
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2720 KB Output is correct
3 Correct 7 ms 2784 KB Output is correct
4 Runtime error 76 ms 34800 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 3 ms 2660 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 2 ms 2660 KB Output is correct
8 Correct 2 ms 2788 KB Output is correct
9 Correct 4 ms 2772 KB Output is correct
10 Correct 4 ms 2772 KB Output is correct
11 Correct 4 ms 2772 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 3 ms 2772 KB Output is correct
15 Correct 4 ms 2772 KB Output is correct
16 Correct 4 ms 2772 KB Output is correct
17 Correct 4 ms 2792 KB Output is correct
18 Correct 4 ms 2800 KB Output is correct
19 Correct 4 ms 2772 KB Output is correct
20 Correct 4 ms 2772 KB Output is correct
21 Correct 4 ms 2772 KB Output is correct
22 Correct 4 ms 2772 KB Output is correct
23 Correct 4 ms 2900 KB Output is correct
24 Correct 4 ms 2788 KB Output is correct
25 Correct 4 ms 2800 KB Output is correct
26 Correct 5 ms 2772 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2784 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
30 Correct 22 ms 3028 KB Output is correct
31 Correct 33 ms 3028 KB Output is correct
32 Correct 34 ms 3252 KB Output is correct
33 Correct 34 ms 3244 KB Output is correct
34 Correct 33 ms 3244 KB Output is correct
35 Correct 6 ms 3248 KB Output is correct
36 Correct 6 ms 3156 KB Output is correct
37 Correct 6 ms 3156 KB Output is correct
38 Correct 34 ms 3284 KB Output is correct
39 Correct 34 ms 3352 KB Output is correct
40 Correct 39 ms 3284 KB Output is correct
41 Correct 7 ms 3380 KB Output is correct
42 Correct 6 ms 3284 KB Output is correct
43 Correct 7 ms 3348 KB Output is correct
44 Correct 35 ms 3296 KB Output is correct
45 Correct 41 ms 3276 KB Output is correct
46 Correct 34 ms 3300 KB Output is correct
47 Correct 6 ms 3284 KB Output is correct
48 Correct 6 ms 3284 KB Output is correct
49 Correct 6 ms 3184 KB Output is correct
50 Correct 34 ms 3252 KB Output is correct
51 Correct 35 ms 3136 KB Output is correct
52 Correct 33 ms 3156 KB Output is correct
53 Correct 37 ms 3184 KB Output is correct
54 Correct 39 ms 3156 KB Output is correct
55 Correct 33 ms 3244 KB Output is correct
56 Correct 7 ms 2772 KB Output is correct
57 Correct 3 ms 3156 KB Output is correct
58 Correct 3 ms 3188 KB Output is correct
59 Correct 2 ms 2656 KB Output is correct
60 Correct 2 ms 2644 KB Output is correct
61 Correct 7 ms 2668 KB Output is correct
62 Runtime error 76 ms 41672 KB Execution killed with signal 11
63 Halted 0 ms 0 KB -