QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#125645 | #6674. Connected Intervals | UNos_maricones# | AC ✓ | 718ms | 47592kb | C++20 | 4.1kb | 2023-07-17 07:08:06 | 2023-07-17 07:08:09 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std ;
#define ff first
#define ss second
#define pb push_back
typedef long long ll;
typedef pair<ll,ll> ii;
typedef long double lf;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll SQ = 320;
const ll N = 6e5+5;
const ll mod = 998244353;
int bit[N];
vector <int> g[N];
int tme = 0;
int tin[N], tout[N];
int fat[N];
void update (int i, int x) {
for (++i; i < N; i += (i & (-i))) bit[i] += x;
}
int query (int i) {
int sum = 0;
for (++i; i > 0; i -= (i & (-i))) sum += bit[i];
return sum;
}
int query (int i, int j) {
return query (j) - query (i - 1);
}
void dfs (int u, int p) {
tin[u] = tme++;
fat[u] = p;
for (auto &v : g[u]) {
if (v == p) continue;
dfs(v, u);
}
tout[u] = tme++;
}
ll solve (int l, int r) {
if (l > r) { return 0; }
if (l == r) { return 1; }
int m = (l + r) / 2;
ll cost = 0;
vector <int> val(r - l + 1);
vector <int> valid(r - l + 1);
val[m - l] = m;
valid[m - l] = 1;
// cout << "mid " << m << endl;
{
vector <int> upds;
update(tin[m], 1);
upds.pb(tin[m]);
int mn = m;
int mx = m;
for (int i = m + 1; i <= r; ++i) {
int lst = -1;
// cout << "adding " << i << " to path" << endl;
int rep = i;
while (query(tin[rep], tin[rep]) == 0) {
// cout << rep << endl;
update(tin[rep], 1);
upds.pb(tin[rep]);
mn = min(mn, rep);
mx = max(mx, rep);
if (mn < l) break;
if (mx > r) break;
int inn = 0;
for (auto &v : g[rep]) {
if (query(tin[v], tout[v]) && v != fat[rep] && v != lst) {
lst=rep;
rep = v;
inn = 1;
break;
}
}
if (!inn) lst=rep, rep = fat[rep];
}
val[i - l] = mn;
if (mx <= i) valid[i - l] = 1;
}
for (auto &e : upds) update(e, -1);
}
// cout << "pass " << endl;
{
vector <int> upds = {tin[m]};
update(tin[m], 1);
int mx = m;
int mn = m;
for (int i = m - 1; i >= l; --i) {
int rep = i;
//cout << "adding " << i << '\n';
int lst = -1;
while (query(tin[rep], tin[rep]) == 0) {
update(tin[rep], 1);
upds.pb(tin[rep]);
mx = max(mx, rep);
mn = min(mn, rep);
//cout << rep << '\n';
if (mx > r) break;
if (mn < l) break;
int inn = 0;
for (auto &v : g[rep]) {
if (query(tin[v], tout[v]) && v != fat[rep] && v!=lst) {
lst = rep;
rep = v;
inn = 1;
break;
}
}
if (!inn) lst = rep, rep = fat[rep];
}
val[i - l] = mx;
if (mn >= i) valid[i - l] = 1;
}
for (auto &e : upds) update(e, -1);
}
// cout << "pass " << endl;
vector <int> sufsum(r - l + 2);
for (int i = r; i >= l; --i) sufsum[i - l] = sufsum[i - l + 1] + valid[i - l];
int idx = m;
int idx2 = m;
for (int i = m; i >= l; --i) {
while (idx <= r && val[idx - l] >= i) idx++;
while (idx2 <= r && val[i - l] > idx2) idx2++;
// cout << i << ' ' << idx - idx2 << '\n';
if (valid[i - l]) {
cost += max(0ll, 1ll * sufsum[idx2 - l] - sufsum[idx - l]);
}
}
// cout << "-pass " << endl;
// cout << l << ' ' << m << ' '<< r << ' ' << cost << endl;
// for (auto &e : val) cout << e << ' ';
// cout << endl;
// for (auto &e : valid) cout << e << ' ';
// cout << endl;
return cost + solve(l, m - 1) + solve(m + 1, r);
}
int main(){
#ifdef LOCAL
freopen("input.txt","r",stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int t; cin >> t;
while (t--) {
int n; cin >> n;
for (int i = 0; i < n; ++i) g[i].clear();
for (int i = 0; i + 1 < n; ++i) {
int u, v; cin >> u >> v;
u--;v--;
g[u].pb(v);
g[v].pb(u);
}
dfs(0, -1);
cout << solve(0, n - 1) << '\n';
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
memory: 23960kb
input:
2 4 1 2 2 3 3 4 4 1 2 2 3 2 4
output:
10 9
result:
ok 2 number(s): "10 9"
Test #2:
score: 0
Accepted
time: 54ms
memory: 23944kb
input:
18249 1 2 1 2 3 1 2 1 3 3 2 1 2 3 3 3 1 2 3 4 1 2 1 3 1 4 4 2 3 1 2 1 4 4 3 2 1 3 1 4 4 4 2 1 3 1 4 4 1 3 2 1 2 4 4 2 1 2 3 2 4 4 3 1 2 3 2 4 4 4 1 2 3 2 4 4 1 2 3 1 3 4 4 2 1 3 2 3 4 4 3 1 3 2 3 4 4 4 1 3 2 3 4 4 1 2 4 1 3 4 4 2 1 4 2 3 4 4 3 1 4 2 3 4 4 4 1 4 2 3 4 5 1 2 1 3 1 4 1 5 5 2 3 1 2 1 4 ...
output:
1 3 5 6 5 7 8 7 5 7 9 8 7 8 10 9 8 7 8 7 7 9 10 9 7 6 9 11 12 10 7 10 10 11 9 7 9 9 10 9 7 7 7 7 6 6 9 10 10 9 7 9 12 11 10 9 9 13 12 11 10 7 11 10 10 9 6 8 7 7 7 9 9 9 7 6 10 12 11 10 9 11 14 13 12 11 10 12 11 11 10 9 10 9 9 9 10 11 10 8 7 10 13 12 11 10 12 15 14 13 12 11 13 12 12 11 10 11 10 10 10...
result:
ok 18249 numbers
Test #3:
score: 0
Accepted
time: 183ms
memory: 25668kb
input:
25007 14 11 5 9 10 3 9 4 11 3 4 6 3 1 12 7 1 8 7 6 8 14 6 2 13 2 14 10 10 3 2 4 1 2 7 1 6 5 9 8 7 9 6 7 6 10 12 10 5 7 6 12 7 2 8 1 2 4 1 11 4 10 11 3 10 9 3 9 12 11 11 2 7 5 1 8 4 1 7 9 4 7 3 10 6 3 4 6 4 11 14 6 2 1 5 3 6 12 3 11 10 9 11 1 13 7 1 8 7 12 8 9 12 4 9 4 14 14 2 4 1 2 6 5 7 6 14 8 11 9...
output:
20 21 16 14 23 25 22 18 26 21 16 18 18 16 14 18 13 17 13 16 19 12 19 18 14 18 21 18 21 15 19 30 19 25 21 25 20 13 17 16 19 18 30 17 13 32 16 24 13 29 18 22 14 23 13 24 14 13 16 22 13 18 21 24 29 13 16 18 16 18 22 33 17 19 21 17 29 18 23 14 14 15 15 22 14 16 19 16 17 18 21 13 20 16 14 20 16 14 17 15 ...
result:
ok 25007 numbers
Test #4:
score: 0
Accepted
time: 180ms
memory: 24516kb
input:
20000 15 8 1 6 2 3 4 11 5 3 6 7 10 7 13 9 7 3 9 3 14 12 3 8 12 11 8 11 15 15 3 2 7 3 1 5 13 1 14 7 6 9 12 6 13 10 8 12 15 8 4 13 11 4 14 11 14 15 15 13 2 14 3 8 6 13 8 4 9 1 4 7 1 12 10 5 11 7 5 12 7 15 12 13 14 13 15 15 15 2 3 6 14 3 9 12 11 9 5 11 5 13 8 5 7 8 10 7 10 14 1 10 4 1 4 15 15 13 1 5 3 ...
output:
28 18 19 17 21 22 23 19 23 18 19 19 18 19 18 19 21 20 24 23 22 17 18 16 18 21 17 17 19 22 20 17 27 18 18 37 20 17 18 21 20 21 18 21 26 22 18 19 18 18 18 25 20 21 17 20 19 21 45 17 18 21 19 18 20 25 19 17 21 22 18 23 27 17 20 23 23 19 18 18 22 24 17 20 27 23 18 18 16 17 30 18 16 19 25 19 19 21 19 19 ...
result:
ok 20000 numbers
Test #5:
score: 0
Accepted
time: 254ms
memory: 26076kb
input:
3000 100 14 1 50 2 12 3 30 6 47 14 39 16 57 22 38 23 12 29 19 31 45 19 46 34 26 36 54 26 27 37 67 39 11 40 81 11 25 48 9 25 5 9 20 49 21 50 96 54 42 56 32 58 42 59 67 61 15 63 57 64 7 57 17 7 69 17 52 67 65 69 91 65 44 70 77 44 96 71 77 74 51 75 45 76 47 45 97 77 30 78 28 30 51 28 86 51 99 79 72 80 ...
output:
109 102 107 106 103 103 105 106 105 106 103 103 105 105 105 103 103 103 105 104 106 102 101 106 104 103 108 103 103 104 108 106 105 103 104 101 103 104 103 105 106 107 107 103 102 106 102 103 102 103 103 106 107 106 104 105 104 105 103 102 107 106 105 103 105 106 105 104 104 107 104 104 104 110 104 ...
result:
ok 3000 numbers
Test #6:
score: 0
Accepted
time: 301ms
memory: 25196kb
input:
300 1000 525 4 24 5 40 7 87 8 512 10 262 14 813 15 920 16 65 17 264 19 263 22 563 25 782 26 836 27 152 29 743 31 697 35 970 38 314 39 524 41 881 42 559 44 363 45 128 48 85 51 980 56 253 57 950 62 65 63 259 67 393 70 789 71 913 72 230 74 428 79 314 81 154 83 292 85 514 86 875 89 496 91 176 92 831 93 ...
output:
1002 1006 1004 1003 1002 1004 1003 1008 1004 1004 1003 1013 1010 1003 1006 1004 1008 1002 1002 1002 1005 1005 1003 1004 1004 1004 1004 1007 1002 1016 1002 1002 1002 1007 1006 1002 1005 1007 1002 1002 1004 1002 1005 1004 1002 1004 1004 1004 1012 1004 1005 1001 1004 1005 1005 1003 1005 1003 1006 1004 ...
result:
ok 300 numbers
Test #7:
score: 0
Accepted
time: 369ms
memory: 25388kb
input:
30 10000 6821 1 9990 2 5302 3 1666 4 6188 7 2395 8 1809 9 9602 12 5294 15 530 20 5389 21 5706 22 3491 23 8268 27 4172 28 529 40 651 41 5961 44 3518 46 3708 49 1308 50 4979 51 1228 52 3466 55 4275 56 3939 60 7197 63 7788 64 7387 65 1091 66 779 68 7651 71 7211 72 7650 73 6368 76 1175 77 3593 80 3885 8...
output:
10006 10004 10006 10007 10007 10001 10003 10009 10002 10006 10005 10008 10001 10002 10004 10003 10003 10004 10007 10007 10007 10003 10007 10008 10021 10005 10015 10005 10003 10007
result:
ok 30 numbers
Test #8:
score: 0
Accepted
time: 518ms
memory: 32344kb
input:
3 100000 38499 2 74037 14 62566 16 12244 22 7435 23 50004 25 85421 27 99298 29 46772 37 627 44 95358 45 80664 48 93303 51 910 52 35696 53 97637 56 27446 57 82638 59 90514 61 24251 64 34056 66 82616 71 60591 73 95904 74 63214 78 17615 81 97332 82 95666 83 48804 84 35814 85 95944 86 30971 88 84660 89 ...
output:
100005 100008 100008
result:
ok 3 number(s): "100005 100008 100008"
Test #9:
score: 0
Accepted
time: 629ms
memory: 43108kb
input:
1 300000 203812 2 251949 3 33092 6 190617 12 259200 13 12637 14 227204 16 170636 19 20596 24 27919 25 192879 26 214547 28 40734 32 223434 35 286662 38 46869 41 76948 48 161483 50 145840 53 99310 55 136689 57 62536 59 135775 64 190846 66 78593 69 38258 71 130241 72 90718 73 290341 75 100531 77 184383...
output:
300003
result:
ok 1 number(s): "300003"
Test #10:
score: 0
Accepted
time: 627ms
memory: 42384kb
input:
1 300000 9994 3 114435 4 12353 10 156766 11 109257 14 159440 15 205944 24 101743 25 81457 26 62123 27 223684 28 207546 29 26268 30 251044 34 31153 37 247131 38 25593 39 156614 40 179607 42 87514 54 151903 56 205384 57 277753 60 15148 63 16798 66 79634 69 7496 73 133288 79 96882 80 82922 81 51268 85 ...
output:
300003
result:
ok 1 number(s): "300003"
Test #11:
score: 0
Accepted
time: 718ms
memory: 43016kb
input:
1 300000 20 21 20 19 19 18 21 22 22 23 19 110104 18 17 110104 110105 110104 127193 127193 127194 127193 161908 23 24 18 204614 21 56150 56150 56151 17 16 127194 127195 24 25 23 20860 22 24875 127194 150240 24 7474 17 245694 204614 204615 16 15 16 267316 56151 56152 7474 7475 204615 204616 161908 161...
output:
11165318
result:
ok 1 number(s): "11165318"
Test #12:
score: 0
Accepted
time: 552ms
memory: 47124kb
input:
1 300000 131273 131272 131272 131271 131271 131270 131270 131269 131269 131268 131268 131267 131267 131266 131266 131265 131265 131264 131264 131263 131263 131262 131262 131261 131261 131260 131260 131259 131259 131258 131258 131257 131257 131256 131256 131255 131255 131254 131254 131253 131253 1312...
output:
19659324646
result:
ok 1 number(s): "19659324646"
Test #13:
score: 0
Accepted
time: 524ms
memory: 47592kb
input:
1 300000 100845 100844 100844 100843 100843 100842 100842 100841 100841 100840 100840 100839 100839 100838 100838 100837 100837 100836 100836 100835 100835 100834 100834 100833 100833 100832 100832 100831 100831 100830 100830 100829 100829 100828 100828 100827 100827 100826 100826 100825 100825 1008...
output:
24916563180
result:
ok 1 number(s): "24916563180"
Test #14:
score: 0
Accepted
time: 496ms
memory: 43800kb
input:
1 300000 9 8 9 10 9 55997 9 111984 9 167971 8 7 8 223958 8 233289 8 242620 8 251951 8 261282 10 11 10 9342 10 18673 10 28004 10 37335 10 46666 55997 55998 55997 65329 55997 74660 55997 83991 55997 93322 55997 102653 111984 111985 111984 121316 111984 130647 111984 139978 111984 149309 111984 158640 ...
output:
4530793
result:
ok 1 number(s): "4530793"
Test #15:
score: 0
Accepted
time: 285ms
memory: 44244kb
input:
1 300000 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 2 26 2 27 2 28 2 29 2 30 2 31 2 32 2 33 2 34 2 35 2 36 2 37 2 38 2 39 2 40 2 41 2 42 2 43 2 44 2 45 2 46 2 47 2 48 2 49 2 50 2 51 2 52 2 53 2 54 2 55 2 56 2 57 2 58 2 59 2 60 2 61 2 6...
output:
899997
result:
ok 1 number(s): "899997"
Test #16:
score: 0
Accepted
time: 512ms
memory: 42948kb
input:
1 300000 394 395 394 1020 394 1645 394 2270 394 2895 394 393 394 3520 394 4145 394 4770 394 5395 394 6020 394 6645 394 7270 394 7895 394 8520 394 9145 394 9770 394 10395 394 11020 394 11645 394 12270 394 12895 394 13520 394 14145 394 14770 394 15395 394 16020 394 16645 394 17270 394 17895 394 18520 ...
output:
211762181
result:
ok 1 number(s): "211762181"
Test #17:
score: 0
Accepted
time: 509ms
memory: 43816kb
input:
1 300000 292255 292254 292254 292381 292254 292253 292381 292382 292382 292383 292382 292658 292381 292765 292383 292384 292253 292252 292252 292932 292252 292251 292384 292385 292255 292256 292256 292257 292251 292250 292765 292766 292932 292933 292385 292386 292383 292609 292250 292249 292251 2930...
output:
1276240728
result:
ok 1 number(s): "1276240728"
Test #18:
score: 0
Accepted
time: 542ms
memory: 43612kb
input:
1 300000 23794 23795 23794 23951 23794 24107 23794 24263 23794 24419 23794 24575 23794 24731 23794 24887 23794 25043 23794 25199 23794 25355 23794 25511 23794 23793 23794 73333 23794 73489 23794 73645 23794 73801 23794 73956 23794 74111 23794 74266 23794 74421 23794 74576 23794 74731 23794 74886 237...
output:
178191978
result:
ok 1 number(s): "178191978"
Test #19:
score: 0
Accepted
time: 544ms
memory: 43832kb
input:
1 300000 109144 109145 109144 109143 109143 109142 109143 111777 111777 111778 111778 111779 111778 113053 111779 111780 113053 113054 111779 112624 109145 109146 113053 123773 113054 113055 111777 128342 113055 113056 128342 128343 109145 110696 113056 113057 113057 113058 109142 133284 128342 1292...
output:
1551521044
result:
ok 1 number(s): "1551521044"
Test #20:
score: 0
Accepted
time: 568ms
memory: 44324kb
input:
1 300000 217422 217421 217421 217420 217421 217423 217420 217419 217420 282074 217423 217424 217423 281819 217419 282462 217419 217418 282074 282075 282074 282330 217424 217425 217424 281031 281819 281820 281819 281947 282462 282463 282462 282839 217418 217417 217418 282966 282075 282076 282075 2822...
output:
1680325579
result:
ok 1 number(s): "1680325579"
Test #21:
score: 0
Accepted
time: 500ms
memory: 44140kb
input:
1 300000 79407 79406 79406 79405 79405 79404 79404 79403 79403 79402 79402 79401 79401 79400 79400 79399 79399 79398 79398 79397 79397 79396 79396 79395 79395 79394 79394 79393 79393 79392 79392 79391 79391 79390 79390 79389 79389 79388 79388 79387 79387 79386 79386 79385 79385 79384 79384 79383 793...
output:
1812167990
result:
ok 1 number(s): "1812167990"