QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#415956 | #8718. 保区间最小值一次回归问题 | feecle6418 | AC ✓ | 439ms | 28352kb | C++17 | 2.1kb | 2024-05-21 13:02:25 | 2024-05-21 13:02:27 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5;
int n, m, lef[N + 5], L[N + 5], R[N + 5], X[N + 5], pl[N + 5];
int fa[N + 5], a[N + 5], q[N + 5];
int tn, pos[N + 5], tq, nd[N + 5], mxl[N + 5];
ll f[N + 5], ans;
void Clear() {
for (int i = 0; i <= max(n, m); i++) lef[i] = pl[i] = fa[i] = 0;
}
int gf(int x) { return x == fa[x] ? x : fa[x] = gf(fa[x]); }
ll Solve_v(int V) {
sort(pos + 1, pos + tn + 1);
fill(mxl, mxl + tn + 1, 0);
for (int i = 1; i <= tq; i++) {
int l = lower_bound(pos + 1, pos + tn + 1, L[nd[i]]) - pos;
int r = upper_bound(pos + 1, pos + tn + 1, R[nd[i]]) - pos - 1;
assert(l <= r);
mxl[r] = max(mxl[r], l);
}
for (int i = 1; i <= tn; i++) mxl[i] = max(mxl[i], mxl[i - 1]);
ll res = 1e18;
int l = 1, r = 1;
q[l] = f[0] = 0;
for (int i = 1; i <= tn; i++) {
mxl[i] = max(mxl[i], mxl[i - 1]);
int val = max(0, a[pos[i]] - V);
ans += max(0, V - a[pos[i]]);
while (l <= r && q[l] < mxl[i - 1]) l++;
f[i] = val + f[q[l]];
if (i >= mxl[tn]) res = min(res, f[i]);
while (l <= r && f[q[r]] > f[i]) r--;
q[++r] = i;
}
return res;
}
void Solve() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= m; i++) scanf("%d%d%d", &L[i], &R[i], &X[i]);
fill(lef + 1, lef + n + 1, 1);
for (int i = 1; i <= m; i++) pl[i] = i;
for (int i = 1; i <= n + 1; i++) fa[i] = i;
sort(pl + 1, pl + m + 1, [](int x, int y) { return X[x] > X[y]; });
ans = 0;
for (int i = 1, j; i <= m; i = j + 1) {
j = i;
while (j < m && X[pl[j + 1]] == X[pl[i]]) j++;
for (int k = i; k <= j; k++) {
int id = pl[k];
int o = gf(L[id]);
if (o > R[id]) return cout << "-1\n", Clear();
}
tn = tq = 0;
for (int k = i; k <= j; k++) {
int id = pl[k];
nd[++tq] = id;
while (1) {
int o = gf(L[id]);
if (o > R[id]) break;
lef[o] = X[id], fa[o] = o + 1, pos[++tn] = o;
}
}
ans += Solve_v(X[pl[i]]);
}
cout << ans << '\n';
Clear();
}
int main() {
int t;
scanf("%d", &t);
while (t--) Solve();
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3780kb
input:
1 3 2 2023 40 41 1 1 2022 2 3 39
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 400ms
memory: 22176kb
input:
1000 100 100 1 35141686 84105222 84105220 7273527 178494861 178494861 112519027 77833654 77833656 261586535 278472336 278472336 261586536 416361017 416361017 426649080 323519513 278472337 420127821 420127823 420127823 482516531 434108818 420127821 631535744 615930922 546346921 546346920 546346920 70...
output:
49 46 43 39 48 47 49 46 46 48 47 56 952 53 50 55 46 62 57 46 49 50 42 55 51 57 50 43 41 44 41 53 57 45 59 45 48 44 37 48 43 52 46 50 909 948 48 50 50 50 45 42 54 53 42 46 55 52 51 48 38 48 43 41 55 41 48 47 38 51 54 46 44 47 46 49 43 48 42 45 47 36 43 45 53 46 48 45 42 45 48 40 49 54 44 43 45 48 49 ...
result:
ok 1000 numbers
Test #3:
score: 0
Accepted
time: 417ms
memory: 21932kb
input:
1000 100 100 40 35141748 84105257 84105343 7273633 178494885 178495007 112519027 77833696 77833671 261586538 278472335 278472427 261586652 416361094 416361130 426649132 323519561 278472520 420127898 420127936 420127900 482516532 434108895 420127875 631535939 615931040 546346933 546346951 546347103 7...
output:
5391 5728 5395 4133 4671 3496 3629 5091 5285 5530 4748 5441 84743 6061 4524 6175 5755 5357 3835 5486 4698 5872 4955 5320 4374 5628 4426 4747 5019 4439 4361 5774 6141 6897 5578 5067 5036 5719 4380 4763 4098 4693 4514 5690 92018 84532 4776 5056 4351 5501 4885 4828 5223 6395 3673 6007 6028 5993 5289 45...
result:
ok 1000 numbers
Test #4:
score: 0
Accepted
time: 408ms
memory: 21756kb
input:
1000 100 100 148088468 98149781 583014390 471155624 805747464 361353765 809227446 405213819 979246312 746234854 328924822 293996764 439427756 927284157 709886804 719570910 987283502 607077506 460561747 601690465 661900658 689494603 664094557 738719400 968257412 849232761 704892887 981300891 63494525...
output:
13272909075 10500705683 12729662919 9598250477 14090962014 12379912845 12111344391 11664729042 11963959801 12049221193 11907426860 12443185625 241361559829 15859152345 12213559141 13098156239 16019877206 13030210346 11413013605 12943939894 15243504691 10167904190 13684987690 12625876652 13823026696 ...
result:
ok 1000 numbers
Test #5:
score: 0
Accepted
time: 2ms
memory: 3620kb
input:
1000 5 4 2 4 4 3 6 1 5 5 3 4 4 3 3 3 1 2 2 3 1 5 1 5 2 3 6 1 6 1 1 1 3 1 1 1 1 1 6 1 1 6 1 1 5 1 1 6 2 3 5 1 1 2 2 1 2 5 1 2 5 2 2 5 2 1 2 2 1 2 5 4 1 2 6 1 5 1 4 6 1 4 3 1 1 2 1 1 1 1 1 5 1 1 1 1 4 6 1 1 2 1 1 3 1 1 2 1 1 3 3 6 5 5 6 2 3 6 1 2 3 1 3 2 2 3 1 2 3 1 1 1 1 4 3 6 5 6 2 1 2 3 1 2 4 1 3 2...
output:
-1 6 -1 -1 -1 10 -1 -1 -1 -1 -1 -1 6 4 -1 -1 16 1 -1 7 9 -1 -1 2 -1 -1 -1 0 -1 1 2 4 -1 -1 -1 -1 -1 6 -1 -1 -1 -1 -1 5 -1 -1 -1 -1 4 -1 2 -1 -1 -1 1 -1 2 0 5 1 -1 -1 -1 -1 -1 8 -1 -1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3 3 5 3 5 4 6 1 1 3 4 1 1 2 3 3 2 2 3 3 2 3 6 4 4 5 2 2 5 1 2 3 4 4 2 3 4 2 1 4 6 2 3 2 4 1 2...
output:
-1 -1 -1 -1 -1 -1 -1 -1 3 6 0 -1 -1 -1 3 -1 -1 -1 -1 -1 4 -1 -1 -1 -1 -1 -1 2 4 15 -1 -1 1 -1 -1 0 -1 -1 -1 10 -1 -1 -1 -1 -1 -1 -1 -1 6 -1 -1 -1 3 -1 -1 -1 -1 -1 -1 -1 -1 6 -1 -1 -1 -1 14 -1 4 -1 -1 -1 -1 5 1 -1 -1 -1 3 -1 -1 -1 -1 -1 -1 -1 2 -1 -1 -1 1 -1 -1 -1 -1 3 -1 0 -1 -1 -1 4 -1 -1 -1 -1 -1 ...
result:
ok 1000 numbers
Test #8:
score: 0
Accepted
time: 407ms
memory: 20884kb
input:
1000 100 100 781659836 728394474 976072309 892982594 984461294 544357318 272504583 429726611 929869328 165593135 780537409 388550208 394106952 536386495 178682760 547490556 415752713 413623503 493296569 744570545 678280769 885687637 835319992 840071843 408228451 781310602 540636792 774246539 6120628...
output:
12705433408 55120033165837 13450258183 12735860443 11247877676 13939137030 12829445572 11574137865 11398808540 10697262762 13371010658 17258148491 16226464358 13897969840 15446107235 16212563843 12247652408 15352057207 14132651030 14226302785 13635621595 13689774675 11319898570 12683442363 902428534...
result:
ok 1000 numbers
Test #9:
score: 0
Accepted
time: 404ms
memory: 20952kb
input:
1000 2000 1141 929561418 449354985 881596928 339908475 381312783 866123819 740424342 565164163 920078862 621669462 436387841 878801388 720806069 460686281 657600480 981836697 309520504 864126423 538867850 248549332 892109348 890903917 752649234 749678128 581123944 623047728 836985413 566880975 59432...
output:
165420933213 12589548503 6726264286 17929568756 15494044403 16099615066 9783562265 13484879490 12984235550 13253784653 12809619989 12889073939 17918190063 281320331425 8099931090 12858519319 12312024494 14690241774 12389253869 12770602489 11442327026 10429814463 13569883942 14886708183 11159865597 1...
result:
ok 1000 numbers
Test #10:
score: 0
Accepted
time: 409ms
memory: 21352kb
input:
1000 2000 1141 2 1 313484123 304013173 9534484 9534484 227237070 227237069 227237070 227237070 227237070 687783316 427776542 227237069 227237070 358455992 227237069 227237068 227237068 227237070 518440217 775137168 227237069 599936591 358259977 358259978 358259977 227237069 227237070 227237070 22723...
output:
874 49 37 39 43 51 38 57 43 40 50 42 44 1058 45 56 47 53 49 44 50 44 47 48 38 48 56 52 48 43 53 49 41 48 50 49 57 44 43 987 37 40 42 55 30 55 57 53 50 50 37 44 54 53 53 43 48 57 54 47 53 45 39 53 44 48 56 49 54 55 56 -1 51 50 51 51 44 60 63 50 48 36 47 50 51 47 50 45 44 50 51 58 37 46 46 44 52 42 50...
result:
ok 1000 numbers
Test #11:
score: 0
Accepted
time: 418ms
memory: 21188kb
input:
1000 100 100 128230043 509596781 128230044 828409747 128230044 128230043 128230045 127019696 670097016 127019694 490051863 127019694 127019695 177747093 177747092 127019694 306342292 306342291 306342291 306342292 306342292 306342292 828409746 306342291 306342290 429863798 127019695 127019694 1401508...
output:
50 233597 40 48 53 52 47 52 51 39 48 50 48 55 48 51 44 50 49 46 42 45 48 49 49 51 43 46 51 50 39 50 56 48 33 41 45 51 49 49 52 47 44 644 38 51 45 43 38 50 43 38 51 42 958 49 47 54 45 42 50 47 48 40 45 48 49 53 47 46 37 40 47 43 50 54 45 40 48 37 47 50 51 46 53 971 49 48 59 39 52 45 37 47 54 49 54 39...
result:
ok 1000 numbers
Test #12:
score: 0
Accepted
time: 427ms
memory: 20880kb
input:
1000 100 100 128230081 509596798 128230064 828409762 128230074 128230055 128230089 127019734 670097053 127019738 490051894 127019712 127019731 177747109 177747100 127019698 306342304 306342301 306342298 306342320 306342326 306342307 828409748 306342296 306342325 429863807 127019738 127019745 1401508...
output:
1334 5078642 1186 943 1109 1552 859 1005 1396 786 1367 1241 1341 1442 1159 1239 1380 1519 1249 1083 1032 1138 1048 821 1221 1332 1309 974 865 971 1001 1140 1733 990 1030 982 999 1151 1020 902 1448 1111 1111 14656 1098 1263 1425 1200 1121 1190 975 1121 1175 999 21145 1123 1078 1401 1351 1229 1050 132...
result:
ok 1000 numbers
Test #13:
score: 0
Accepted
time: 439ms
memory: 21180kb
input:
1000 2000 1141 7 5 313484167 304013181 9534514 9534495 227237108 227237091 227237081 227237072 227237101 687783328 427776553 227237100 227237106 358456003 227237074 227237098 227237082 227237110 518440242 775137211 227237079 599936621 358259980 358260015 358260008 227237085 227237105 227237079 22723...
output:
15422 1156 1055 1453 1113 1312 1053 1385 1186 1147 1133 1015 1281 27883 1214 1390 1194 1138 1251 1263 1251 1044 1295 1057 938 1416 1505 1295 1372 1178 1234 1276 938 1041 1345 1849 1546 1064 1559 27273 1289 1238 1247 1480 1253 1570 1023 1414 1379 1172 838 1324 1283 1307 1092 1138 1383 1287 1377 1136 ...
result:
ok 1000 numbers
Test #14:
score: 0
Accepted
time: 264ms
memory: 28352kb
input:
2 500000 299999 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 100000000...
output:
2999999997 0
result:
ok 2 number(s): "2999999997 0"
Test #15:
score: 0
Accepted
time: 190ms
memory: 28176kb
input:
2 500000 199999 999999999 999999998 999999997 999999996 999999995 999999994 999999993 999999992 999999991 999999990 999999989 999999988 999999987 999999986 999999985 999999984 999999983 999999982 999999981 999999980 999999979 999999978 999999977 999999976 999999975 999999974 999999973 999999972 9999...
output:
199958999500003 499999999500000
result:
ok 2 number(s): "199958999500003 499999999500000"
Extra Test:
score: 0
Extra Test Passed