답안 #944919

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
944919 2024-03-13T08:04:01 Z phoenix0423 Tourism (JOI23_tourism) C++17
100 / 100
4581 ms 91272 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0);
#pragma GCC optimize("Ofast","unroll-loops")
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define int long long
#define lowbit(x) x&-x
const int maxn = 3e5 + 5;
const int INF = 1e9 + 7;
int n, m, q;
vector<int> adj[maxn];
int dep[maxn], in[maxn], dfn = 0;
int arr[maxn][19];
int id[maxn];

void dfs(int pos, int prev){
    in[pos] = ++dfn;
    id[dfn] = pos;
    arr[dfn][0] = dep[pos];
    for(auto x : adj[pos]){
        if(x == prev) continue;
        dep[x] = dep[pos] + 1;
        dfs(x, pos);
        arr[++dfn][0] = dep[pos];
    }
    arr[++dfn][0] = dep[pos];
}

int lca(int a, int b){
    if(a > b) swap(a, b);
    int len = __lg(b - a + 1);
    return min(arr[a][len], arr[b - (1 << len) + 1][len]);
}

struct info{
    int l, r, id;
    info(){}
    info(int _l, int _r, int _id) : l(_l), r(_r), id(_id){}
    bool operator < (const info& other) const{
        return r < other.r;
    };
};
vector<info> b[500];
const int block = 320;
int ans[maxn];
int c[maxn], loc[maxn];
int cur = 0;
int path(int a, int b){
    return dep[a] + dep[b] - 2 * lca(in[a], in[b]);
}
struct node{
    int l, r, val;
} st[maxn];

struct rollback{
    int l, r, m;
};
stack<rollback> stk;
void roll_back(){
    auto [l, r, m] = stk.top(); stk.pop();
    cur -= path(st[l].val, st[r].val);
    cur += path(st[l].val, st[m].val) + path(st[m].val, st[r].val);
    st[l].r = m, st[r].l = m;
}
void del(int x){
    x = loc[x];
    int prv = st[x].l, nxt = st[x].r;
    stk.push({prv, nxt, x});
    cur -= path(st[prv].val, st[x].val) + path(st[x].val, st[nxt].val);
    cur += path(st[prv].val, st[nxt].val);
    st[prv].r = nxt, st[nxt].l = prv;
}

signed main(void){
    fastio;
    cin>>n>>m>>q;
    for(int i = 0; i < n - 1; i++){
        int a, b;
        cin>>a>>b;
        a--, b--;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    dfs(0, 0);
    for(int i = 1; (1 << i) <= dfn; i++){
        for(int j = 0; j + (1 << i) - 1 <= dfn; j++){
            arr[j][i] = min(arr[j][i - 1], arr[j + (1 << (i - 1))][i - 1]);
        }
    }
    for(int i = 0; i < m; i++) cin>>c[i], c[i] --;
    for(int i = 0; i < q; i++){
        int l, r;
        cin>>l>>r;
        l--, r--;
        b[l / block].pb(info(l, r, i));
    }
    for(int i = 0; i <= m / block; i++){
        sort(b[i].begin(), b[i].end());
    }
    vector<pll> cpy;
    for(int i = 0; i < m; i++) cpy.eb(c[i], i);
    sort(cpy.begin(), cpy.end(), [&](pll a, pll b){ return in[a.f] < in[b.f];});
    for(int i = 0; i < m; i++){
        auto [val, id] = cpy[i];
        loc[id] = i;
        st[i].val = val;
        if(!i) st[i].l = m - 1;
        else st[i].l = i - 1;
        if(i == m - 1) st[i].r = 0;
        else st[i].r = i + 1;
    }
    for(int i = 0; i < m; i++) cur += path(st[i].val, st[st[i].r].val);
    int cl = 0, cr = m - 1;
    for(int i = 0; i <= m / block; i++){
        if(!b[i].size()) continue;
        while(cr < m - 1){
            roll_back();
            cr++;
        }
        while(cl < i * block){
            del(cl++);
        }
        while(stk.size()) stk.pop();
        for(auto [l, r, id] : b[i]){
            while(r > cr){
                roll_back();
                cr++;
            }
            while(r < cr){
                del(cr--);
            }
            while(cl < l) del(cl++);
            ans[id] = cur;
            while(cl > i * block){
                roll_back();
                cl--;
            }
        }
    }
    for(int i = 0; i < q; i++) cout<<ans[i] / 2 + 1<<"\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 19544 KB Output is correct
2 Correct 4 ms 19748 KB Output is correct
3 Correct 3 ms 19548 KB Output is correct
4 Correct 4 ms 19804 KB Output is correct
5 Correct 4 ms 20056 KB Output is correct
6 Correct 4 ms 19800 KB Output is correct
7 Correct 4 ms 19800 KB Output is correct
8 Correct 5 ms 19804 KB Output is correct
9 Correct 6 ms 21852 KB Output is correct
10 Correct 5 ms 21852 KB Output is correct
11 Correct 6 ms 21848 KB Output is correct
12 Correct 4 ms 21852 KB Output is correct
13 Correct 4 ms 21852 KB Output is correct
14 Correct 4 ms 21848 KB Output is correct
15 Correct 5 ms 21852 KB Output is correct
16 Correct 5 ms 21852 KB Output is correct
17 Correct 5 ms 21696 KB Output is correct
18 Correct 5 ms 21852 KB Output is correct
19 Correct 5 ms 21848 KB Output is correct
20 Correct 5 ms 21852 KB Output is correct
21 Correct 5 ms 21692 KB Output is correct
22 Correct 5 ms 21936 KB Output is correct
23 Correct 5 ms 21852 KB Output is correct
24 Correct 5 ms 21816 KB Output is correct
25 Correct 5 ms 21848 KB Output is correct
26 Correct 5 ms 21852 KB Output is correct
27 Correct 4 ms 19800 KB Output is correct
28 Correct 4 ms 21848 KB Output is correct
29 Correct 6 ms 21852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 19544 KB Output is correct
2 Correct 4 ms 19748 KB Output is correct
3 Correct 3 ms 19548 KB Output is correct
4 Correct 4 ms 19804 KB Output is correct
5 Correct 4 ms 20056 KB Output is correct
6 Correct 4 ms 19800 KB Output is correct
7 Correct 4 ms 19800 KB Output is correct
8 Correct 5 ms 19804 KB Output is correct
9 Correct 6 ms 21852 KB Output is correct
10 Correct 5 ms 21852 KB Output is correct
11 Correct 6 ms 21848 KB Output is correct
12 Correct 4 ms 21852 KB Output is correct
13 Correct 4 ms 21852 KB Output is correct
14 Correct 4 ms 21848 KB Output is correct
15 Correct 5 ms 21852 KB Output is correct
16 Correct 5 ms 21852 KB Output is correct
17 Correct 5 ms 21696 KB Output is correct
18 Correct 5 ms 21852 KB Output is correct
19 Correct 5 ms 21848 KB Output is correct
20 Correct 5 ms 21852 KB Output is correct
21 Correct 5 ms 21692 KB Output is correct
22 Correct 5 ms 21936 KB Output is correct
23 Correct 5 ms 21852 KB Output is correct
24 Correct 5 ms 21816 KB Output is correct
25 Correct 5 ms 21848 KB Output is correct
26 Correct 5 ms 21852 KB Output is correct
27 Correct 4 ms 19800 KB Output is correct
28 Correct 4 ms 21848 KB Output is correct
29 Correct 6 ms 21852 KB Output is correct
30 Correct 13 ms 21852 KB Output is correct
31 Correct 17 ms 22008 KB Output is correct
32 Correct 19 ms 22128 KB Output is correct
33 Correct 19 ms 22108 KB Output is correct
34 Correct 23 ms 22104 KB Output is correct
35 Correct 7 ms 22104 KB Output is correct
36 Correct 7 ms 22104 KB Output is correct
37 Correct 6 ms 22108 KB Output is correct
38 Correct 20 ms 22108 KB Output is correct
39 Correct 22 ms 22108 KB Output is correct
40 Correct 19 ms 22108 KB Output is correct
41 Correct 8 ms 22108 KB Output is correct
42 Correct 6 ms 22108 KB Output is correct
43 Correct 7 ms 22104 KB Output is correct
44 Correct 19 ms 22168 KB Output is correct
45 Correct 19 ms 22108 KB Output is correct
46 Correct 22 ms 22364 KB Output is correct
47 Correct 6 ms 22104 KB Output is correct
48 Correct 7 ms 22104 KB Output is correct
49 Correct 7 ms 21948 KB Output is correct
50 Correct 18 ms 22108 KB Output is correct
51 Correct 19 ms 22112 KB Output is correct
52 Correct 19 ms 22108 KB Output is correct
53 Correct 18 ms 22108 KB Output is correct
54 Correct 19 ms 22132 KB Output is correct
55 Correct 18 ms 22108 KB Output is correct
56 Correct 15 ms 19804 KB Output is correct
57 Correct 5 ms 21852 KB Output is correct
58 Correct 20 ms 22104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19548 KB Output is correct
2 Correct 6 ms 19804 KB Output is correct
3 Correct 17 ms 19804 KB Output is correct
4 Correct 1835 ms 59648 KB Output is correct
5 Correct 1210 ms 73532 KB Output is correct
6 Correct 1894 ms 79532 KB Output is correct
7 Correct 2905 ms 91144 KB Output is correct
8 Correct 3195 ms 91224 KB Output is correct
9 Correct 3088 ms 91200 KB Output is correct
10 Correct 3255 ms 91204 KB Output is correct
11 Correct 3315 ms 91272 KB Output is correct
12 Correct 559 ms 89828 KB Output is correct
13 Correct 544 ms 89832 KB Output is correct
14 Correct 558 ms 89832 KB Output is correct
15 Correct 77 ms 80328 KB Output is correct
16 Correct 2723 ms 91196 KB Output is correct
17 Correct 1221 ms 32512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 19768 KB Output is correct
2 Correct 654 ms 51688 KB Output is correct
3 Correct 1698 ms 55336 KB Output is correct
4 Correct 1004 ms 58928 KB Output is correct
5 Correct 107 ms 74848 KB Output is correct
6 Correct 121 ms 78124 KB Output is correct
7 Correct 199 ms 78808 KB Output is correct
8 Correct 383 ms 78944 KB Output is correct
9 Correct 1004 ms 78864 KB Output is correct
10 Correct 2836 ms 78892 KB Output is correct
11 Correct 2836 ms 79176 KB Output is correct
12 Correct 2846 ms 79300 KB Output is correct
13 Correct 3270 ms 79572 KB Output is correct
14 Correct 3485 ms 80432 KB Output is correct
15 Correct 3851 ms 83908 KB Output is correct
16 Correct 1126 ms 78276 KB Output is correct
17 Correct 1002 ms 78424 KB Output is correct
18 Correct 1038 ms 78428 KB Output is correct
19 Correct 87 ms 76232 KB Output is correct
20 Correct 115 ms 77256 KB Output is correct
21 Correct 143 ms 78088 KB Output is correct
22 Correct 207 ms 78280 KB Output is correct
23 Correct 292 ms 78348 KB Output is correct
24 Correct 496 ms 78468 KB Output is correct
25 Correct 902 ms 78528 KB Output is correct
26 Correct 1654 ms 78536 KB Output is correct
27 Correct 2502 ms 78700 KB Output is correct
28 Correct 2592 ms 78572 KB Output is correct
29 Correct 2666 ms 78784 KB Output is correct
30 Correct 2778 ms 78784 KB Output is correct
31 Correct 2492 ms 79112 KB Output is correct
32 Correct 2507 ms 79560 KB Output is correct
33 Correct 2910 ms 81096 KB Output is correct
34 Correct 3385 ms 83528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19548 KB Output is correct
2 Correct 5 ms 19804 KB Output is correct
3 Correct 15 ms 19804 KB Output is correct
4 Correct 2894 ms 60120 KB Output is correct
5 Correct 2982 ms 60236 KB Output is correct
6 Correct 4096 ms 76392 KB Output is correct
7 Correct 4135 ms 83060 KB Output is correct
8 Correct 4041 ms 84152 KB Output is correct
9 Correct 4032 ms 84100 KB Output is correct
10 Correct 4304 ms 84232 KB Output is correct
11 Correct 4135 ms 84100 KB Output is correct
12 Correct 3626 ms 84132 KB Output is correct
13 Correct 4262 ms 84076 KB Output is correct
14 Correct 1193 ms 32512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 19544 KB Output is correct
2 Correct 4 ms 19748 KB Output is correct
3 Correct 3 ms 19548 KB Output is correct
4 Correct 4 ms 19804 KB Output is correct
5 Correct 4 ms 20056 KB Output is correct
6 Correct 4 ms 19800 KB Output is correct
7 Correct 4 ms 19800 KB Output is correct
8 Correct 5 ms 19804 KB Output is correct
9 Correct 6 ms 21852 KB Output is correct
10 Correct 5 ms 21852 KB Output is correct
11 Correct 6 ms 21848 KB Output is correct
12 Correct 4 ms 21852 KB Output is correct
13 Correct 4 ms 21852 KB Output is correct
14 Correct 4 ms 21848 KB Output is correct
15 Correct 5 ms 21852 KB Output is correct
16 Correct 5 ms 21852 KB Output is correct
17 Correct 5 ms 21696 KB Output is correct
18 Correct 5 ms 21852 KB Output is correct
19 Correct 5 ms 21848 KB Output is correct
20 Correct 5 ms 21852 KB Output is correct
21 Correct 5 ms 21692 KB Output is correct
22 Correct 5 ms 21936 KB Output is correct
23 Correct 5 ms 21852 KB Output is correct
24 Correct 5 ms 21816 KB Output is correct
25 Correct 5 ms 21848 KB Output is correct
26 Correct 5 ms 21852 KB Output is correct
27 Correct 4 ms 19800 KB Output is correct
28 Correct 4 ms 21848 KB Output is correct
29 Correct 6 ms 21852 KB Output is correct
30 Correct 13 ms 21852 KB Output is correct
31 Correct 17 ms 22008 KB Output is correct
32 Correct 19 ms 22128 KB Output is correct
33 Correct 19 ms 22108 KB Output is correct
34 Correct 23 ms 22104 KB Output is correct
35 Correct 7 ms 22104 KB Output is correct
36 Correct 7 ms 22104 KB Output is correct
37 Correct 6 ms 22108 KB Output is correct
38 Correct 20 ms 22108 KB Output is correct
39 Correct 22 ms 22108 KB Output is correct
40 Correct 19 ms 22108 KB Output is correct
41 Correct 8 ms 22108 KB Output is correct
42 Correct 6 ms 22108 KB Output is correct
43 Correct 7 ms 22104 KB Output is correct
44 Correct 19 ms 22168 KB Output is correct
45 Correct 19 ms 22108 KB Output is correct
46 Correct 22 ms 22364 KB Output is correct
47 Correct 6 ms 22104 KB Output is correct
48 Correct 7 ms 22104 KB Output is correct
49 Correct 7 ms 21948 KB Output is correct
50 Correct 18 ms 22108 KB Output is correct
51 Correct 19 ms 22112 KB Output is correct
52 Correct 19 ms 22108 KB Output is correct
53 Correct 18 ms 22108 KB Output is correct
54 Correct 19 ms 22132 KB Output is correct
55 Correct 18 ms 22108 KB Output is correct
56 Correct 15 ms 19804 KB Output is correct
57 Correct 5 ms 21852 KB Output is correct
58 Correct 20 ms 22104 KB Output is correct
59 Correct 4 ms 19548 KB Output is correct
60 Correct 6 ms 19804 KB Output is correct
61 Correct 17 ms 19804 KB Output is correct
62 Correct 1835 ms 59648 KB Output is correct
63 Correct 1210 ms 73532 KB Output is correct
64 Correct 1894 ms 79532 KB Output is correct
65 Correct 2905 ms 91144 KB Output is correct
66 Correct 3195 ms 91224 KB Output is correct
67 Correct 3088 ms 91200 KB Output is correct
68 Correct 3255 ms 91204 KB Output is correct
69 Correct 3315 ms 91272 KB Output is correct
70 Correct 559 ms 89828 KB Output is correct
71 Correct 544 ms 89832 KB Output is correct
72 Correct 558 ms 89832 KB Output is correct
73 Correct 77 ms 80328 KB Output is correct
74 Correct 2723 ms 91196 KB Output is correct
75 Correct 1221 ms 32512 KB Output is correct
76 Correct 3 ms 19768 KB Output is correct
77 Correct 654 ms 51688 KB Output is correct
78 Correct 1698 ms 55336 KB Output is correct
79 Correct 1004 ms 58928 KB Output is correct
80 Correct 107 ms 74848 KB Output is correct
81 Correct 121 ms 78124 KB Output is correct
82 Correct 199 ms 78808 KB Output is correct
83 Correct 383 ms 78944 KB Output is correct
84 Correct 1004 ms 78864 KB Output is correct
85 Correct 2836 ms 78892 KB Output is correct
86 Correct 2836 ms 79176 KB Output is correct
87 Correct 2846 ms 79300 KB Output is correct
88 Correct 3270 ms 79572 KB Output is correct
89 Correct 3485 ms 80432 KB Output is correct
90 Correct 3851 ms 83908 KB Output is correct
91 Correct 1126 ms 78276 KB Output is correct
92 Correct 1002 ms 78424 KB Output is correct
93 Correct 1038 ms 78428 KB Output is correct
94 Correct 87 ms 76232 KB Output is correct
95 Correct 115 ms 77256 KB Output is correct
96 Correct 143 ms 78088 KB Output is correct
97 Correct 207 ms 78280 KB Output is correct
98 Correct 292 ms 78348 KB Output is correct
99 Correct 496 ms 78468 KB Output is correct
100 Correct 902 ms 78528 KB Output is correct
101 Correct 1654 ms 78536 KB Output is correct
102 Correct 2502 ms 78700 KB Output is correct
103 Correct 2592 ms 78572 KB Output is correct
104 Correct 2666 ms 78784 KB Output is correct
105 Correct 2778 ms 78784 KB Output is correct
106 Correct 2492 ms 79112 KB Output is correct
107 Correct 2507 ms 79560 KB Output is correct
108 Correct 2910 ms 81096 KB Output is correct
109 Correct 3385 ms 83528 KB Output is correct
110 Correct 4 ms 19548 KB Output is correct
111 Correct 5 ms 19804 KB Output is correct
112 Correct 15 ms 19804 KB Output is correct
113 Correct 2894 ms 60120 KB Output is correct
114 Correct 2982 ms 60236 KB Output is correct
115 Correct 4096 ms 76392 KB Output is correct
116 Correct 4135 ms 83060 KB Output is correct
117 Correct 4041 ms 84152 KB Output is correct
118 Correct 4032 ms 84100 KB Output is correct
119 Correct 4304 ms 84232 KB Output is correct
120 Correct 4135 ms 84100 KB Output is correct
121 Correct 3626 ms 84132 KB Output is correct
122 Correct 4262 ms 84076 KB Output is correct
123 Correct 1193 ms 32512 KB Output is correct
124 Correct 3351 ms 80988 KB Output is correct
125 Correct 1741 ms 76708 KB Output is correct
126 Correct 4006 ms 84012 KB Output is correct
127 Correct 4370 ms 83968 KB Output is correct
128 Correct 4581 ms 84032 KB Output is correct
129 Correct 4221 ms 84132 KB Output is correct
130 Correct 3971 ms 84080 KB Output is correct
131 Correct 3600 ms 89972 KB Output is correct
132 Correct 3160 ms 91128 KB Output is correct
133 Correct 3594 ms 87244 KB Output is correct
134 Correct 3454 ms 83556 KB Output is correct
135 Correct 3528 ms 83488 KB Output is correct
136 Correct 3603 ms 83484 KB Output is correct
137 Correct 4240 ms 84252 KB Output is correct
138 Correct 4563 ms 84128 KB Output is correct
139 Correct 4121 ms 84244 KB Output is correct
140 Correct 3951 ms 84104 KB Output is correct
141 Correct 4026 ms 84128 KB Output is correct
142 Correct 4005 ms 84060 KB Output is correct
143 Correct 90 ms 73332 KB Output is correct
144 Correct 3646 ms 83928 KB Output is correct