QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#641867 | #7444. rldcot | Elegia | 100 ✓ | 284ms | 69936kb | C++23 | 3.2kb | 2024-10-15 02:58:28 | 2024-10-15 02:58:28 |
Judging History
answer
/*
Every vectex will be counted in the union of several retangle.
The sum of the rectangles is O(nlogn) and can be obtained through
SGT merging.
compute the union of each [dep] then done in O(nlog^2n + mlogn).
*/
#include <bits/stdc++.h>
typedef unsigned long long ull;
const int N = 100010, L = 18, M = 500010;
struct Node {
int min, max, ls, rs;
} P[N * L];
int n;
int ans[M];
ull dep[N];
std::vector<std::pair<int, int>> g[N], qry[N], points[N];
std::unordered_map<ull, std::vector<std::pair<int, int>>> mp;
int nnode() { static int top; P[++top].min = n + 1; return top; }
int merge(int p, int q, int l, int r, std::vector<std::pair<int, int>>& rects) {
if (!p || !q) return p ^ q;
int mid = (l + r) >> 1;
auto lv = std::max(std::make_pair(P[P[p].ls].max, 0), std::make_pair(P[P[q].ls].max, 1)),
rv = std::min(std::make_pair(P[P[p].rs].min, 0), std::make_pair(P[P[q].rs].min, 1));
if (lv.second != rv.second && lv.first >= 1 && rv.first <= n) rects.emplace_back(lv.first, rv.first);
P[p].min = std::min(P[p].min, P[q].min);
P[p].max = std::max(P[p].max, P[q].max);
P[p].ls = merge(P[p].ls, P[q].ls, l, mid, rects);
P[p].rs = merge(P[p].rs, P[q].rs, mid + 1, r, rects);
return p;
}
int ins(int o, int l, int r, int x) {
if (!o) o = nnode();
P[o].min = std::min(P[o].min, x); P[o].max = std::max(P[o].max, x);
if (l == r) return o;
int mid = (l + r) >> 1;
if (x <= mid) P[o].ls = ins(P[o].ls, l, mid, x);
else P[o].rs = ins(P[o].rs, mid + 1, r, x);
return o;
}
int dfs(int u, int p) {
int ret = 0;
mp[dep[u]].emplace_back(u, u);
for (const auto& pr : g[u]) {
int v, d; std::tie(v, d) = pr; if (v == p) continue;
dep[v] = dep[u] + d; int cur = dfs(v, u);
ret = merge(ret, cur, 1, n, mp[dep[u]]);
}
ret = ins(ret, 1, n, u);
return ret;
}
int fw[N];
void change(int k, int x) { for (; k <= n; k += k & -k) fw[k] += x; }
int query(int k) { int ret = 0; for (; k; k &= k - 1) ret += fw[k]; return ret; }
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr); std::cout.tie(nullptr);
int m; std::cin >> n >> m;
for (int rep = 1; rep != n; ++rep) {
int u, v, d; std::cin >> u >> v >> d;
g[u].emplace_back(v, d);
g[v].emplace_back(u, d);
}
for (int i = 1; i <= m; ++i) {
int l, r; std::cin >> l >> r;
qry[l].emplace_back(r, i);
}
P[0].min = n + 1;
dfs(1, 0);
for (auto& pr : mp) {
auto& rects = pr.second;
if (rects.empty()) continue;
std::sort(rects.begin(), rects.end());
std::vector<std::pair<int, int>> critical; critical.push_back(rects.back());
for (int i = (int)rects.size() - 2; i >= 0; --i) if (rects[i].second < critical.back().second) critical.push_back(rects[i]);
std::reverse(critical.begin(), critical.end());
rects.clear();
for (int i = 0; i != critical.size(); ++i) {
points[critical[i].first].emplace_back(critical[i].second, 1);
if (i) points[critical[i - 1].first].emplace_back(critical[i].second, -1);
}
}
mp.clear();
for (int i = n; i; --i) {
for (const auto& pr : points[i]) change(pr.first, pr.second);
for (const auto& pr : qry[i]) ans[pr.second] = query(pr.first);
}
for (int i = 1; i <= m; ++i) std::cout << ans[i] << '\n';
return 0;
}
詳細信息
Pretests
Final Tests
Test #1:
score: 5
Accepted
time: 2ms
memory: 7800kb
input:
100 99 62 11 -5 62 89 -8 89 66 -10 66 64 -9 89 65 2 11 98 -4 64 82 4 66 73 3 82 83 6 65 7 2 62 22 1 65 29 -9 89 42 -8 73 100 -5 65 93 -1 7 24 9 82 49 -6 64 44 8 11 18 4 29 5 5 66 56 -10 56 63 -5 100 34 -7 98 30 -10 73 96 -10 7 90 8 64 50 8 90 74 -4 82 87 5 73 88 6 66 58 -10 62 32 0 65 23 -10 29 12 1...
output:
1 28 7 9 18 13 21 1 32 25 8 8 19 17 18 18 12 16 34 22 15 14 15 23 37 41 8 8 14 13 5 28 11 23 10 37 15 1 37 15 7 4 24 13 16 23 22 20 21 17 17 15 5 25 20 19 12 37 1 24 22 3 21 10 17 7 18 27 34 7 19 28 30 30 16 11 11 3 27 15 15 34 32 28 15 18 1 8 12 17 33 16 21 14 33 15 12 27 34
result:
ok 99 numbers
Test #2:
score: 5
Accepted
time: 1ms
memory: 5736kb
input:
100 99 37 56 -33425126 37 1 -877848916 37 25 414902437 37 93 543690285 25 58 84143259 1 23 877848916 37 82 -287809646 23 10 -472533817 25 86 -882640213 86 4 -487426875 86 8 -931312160 1 16 894130728 58 60 934148052 37 64 -290158267 4 39 -979802293 16 95 731320900 16 21 857399681 16 49 -398574308 23 ...
output:
3 1 7 33 36 32 22 64 21 73 47 21 6 11 1 14 16 43 56 44 22 18 46 42 8 57 29 1 6 1 15 35 18 35 5 40 28 9 45 34 42 15 3 1 11 13 22 56 28 17 85 42 56 48 8 6 38 8 52 72 27 13 50 41 63 26 39 57 34 3 55 34 23 28 47 1 15 46 6 11 5 43 49 66 7 20 17 35 6 13 57 71 35 12 56 3 12 41 15
result:
ok 99 numbers
Test #3:
score: 5
Accepted
time: 6ms
memory: 11456kb
input:
5000 4999 876 3519 117316562 876 2076 -821917632 876 4112 45313017 876 1366 -530944154 1366 865 274269389 865 3146 -413493167 876 4885 -70219503 3519 1036 304876272 3146 4535 -976601513 876 4760 424912662 4885 736 856554066 1036 141 -370659484 4760 588 -264305039 865 2081 644590500 865 3368 -4298207...
output:
2543 39 3808 1405 265 1965 1331 989 2451 1874 327 539 1406 2791 1363 1879 688 2155 1244 176 2053 1136 122 202 885 859 246 778 2082 536 837 247 4059 661 200 1110 1141 869 3540 3104 1053 1341 3958 3107 217 491 4088 1549 654 1191 31 2043 858 1737 735 1686 1567 748 718 1004 529 1648 2702 4537 635 2100 9...
result:
ok 4999 numbers
Test #4:
score: 5
Accepted
time: 6ms
memory: 9472kb
input:
5000 4999 3792 3897 423 3897 1740 559 3792 3095 -749 3095 3282 -341 3897 4671 118 4671 3040 -373 3897 1137 890 3095 1268 -809 1137 4293 289 3897 3097 -332 1268 1060 598 1060 629 -966 629 4741 553 3282 4377 507 4293 873 -484 873 343 -46 629 1475 -400 3792 328 902 1475 927 291 927 4000 -690 343 3011 -...
output:
306 661 2275 604 2475 576 68 1810 876 305 730 3105 1854 1687 2044 432 63 2921 293 3079 831 10 2818 1412 532 2582 1009 2843 1719 1716 562 2336 796 1865 2569 1095 1685 1455 2529 2117 2267 15 2510 2946 806 620 2169 3200 1508 2429 1945 372 3121 1913 1991 362 1067 599 244 870 2453 2034 124 241 120 423 24...
result:
ok 4999 numbers
Test #5:
score: 5
Accepted
time: 9ms
memory: 11564kb
input:
5000 4999 882 3928 282751395 3928 2121 84246268 3928 478 592840766 2121 1945 495023455 3928 180 45799778 3928 3294 472612557 1945 1539 292986171 2121 3634 304926640 882 4363 231052615 3634 1709 282277794 1539 348 254583408 1709 4059 368255757 2121 3637 810488627 4059 3521 257973651 3521 2562 1509469...
output:
3873 462 2060 4829 2237 587 463 1365 1521 516 20 2921 776 2224 3230 405 403 924 127 1047 28 433 96 3299 438 1085 2484 71 1446 645 2140 2184 1331 2185 1451 3013 831 2685 1432 2071 3442 2392 832 1523 750 316 2320 249 2121 2264 1906 1999 1179 3009 236 2084 985 2407 1736 939 1868 2348 1545 1930 47 1958 ...
result:
ok 4999 numbers
Test #6:
score: 5
Accepted
time: 4ms
memory: 11420kb
input:
5000 4999 1507 3069 3527 3069 3095 -9590 3095 4842 -6770 1507 632 7634 632 3393 -7890 1507 2660 -1522 3069 3598 -6920 3393 1875 3176 3095 3995 4985 632 4710 -3510 3095 4356 5190 4356 4528 -1875 4356 80 -8903 3095 1147 2348 4710 4805 -1858 4528 3929 4976 4805 4716 2667 3069 946 -8927 4710 2860 5870 8...
output:
4678 4719 4699 4689 4693 4762 4678 4699 4735 4653 4745 4690 4658 4704 4684 4670 4733 4695 4672 4653 4697 4733 4700 4655 4696 4744 4712 4663 4744 4696 4691 4745 4702 4701 4716 4737 4754 4680 4692 4686 4708 4713 4717 4680 4688 4669 4697 4764 4714 4704 4701 4754 4659 4701 4701 4724 4681 4736 4714 4704 ...
result:
ok 4999 numbers
Test #7:
score: 5
Accepted
time: 86ms
memory: 36616kb
input:
50000 49999 28543 17017 98022 17017 42826 20726 42826 2445 49643 28543 17948 5277 17948 46938 -220 46938 32802 51890 32802 14389 -7451 14389 11344 -4392 11344 5010 4312 5010 9928 53064 9928 15769 29588 15769 21594 19572 21594 10020 89784 10020 36292 6905 36292 39666 23856 39666 17518 67733 42826 347...
output:
40799 32113 18977 3117 12009 71 10434 3023 14138 13582 13334 10765 9110 1327 2591 2434 9482 7321 4962 1236 9958 30925 338 2168 3113 20048 5476 32727 1097 30314 18825 16896 472 1669 3959 13788 1983 18341 633 16921 1726 3385 1454 5994 4933 23330 34441 27809 14669 25543 8572 2673 10266 33988 32015 3651...
result:
ok 49999 numbers
Test #8:
score: 5
Accepted
time: 77ms
memory: 32308kb
input:
50000 49999 1391 22386 47 1391 45769 6 1391 15535 86 15535 48735 -99 48735 40714 -31 22386 38631 25 38631 35750 -24 35750 29613 16 29613 5199 -25 5199 49973 19 49973 41966 -25 41966 30206 46 30206 5830 -77 5830 31305 -93 31305 30946 -26 30946 43271 -39 43271 12182 -50 12182 47941 29 47941 23870 23 4...
output:
1941 1963 1932 1936 1953 1955 1923 1928 1940 1948 1931 1944 1947 1921 1943 1952 1949 1937 1931 1954 1945 1944 1948 1950 1929 1956 1945 1935 1932 1933 1945 1926 1932 1920 1933 1930 1946 1938 1951 1933 1956 1947 1948 1934 1941 1932 1949 1948 1944 1949 1950 1943 1922 1934 1936 1944 1930 1947 1959 1950 ...
result:
ok 49999 numbers
Test #9:
score: 5
Accepted
time: 86ms
memory: 36616kb
input:
50000 49999 47689 18519 -101548786 18519 4870 118680825 4870 30744 881553913 18519 22282 -435727671 22282 27875 819130228 27875 25223 204438576 25223 45899 220923301 45899 7043 -560784147 18519 18652 66226294 18652 27944 85044965 27944 2280 68694727 2280 19693 810360687 18519 30928 -797861623 30928 ...
output:
44049 44799 45787 44351 43676 44880 47218 46544 46141 44314 49229 46498 44012 43945 41883 46785 45211 42429 47284 44203 45972 42657 48754 47677 45373 43618 46954 46444 44562 46257 46564 44119 44804 44953 47948 44696 45903 42876 44062 43520 43466 47124 47635 42918 48192 49067 45173 46542 44105 46387 ...
result:
ok 49999 numbers
Test #10:
score: 5
Accepted
time: 69ms
memory: 33144kb
input:
50000 49999 22614 44396 734026505 44396 45044 -574053935 45044 3501 147984457 3501 46353 -777744427 46353 40970 484373035 40970 21085 -671528355 21085 40612 -256173294 40612 37624 -769918341 37624 32467 54471988 32467 5573 906602800 5573 31208 39798831 31208 28871 286762042 28871 44683 -991851574 44...
output:
47946 45571 44821 47221 45688 47756 45082 45879 48722 46557 46103 43405 47056 46980 47061 49064 47373 45950 45815 49097 46515 45745 48402 48709 45417 46500 47668 47190 47111 48571 48047 47031 47364 48962 48444 46694 43694 46529 47205 48362 46630 46357 46848 44149 44086 45375 43739 47777 48327 44956 ...
result:
ok 49999 numbers
Test #11:
score: 5
Accepted
time: 181ms
memory: 56536kb
input:
100000 499999 68557 60748 1 60748 67092 1 67092 60741 1 60741 94513 1 94513 43807 1 43807 17655 1 17655 31712 1 31712 41314 1 41314 70546 1 70546 98638 1 98638 79927 1 79927 30210 1 30210 33623 1 33623 76963 1 76963 61376 1 61376 94592 1 94592 62406 1 62406 76984 1 76984 65489 1 65489 98064 1 98064 ...
output:
5049 5047 5050 5045 5048 5049 5051 5049 5043 5043 5054 5043 5053 5043 5049 5054 5049 5043 5048 5043 5045 5049 5045 5044 5050 5041 5042 5049 5051 5050 5050 5047 5042 5042 5052 5051 5046 5045 5047 5051 5051 5039 5045 5049 5041 5054 5050 5049 5049 5051 5049 5041 5043 5053 5047 5049 5048 5050 5045 5048 ...
result:
ok 499999 numbers
Test #12:
score: 5
Accepted
time: 242ms
memory: 59908kb
input:
100000 499999 1639 29781 1 29781 17380 1 17380 97020 1 97020 83569 1 83569 10110 1 10110 68724 1 68724 18586 1 18586 22484 1 68724 3768 1 3768 3210 1 3210 67286 1 67286 85203 1 85203 20400 1 20400 58907 1 3768 31497 1 31497 77841 1 77841 11051 1 18586 56669 1 56669 87732 1 87732 36371 1 36371 16727 ...
output:
75 74 75 75 75 73 75 75 74 74 74 74 74 74 74 75 75 75 75 74 74 75 75 71 74 74 74 75 75 75 74 75 75 75 75 74 75 74 75 74 74 73 74 75 75 75 75 74 74 74 75 74 75 74 74 75 74 74 74 74 75 75 73 74 73 75 73 75 73 75 74 74 75 72 75 75 74 75 75 72 74 74 75 75 74 75 74 72 72 74 71 74 74 75 74 74 75 74 75 75 ...
result:
ok 499999 numbers
Test #13:
score: 5
Accepted
time: 265ms
memory: 60524kb
input:
100000 499999 34375 86434 1 86434 3362 1 3362 94790 1 94790 21710 1 21710 27418 1 86434 39689 1 39689 20651 1 20651 64980 1 64980 12228 1 12228 39401 1 27418 72806 1 39689 84176 1 84176 27179 1 27179 40891 1 20651 92092 1 92092 5025 1 5025 44465 1 44465 81432 1 81432 67840 1 67840 57231 1 57231 7159...
output:
71 68 65 71 58 65 70 65 51 67 69 59 62 71 70 69 70 68 65 66 63 71 71 54 70 66 61 69 65 69 71 66 70 71 70 65 60 65 71 65 71 63 62 65 71 67 69 55 61 67 70 66 56 71 71 69 59 67 68 72 71 64 71 70 66 61 70 67 71 70 60 66 71 69 71 68 69 66 68 63 71 62 57 71 71 69 65 70 70 65 65 65 70 69 69 68 70 65 64 71 ...
result:
ok 499999 numbers
Test #14:
score: 5
Accepted
time: 222ms
memory: 58172kb
input:
100000 499999 46085 30675 1 30675 71652 1 71652 48365 1 48365 587 1 587 76053 1 76053 32407 1 32407 59317 1 59317 12067 1 59317 6528 1 6528 56738 1 48365 30206 1 30675 58854 1 58854 47823 1 47823 76155 1 76155 66370 1 66370 17980 1 17980 55051 1 17980 94931 1 94931 89349 1 89349 50580 1 50580 6005 1...
output:
171 171 169 171 172 172 170 172 169 169 170 171 175 173 175 171 170 175 170 170 175 174 173 171 171 171 171 173 173 170 172 172 172 172 170 171 174 169 174 171 173 171 172 172 169 170 171 170 171 173 175 172 170 172 173 172 173 170 175 171 173 171 173 171 171 173 175 172 170 171 173 171 170 169 172 ...
result:
ok 499999 numbers
Test #15:
score: 5
Accepted
time: 283ms
memory: 69936kb
input:
100000 499999 20451 19478 642 19478 81744 4627 81744 59694 5795 59694 99426 -310 99426 5111 5007 5111 55779 -7724 55779 13425 9393 13425 32133 7330 32133 14060 -3205 14060 56450 -8168 56450 21480 -4277 21480 56575 3073 56575 76157 -2986 76157 62379 9013 62379 85585 1191 85585 35458 -4374 35458 3988 ...
output:
65266 61352 60629 60931 62845 63152 57132 58525 58662 51506 57774 63608 61225 55587 56920 61720 62369 53242 63541 59076 61476 58349 66032 52172 58110 59986 62953 64462 60554 60236 61527 63061 60333 65251 62811 64362 62648 63911 62561 56236 57488 68309 58934 64766 64131 58235 59343 66134 60995 61305 ...
result:
ok 499999 numbers
Test #16:
score: 5
Accepted
time: 269ms
memory: 65848kb
input:
100000 499999 62205 24801 -62206 24801 80722 -954108 62205 49456 -62206 49456 11094 62206 11094 10386 -627510 62205 94471 219365 94471 87734 -98218 87734 93726 98218 93726 26651 -735226 26651 77595 735226 77595 78734 -741167 78734 70782 600552 70782 22846 -600552 22846 15263 600552 80722 66378 95410...
output:
43860 42353 36708 39166 42571 38394 40946 42995 43064 39108 38862 41537 40227 39747 42466 42152 39648 41857 41453 40697 39585 43231 37453 39656 42337 42668 41759 39593 40076 44480 37524 40892 44461 41995 39561 40361 43379 39482 40539 41627 40041 41845 35709 43211 43106 39921 38087 35909 40238 38176 ...
result:
ok 499999 numbers
Test #17:
score: 5
Accepted
time: 284ms
memory: 67256kb
input:
100000 499999 65675 77455 353062579 77455 37615 -275623342 37615 67788 938075854 67788 2238 -938075854 67788 21871 213107287 2238 68047 458737969 68047 45815 -458737969 45815 36170 458737969 36170 35531 -989820394 35531 70108 989820394 70108 90805 -574502962 90805 23286 185052275 36170 16690 -780330...
output:
32953 28006 35439 30323 35281 36900 21898 30189 40120 32906 31701 34395 23500 30847 44025 31352 25036 36612 30819 35308 34660 28980 33951 37677 33136 27944 38983 33986 22925 34462 36989 33470 34156 32231 41400 32005 26571 36974 28475 39849 34933 36752 29154 31148 38566 27193 30338 28105 33902 39934 ...
result:
ok 499999 numbers
Test #18:
score: 5
Accepted
time: 213ms
memory: 62496kb
input:
100000 499999 22686 25086 -709837952 25086 40129 814501909 40129 16722 -814501909 16722 78290 877592649 78290 90922 -877592649 90922 79563 877592649 79563 55572 -877592649 55572 45256 877592649 45256 89548 -958917012 89548 95688 958917012 95688 84159 -958917012 84159 21676 958917012 21676 73960 -871...
output:
42339 44625 42087 43001 44130 42258 45370 43865 42273 44165 44061 44333 44772 45026 44133 45752 43509 42502 43611 42656 44444 43832 44605 42666 44541 41730 45202 42657 43590 43807 43025 42966 42814 43088 42848 45202 44139 42745 42211 41504 43591 44637 44586 43550 41679 42492 44694 42814 42631 43940 ...
result:
ok 499999 numbers
Test #19:
score: 5
Accepted
time: 165ms
memory: 56592kb
input:
100000 499999 96900 41954 -96901 41954 98151 -550471687 98151 5654 550471687 5654 14166 -550471687 14166 93148 -484179267 93148 84881 -955638816 84881 53166 955638816 53166 30837 -955638816 30837 57043 955638816 57043 13003 235324875 13003 82543 -673860924 82543 61159 673860924 61159 54181 -67386092...
output:
43341 44042 41991 42621 41859 42526 42797 42830 41802 44745 42003 43968 44287 42688 44274 41192 42514 40893 42719 44626 42119 42553 44895 41705 42777 44452 43390 43343 42365 42938 43111 41622 41930 44379 44168 43554 41457 43208 43695 41506 45439 43165 43575 42095 40423 41754 44275 45039 42955 41241 ...
result:
ok 499999 numbers
Test #20:
score: 5
Accepted
time: 216ms
memory: 55296kb
input:
100000 499999 35298 73116 -35299 73116 66523 -640229255 66523 49903 640229255 49903 43352 -640229255 43352 53156 640229255 53156 21662 -132043039 21662 44684 730483005 44684 66601 -730483005 66601 11719 730483005 11719 49011 434213061 49011 53634 -813739159 53634 4019 813739159 4019 1943 -813739159 ...
output:
16869 3906 8397 15617 7265 33166 18041 1240 34559 1204 13561 30078 8060 33396 12234 83 9940 25335 21440 23985 17425 41223 10262 17148 9654 2739 26217 2578 12133 16190 27823 20037 18670 15553 7409 29890 22635 38694 19713 13150 1514 16907 26250 26116 37154 3953 3694 17796 8654 27237 27898 16563 26477 ...
result:
ok 499999 numbers