QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#35646 | #971. Binary Search Tree | AutumnKite | AC ✓ | 903ms | 43332kb | C++17 | 3.6kb | 2022-06-17 16:30:05 | 2022-06-17 16:30:07 |
Judging History
answer
#include <bits/stdc++.h>
class seg_tree {
int n, en, m;
std::vector<int> min;
std::vector<long long> sum, sumr;
static int enlarge(int n) {
int res = 1;
while (res < n) {
res <<= 1;
}
return res;
}
long long query(int u, int l, int r, int pre) {
if (l + 1 == r) {
if (min[u] < pre) {
return sum[u];
} else {
return 0;
}
}
int mid = (l + r) >> 1;
if (min[u << 1] < pre) {
return query(u << 1, l, mid, pre) + sumr[u];
} else {
return query(u << 1 | 1, mid, r, pre);
}
}
void up(int u, int l, int mid, int r) {
min[u] = std::min(min[u << 1], min[u << 1 | 1]);
sumr[u] = query(u << 1 | 1, mid, r, min[u << 1]);
sum[u] = sum[u << 1] + sumr[u];
}
void build(int u, int l, int r, const std::vector<int> &w) {
if (l + 1 == r) {
min[u] = m;
sum[u] = w[l];
return;
}
int mid = (l + r) >> 1;
build(u << 1, l, mid, w);
build(u << 1 | 1, mid, r, w);
up(u, l, mid, r);
}
void modify(int u, int l, int r, int x, int v) {
if (l + 1 == r) {
min[u] = v;
return;
}
int mid = (l + r) >> 1;
if (x < mid) {
modify(u << 1, l, mid, x, v);
} else {
modify(u << 1 | 1, mid, r, x, v);
}
up(u, l, mid, r);
}
long long query(int u, int l, int r, int L, int R, int &pre) {
if (L <= l && r <= R) {
long long res = query(u, l, r, pre);
pre = std::min(pre, min[u]);
return res;
}
int mid = (l + r) >> 1;
if (R <= mid) {
return query(u << 1, l, mid, L, R, pre);
} else if (L >= mid) {
return query(u << 1 | 1, mid, r, L, R, pre);
} else {
long long tmp = query(u << 1, l, mid, L, R, pre);
return tmp + query(u << 1 | 1, mid, r, L, R, pre);
}
}
public:
seg_tree(std::vector<int> w, int t_m)
: n(w.size()), en(enlarge(n) << 1), m(t_m), min(en), sum(en), sumr(en) {
if (n) {
build(1, 0, n, w);
}
}
void modify(int x, int v) {
modify(1, 0, n, x, v);
}
long long query(int l, int r, int v) {
if (l >= r) {
return 0;
}
return query(1, 0, n, l, r, v);
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, q;
std::cin >> n >> q;
std::vector<std::vector<std::pair<int, int>>> M(n), Q(n);
std::vector<int> val;
for (int i = 0; i < q; ++i) {
int op;
std::cin >> op;
if (op == 1) {
int l, r, w;
std::cin >> l >> r >> w;
val.push_back(w);
--l;
M[l].emplace_back(w, i);
if (r < n) {
M[r].emplace_back(w, q);
}
} else {
int x, w;
std::cin >> x >> w;
--x;
Q[x].emplace_back(w, i);
}
}
std::sort(val.begin(), val.end());
int m = val.size();
seg_tree S(val, q), T(std::vector<int>(val.rbegin(), val.rend()), q);
std::vector<long long> ans(q, -1);
for (int i = 0; i < n; ++i) {
for (auto p : M[i]) {
p.first = std::lower_bound(val.begin(), val.end(), p.first) - val.begin();
S.modify(p.first, p.second);
T.modify(m - 1 - p.first, p.second);
}
for (auto p : Q[i]) {
int x = std::lower_bound(val.begin(), val.end(), p.first) - val.begin();
int y = std::upper_bound(val.begin(), val.end(), p.first) - val.begin();
ans[p.second] = S.query(x, m, p.second) +
T.query(m - y, m, p.second) -
S.query(x, y, p.second);
}
}
for (int i = 0; i < q; ++i) {
if (ans[i] != -1) {
std::cout << ans[i] << "\n";
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3656kb
input:
3 9 1 1 2 2 1 1 3 1 1 2 3 3 2 1 2 2 1 4 2 2 2 2 2 4 2 3 2 2 3 4
output:
2 2 2 5 4 4
result:
ok 6 tokens
Test #2:
score: 0
Accepted
time: 3ms
memory: 3640kb
input:
463 469 1 133 459 764026743 2 183 117202921 1 158 440 909903500 2 435 764026743 2 211 764026743 2 42 367154098 1 69 368 922135695 2 402 822018114 1 202 356 216161481 2 164 868998762 2 241 425329855 2 418 359511579 1 93 193 705174793 1 152 400 189968120 2 249 216161481 2 203 705174793 2 163 705174793...
output:
764026743 764026743 764026743 0 1673930243 1673930243 980188224 764026743 980188224 980188224 1469201536 980188224 1318123566 866824493 0 1170156344 1673930243 1318123566 1377402832 1377402832 2181407311 3600267814 1170156344 2973487440 2973487440 2596065938 2730635983 4722051084 1802391517 11505832...
result:
ok 244 tokens
Test #3:
score: 0
Accepted
time: 3ms
memory: 3704kb
input:
484 474 1 406 463 109881109 2 85 109881109 1 71 279 296583468 1 315 405 845762420 2 115 135758925 1 128 426 231832375 2 89 231832375 1 167 337 318859441 1 25 470 274413310 1 96 390 756372223 1 61 295 165754375 1 35 172 978585214 2 419 41174840 2 117 294153522 1 251 451 944227634 2 236 521058490 1 21...
output:
0 296583468 296583468 109881109 570996778 1371815132 1328522053 2444444094 845762420 345295930 2255904798 2444444094 765052838 1328522053 2444444094 3200132432 4050100321 736751153 2031540905 1848365562 2031540905 1328522053 1371815132 2607571312 1710265211 2839403687 1371815132 2575413683 127516868...
result:
ok 247 tokens
Test #4:
score: 0
Accepted
time: 2ms
memory: 3624kb
input:
470 497 1 213 315 688950079 2 182 935243607 2 284 759121488 1 17 412 380215020 2 85 579963660 1 181 196 334962150 2 370 192806152 2 215 224781692 2 131 872288452 2 337 26050024 1 114 431 814931189 2 52 669029567 2 249 380215020 2 309 441395068 1 1 274 975212452 1 183 246 274176464 2 53 334962150 2 2...
output:
0 688950079 380215020 380215020 1069165099 380215020 380215020 380215020 1069165099 1069165099 380215020 1069165099 1503881268 380215020 1355427472 1965561652 1965561652 1209360140 3103822294 1374266992 3264103557 2243231610 2507101297 380215020 1374266992 975212452 1444751791 4032072871 975212452 7...
result:
ok 247 tokens
Test #5:
score: 0
Accepted
time: 0ms
memory: 3720kb
input:
493 463 2 45 257485474 2 159 328451300 2 86 755935133 1 109 314 662788126 1 105 137 652307266 1 220 230 585078539 1 40 152 987732902 1 403 453 701143316 2 311 662788126 2 137 144772224 1 19 469 660871707 1 261 488 561462797 2 106 532966299 1 185 447 301674848 2 376 585078539 1 132 230 930489923 1 17...
output:
0 0 0 662788126 1315095392 652307266 1222334504 1323659833 1315095392 1602523819 1315095392 1623022004 2004107238 0 1902781909 2904760316 2071757535 2064739823 2440820492 1602523819 1923477820 3447171476 2415300834 2618238422 4416419392 561462797 2440820492 2343297519 3687668206 1741013550 248258922...
result:
ok 236 tokens
Test #6:
score: 0
Accepted
time: 3ms
memory: 3636kb
input:
457 470 1 292 453 720075728 2 452 720075728 2 216 720075728 2 391 905797884 2 164 509201732 1 158 411 33220056 2 258 364791470 1 240 445 997195517 2 249 889854506 1 66 234 672803045 1 93 131 525803264 1 59 390 995582144 2 112 730128063 2 352 627612593 2 104 776864255 2 312 476638972 2 114 997195517 ...
output:
720075728 0 720075728 0 33220056 1030415573 1668385189 753295784 1668385189 753295784 1668385189 720075728 720075728 1094109080 891677608 2494182634 1127329136 1914311192 1094109080 730721935 3475630869 1388507928 753295784 4169321965 720075728 500130774 2242082083 3256960114 672803045 2254587663 17...
result:
ok 245 tokens
Test #7:
score: 0
Accepted
time: 0ms
memory: 3644kb
input:
486 482 1 189 420 13562978 1 71 98 497680109 1 386 439 26860959 2 126 819724475 1 155 187 417451673 2 105 190622777 1 33 412 259382111 1 68 249 985928578 2 206 26860959 2 113 985928578 1 252 413 690032059 2 82 746894278 2 107 985928578 1 37 141 971858690 1 75 269 804759571 2 146 955713461 2 435 9718...
output:
0 0 272945089 1245310689 1483608687 1245310689 2050070260 26860959 962977148 272945089 962977148 989838107 1653135748 1959236505 1231240801 797587416 2829833337 448755906 259382111 2565781273 2565781273 1671737963 711397853 1178957167 259382111 1245310689 299806048 4236840922 4012233640 5008510123 5...
result:
ok 228 tokens
Test #8:
score: 0
Accepted
time: 0ms
memory: 3776kb
input:
456 485 1 284 381 243870266 2 294 243870266 2 221 333824628 2 344 257955215 1 169 390 555342089 1 412 451 890170638 1 134 215 413822665 1 227 248 467839622 1 416 438 482783488 1 43 226 690625572 2 80 690625572 1 14 93 242409105 1 317 421 812041726 1 60 170 30827120 1 6 93 112859626 1 78 423 43382958...
output:
243870266 0 243870266 690625572 1233041940 413822665 933034677 1245871311 444649785 799212355 444649785 1402994339 690625572 989171674 1155282277 1806783711 1689029006 3510858505 1155282277 1366864262 1245871311 1956572557 890170638 1233041940 3068360617 435895503 435895503 0 1427470248 1870249657 8...
result:
ok 245 tokens
Test #9:
score: 0
Accepted
time: 610ms
memory: 31696kb
input:
200000 200000 1 51313 114521 555056623 2 68565 479879347 2 160321 237655796 2 19629 323877634 1 74786 100241 586157367 1 79239 127700 292087607 2 160601 753692216 2 59935 594282500 2 199881 669670954 2 176742 478253487 2 58315 264892384 2 113864 836807945 2 191556 390162169 2 167124 883288716 1 4460...
output:
555056623 0 0 0 555056623 0 0 555056623 555056623 0 0 0 0 0 722918923 722918923 0 0 2833103567 968970654 1093577941 1215404365 3770087344 968970654 1277975546 1479485776 1093577941 1365240576 1111979640 4662965462 1111979640 81436293 1691889577 1584895623 1690533369 3770087344 1093577941 960688644 1...
result:
ok 100000 tokens
Test #10:
score: 0
Accepted
time: 129ms
memory: 18324kb
input:
200000 200000 2 94215 513319602 2 1176 117102044 2 181501 966490616 2 176617 402557165 2 27370 853611260 2 22211 12037731 2 162028 172866946 2 133426 573067240 2 145092 515765055 2 166059 204135617 2 135852 25575048 2 1837 397823360 2 99842 599957916 2 27523 398547296 2 27869 843297418 2 107494 5641...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 199900 tokens
Test #11:
score: 0
Accepted
time: 903ms
memory: 43332kb
input:
200000 200000 1 127950 193540 935545942 1 189440 190158 729551837 1 62857 119213 837264991 1 132519 148959 125766745 1 33229 81639 268951386 1 9463 91602 165627488 1 1525 71111 425080822 1 150182 191759 175255108 1 79455 119007 524444494 1 154893 166018 485699004 1 111658 141242 149759835 1 71643 96...
output:
707450694 8213350416 9024201226 1311461396 4528731386 9573074920 14797584372 5003543880 13046984674 7168329131 14306216062 4461150669 4555963051 12557676360 11174445890 3906033236 7613458168 9940604226 3055680653 8070176831 5994428170 7245015227 13409638878 14431145558 14267046265 14276682538 258380...
result:
ok 100 tokens
Test #12:
score: 0
Accepted
time: 539ms
memory: 29848kb
input:
200000 200000 2 119016 236789749 1 83 199029 108567257 2 169818 163281875 2 10238 5752673 1 468 199152 145551572 1 801 199683 897743813 1 4 199815 523438729 2 162775 263324119 1 711 199933 347795549 2 156957 248308862 2 147094 754712526 1 94 199323 250957293 2 197246 9999436 2 15011 873805051 1 453 ...
output:
0 108567257 108567257 1675301371 2023096920 1675301371 108567257 1675301371 2514007470 2274054213 2274054213 2514007470 2023096920 2274054213 2023096920 2274054213 3263794482 3263794482 3263794482 3263794482 2023096920 395984696 2514007470 2023096920 2023096920 2274054213 516401360 395984696 4072412...
result:
ok 100000 tokens
Test #13:
score: 0
Accepted
time: 361ms
memory: 28372kb
input:
200000 200000 2 134521 901986931 1 1 200000 550103434 1 1 200000 971671841 1 1 200000 942493350 2 29335 875123547 1 1 200000 558507112 1 1 200000 914437913 1 1 200000 829138829 1 1 200000 835881439 2 179046 314018872 2 76228 273248837 2 113816 700216405 2 168503 990187806 1 1 200000 712590545 1 1 20...
output:
0 2464268625 550103434 550103434 4766352479 1521775275 6037905698 6037905698 6441082010 3022775737 1931538186 6208123719 1931538186 6441082010 3433225655 1931538186 1931538186 6208123719 6208123719 4872801910 2227236572 1050384441 2422185320 2422185320 1494530179 6609991584 6208123719 2422185320 651...
result:
ok 100000 tokens
Test #14:
score: 0
Accepted
time: 822ms
memory: 43288kb
input:
200000 200000 1 78168 171119 1958 1 80444 149874 11768 1 56846 156923 13406 1 83143 184925 17882 1 12565 163196 21847 1 10704 197187 33248 1 94918 129949 35892 1 37100 168273 37178 1 49859 166583 40976 2 15399 713882729 1 33859 137898 43679 1 40673 137429 46146 1 67914 151456 48446 1 49683 164460 58...
output:
55095 460006 0 19633120 17727329 87698206 55933795 67332525 127268752 6029963 71925438 5396213 33463719 98351104 199105683 125519423 10131862 236146848 0 168445792 8874230 440924569 458386493 331038510 545258701 83165472 221451191 64845852 292666250 623280369 743856895 121231190 527548256 463760643 ...
result:
ok 10000 tokens
Test #15:
score: 0
Accepted
time: 764ms
memory: 41760kb
input:
200000 200000 1 5520 199378 999994999 1 3561 196227 999981541 1 8986 192575 999975739 1 9648 192417 999974741 1 5985 194355 999973599 1 8039 190033 999967787 1 3868 198209 999962646 1 2627 193683 999962333 1 8105 193561 999953290 1 7653 196980 999944743 1 5210 198551 999940586 1 6095 197532 99993674...
output:
17999066991 23736404052 7737422358 23736404052 37734127593 24218949120 12219783478 24514116566 95613169057 24514116566 96438659702 95613169057 24514116566 25187167263 25187167263 26040947724 25520398204 25503344002 26040947724 95613169057 3550585863 81615902304 25971562998 48885213006 26058535763 26...
result:
ok 10000 tokens
Test #16:
score: 0
Accepted
time: 595ms
memory: 31716kb
input:
200000 200000 2 160547 842350992 2 67014 443099976 2 63168 86066200 2 168437 924227232 2 159000 479779285 1 61568 199136 984363344 2 9952 745313897 2 55794 51205764 2 68840 994631863 2 151784 469771583 1 16303 156377 142426625 2 149321 210014038 2 107873 586153542 1 37809 79925 532 2 30139 15140317 ...
output:
0 0 0 0 0 0 0 984363344 984363344 1126789969 1126789969 142426625 1126790501 2027198244 854506623 2447303172 2447303172 2447303172 0 2447303172 3076265655 2013853155 854506623 3076265655 1884771619 2964429523 2964429523 2447303172 2013853155 2447303172 1060627232 1060627232 1547550784 1905688591 205...
result:
ok 100000 tokens