QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#645893 | #1365. Dominating Duos | njupt_zy | AC ✓ | 366ms | 273248kb | C++23 | 2.4kb | 2024-10-16 20:23:47 | 2024-10-16 20:23:47 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using u32 = unsigned;
using i64 = long long;
using u64 = unsigned long long;
constexpr int N = 1000010;
int a[N];
int l[N], r[N], cnt[50 * N], ls[50 * N], rs[50 * N];
int idx;
void add(int p, int &q, int l, int r, int x, int v) {
cnt[q = ++idx] = cnt[p];
ls[q] = ls[p];
rs[q] = rs[p];
if (l == r) {
cnt[q] += v;
return;
}
int m = l + r >> 1;
if (x <= m) {
add(ls[p], ls[q], l, m, x, v);
} else {
add(rs[p], rs[q], m + 1, r, x, v);
}
cnt[q] = cnt[ls[q]] + cnt[rs[q]];
}
int query(int p, int q, int l, int r, int x, int y) {
if (l > y || r < x) {
return 0;
}
if (l >= x && r <= y) {
return cnt[q] - cnt[p];
}
int m = l + r >> 1;
return query(ls[p], ls[q], l, m, x, y) + query(rs[p], rs[q], m + 1, r, x, y);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<int> stk;
for (int i = 1; i <= n; i++) {
if (stk.empty()) {
stk.push_back(i);
}
while (!stk.empty() && a[i] > a[stk.back()]) {
stk.pop_back();
}
if (!stk.empty() && i != 1) {
l[i] = stk.back();
}
stk.push_back(i);
}
stk.clear();
for (int i = 1; i <= n; i++) {
r[i] = n + 1;
}
for (int i = n; i >= 1; i--) {
if (stk.empty()) {
stk.push_back(i);
}
while (!stk.empty() && a[i] > a[stk.back()]) {
stk.pop_back();
}
if (!stk.empty() && i != n) {
r[i] = stk.back();
}
stk.push_back(i);
}
// for (int i = 1; i <= n; i++) {
// cerr << l[i] << " \n"[i == n];
// }
vector<int> root(n + 2);
for (int i = 1; i <= n; i++) {
add(root[i - 1], root[i], 0, n + 1, l[i], 1);
}
i64 ans = n - 1;
// int ans = 0;
for (int i = 1; i < n; i++) {
int R = (r[i] != n + 1 ? r[i] : n);
if (R >= i + 1) {
ans += query(root[i + 1], root[R], 0, n + 1, 0, i);
}
// cerr << i << " " << r[i] << " " << query(root[i], r[i] != n + 1 ? root[r[i] - 1] : root[n - 1], 0, n + 1, 0, i) << "\n";
}
cout << ans << "\n";
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 9720kb
input:
6 5 4 1 3 2 6
output:
9
result:
ok single line: '9'
Test #2:
score: 0
Accepted
time: 2ms
memory: 9708kb
input:
7 7 5 3 1 2 4 6
output:
11
result:
ok single line: '11'
Test #3:
score: 0
Accepted
time: 1ms
memory: 7928kb
input:
2 1 2
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 2ms
memory: 9972kb
input:
2 2 1
output:
1
result:
ok single line: '1'
Test #5:
score: 0
Accepted
time: 344ms
memory: 271440kb
input:
1000000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 ...
output:
999999
result:
ok single line: '999999'
Test #6:
score: 0
Accepted
time: 357ms
memory: 268528kb
input:
1000000 132688 285164 789988 995739 957234 130646 181990 390985 39400 169852 338142 243039 138781 101935 65454 181372 669660 552019 306420 956162 250294 690026 762175 81413 111955 706117 559279 227057 329009 170181 31563 832603 284155 435293 806726 908654 2086 183340 740499 786030 466870 235655 7314...
output:
1999975
result:
ok single line: '1999975'
Test #7:
score: 0
Accepted
time: 334ms
memory: 273248kb
input:
1000000 1000000 999999 999998 999997 999996 999995 999994 999993 999992 999991 999990 999989 999988 999987 999986 999985 999984 999983 999982 999981 999980 999979 999978 999977 999976 999975 999974 999973 999972 999971 999970 999969 999968 999967 999966 999965 999964 999963 999962 999961 999960 9999...
output:
999999
result:
ok single line: '999999'
Test #8:
score: 0
Accepted
time: 315ms
memory: 270264kb
input:
1000000 1000000 999998 999996 999994 999992 999990 999988 999986 999984 999982 999980 999978 999976 999974 999972 999970 999968 999966 999964 999962 999960 999958 999956 999954 999952 999950 999948 999946 999944 999942 999940 999938 999936 999934 999932 999930 999928 999926 999924 999922 999920 9999...
output:
1999997
result:
ok single line: '1999997'
Test #9:
score: 0
Accepted
time: 332ms
memory: 270064kb
input:
1000000 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173...
output:
999999
result:
ok single line: '999999'
Test #10:
score: 0
Accepted
time: 366ms
memory: 268292kb
input:
1000000 337797 203097 236860 865126 543423 61858 481710 408580 297968 91437 31136 171854 787011 226820 754784 40023 145010 359212 631215 31698 772587 797247 250481 383284 217285 58099 648128 130733 22390 881861 487374 216476 958000 497478 229846 577205 163656 287148 699792 936572 984840 276225 29816...
output:
1999976
result:
ok single line: '1999976'
Test #11:
score: 0
Accepted
time: 331ms
memory: 269864kb
input:
1000000 673837 797266 510963 254248 13430 483474 837350 50074 700533 242241 230110 440315 185351 444253 207160 253498 948305 744966 78889 102864 636401 684630 305587 294176 600274 471943 135247 148504 672589 392850 345380 736448 303704 94133 108650 34863 667351 437483 54827 718070 45577 392673 96373...
output:
1999962
result:
ok single line: '1999962'
Test #12:
score: 0
Accepted
time: 362ms
memory: 268264kb
input:
1000000 327972 326281 681409 735858 756844 259353 374674 761914 122194 955302 884641 632105 221601 33531 654301 4287 418605 552998 654262 428460 164206 722364 404226 90520 692255 315429 795290 695807 603028 733212 707347 15921 607362 202958 66473 184500 252820 891566 319860 473292 504545 120087 5437...
output:
1999976
result:
ok single line: '1999976'
Test #13:
score: 0
Accepted
time: 345ms
memory: 269252kb
input:
1000000 416083 864576 527877 370035 116102 808795 327984 170796 205941 140392 567168 604125 492140 782682 397165 247376 586565 75480 19463 366661 661866 802895 435373 532505 676951 480728 694120 700019 305600 273297 247734 789740 622332 470625 639610 159421 272730 855600 862359 545455 634570 203687 ...
output:
1999971
result:
ok single line: '1999971'
Test #14:
score: 0
Accepted
time: 357ms
memory: 269696kb
input:
1000000 199055 144639 173154 455157 240891 762427 572211 339536 747565 768041 543857 32881 84456 358024 901190 131457 249557 329412 341805 229822 874580 854642 375474 514882 270194 638371 62564 404941 211448 171215 678268 144888 695971 598430 185697 104684 423733 654433 457245 330503 741697 25102 97...
output:
1999970
result:
ok single line: '1999970'
Test #15:
score: 0
Accepted
time: 265ms
memory: 222628kb
input:
830261 767972 267074 19373 102358 362220 290434 254657 421114 382178 489500 327847 430258 476006 662738 162433 448624 45104 569034 430581 454621 123743 310615 285474 441775 410869 493192 608556 28415 294933 389041 698040 602700 457083 69512 170721 418407 215850 771777 145875 46163 207662 87049 1153 ...
output:
1660498
result:
ok single line: '1660498'
Test #16:
score: 0
Accepted
time: 50ms
memory: 45884kb
input:
157371 87916 34968 35100 65676 95352 97845 93262 79662 39556 154855 134617 41017 57875 38914 36979 53365 298 105384 118973 133256 136277 55982 413 38698 67695 115856 24876 98853 19408 100947 105536 147410 35822 90222 70542 20548 63079 25802 125029 110710 7718 140237 10195 99476 125265 19627 142762 5...
output:
314724
result:
ok single line: '314724'
Test #17:
score: 0
Accepted
time: 178ms
memory: 143084kb
input:
519525 45611 171943 113713 109866 119074 290576 118916 508864 352356 205928 453792 321117 50169 465013 89922 297257 279567 218702 66094 311095 364510 123463 207014 491052 43977 252690 80642 256993 118396 272599 401246 297077 173377 54486 158425 159897 16438 27038 216927 111072 269922 182179 26582 77...
output:
1039014
result:
ok single line: '1039014'
Test #18:
score: 0
Accepted
time: 0ms
memory: 11484kb
input:
8638 2908 148 928 3685 5096 2907 8513 7252 1056 207 2375 6701 1841 4057 4577 1070 83 8250 2624 2053 6414 6752 1909 482 4 6515 3963 7816 3918 4722 1011 6202 7335 2006 66 829 5651 527 3577 2540 1178 1202 3440 8419 5304 5832 6425 3228 1097 7551 2536 4720 601 2475 378 1324 8115 2276 6264 93 808 2777 714...
output:
17256
result:
ok single line: '17256'
Test #19:
score: 0
Accepted
time: 280ms
memory: 214412kb
input:
793943 529927 520907 689960 348007 116812 586703 94030 89155 161391 557863 38610 465132 281240 277 111111 651752 640145 117867 133270 332307 15133 132605 328591 85185 613776 612332 563030 647244 307822 428319 234954 556971 117206 328877 688595 361685 609138 675657 326201 293342 295080 225626 17273 1...
output:
1587858
result:
ok single line: '1587858'