QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#862013 | #9904. 最小生成树 | Fffoooo | 100 ✓ | 828ms | 38232kb | C++14 | 4.9kb | 2025-01-18 21:07:50 | 2025-02-04 17:19:54 |
Judging History
This is the latest submission verdict.
- [2025-02-04 17:09:58]
- hack成功,自动添加数据
- (/hack/1513)
- [2025-02-04 16:41:38]
- hack成功,自动添加数据
- (/hack/1510)
- [2025-01-18 21:07:50]
- Submitted
answer
#include<bits/stdc++.h>
using namespace std;
int mian(); int main() { return mian(); }
#define ll long long
#define ull unsigned ll
const int N = 2e5 + 25;
int n, a[N << 1], _;
int id[N << 1];
const ull base = 1e9 + 9;
ull pw[N];
void init_pw() {
pw[0] = 1; for(int i = 1; i <= n; ++i) pw[i] = pw[i - 1] * base;
//初始化不要写错
}
struct UF {
int fa[N];
vector<int> mbs[N];
void init() { for(int i = 1; i <= n; ++i) fa[i] = i, mbs[i].push_back(i); }
int get(const int x) { if(x == fa[x]) return x;; return fa[x] = get(fa[x]); }
int us(int u, int v) {
u = get(u); v = get(v);
if(u == v) return 0;
if((int)mbs[u].size() < (int)mbs[v].size()) swap(u, v);
for(auto vp : mbs[v]) mbs[u].push_back(vp);//不要写成 v
fa[v] = u;
return v;
}
}uf;
#define lson (k << 1)
#define rson (k << 1 | 1)
#define mid ((l + r) >> 1)
ull t1[N << 2], t2[N << 2];
void updata(const int k, const int l, const int r) {
t1[k] = pw[r - mid] * t1[lson] + t1[rson];
t2[k] = t2[lson] + pw[mid - l + 1] * t2[rson];
}
void build(const int k, const int l, const int r) {
if(l == r) return t1[k] = t2[k] = l, void();
build(lson, l, mid); build(rson, mid + 1, r);
updata(k, l, r);
}
void change(const int k, const int l, const int r, const int x) {
if(l == r) return t1[k] = t2[k] = uf.get(l), void();
if(x <= mid) change(lson, l, mid, x);
else change(rson, mid + 1, r, x);
updata(k, l, r);
}
ull ask1(const int k, const int l, const int r, const int x, const int y) {
// ull ans = 0;
// for(int i = x; i <= y; ++i) ans = ans * base + uf.get(i);
// return ans;
if(x <= l and r <= y) return t1[k];
if(x > mid) return ask1(rson, mid + 1, r, x, y);
if(y <= mid) return ask1(lson, l, mid, x, y);
return pw[min(r, y) - mid] * ask1(lson, l, mid, x, y) + ask1(rson, mid + 1, r, x, y);
}
ull ask2(const int k, const int l, const int r, const int x, const int y) {
// ull ans = 0;
// for(int i = y; i >= x; --i) ans = ans * base + uf.get(i);
// return ans;
if(x <= l and r <= y) return t2[k];
if(x > mid) return ask2(rson, mid + 1, r, x, y);
if(y <= mid) return ask2(lson, l, mid, x, y);
return ask2(lson, l, mid, x, y) + ask2(rson, mid + 1, r, x, y) * pw[mid - max(x, l) + 1];
}
#undef lson
#undef rson
#undef mid
ll ans;
int ceii(const int x, const int y) {
if(x % y == 0) return x / y;;
return x / y + 1;
}
void solve(const int u) {
int Ll = max(1, u - n), Lr = (u - 1) >> 1, Rl = ceii(u + 1, 2), Rr = min(n, u - 1);
while( ask1(1, 1, n, Ll, Lr) != ask2(1, 1, n, Rl, Rr) ) {
// int l1 = Ll, r1 = Lr, l2 = Rl, r2 = Rr, ans1 = 0, ans2 = 0;
// while(l1 <= r1) {
// // if(ask1(1, 1, n, l1, r1) == ask2(1, 1, n, l2, r2)) break;
// const int mid1 = (l1 + r1) >> 1, mid2 = ceii(l2 + r2, 2);
// if( ask1(1, 1, n, Ll, mid1) != ask2(1, 1, n, mid2, Rr) ) ans1 = mid1, ans2 = mid2, r1 = mid1 - 1, l2 = mid2 + 1;
// else l1 = mid1 + 1, r2 = mid2 - 1;
// }
// const int out = uf.us(ans1, ans2);
// assert(out);
// assert(ans1 + ans2 == u and ans1 and ans2);
int l = Ll, r = Lr, ant = 0;
while(l <= r) {
const int mid = (l + r) >> 1;
// cout<<Ll<<" "<<mid<<" "<<u - mid<<" "<<Rr<<endl;
if(ask1(1, 1, n, Ll, mid) != ask2(1, 1, n, u - mid, Rr)) ant = mid, r = mid - 1;
else l = mid + 1;
}
const int out = uf.us(ant, u - ant);
// cout<<ans<<" "<<u - ans<<" "<<out<<endl;
// cout<<ask1(1, 1, n, 1, 1)<<" "<<ask2(1, 1, n, 3, 3)<<endl;
assert(ant and out);
ans += a[u];
// for(int i = 1; i <= n; ++i) cout<<uf.get(i)<<" ";; puts("");
for(auto xf : uf.mbs[out]) change(1, 1, n, xf);
}
}
int mian() {
_ = scanf("%d", &n);
for(int i = 3; i < (n << 1); ++i) _ = scanf("%d", &a[i]), id[i] = i;
sort(id + 3, id + (n << 1), [](const int A, const int B){ return a[A] < a[B]; });
init_pw(); uf.init(); build(1, 1, n);
for(int i = 3; i < (n << 1); ++i) solve(id[i]);
printf("%lld\n", ans);
return 0;
}/*
给定一个 $n$ 个点的完全图,两个点 $i,j$ 的边权为 $a_{i+j}$,求最小生成树的边权和
$2\le n\le 2\times 10^5, 1\le a_i\le 10^9$
$1s, 1G$
不好用 bru 实现,所以考虑 kruskal
从大到小枚举所有的边权,假设当前是 $a_u$
那么就是要快速判断每一对 $i+j=u$ 是否不再同一个连通块中
不妨假设 $i\lt j$,那么就有 $i\le \frac{u-1}{2},j\lt u$
此时会发现如果一个 $i,j$ 在同一个连通块内,那么会关于中间的地方对称
那么人、如果将一个点的权值设置为他的连通块,相当于判断回文
于是用字符串 hash 的形式判断回文,对于每两个位置,如果他们不合法,那么每一次用二分找到第一个不在连通块内的并把他们合并
合并的时候可以用启发式合并,因为会涉及到修改每一个位置。
那么他因为有修改,所以也就是线段树维护。因为至多会进行 $n-1$ 次二分操作,且启发式合并,所以时间复杂度为 $O(n\log^2 n)$
*/
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 1ms
memory: 16216kb
input:
1000 345 432 641 771 49 37 930 572 1083 733 1309 1495 818 883 1510 1079 1718 1873 1795 1903 1850 2255 2309 2022 2503 2619 2633 2628 3089 3263 2021 3361 2718 3104 3509 2714 3708 3994 3716 3830 3429 4128 4272 4464 4137 4719 4383 4775 3639 4700 5203 4170 4797 5033 4930 5201 5254 5705 5682 5753 5327 611...
output:
23841384
result:
ok 1 number(s): "23841384"
Test #2:
score: 10
Accepted
time: 1ms
memory: 16240kb
input:
1000 7907859 8299401 2595272 8647244 4654300 116556 9058074 9457856 3976958 11518861 1620710 10332687 19015127 15766719 18789607 17873140 21089373 14170673 12490174 24529096 25019840 25561985 98735 23117075 25603838 24631554 24173505 19647554 31525691 31244267 27972151 24041201 29476743 32211677 381...
output:
245822716951
result:
ok 1 number(s): "245822716951"
Test #3:
score: 10
Accepted
time: 14ms
memory: 16908kb
input:
10000 340635 376047 136654 618815 159696 710061 835001 815618 256229 497032 1343755 1320451 1132380 1539973 1623473 1693936 742042 1524274 1360190 2316919 2412274 1845055 1512219 1892430 2508203 2868404 2749708 2440259 3056384 3057456 3265524 2966678 3189137 3378488 3643366 3679507 3257152 3353380 3...
output:
2486173886938
result:
ok 1 number(s): "2486173886938"
Test #4:
score: 10
Accepted
time: 744ms
memory: 35272kb
input:
199999 573770324 414266051 954722842 267823478 158683708 565067390 925135266 174993733 436964444 553816220 604600347 779139383 174739126 949957679 854403239 570026878 788469644 953674374 876965030 454165131 744511837 657999668 214085562 499964686 298322155 359355913 174185410 539605497 272107319 335...
output:
1369136336
result:
ok 1 number(s): "1369136336"
Test #5:
score: 10
Accepted
time: 828ms
memory: 38232kb
input:
199995 915098216 2961244 759529641 482072770 96198925 737065230 449814468 129834835 621735650 54036166 218513119 558467906 677940504 13599685 995819084 510352113 707972383 120352652 589903002 16908544 762512553 739109339 372437660 222576208 513404326 217333429 185120609 391592963 508209737 783114329...
output:
1028748453
result:
ok 1 number(s): "1028748453"
Test #6:
score: 10
Accepted
time: 458ms
memory: 33436kb
input:
190000 12647 1015 25075 32877 14131 35973 34224 413 44756 46889 54548 54872 63138 61206 71980 85368 66953 33960 95604 43956 100468 110045 110651 72002 125677 123806 133907 140928 103006 130963 143904 169665 170636 165176 183128 175044 182777 187911 196733 199113 207536 212192 226285 232248 234909 21...
output:
42711887507849
result:
ok 1 number(s): "42711887507849"
Test #7:
score: 10
Accepted
time: 455ms
memory: 33980kb
input:
200000 8109 14228 14749 32990 56681 15919 53325 47174 74650 67783 86459 63058 88143 36203 104344 35793 26382 110478 59803 128301 99948 86098 111738 64341 86847 72244 140370 82851 137577 177720 131119 154285 147726 149828 200941 221507 192221 224332 229763 181710 202066 252618 208158 257523 235035 26...
output:
45006690224362
result:
ok 1 number(s): "45006690224362"
Test #8:
score: 10
Accepted
time: 446ms
memory: 33980kb
input:
200000 41774 48021 118119 29186 108563 102700 86530 135784 76652 29845 41180 3354 87818 104774 155088 70654 152906 70557 171701 185768 3583 5786 145566 29866 215765 51785 226476 148960 212212 10833 250086 183769 173223 143125 136002 151292 278423 156833 262389 113210 283038 103297 278088 183899 3260...
output:
49872168142307
result:
ok 1 number(s): "49872168142307"
Test #9:
score: 10
Accepted
time: 450ms
memory: 33940kb
input:
199995 21953 33999 14594 52551 36028 54566 56411 22575 110079 80430 106838 65069 106309 6700 140637 96336 15178 37399 147814 148267 143572 133807 33223 196777 152621 187806 141393 206582 219417 202758 224038 219972 226860 188342 204760 226982 226678 131378 276188 192400 16500 296583 235472 308907 28...
output:
50034940148604
result:
ok 1 number(s): "50034940148604"
Test #10:
score: 10
Accepted
time: 464ms
memory: 33988kb
input:
199992 15109 10774 19215 25971 19703 9215 31839 29367 42010 30812 43875 31508 40758 64566 22408 68451 60879 61748 29806 78007 76364 88577 52117 73605 106032 86763 127486 99171 117902 139055 167133 87789 107406 157602 160466 182539 183096 172886 187343 195740 175495 199166 125460 167310 209709 203205...
output:
50087500724413
result:
ok 1 number(s): "50087500724413"
Extra Test:
score: 0
Extra Test Passed