QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#140547 | #6674. Connected Intervals | OvO_Zuo | AC ✓ | 224ms | 34900kb | C++14 | 1.9kb | 2023-08-16 08:59:39 | 2023-08-16 08:59:40 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3e5+5;
int n;
vector<int> edg[N];
int tag[N<<2];
struct node{
int mn,cnt;
node(int a=0,int b=0):mn(a),cnt(b){}
node operator+(node a)
{
node res;
res.mn=min(mn,a.mn);
if(mn==res.mn) res.cnt+=cnt;
if(res.mn==a.mn) res.cnt+=a.cnt;
return res;
}
}v[N<<2];
void push_down(int idx)
{
if(!tag[idx]) return;
v[idx<<1].mn+=tag[idx];
tag[idx<<1]+=tag[idx];
v[idx<<1|1].mn+=tag[idx];
tag[idx<<1|1]+=tag[idx];
tag[idx]=0;
}
void add(int l,int r,int tl,int tr,int vv,int idx)
{
if(l>=tl&&r<=tr)
{
v[idx].mn+=vv;
tag[idx]+=vv;
return;
}
push_down(idx);
int mid=(l+r)>>1;
if(mid>=tl) add(l,mid,tl,tr,vv,idx<<1);
if(mid<tr) add(mid+1,r,tl,tr,vv,idx<<1|1);
v[idx]=v[idx<<1]+v[idx<<1|1];
}
node query(int l,int r,int tl,int tr,int idx)
{
if(l>=tl&&r<=tr) return v[idx];
push_down(idx);
int mid=(l+r)>>1;
if(mid>=tr) return query(l,mid,tl,tr,idx<<1);
else if(mid<tl) return query(mid+1,r,tl,tr,idx<<1|1);
else return query(l,mid,tl,tr,idx<<1)+query(mid+1,r,tl,tr,idx<<1|1);
}
void sol()
{
scanf("%d",&n);
int i,j,k;
for(i=1;i<=n;i++) edg[i].clear();
for(i=1;i<n;i++)
{
scanf("%d%d",&j,&k);
edg[j].push_back(k);
edg[k].push_back(j);
}
for(i=1;i<=4*n;i++) v[i].mn=0,v[i].cnt=1,tag[i]=0;
ll ans=0;
for(i=1;i<=n;i++)
{
add(1,n,1,i,1,1);
for(int to:edg[i])
if(to<i) add(1,n,1,to,-1,1);
ans+=query(1,n,1,i,1).cnt;
}
printf("%lld\n",ans);
return;
}
int main()
{
int t;
scanf("%d",&t);
while(t)
{
--t;
sol();
}
return 0;
}
/*
枚举 r
考虑维护一个数组 f[i] 表示 i 作为左端点是否合法
当前考虑 x 作为右端点
先让 f[1]~f[i] 均 +1
考虑所有边 (x,y),满足 y<x
令 f[1]~f[y] 均 -1
当 f[i]=1 时,该点作为左端点合法
因为结构是一棵树 所以可以依次将编号小的向编号大的合并,不会成环
*/
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 20640kb
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: 16ms
memory: 22716kb
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: 50ms
memory: 20704kb
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: 48ms
memory: 22888kb
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: 66ms
memory: 22896kb
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: 94ms
memory: 20712kb
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: 120ms
memory: 23088kb
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: 186ms
memory: 28080kb
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: 208ms
memory: 34204kb
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: 224ms
memory: 34208kb
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: 199ms
memory: 34112kb
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: 174ms
memory: 34464kb
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: 140ms
memory: 34112kb
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: 148ms
memory: 34900kb
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: 134ms
memory: 34784kb
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: 138ms
memory: 34204kb
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: 129ms
memory: 34528kb
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: 160ms
memory: 34392kb
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: 151ms
memory: 34744kb
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: 167ms
memory: 34460kb
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: 150ms
memory: 34384kb
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"