QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#449859 | #8296. Constructing Ranches | PetroTarnavskyi# | AC ✓ | 2268ms | 37564kb | C++20 | 2.8kb | 2024-06-21 18:24:48 | 2024-06-21 18:24:48 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
struct Fenwick
{
int n;
vector<int> t;
void init(int _n)
{
n = _n;
t.clear();
t.assign(n, 0);
}
void upd(int i, int x)
{
for(; i < n; i |= i + 1)
t[i] += x;
}
int query(int i)
{
int ans = 0;
for(; i >= 0; i = (i & (i + 1)) - 1)
ans += t[i];
return ans;
}
}F;
const int N = 1 << 18;
VI g[N];
int w[N];
LL solve(vector<pair<int, LL>> vals, int cent)
{
vector<LL> sums(SZ(vals));
FOR(i, 0, SZ(vals))
sums[i] = vals[i].S;
sort(ALL(vals));
sort(ALL(sums));
F.init(SZ(sums));
LL res = 0;
for(auto [m, s] : vals)
{
//2 * max(m, w[cent]) + 1 <= s + s' + w[cent]
//s' >= 2 * max + 1 - s - w[cent];
LL low = 2 * max(m, w[cent]) + 1 - s - w[cent];
int pos = lower_bound(ALL(sums), low) - sums.begin();
res += F.query(SZ(sums) - 1) - F.query(pos - 1);
pos = lower_bound(ALL(sums), s) - sums.begin();
F.upd(pos, 1);
}
return res;
}
int sz[N];
bool usedC[N];
int dfsSZ(int v, int par)
{
sz[v] = 1;
for(auto to : g[v])
{
if(to != par && !usedC[to])
sz[v] += dfsSZ(to, v);
}
return sz[v];
}
vector<pair<int, LL>> verts;
void dfs(int v, int par, int d, LL sum)
{
d = max(d, w[v]);
sum += w[v];
verts.PB({d, sum});
for(int to : g[v])
{
if(to == par || usedC[to])
continue;
dfs(to, v, d, sum);
}
}
LL ans = 0;
void build(int u)
{
dfsSZ(u, -1);
int szAll = sz[u];
int pr = u;
while(true)
{
int v = -1;
for(auto to : g[u])
{
if(to == pr || usedC[to])
continue;
if(sz[to] * 2 > szAll)
{
v = to;
break;
}
}
if(v == -1)
break;
pr = u;
u = v;
}
int cent = u;
usedC[cent] = true;
vector<pair<int, LL>> vertsAll = {{0, 0}};
for(int to : g[cent])
{
if(usedC[to])
continue;
dfs(to, cent, 0, 0);
ans -= solve(verts, cent);
vertsAll.insert(vertsAll.end(), ALL(verts));
verts.clear();
}
ans += solve(vertsAll, cent);
for(auto to : g[cent])
{
if(!usedC[to])
{
build(to);
}
}
}
void solve()
{
int n;
cin >> n;
FOR(i, 0, n)
cin >> w[i];
FOR(i, 0, n - 1)
{
int a, b;
cin >> a >> b;
a--; b--;
g[a].PB(b);
g[b].PB(a);
}
ans = 0;
build(0);
cout << ans << "\n";
FOR(i, 0, n)
{
g[i].clear();
usedC[i] = 0;
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while(t--)
solve();
return 0;
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 5608kb
input:
2 3 1 10 100 1 2 3 2 5 1 1 1 1 1 1 2 1 3 1 4 1 5
output:
0 6
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 173ms
memory: 5636kb
input:
34763 19 98 27 61 17 77 9 97 23 24 5 94 61 100 88 98 43 8 75 96 4 17 12 17 7 3 19 4 12 5 1 18 10 5 7 2 1 4 2 11 19 9 18 16 1 11 17 15 6 3 16 8 15 14 15 13 9 49 4 97 14 1 11 52 84 84 1 3 6 4 9 7 2 6 7 8 1 2 3 8 5 9 19 92 74 62 54 60 78 74 6 76 80 13 94 4 86 85 89 23 46 2 6 17 1 8 8 2 8 14 13 7 12 9 1...
output:
135 22 128 132 148 107 9 5 122 4 1 113 23 5 15 60 54 16 24 145 14 63 122 2 32 70 2 125 2 160 0 5 128 74 130 15 70 114 33 92 146 151 14 87 8 63 31 9 82 10 13 35 19 0 144 0 18 0 24 21 60 157 73 1 5 130 33 110 20 55 19 24 51 90 16 45 16 24 13 1 63 159 12 19 73 89 15 142 83 5 45 163 21 103 35 117 10 28 ...
result:
ok 34763 lines
Test #3:
score: 0
Accepted
time: 191ms
memory: 5948kb
input:
24286 23 877 532 121 81 687 796 904 979 93 712 828 161 866 704 298 51 237 983 924 202 730 615 326 16 6 5 19 16 9 13 10 16 21 19 22 10 20 2 23 18 4 17 9 23 21 3 19 21 10 8 20 1 15 1 10 20 5 16 18 7 21 14 8 9 12 13 11 24 304 993 567 447 471 201 919 544 652 998 475 918 153 388 935 53 259 889 700 342 52...
output:
199 217 211 40 138 57 22 69 255 208 159 292 127 245 165 68 14 83 2 122 133 292 75 288 257 9 131 136 11 324 143 343 128 30 31 72 20 165 36 265 57 282 8 134 251 154 338 0 80 65 334 292 232 21 45 78 156 23 6 218 335 25 1 7 94 274 308 157 66 0 5 78 168 310 11 186 87 14 104 92 69 336 2 363 18 3 129 91 27...
result:
ok 24286 lines
Test #4:
score: 0
Accepted
time: 115ms
memory: 5872kb
input:
53323 8 4 48 40 98 62 85 66 42 4 6 2 7 8 7 4 7 1 7 3 7 5 7 6 24 20 19 9 7 99 5 6 6 4 1 2 6 3 2 6 6 97 6 52 57 2 7 2 3 6 3 5 3 3 4 6 1 10 50 5 40 15 71 75 99 83 67 27 10 2 9 5 1 10 4 10 10 3 10 8 7 5 10 6 5 10 8 66 63 66 50 37 11 35 20 1 3 2 1 1 4 8 1 5 1 7 6 7 1 6 90 94 72 44 10 38 5 3 3 1 3 2 5 4 6...
output:
16 0 3 21 17 6 18 10 5 1 30 14 10 12 3 7 3 5 18 1 5 3 2 12 16 24 13 1 13 10 0 8 5 24 10 2 2 6 14 15 19 4 8 27 4 12 2 3 2 4 35 25 3 4 11 7 4 8 8 9 15 16 20 4 15 5 3 9 10 3 11 4 34 8 9 4 15 10 22 10 9 3 26 9 0 0 10 2 16 14 12 4 6 22 7 6 7 10 2 1 20 2 28 2 15 4 33 21 10 22 22 6 12 25 7 3 24 3 6 8 5 3 1...
result:
ok 53323 lines
Test #5:
score: 0
Accepted
time: 569ms
memory: 3824kb
input:
717 147 19067 27655 2908 26474 11068 81636 48333 12988 62387 74426 8774 17796 36220 774 77734 26097 52954 55909 23250 38739 15795 17276 69234 8639 48402 66578 56241 54894 75492 65360 73728 75070 15739 97224 62547 27312 1614 21227 32738 39336 18551 96753 64074 54513 84595 37109 61162 16342 98150 6408...
output:
10392 103756 126073 204967 13047 170220 414398 7454 85119 445772 164936 146009 329931 216726 323444 5280 31559 6791 22092 449559 147003 105600 268153 11060 118644 137078 179044 497320 168978 173679 179527 5247 479624 104970 334826 290541 135967 238290 335721 370898 210187 39282 112966 201368 20319 2...
result:
ok 717 lines
Test #6:
score: 0
Accepted
time: 1659ms
memory: 15624kb
input:
7 69369 626676 9738474 5959848 7283711 402336 1530515 5163533 6190777 4056057 5798962 5716235 6787673 6086922 9509443 449439 900898 7658590 7102057 47367 7643957 8469384 4223211 4772166 2751901 2519196 1075889 5604131 5011140 2762803 6924619 3067178 914683 5533547 7582698 738985 4509686 3833317 7930...
output:
2405849897 2749513626 1465324085 1580257364 2321423267 78358480 2141814839
result:
ok 7 lines
Test #7:
score: 0
Accepted
time: 416ms
memory: 18224kb
input:
4 100000 9103 3816 2045 7953 8503 7526 2999 7866 5868 6513 4846 6691 9317 816 2331 2004 9487 4012 2060 8152 9370 6977 2291 9981 3082 513 5848 5094 2311 4816 3347 7292 3565 8889 7426 1065 2157 6343 1161 9051 6810 4093 2429 1831 6189 7263 1048 2916 7010 534 5865 8348 3481 3009 7681 7998 1317 174 7526 ...
output:
4942486604 4964038489 4955391200 4961822766
result:
ok 4 lines
Test #8:
score: 0
Accepted
time: 1679ms
memory: 19560kb
input:
4 100000 8 9 4 5 7 3 7 9 5 7 2 8 6 4 1 6 1 1 7 1 1 9 9 5 7 7 4 7 2 4 9 4 10 5 9 8 6 5 2 2 8 3 7 6 4 8 10 8 9 10 6 4 10 6 8 3 2 8 6 7 6 3 8 8 4 10 1 6 1 2 9 4 7 9 5 9 10 8 3 6 1 9 4 8 3 2 6 1 3 9 10 1 7 1 3 9 1 10 9 5 10 7 4 1 9 3 1 3 7 3 7 6 2 7 2 10 3 4 7 10 3 5 8 7 5 8 9 8 3 7 3 8 10 6 8 3 10 2 7 ...
output:
4999751918 4999751041 4999752423 4999750599
result:
ok 4 lines
Test #9:
score: 0
Accepted
time: 2095ms
memory: 33364kb
input:
2 200000 176303929 839427224 893897551 997635203 316567607 40599477 733158196 682607753 556363048 869552578 576714297 317960399 596690353 331854928 668183078 630422745 514197466 185834485 598821732 858773356 554448527 348853960 107368833 897128726 940482591 69670121 874124208 691029667 949032640 967...
output:
19999488767 19999487937
result:
ok 2 lines
Test #10:
score: 0
Accepted
time: 2268ms
memory: 34960kb
input:
2 200000 99849 59005 3912 64180 96626 17927 40845 30806 89147 52568 2495 23387 64449 99375 19917 41123 7841 21840 58728 96598 57440 9316 37603 8125 68069 47927 17337 12006 38137 75353 64838 83267 16750 8478 10925 43372 39861 16542 38641 88120 65190 69717 25692 98376 19068 69348 92507 10717 69942 605...
output:
19999498928 19999499125
result:
ok 2 lines
Test #11:
score: 0
Accepted
time: 138ms
memory: 5692kb
input:
34711 14 94 89 95 97 98 81 100 80 69 98 51 98 91 96 6 7 12 6 3 6 8 6 10 13 6 10 6 4 12 2 6 9 10 1 11 12 5 10 14 6 20 78 92 81 94 92 99 94 97 96 91 87 92 82 74 92 100 86 89 89 85 4 10 2 1 2 12 15 2 5 12 4 20 11 2 12 3 16 2 3 9 12 8 13 2 6 12 12 4 12 14 2 7 18 12 2 17 5 19 5 4 6 2 5 6 3 5 5 4 1 5 2 5 ...
output:
78 171 5 9 6 51 73 102 9 3 3 23 78 21 5 4 6 93 66 45 45 0 36 21 56 7 1 171 33 19 78 153 20 105 91 91 1 3 61 136 32 55 6 91 120 171 100 91 34 41 136 3 14 55 18 21 2 27 15 1 136 171 45 78 3 66 28 23 78 6 93 83 9 0 12 153 76 6 2 3 3 3 59 10 0 0 24 66 28 105 18 28 10 136 0 3 78 21 7 21 136 36 1 45 17 17...
result:
ok 34711 lines
Test #12:
score: 0
Accepted
time: 238ms
memory: 5892kb
input:
14599 26 26 52 10 19 12 7 58 20 11 1 7 15 22 42 22 7 24 41 10 4 9 25 46 10 52 6 3 17 11 15 6 9 1 4 16 21 19 6 12 4 4 22 11 3 10 20 10 22 19 23 7 26 18 15 23 25 13 21 7 24 25 12 3 14 19 8 3 26 16 26 10 2 17 5 1 18 33 48 34 26 14 44 29 17 47 8 5 3 15 37 9 31 71 5 33 44 8 4 33 20 20 109 14 73 29 31 102...
output:
257 433 325 673 41 19 199 185 8 608 861 741 35 212 247 820 349 561 300 820 641 409 93 171 71 1 301 55 14 2 630 687 36 200 1035 66 153 498 28 81 741 3 293 815 595 630 1041 2 903 794 6 990 350 78 1128 561 91 63 374 29 438 63 91 63 1176 595 253 370 1176 207 942 1035 666 8 19 51 218 403 28 496 666 779 6...
result:
ok 14599 lines
Test #13:
score: 0
Accepted
time: 126ms
memory: 3616kb
input:
53319 7 1 2 1 2 1 2 1 5 4 3 1 5 7 4 2 6 3 3 4 10 1 1 1 1 1 2 1 1 2 2 10 7 4 1 9 2 2 6 1 9 3 1 7 8 9 5 7 4 9 100 100 100 100 100 100 99 100 100 9 8 9 5 5 3 7 9 5 1 6 5 2 6 3 4 9 100 100 100 100 100 100 100 100 100 1 7 5 6 8 5 9 2 6 3 4 2 1 6 2 6 5 100 100 98 100 100 3 1 5 3 3 2 4 2 9 2 1 1 1 1 1 3 1 ...
output:
11 29 28 28 6 23 9 28 18 24 28 24 19 10 21 36 33 24 18 15 34 18 36 12 10 14 15 28 19 33 4 21 6 6 36 10 6 15 6 36 21 21 36 23 6 24 26 28 24 6 15 5 18 25 21 12 36 28 21 2 6 21 11 20 6 36 28 22 14 6 4 6 21 15 13 10 36 10 12 27 36 21 15 23 6 36 17 13 5 4 21 21 10 17 10 13 3 28 14 7 33 21 23 18 21 36 10 ...
result:
ok 53319 lines
Test #14:
score: 0
Accepted
time: 471ms
memory: 3724kb
input:
1453 469 99987 100000 99981 99993 99991 99955 99991 99974 99987 99992 99986 99998 99991 99955 99988 99974 99998 99999 100000 99984 99949 99989 99973 99990 99985 99987 99986 99985 99926 99948 99985 99997 99991 99985 99973 99929 99992 99951 99983 99994 99984 99938 99987 99989 99993 99999 99973 99998 9...
output:
109278 4813 1225 67896 1401 76410 4830 9057 5144 1596 77717 16471 32131 5945 42579 20706 16110 3156 6327 91378 77815 12720 14535 45981 87990 62189 72084 17955 37727 90529 35324 34820 95590 32896 119805 8669 2064 44850 7298 53566 25878 2485 79800 13530 88581 17828 90205 60031 23542 19187 4656 43718 1...
result:
ok 1453 lines
Test #15:
score: 0
Accepted
time: 1215ms
memory: 9832kb
input:
13 23898 9999553 9999739 9999795 9999433 9999620 9998196 9998113 9999948 9999250 9999868 9999428 9998600 9999445 9998988 9999901 9999729 9999782 9999447 9999321 9999289 9999419 9999972 9999986 9999710 9999709 9999208 9999873 9998992 9998886 9998276 9999319 9999300 9999695 9999999 9999496 9999625 999...
output:
285521356 322059510 414532821 474674266 216156808 615412772 1204987686 1242685731 147342435 544978605 564564003 685591935 119337390
result:
ok 13 lines
Test #16:
score: 0
Accepted
time: 385ms
memory: 30568kb
input:
2 200000 19 53 2 4 58 2 44 41 20 16 18 37 35 3 2 45 11 7 43 68 14 31 37 12 59 7 66 23 8 70 5 8 4 153 10 42 2 13 21 103 8 75 22 39 12 31 4 12 74 11 6 34 2 9 38 5 18 42 46 1 12 52 19 82 42 10 13 27 5 101 16 1 40 78 11 11 37 2 5 23 8 2 2 18 61 43 50 11 22 21 46 62 12 178 23 54 60 9 26 17 113 3 88 29 8 ...
output:
16791620244 17210018589
result:
ok 2 lines
Test #17:
score: 0
Accepted
time: 2108ms
memory: 34960kb
input:
2 200000 8 19 5 16 16 53 1 32 10 46 4 97 11 6 1 92 23 10 52 4 19 33 22 4 26 12 16 3 4 34 19 38 5 29 25 11 20 1 39 5 27 3 12 47 28 17 11 45 15 79 1 28 30 30 1 27 21 3 15 46 31 43 26 15 8 75 27 7 33 3 27 13 34 18 1 35 39 47 10 1 20 8 29 12 8 14 73 6 62 7 5 10 22 24 80 19 34 24 2 48 64 17 30 11 155 42 ...
output:
19999059744 19999700001
result:
ok 2 lines
Test #18:
score: 0
Accepted
time: 544ms
memory: 5896kb
input:
816 294 1000 1 1 1 1 1 1 1 1 1 1 1 1 1 1000 1 1 1 1 1 1 1000 1 1 1 1 1 1000 1 1 1 1 1 1 1 1 1 1 1000 1 1000 1 1 1 1 1000 1 1 1000 1000 1 1 1 1 1000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1000 1 1 1 1 1 1 1 1 1 1 1000 1 1 1 1000 1 1 1 1 ...
output:
37198 393428 204397 292790 281092 48442 58781 13654 333665 72692 199662 120068 100260 161346 14676 329 390604 33626 39364 241653 199388 4460 621 176354 940 28709 55299 113710 249335 345761 194063 443871 181464 418673 4970 123 229954 10561 212892 228266 253858 358552 36001 194340 256961 110511 207810...
result:
ok 816 lines
Test #19:
score: 0
Accepted
time: 1830ms
memory: 19908kb
input:
4 100000 2690 694 461 3925 9030 9806 1653 11804 999992379 6456 3051 8119 4753 3279 3260 999997492 3164 15841 1099 8675 59 265 1647 999998967 7928 5121 931 1599 999990849 999996079 7808 2562 999999640 1864 999999179 993 1212 626 2096 2292 3740 999999550 1649 10219 698 13216 2618 15432 9614 2416 12543...
output:
4998127422 4998360897 4998127244 4997765266
result:
ok 4 lines
Test #20:
score: 0
Accepted
time: 1430ms
memory: 21192kb
input:
4 126234 999983724 4337 21288 13829 24963 8038 30983 35242 42434 19540 999993733 12373 999998689 999965912 27643 8569 36361 3643 999951205 31981 12028 999975060 6190 999940288 13714 17318 6148 999984529 2214 999989485 8356 999979932 2424 14493 3203 12205 23969 9893 999994642 21441 23522 16828 43861 ...
output:
7955197352 10206211898 3480709070 1115295331
result:
ok 4 lines
Test #21:
score: 0
Accepted
time: 2126ms
memory: 34300kb
input:
2 200000 265 70 999999794 486 233 999999483 203 165 999999815 245 383 999999150 999999597 61 192 999999982 83 291 217 999999936 84 70 999999993 136 50 778 372 120 20 7 120 81 451 757 27 290 999999817 243 4 97 276 190 5 999999897 999999845 12 999999966 999999720 213 203 38 472 658 106 354 150 268 76 ...
output:
19996592932 19992596978
result:
ok 2 lines
Test #22:
score: 0
Accepted
time: 333ms
memory: 3720kb
input:
7302 59 92294 97308 498 3972 80339 3145 1799 17343 91737 7869 22038 72427 96649 4581 1669 5689 92501 91441 98833 96717 16461 74727 17639 92681 33658 2977 26086 98207 834 3203 92338 1364 92358 13823 97684 89138 81485 95478 56854 10312 3744 23070 43726 87352 2378 97622 97804 5975 98016 2796 95233 2686...
output:
1522 754 958 542 536 34 2078 1179 351 3636 1657 188 120 2244 2547 1258 3777 68 3333 133 1865 300 1082 3647 519 2716 36 1058 830 2673 109 429 791 250 27 3107 3658 4408 611 232 1269 43 1396 589 2599 1000 185 4622 223 2036 517 748 563 929 125 1244 3974 1253 2192 64 2930 1354 797 3805 842 1102 150 244 1...
result:
ok 7302 lines
Test #23:
score: 0
Accepted
time: 1421ms
memory: 17772kb
input:
4 100000 264979 32559 999584550 96474 999990016 999989546 999906703 17503 999962610 96406 139544 994901691 999919908 23707 316565 99406 31542 999952200 999854824 22910 8318 34174 998339383 999958966 113403 115967 999973942 1124675 999997097 999781491 95996 20704 44289 10838 999946348 999832810 99947...
output:
4999481894 4999477261 4999477149 4999477334
result:
ok 4 lines
Test #24:
score: 0
Accepted
time: 1902ms
memory: 22260kb
input:
4 100000 86556 97648 89521 1736 9175 340 59 54738 13196 86027 89986 78759 1106 99487 5610 12338 97065 99027 99396 31741 95035 2865 2403 31 48644 16113 338 14788 1039 6208 3819 2599 25153 942 36726 96918 57649 8619 2818 88515 44 545 75522 341 65069 14745 3655 5414 99877 77434 96032 13259 1631 9898 10...
output:
4999613995 4999614110 4999615881 4999616377
result:
ok 4 lines
Test #25:
score: 0
Accepted
time: 2128ms
memory: 34156kb
input:
2 200000 999712516 999141311 999588717 999898928 475218 129302 998357169 950937945 58889 222330 28481 8481 38826 999973961 999694332 25365 46070 998220558 949242 999920896 999785252 284564 619159 999143783 999912711 273456 999703144 999509396 807777 999818627 999481847 999841243 48921 5243 999561906...
output:
19999126611 19999124253
result:
ok 2 lines
Test #26:
score: 0
Accepted
time: 1404ms
memory: 13248kb
input:
8 50000 43793 165865 39613 11525 35928 95607 87120 64111 124083 194832 25264 58275 63275 50253 114403 60836 87830 130849 20174 125173 100641 106880 197773 136282 108920 108273 12049 138773 159424 133810 6766 179514 181164 68672 75731 92208 103406 148558 109428 189288 189921 38357 133906 23820 7298 1...
output:
1249880872 1249880785 1249880957 1249880518 1249881269 1249881018 1249881099 1249881292
result:
ok 8 lines
Test #27:
score: 0
Accepted
time: 1710ms
memory: 22304kb
input:
4 100000 373500 176197 301827 158200 252619 392174 153328 304793 182943 392013 310081 398031 202340 126734 250576 374343 95955 355895 113935 348769 285171 340033 10901 303831 197291 88002 372111 271234 299545 48309 57840 86584 266996 156482 342759 283658 243256 158676 321693 247238 387656 255540 116...
output:
4999761515 4999761818 4999760606 4999761216
result:
ok 4 lines
Test #28:
score: 0
Accepted
time: 1925ms
memory: 37564kb
input:
2 200000 41208 198207 406964 640919 72426 556243 444185 245886 306486 653879 524083 374218 779216 535250 653600 555472 11622 1339 82567 709339 358125 361084 327877 350884 306715 233827 699881 451115 115513 209931 739512 25729 639872 546863 642271 559301 798093 513134 88825 653180 203757 422296 52344...
output:
19999523059 19999523537
result:
ok 2 lines
Extra Test:
score: 0
Extra Test Passed