QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#115814 | #6520. Classic Problem | xaphoenix | AC ✓ | 947ms | 77332kb | C++14 | 4.0kb | 2023-06-27 13:20:35 | 2023-06-27 13:20:37 |
Judging History
你现在查看的是测评时间为 2023-06-27 13:20:37 的历史记录
- [2023-12-06 00:08:28]
- hack成功,自动添加数据
- (//qoj.ac/hack/481)
- [2023-08-10 23:21:45]
- System Update: QOJ starts to keep a history of the judgings of all the submissions.
- [2023-06-27 13:20:35]
- 提交
answer
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pf push_front
#define LC k<<1
#define RC k<<1|1
#define IO cin.sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repn(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = (n) - 1; i >= a; i--)
#define pern(i, a, n) for (int i = n; i >= a; i--)
typedef long long LL;
typedef long double LD;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;
typedef pair<double, double> PDD;
typedef pair<ull, ull> PUU;
typedef pair<LL, LL> PLL;
const int N = 410000;
const int M = 1100000;
const int mod = 1e9+7;
const int inf = (int)1e9;
const LL INF = 1e18;
const double eps = 1e-9;
mt19937_64 Rand((unsigned long long)new char);
#define rand Rand
int T, n, m, c[N], cnt, num, pl[N], pr[N];
struct edge {
int x, y, w;
friend bool operator < (edge a, edge b) {
return a.w < b.w;
}
}e[M], mn[N];
PII a[M];
LL ans;
map<int, int> S;
int f[N], fpre[N], fnxt[N], pre[N], nxt[N];
int find(int f[], int x) {
return f[x] == x ? x : f[x] = find(f, f[x]);
}
void update(int x, int y, int w) {
edge cur = {x, y, w};
if (cur < mn[x]) mn[x] = cur;
if (cur < mn[y]) mn[y] = cur;
}
vector<int> gpre[N], gnxt[N];
int main() {
IO;
cin >> T;
while (T--) {
cin >> n >> m;
cnt = num = ans = 0;
repn(i, 1, m) {
cin >> e[i].x >> e[i].y >> e[i].w;
c[++cnt] = e[i].x, c[++cnt] = e[i].y;
}
sort(c + 1, c + cnt + 1);
c[++cnt] = n + 1;
S.clear();
repn(i, 1, cnt) {
if (S.count(c[i])) continue;
int l = c[i - 1] + 1, r = c[i] - 1;
if (l <= r) S[r] = ++num, ans += r - l, pl[num] = l, pr[num] = r;
if (c[i] <= n) {
S[c[i]] = ++num;
pl[num] = pr[num] = c[i];
}
}
repn(i, 1, num) f[i] = i, gpre[i].clear(), gnxt[i].clear();
repn(i, 1, m) {
e[i].x = S.lower_bound(e[i].x) -> se, e[i].y = S.lower_bound(e[i].y) -> se;
gpre[e[i].y].pb(e[i].x), gnxt[e[i].x].pb(e[i].y);
}
repn(i, 1, num) sort(all(gnxt[i])), sort(all(gpre[i])), reverse(all(gpre[i]));
pre[1] = nxt[num] = 0;
while (1) {
int flag = 0;
repn(i, 2, num) if (find(f, i) != find(f, 1)) flag = 1;
if (!flag) break;
repn(i, 1, num) mn[find(f, i)] = {0, 0, inf + 10}, fpre[i] = fnxt[i] = i, pre[i] = nxt[i] = 0;
// left
repn(i, 2, num) {
int fx = find(f, i - 1), fy = find(f, i);
if (fx == fy) fpre[i] = i - 1;
}
repn(i, 1, num) {
int head = 0;
pern(cur, 1, i) {
if (find(f, cur) == find(f, i)) cur = find(fpre, cur), cur--;
if (cur == 0) break;
while (head < gpre[i].size() && cur < gpre[i][head]) head++;
if (head < gpre[i].size() && cur == gpre[i][head]) continue;
pre[i] = cur;
break;
}
}
// right
repn(i, 1, num) fnxt[i] = i;
per(i, 1, num) {
int fx = find(f, i + 1), fy = find(f, i);
if (fx == fy) fnxt[i] = i + 1;
}
pern(i, 1, num) {
int head = 0;
repn(cur, i, num) {
if (find(f, cur) == find(f, i)) cur = find(fnxt, cur), cur++;
if (cur > num) break;
while (head < gnxt[i].size() && cur > gnxt[i][head]) head++;
if (head < gnxt[i].size() && cur == gnxt[i][head]) continue;
nxt[i] = cur;
break;
}
}
// adjacent
repn(i, 1, num) {
if (pre[i]) update(find(f, i), find(f, pre[i]), pl[i] - pr[pre[i]]);
if (nxt[i]) update(find(f, i), find(f, nxt[i]), pl[nxt[i]] - pr[i]);
}
// special edges
repn(i, 1, m) {
int fx = find(f, e[i].x), fy = find(f, e[i].y);
if (fx != fy) update(fx, fy, e[i].w);
}
repn(i, 1, num) {
if (find(f, i) == i) {
int fx = find(f, mn[i].x), fy = find(f, mn[i].y);
if (fx != fy) {
ans += mn[i].w;
f[fx] = fy;
}
}
}
}
cout << ans << "\n";
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 40704kb
input:
3 5 3 1 2 5 2 3 4 1 5 0 5 0 5 4 1 2 1000000000 1 3 1000000000 1 4 1000000000 1 5 1000000000
output:
4 4 1000000003
result:
ok 3 number(s): "4 4 1000000003"
Test #2:
score: 0
Accepted
time: 947ms
memory: 74776kb
input:
16 1000000000 0 447 99681 1 2 1000000000 1 3 1000000000 2 3 1000000000 1 4 1000000000 2 4 1000000000 3 4 1000000000 1 5 1000000000 2 5 1000000000 3 5 1000000000 4 5 1000000000 1 6 1000000000 2 6 1000000000 3 6 1000000000 4 6 1000000000 5 6 1000000000 1 7 1000000000 2 7 1000000000 3 7 1000000000 4 7 ...
output:
999999999 446000000000 732256441 999999680 999899999 999966830 127 4 2186 1562 1176 5100 5 503 679 4
result:
ok 16 numbers
Test #3:
score: 0
Accepted
time: 835ms
memory: 75244kb
input:
5 1000000000 100000 158260500 877914550 822889311 24979400 861648750 548830120 433933400 476190600 342858071 211047200 971407750 480845266 731963950 822804750 449656269 430302150 982631900 545923679 880895700 923078500 816372580 189330700 910286900 135599877 303238500 404539650 605997004 492686550 7...
output:
999999999 999999999 999999999 999999999 999999999
result:
ok 5 number(s): "999999999 999999999 999999999 999999999 999999999"
Test #4:
score: 0
Accepted
time: 563ms
memory: 74792kb
input:
5 2000 100000 1873 1874 822889311 1290 1291 548830120 1162 1163 342858071 814 815 480845266 742 743 449656269 1609 1610 545923679 1431 1432 816372580 1143 1144 135599877 414 415 605997004 1117 1118 921749002 121 122 786119025 1672 1673 655702582 38 39 211428413 1639 1640 393671555 922 923 501983447 ...
output:
4097 20020 198635 2099999 1000099999
result:
ok 5 number(s): "4097 20020 198635 2099999 1000099999"
Test #5:
score: 0
Accepted
time: 517ms
memory: 66400kb
input:
5 100000 100000 36426 60522 446755913 60522 90081 181424399 3497 60522 625711486 60522 94325 647639160 60522 68417 160714711 35902 60522 61020590 23857 60522 626006012 29211 60522 547865075 60522 63340 561330066 60016 60522 650693494 24593 60522 849028504 60522 90310 285323416 11431 60522 990607689 ...
output:
100024 150003 200003 250000 999999999
result:
ok 5 number(s): "100024 150003 200003 250000 999999999"
Test #6:
score: 0
Accepted
time: 258ms
memory: 45860kb
input:
5 4000 100000 694 696 619136615 1611 1614 829153748 2551 2552 370724298 3034 3037 49559541 98 105 443754249 822 827 735959328 878 885 201346483 2729 2731 304071225 3961 3965 557890416 1631 1637 430215474 3195 3205 212882505 503 507 937363346 3141 3150 47574456 577 583 727402691 3673 3675 279029853 3...
output:
43935 6913 4336 18350 12678
result:
ok 5 number(s): "43935 6913 4336 18350 12678"
Test #7:
score: 0
Accepted
time: 769ms
memory: 77332kb
input:
5 100000 100000 73979 73980 5250107 1946 1947 757506029 87433 87434 443673136 64352 64354 338125488 69103 69104 235256717 60843 60845 20769731 23601 23602 214534313 92417 92419 669411181 57364 57365 519766962 42029 42031 827237806 98524 98525 593643533 71482 71483 662414293 6709 6710 745687608 36460...
output:
166767 127085 110197 1000033410 1000009975
result:
ok 5 number(s): "166767 127085 110197 1000033410 1000009975"
Test #8:
score: 0
Accepted
time: 792ms
memory: 62592kb
input:
5 1000000000 50000 46934929 214668318 1 539508531 807854772 3 81957905 439197851 0 259767671 917896915 1 415730678 521821941 0 728421737 764345460 2 266428928 403328188 2 287022349 739465773 2 374255863 866291129 2 260858097 908362294 1 413843056 908357026 3 498835884 656136616 4 294506717 617875204...
output:
999989920 999949999 999990020 999949999 999991707
result:
ok 5 number(s): "999989920 999949999 999990020 999949999 999991707"
Test #9:
score: 0
Accepted
time: 187ms
memory: 41500kb
input:
10 318 50000 202 215 664792164 27 240 237398902 211 303 234481289 30 100 677362025 22 264 146815592 119 291 400424695 259 315 252405962 216 247 5088627 157 290 178189323 155 316 734981880 193 247 88649525 111 284 566532841 131 228 543880192 35 295 517550073 11 162 662675576 222 260 74089536 317 318 ...
output:
23819960 39448612 8097046 59114094 45057369 51858561 4062351 79061552 43095015 16693179
result:
ok 10 numbers
Test #10:
score: 0
Accepted
time: 173ms
memory: 39384kb
input:
100 102 5000 48 70 8432902 25 75 334675824 56 84 328506917 62 66 204777391 14 27 171625816 26 63 83629619 57 101 321540985 41 85 311469363 15 66 351390467 25 26 211230409 35 74 290758638 11 52 127872342 29 41 36722590 7 40 305168650 57 59 281651751 13 19 227794663 11 68 263248839 11 13 201312984 4 3...
output:
26178167 22787545 57728222 59428785 9611076 18313917 46857052 2980349 14019519 7766478 32126352 23015186 24398047 5999746 38936614 16904414 35847382 20532753 4984654 19717352 5263678 25996814 15710632 604534 14654144 6955086 27641478 3343419 23105160 26432195 5553390 7056453 53902303 169550500 23592...
result:
ok 100 numbers
Test #11:
score: 0
Accepted
time: 160ms
memory: 40028kb
input:
500 50 1000 30 37 244841531 34 50 162121748 2 24 54057560 13 25 99874751 6 23 8670341 34 39 89926367 1 22 221235953 6 24 105286564 13 34 326397341 3 46 290792604 11 24 346530259 28 36 211580979 3 9 238363354 11 28 280948971 6 36 179816637 18 32 156586941 20 23 98299400 12 50 111363373 22 26 11434081...
output:
189 220 230 182 158 173 200 220 189 164 176 202 142 198 172 239 197 192 183 162 157 250 152 216 243 164 196 189 201 180 183 210 191 202 191 184 178 181 228 210 192 204 219 200 198 215 224 202 143 182 223 186 187 196 218 179 209 178 177 184 182 223 227 199 182 220 208 203 165 205 187 191 201 188 197 ...
result:
ok 500 numbers
Test #12:
score: 0
Accepted
time: 157ms
memory: 39792kb
input:
5000 16 100 7 11 287768961 9 16 301149044 4 6 274782222 9 10 450284038 4 7 235183489 2 16 58894948 2 7 238261638 1 15 4511525 4 14 199094011 9 15 18708487 10 14 424492139 5 12 289396677 6 10 205753555 14 16 379921511 9 14 361051609 8 11 462083923 12 16 49451818 7 13 207151488 9 11 468320084 7 9 4016...
output:
84 71179265 61 63 4255393 72 25533862 58 87249761 71 84 19481903 71 46709478 822790 10042255 24930386 63 69894 58 48150185 862500 224220 42138089 740049 8599540 58154641 67865973 14516424 70 87090761 67494787 13034240 91 5581460 331588 67 9506689 80 34867161 43217292 75264458 56 1335841 22470126 266...
result:
ok 5000 numbers
Test #13:
score: 0
Accepted
time: 273ms
memory: 39868kb
input:
5000 2000 100 268 269 623604670 964 965 850016410 1696 1697 958745313 1507 1508 723608729 970 971 753143786 1384 1385 247122568 1448 1449 877299742 1096 1097 795129348 1042 1043 221000653 357 358 298701522 1767 1768 316413195 515 516 229633720 1682 1683 901777899 1181 1182 693437267 1960 1961 867110...
output:
2099 10099 100099 2000099 1000000099 2099 10099 100099 2000099 1000000099 2099 10099 100099 2000099 1000000099 2099 10099 100099 2000099 1000000099 2099 10099 100099 2000099 1000000099 2099 10099 100099 2000099 1000000099 2099 10099 100099 2000099 1000000099 2099 10099 100099 2000099 1000000099 2099...
result:
ok 5000 numbers