QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#601898#6340. TourismDimash0 174ms22212kbC++234.2kb2024-09-30 15:30:582024-09-30 15:31:00

Judging History

你现在查看的是最新测评结果

  • [2024-09-30 15:31:00]
  • 评测
  • 测评结果:0
  • 用时:174ms
  • 内存:22212kb
  • [2024-09-30 15:30:58]
  • 提交

answer

#include <bits/stdc++.h>
    
using namespace std;
    
typedef long long ll;
const int  N = 1e5 + 12, MOD = (int)1e9 + 7;

const int L = 19;
vector<int> g[N];
int n, m, q, c[N], tin[N], tout[N], timer, s[N], head[N], ver[N];
int up[N][19], dep[N];
void bb(int v, int pr = -1) {
    s[v] = 1;
    for(int i = 0; i < (int)g[v].size(); i++) {
        int to = g[v][i];
        if(to == pr) continue;
        bb(to, v);
        s[v] += s[to];
        if(s[to] > s[g[v][0]] || g[v][0] == pr) {
            swap(g[v][i], g[v][0]);
        }
    }
}
void hld(int v, int pr = -1) {
    tin[v] = ++timer;
    ver[timer] = v;
    for(int to:g[v]) {
        if(to == pr) continue;
        if(to == g[v][0]) {
            head[to] = head[v];
        } else {
            head[to] = to;
        }
        hld(to, v);
    }
    tout[v] = timer;
}
vector<pair<int,int>> qr[N];

void dfs(int v, int pr = 1) {
    up[v][0] = pr;
    for(int i = 1; i < L; ++i) {
        up[v][i] = up[up[v][i - 1]][i - 1];
    }
    for(int to:g[v]) {
        if(to != pr) {
            dep[to] = dep[v] + 1;
            dfs(to, v);
        }
    }
}
bool is(int v, int u) {
    return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}
int lca(int v, int u) {
    if(is(v, u)) return v;
    if(is(u, v)) return u;
    for(int i = L - 1; i >= 0; i--) {
        if(!is(up[v][i], u)) {
            v = up[v][i];
        }
    }
    return up[v][0];
}
pair<int, int> t[N * 4];
void build(int v = 1, int tl = 1, int tr = m - 1) {
    if(tl == tr) {
        int x = lca(c[tl], c[tl + 1]);
        t[v] = {dep[x], x};
    } else {
        int tm = (tl + tr) >> 1;
        build(v + v, tl, tm);
        build(v + v + 1, tm + 1, tr);
        t[v] = min(t[v + v], t[v + v + 1]);
    }
}
const int inf = 1e9;
pair<int, int> get(int l, int r, int v = 1, int tl = 1, int tr = m - 1) {
    if(l > r || tl > r || l > tr) return {inf, inf};
    if(tl >= l && tr <= r) return t[v];
    int tm = (tl + tr) >> 1;
    return min(get(l, r, v + v, tl, tm), get(l, r, v + v + 1, tm + 1, tr));
}

int mn[N * 4], mod[N * 4], mx[N * 4];

void make(int v, int val) {
    mod[v] = mx[v] = mn[v] = val;
}
void push(int v) {
    if(mod[v]) {
        make(v + v, mod[v]);
        make(v + v + 1, mod[v]);
        mod[v] = 0;
    }
}
void upd(int l, int r, int val, int v = 1, int tl = 1, int tr = n) {
    if(l > r|| tl > r || l > tr) return;
    if(tl >= l && tr <= r) {
        make(v, val);
    } else {
        push(v);
        int tm = (tl + tr) >> 1;
        upd(l, r, val, v + v, tl, tm);
        upd(l, r, val, v + v + 1, tm + 1, tr);
        mx[v] = max(mx[v + v], mx[v + v + 1]);
        mn[v] = min(mn[v + v], mn[v + v + 1]);
    }
}
int find(int val, int v = 1, int tl = 1, int tr = n) { 
    if(mn[v] >= val) {
        return (tr - tl + 1);
    } 
    if(mx[v] < val) {
        return 0;
    }
    push(v);
    int tm = (tl + tr) >> 1;
    return find(val,v + v, tl, tm) + find(val, v + v + 1, tm + 1, tr);
}
void nv(int v, int i) {
    do {
        upd(tin[v], tin[v], i);
        v = up[v][0];
        // upd(tin[head[v]], tin[v], i);
        // v = up[head[v]][0];
    }while(v > 1);
}
int res[N];
void test() {
    cin >> n >> m >> q;
    for(int i = 1; i <= n - 1; i++) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    for(int i = 1; i <= m; i++) {
        cin >> c[i];
    }
    dep[1] = 1;
    bb(1);
    head[1] = 1;
    hld(1);
    dfs(1);
    if(m != 1)build();
    for(int i = 1; i <= q; i++) {
        int l, r;
        cin >> l >> r;
        if(l == r) {
            res[i] = 1;
            continue;
        }
        qr[r].push_back({l, i});
    }
    for(int i = 1; i <= m; i++) {
        nv(c[i], i);
        for(auto [l, id]:qr[i]) {
            int x = get(l, i - 1).second;
            res[id] = find(l) - dep[x] + 1;
        }
    }
    for(int i = 1; i <= q; i++) {
        cout << res[i] << '\n';
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); 
    
    int t = 1; 
    // cin >> t;
    
    while(t--) 
        test();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 11876kb

input:

166 249 224
158 52
52 82
158 36
117 158
119 117
5 82
158 18
22 36
82 143
105 36
22 152
36 92
117 2
123 158
5 134
119 89
31 119
92 48
105 149
149 17
108 31
134 50
3 52
63 158
3 51
42 22
17 10
103 158
50 122
92 85
50 78
117 159
36 20
143 115
158 83
20 4
142 22
23 3
96 10
19 134
8 10
151 92
65 108
89 5...

output:

66
96
109
92
109
138
83
23
125
120
69
96
82
95
24
112
85
59
78
48
106
130
65
32
3
40
85
109
58
122
45
129
72
62
92
74
112
84
24
94
126
68
84
120
76
126
122
82
33
61
98
120
62
55
98
105
27
68
126
47
73
133
67
93
21
119
104
115
35
82
123
111
129
18
33
111
137
36
97
63
121
108
1
104
136
72
104
108
88
9...

result:

wrong answer 1st numbers differ - expected: '67', found: '66'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Time Limit Exceeded

Test #56:

score: 0
Time Limit Exceeded

input:

55321 88650 75523
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51...

output:


result:


Subtask #4:

score: 0
Wrong Answer

Test #69:

score: 0
Wrong Answer
time: 174ms
memory: 22212kb

input:

54738 54525 1797
45211 4527
4527 43609
4527 19876
16325 43609
32183 4527
16325 32579
43609 25554
32183 38972
45211 53953
16325 19810
10881 19810
45211 12698
27967 19810
25554 46338
51894 45211
25388 16325
512 25554
43609 7820
10206 512
30021 32183
48647 43609
46338 44138
16766 7820
10023 53953
19810...

output:

275
237
197
213
293
239
232
265
183
240
206
240
255
224
214
221
189
268
220
241
286
224
214
251
272
202
280
185
258
194
151
182
205
240
217
204
240
232
193
238
257
243
266
338
223
204
241
201
259
274
172
242
177
227
250
241
238
230
202
248
276
214
201
168
242
257
207
305
231
230
210
223
248
255
202
...

result:

wrong answer 1st numbers differ - expected: '276', found: '275'

Subtask #5:

score: 0
Time Limit Exceeded

Test #102:

score: 0
Time Limit Exceeded

input:

55965 89652 95687
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
8 17
9 18
9 19
10 20
10 21
11 22
11 23
12 24
12 25
13 26
13 27
14 28
14 29
15 30
15 31
16 32
16 33
17 34
17 35
18 36
18 37
19 38
19 39
20 40
20 41
21 42
21 43
22 44
22 45
23 46
23 47
24 48
24 49
25 50
25 51
26 52
26...

output:


result:


Subtask #6:

score: 0
Skipped

Dependency #1:

0%