QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#598326 | #6340. Tourism | Dimash# | 0 | 138ms | 26800kb | C++17 | 4.7kb | 2024-09-28 21:18:53 | 2024-09-28 21:18:57 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 12, MOD = (int)1e9 + 7;
const int L = 17;
int n, m, q, tin[N], tout[N], timer, dep[N], s[N], up[N][18], ver[N * 2];
vector<int> g[N];
void dfs(int v, int pr = 1) {
tin[v] = ++timer;
s[v] = 1;
up[v][0] = pr;
ver[timer] = v;
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);
s[v] += s[to];
}
}
tout[v] = ++timer;
}
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];
}
int c[N];
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));
}
struct query{
int l, r, id;
};
vector<query> v;
int res[N];
int bl = 316;
bool cmp(query l, query r) {
if(l.l / bl == r.l / bl) {
return l.r < r.r;
} else {
return l.l < r.l;
}
}
multiset<int> cur;
int ans = 0;
int find(int x, int y) {
return dep[ver[y]] - dep[lca(ver[x], ver[y])];
}
void add(int v) {
v = c[v];
int t = tin[v];
if(cur.count(t)) {
cur.insert(t);
return;
}
if(cur.empty() || t < (*cur.begin())) {
if(!cur.empty()) {
ans -= dep[ver[*cur.begin()]];
ans += find(t, (*cur.begin()));
}
ans += dep[v];
cur.insert(t);
} else {
auto it = cur.upper_bound(t);
auto it1 = cur.lower_bound(t);
it1--;
ans += find(*it1, t);
// cout << ver[*it1] << ' ' << ver[t] << ' ' << find(*it1, t) << '\n';
if(it != cur.end()) {
ans += find(t, *it);
ans -= find(*it1, *it);
}
cur.insert(t);
}
}
void del(int v) {
v = c[v];
int t = tin[v];
cur.erase(cur.find(t));
if(cur.count(t)) {
return;
}
if(cur.empty() || t < (*cur.begin())) {
if(!cur.empty()) {
ans += dep[ver[*cur.begin()]];
ans -= find(t, (*cur.begin()));
}
ans -= dep[v];
} else {
auto it = cur.upper_bound(t);
auto it1 = cur.lower_bound(t);
it1--;
ans -= find(*it1, t);
// cout << ver[*it1] << ' ' << ver[t] << ' ' << find(*it1, t) << '\n';
if(it != cur.end()) {
ans -= find(t, *it);
ans += find(*it1, *it);
}
}
}
void test() {
cin >> n >> m >> q;
for(int i = 1; i <= n - 1; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dep[1] = 1;
dfs(1);
for(int i = 1; i <= m; i++) {
cin >> c[i];
}
if(m > 1) build();
for(int i = 1; i <= q; i++) {
query f;
int l, r;
cin >> l >> r;
if(l == r) {
res[i] = 1;
continue;
}
f.l = l;
f.r = r;
f.id = i;
v.push_back(f);
// ans = 0;
// cur.clear();
// for(int j = l; j <= r; j++) {
// add(j);
// }
// int v = c[l];
// if(l < r) {
// v = get(l, r - 1).second;
// }
// cout << ans - dep[v] + 1 << '\n';
}
// return;
sort(v.begin(), v.end(), cmp);
int l = 1, r = 0;
for(int i = 0; i < (int)v.size(); i++) {
while(l < v[i].l) del(l++);
while(l > v[i].l) add(--l);
while(r < v[i].r) add(++r);
while(r > v[i].r) del(r--);
int x = get(v[i].l, v[i].r - 1).second;
// cout << l << ' ' << r << ' ' << ans << ' ' << x << '\n';
res[v[i].id] = ans - 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
Time Limit Exceeded
Test #1:
score: 0
Time Limit Exceeded
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:
result:
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
Time Limit Exceeded
Test #69:
score: 18
Accepted
time: 37ms
memory: 20512kb
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:
276 238 198 214 294 240 233 266 184 241 207 241 256 225 215 222 190 269 221 242 287 225 215 252 273 203 281 186 259 195 152 183 206 241 218 205 241 233 194 239 258 244 267 339 224 205 242 202 260 275 173 243 178 228 251 242 239 231 203 249 277 215 202 169 243 258 208 306 232 231 211 224 249 256 203 ...
result:
ok 1797 numbers
Test #70:
score: 18
Accepted
time: 68ms
memory: 20588kb
input:
59284 89368 1930 4029 716 1741 4029 14542 4029 48937 4029 716 24494 29506 1741 4029 3097 2898 716 3097 8627 2898 46025 29506 15319 716 12015 1741 5566 8627 58178 2898 14837 7742 1741 21507 24494 20151 24494 48937 9926 55162 7742 32033 14837 2898 27435 12292 8627 24972 58178 55074 48937 45787 21507 1...
output:
369 311 313 353 339 335 284 364 434 382 298 243 268 306 282 383 400 371 343 357 399 329 285 264 350 350 372 391 378 398 281 257 419 308 307 462 379 357 327 356 323 427 360 368 312 394 268 310 310 324 275 312 279 347 298 281 314 291 447 257 320 269 343 311 397 279 332 319 238 268 369 334 301 321 390 ...
result:
ok 1930 numbers
Test #71:
score: 18
Accepted
time: 60ms
memory: 18436kb
input:
67606 66951 1824 37697 58269 58269 20521 53476 37697 51085 20521 20521 3727 3727 59823 38837 53476 38837 40963 20521 28388 43757 51085 14394 58269 43757 1117 53476 60607 58269 57399 31600 57399 52004 3727 53476 44312 44312 49253 58269 2843 16982 43757 16982 60419 14394 5307 21031 20521 16982 13147 5...
output:
369 309 338 203 348 299 298 331 273 247 281 248 318 311 268 293 276 247 300 285 354 257 219 227 325 271 286 376 305 294 230 276 261 268 292 217 345 240 296 258 300 296 328 284 284 265 300 278 331 292 278 300 286 231 222 261 425 274 259 226 391 286 207 267 366 231 275 249 287 252 209 273 278 279 267 ...
result:
ok 1824 numbers
Test #72:
score: 18
Accepted
time: 92ms
memory: 26800kb
input:
100000 100000 1 26451 75404 81327 75404 26451 29978 26451 40155 89550 29978 26451 16783 40584 40155 45697 16783 45697 46663 23828 46663 29978 77426 76408 26451 46663 8400 70202 29978 49633 40584 70202 77511 89375 76408 15804 29978 49633 38747 89550 42375 89550 81055 75404 88488 41733 89550 70202 137...
output:
78872
result:
ok 1 number(s): "78872"
Test #73:
score: 18
Accepted
time: 132ms
memory: 23660kb
input:
100000 100000 3 82208 40540 80548 82208 51536 80548 80548 84376 93726 82208 16865 84376 40540 39439 35540 93726 94810 16865 58036 35540 41837 80548 41837 5186 48275 41837 81090 93726 13718 35540 16865 77 39439 33192 58036 56787 40540 21934 13718 41327 46616 16865 77 88013 98749 40540 33949 16865 460...
output:
50026 49745 49971
result:
ok 3 number(s): "50026 49745 49971"
Test #74:
score: 18
Accepted
time: 138ms
memory: 22568kb
input:
100000 100000 10 34595 88812 53602 88812 51012 88812 34595 25192 96336 34595 25192 61224 21676 25192 34595 84305 25192 87097 87097 78572 78572 56128 76900 56128 47761 88812 99770 61224 47761 12554 56128 58321 63156 58321 58321 9825 12554 15673 99770 81857 63156 29843 81905 88812 38032 34595 96336 36...
output:
24893 24697 24858 24512 24636 24838 24613 24786 24626 24729
result:
ok 10 numbers
Test #75:
score: 18
Accepted
time: 137ms
memory: 22324kb
input:
100000 100000 30 42863 76067 63251 42863 76067 48333 42863 53221 29390 42863 85971 42863 3185 42863 63251 42450 3185 19834 19834 44010 48470 42450 19834 22824 54068 85971 63251 65339 97059 44010 42450 66115 98472 97059 33557 42863 74927 54068 69415 42450 69415 3326 63251 41552 85971 67253 93346 5406...
output:
11576 11233 11667 11966 11490 11509 11586 11307 11704 11674 11520 11361 11595 11324 11586 11570 11668 11588 11448 11759 11601 11651 11455 11452 11714 11746 11560 11602 11567 11750
result:
ok 30 numbers
Test #76:
score: 18
Accepted
time: 115ms
memory: 22092kb
input:
100000 100000 100 22634 75465 19501 75465 25894 19501 14338 19501 5523 25894 52399 14338 52399 42507 67866 75465 25894 61608 25894 93402 87416 22634 45236 52399 36472 75465 61608 41436 92396 93402 85899 25894 41436 21440 14228 14338 85899 56625 83318 41436 21440 51711 6339 56625 51711 80000 36472 53...
output:
4811 4631 4591 4481 4686 4687 4512 4917 4559 4688 4809 4515 4695 4519 4834 4446 4875 4768 4769 4793 4894 4685 4708 4629 4672 4594 4624 4547 4715 4922 4780 4686 4616 4464 4474 4537 4486 4697 4616 4643 4786 4509 4533 4510 4691 4667 4409 4687 4535 4578 4560 4699 4732 4490 4671 4722 4894 4658 4648 4568 ...
result:
ok 100 numbers
Test #77:
score: 18
Accepted
time: 105ms
memory: 22124kb
input:
100000 100000 300 90578 89684 89684 3831 90578 66618 77524 66618 90578 48241 57189 3831 64968 57189 69650 3831 25557 77524 86156 66618 90578 34093 16791 89684 85977 34093 8297 85977 57189 60674 89684 75021 75021 49529 14470 34093 75021 13628 64968 23217 49529 13292 13292 9531 86156 64479 95832 89684...
output:
1913 1972 1803 1934 1795 1923 1896 1786 2012 1767 1870 1846 1898 2015 1895 1950 1959 1977 1824 1970 1758 1816 1996 1894 1828 1983 1948 1908 1801 1901 1964 1941 1860 1954 1856 2028 1833 1940 1754 1920 1791 2053 2068 1837 1889 1949 1714 1951 1879 1913 1925 1932 1856 1855 1951 1834 1843 1981 1938 1816 ...
result:
ok 300 numbers
Test #78:
score: 18
Accepted
time: 86ms
memory: 22376kb
input:
100000 100000 1000 39153 45943 94392 39153 79053 39153 94392 33885 2756 79053 51903 33885 38859 51903 79053 36974 2767 36974 2756 15571 36974 72001 15933 79053 15933 74976 51127 45943 19196 38859 12936 2756 25536 38859 79053 97016 39585 15571 56150 12936 44998 39153 80397 79053 2767 37989 32196 7905...
output:
707 671 798 647 770 720 746 616 654 695 702 663 590 666 621 653 672 668 761 680 567 697 701 844 723 581 773 668 816 746 727 777 602 763 798 605 647 644 709 633 523 742 605 805 613 640 654 673 605 765 707 697 674 694 676 665 596 791 588 664 711 726 675 582 696 698 574 687 652 733 741 616 674 604 728 ...
result:
ok 1000 numbers
Test #79:
score: 18
Accepted
time: 84ms
memory: 22180kb
input:
100000 100000 3000 24460 92288 92288 78218 92288 90483 92288 23779 23145 78218 23145 86709 78006 86709 19093 92288 23145 15895 78006 10663 15895 19595 24460 29358 51460 19093 19093 96262 24460 64190 15895 91390 15895 22891 56695 78006 78218 81580 99267 91390 47670 19093 96262 46523 81580 62720 55665...
output:
278 219 186 361 306 260 294 267 260 289 326 149 236 389 265 311 250 268 300 264 277 311 325 220 364 303 241 173 251 274 236 242 157 293 242 265 275 231 294 245 303 254 285 199 279 311 245 293 290 238 329 309 242 258 275 407 322 276 272 297 329 295 310 230 227 288 316 276 357 310 201 268 296 264 315 ...
result:
ok 3000 numbers
Test #80:
score: 0
Time Limit Exceeded
input:
100000 100000 10000 60471 67901 60471 79481 67901 70274 43259 60471 40484 60471 70274 91612 70274 95567 30745 70274 25482 40484 67901 68399 68978 79481 57690 79481 61927 95567 31670 67901 99069 30745 58593 99069 29956 79481 106 67901 43259 74994 40484 4306 40484 59011 62413 4306 59011 81345 31670 52...
output:
result:
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%