UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#204972#3643. Bsnow_trace1005989ms93024kbC++112.9kb2024-06-16 11:58:332024-06-16 13:20:00

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int M = 1000000005;
int a[500005];
int n,m,k,l;
int val[500005],pos[500005];int h = 0;
vector<int>ll[500005];
int nxt[500005],pre[500005];
vector<int>pp;
struct tree{
	int ls,rs,mx;
}tree[20000005];int nw = 1;
void update(int l,int r,int k,int pos,int add){
	if(l+1 == r){
		tree[k].mx = max(tree[k].mx,add);return;
	}
	int mid = l+r>>1;
	if(pos<mid){
		if(!tree[k].ls)tree[k].ls = ++nw;
		update(l,mid,tree[k].ls,pos,add);
	}else{
		if(!tree[k].rs)tree[k].rs = ++nw;
		update(mid,r,tree[k].rs,pos,add);
	}
	tree[k].mx = max(tree[tree[k].ls].mx,tree[tree[k].rs].mx);
}
int query(int l,int r,int ll,int rr,int k){
	if(!k)return 0;
	if(l<=ll and rr<=r)return tree[k].mx;
	int mid =ll+rr>>1;
	if(l>=mid){
		return query(l,r,mid,rr,tree[k].rs);
	}
	if(r<=mid){
		if(tree[k].ls)return query(l,r,ll,mid,tree[k].ls);
	}
	return max(query(l,r,ll,mid,tree[k].ls),query(l,r,mid,rr,tree[k].rs));
}
int calc(int x){x-=2*k;return (x+k)/(k+1);}
void solve(){
	cin >> n >> m >> k >> l;
	//--------------------------------------------------------------------------
	for(int i = 1;i<=n;i++)cin >> a[i];
	for(int i =0;i<=n+5;i++)ll[i].clear();pp.clear();
	for(int i =0;i<=nw;i++)tree[i].ls = tree[i].rs = tree[i].mx = 0;nw = 1;h =0;
	//--------------------------------------------------------------------------检查!!!!
	/*
	tree把所有节点都清了肯定没问题
	h 每次归零不会有问题
	h最大是 n+3 ll 清空是对的
	nxt 和 pre 每次用之前都清了
	pp clear
	保佑我不要挂多测qwq
	*/
	vector<int>p;for(int i = 1;i<=n;i++)p.push_back(a[i]);
	sort(p.begin(),p.end());p.erase(unique(p.begin(),p.end()),p.end());
	for(int i = 1;i<=n;i++){
		int mx = query(max(1ll,a[i]-k),min(m+1,a[i]+k+1),1,M+1,1);
		update(1,M+1,1,a[i],mx+1);
	}
	int ans = n,nowa = 0;
	pos[++h] = -k,val[h] = l;
	for(int i = 0;i<p.size();i++){
		val[++h] = query(p[i],p[i]+1,1,M+1,1);
		pos[h] = p[i];
	}
	pos[++h] = m+k+1,val[h] = l;
	for(int i = 1;i<=h;i++)pp.push_back(val[i]);pp.push_back(0);
	for(int i = 1;i<h;i++)nowa+=calc(pos[i+1]-pos[i]-1);
	sort(pp.begin(),pp.end());pp.erase(unique(pp.begin(),pp.end()),pp.end());
	for(int i = 1;i<=h;i++)val[i] = lower_bound(pp.begin(),pp.end(),val[i])-pp.begin()+1;
	for(int i = 1;i<=h;i++)ll[val[i]].push_back(i);
	for(int i = 0;i<=h+1;i++)nxt[i] = i+1,pre[i] = i-1;nxt[h+1] = h+1,pre[0] = 0;
	ans+=nowa*(pp[1]-pp[0]);
	for(int i = 2;i<pp.size();i++){
		for(int x:ll[i]){
			int p1 = pos[pre[x]],p2 = pos[nxt[x]],p = pos[x];
			nowa-=calc(p-p1-1)+calc(p2-p-1);
			nowa+=calc(p2-p1-1);
			pre[nxt[x]] = pre[x],nxt[pre[x]] = nxt[x];
		}
		ans+=nowa*(pp[i]-pp[i-1]);
	}
	cout << ans << '\n';
}
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t;cin >> t;
	while(t--){
		solve();
	}
	return 0;
}/*
1
3 3 1 3
1 3 2
*/

Details

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 5
Accepted

Test #1:

score: 5
Accepted
time: 164ms
memory: 13016kb

input:

20000
10 1000000000 1000000000 1000000000
226659035 250054074 546576995 203093961 170837339 20860008...

output:

1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1000000000
1...

result:

ok 20000 numbers

Test #2:

score: 0
Accepted
time: 487ms
memory: 93024kb

input:

1
200000 1000000000 1000000000 1000000000
992449362 651860722 269632286 716245558 965251943 31661828...

output:

1000000000

result:

ok 1 number(s): "1000000000"

Subtask #2:

score: 5
Accepted

Test #3:

score: 5
Accepted
time: 187ms
memory: 13016kb

input:

20000
10 1000000000 0 1000000000
246183521 96198515 305798977 625503123 211932925 163211461 15744794...

output:

1000000000000000000
1000000000000000000
1000000000000000000
1000000000000000000
1000000000000000000
...

result:

ok 20000 numbers

Test #4:

score: 0
Accepted
time: 406ms
memory: 88468kb

input:

1
200000 1000000000 0 1000000000
600021167 265035305 374864967 680233689 752439987 270740797 8326820...

output:

1000000000000000000

result:

ok 1 number(s): "1000000000000000000"

Subtask #3:

score: 10
Accepted

Test #5:

score: 10
Accepted
time: 99ms
memory: 13016kb

input:

200000
0 1000000000 3 1000000000
0 1000000000 3 1000000000
0 1000000000 3 1000000000
0 1000000000 3 ...

output:

250000000000000000
250000000000000000
250000000000000000
250000000000000000
250000000000000000
25000...

result:

ok 200000 numbers

Test #6:

score: 0
Accepted
time: 103ms
memory: 13012kb

input:

200000
0 1000000000 100 1000000000
0 1000000000 100 1000000000
0 1000000000 100 1000000000
0 1000000...

output:

9900991000000000
9900991000000000
9900991000000000
9900991000000000
9900991000000000
990099100000000...

result:

ok 200000 numbers

Test #7:

score: 0
Accepted
time: 107ms
memory: 13016kb

input:

200000
0 1000000000 100000 1000000000
0 1000000000 100000 1000000000
0 1000000000 100000 1000000000
...

output:

10000000000000
10000000000000
10000000000000
10000000000000
10000000000000
10000000000000
1000000000...

result:

ok 200000 numbers

Test #8:

score: 0
Accepted
time: 111ms
memory: 13016kb

input:

200000
0 1000000000 1000000 1000000000
0 1000000000 1000000 1000000000
0 1000000000 1000000 10000000...

output:

1000000000000
1000000000000
1000000000000
1000000000000
1000000000000
1000000000000
1000000000000
10...

result:

ok 200000 numbers

Subtask #4:

score: 15
Accepted

Test #9:

score: 15
Accepted
time: 240ms
memory: 13008kb

input:

200000
1 1000000000 3 1000000000
813113588
1 1000000000 3 1000000000
162432886
1 1000000000 3 100000...

output:

250000000000000000
250000000000000000
250000000000000000
250000000000000000
250000000000000000
25000...

result:

ok 200000 numbers

Test #10:

score: 0
Accepted
time: 249ms
memory: 13012kb

input:

200000
1 1000000000 100 1000000000
65059908
1 1000000000 100 1000000000
780925625
1 1000000000 100 1...

output:

9900990999999999
9900990999999999
9900990999999999
9900990999999999
9900990999999999
990099100000000...

result:

ok 200000 numbers

Test #11:

score: 0
Accepted
time: 260ms
memory: 13012kb

input:

200000
1 1000000000 100000 1000000000
310169056
1 1000000000 100000 1000000000
130497483
1 100000000...

output:

10000000000000
9999999999999
10000000000000
10000000000000
10000000000000
10000000000000
10000000000...

result:

ok 200000 numbers

Test #12:

score: 0
Accepted
time: 246ms
memory: 13008kb

input:

200000
1 1000000000 1000000 1000000000
42196964
1 1000000000 1000000 1000000000
116096811
1 10000000...

output:

1000000000000
1000000000000
1000000000000
1000000000000
1000000000000
1000000000000
1000000000000
10...

result:

ok 200000 numbers

Subtask #5:

score: 30
Accepted

Test #13:

score: 30
Accepted
time: 8ms
memory: 13012kb

input:

300
10 10 3 10
6 7 5 1 7 6 4 8 8 7
10 10 3 10
6 7 10 10 9 3 3 3 5 6
10 10 3 10
8 1 4 2 4 6 6 4 3 5
1...

output:

18
25
22
24
19
24
23
23
19
25
20
21
25
25
17
24
24
19
24
26
18
26
24
22
26
24
23
25
23
25
19
25
20
2...

result:

ok 300 numbers

Test #14:

score: 0
Accepted
time: 10ms
memory: 13012kb

input:

300
10 10 4 10
3 9 3 9 7 2 10 4 9 5
10 10 4 10
3 7 8 8 2 5 8 9 5 2
10 10 4 10
7 10 10 2 8 2 7 6 9 10...

output:

19
14
20
13
19
16
19
20
18
17
17
15
18
13
17
15
16
14
15
16
19
19
13
15
14
13
18
19
15
16
15
18
18
1...

result:

ok 300 numbers

Test #15:

score: 0
Accepted
time: 3ms
memory: 13020kb

input:

30
100 100 30 100
11 97 61 92 62 96 1 14 97 94 98 1 70 77 58 42 51 34 24 16 98 1 21 74 79 65 87 24 9...

output:

249
263
255
278
258
284
267
256
265
285
259
231
249
307
261
262
250
289
272
262
282
253
267
250
280
...

result:

ok 30 numbers

Test #16:

score: 0
Accepted
time: 7ms
memory: 13020kb

input:

30
100 100 13 100
81 75 72 3 50 68 39 54 15 25 64 22 39 61 40 71 58 76 68 17 35 32 18 9 60 30 52 71 ...

output:

634
658
671
696
680
672
675
649
668
613
613
660
657
654
671
651
648
593
639
655
617
665
666
656
687
...

result:

ok 30 numbers

Test #17:

score: 0
Accepted
time: 8ms
memory: 13276kb

input:

1
3000 3000 121 3000
1594 2003 742 1589 318 301 1116 1085 2883 180 2522 1755 1722 781 2082 712 887 2...

output:

70042

result:

ok 1 number(s): "70042"

Test #18:

score: 0
Accepted
time: 9ms
memory: 13296kb

input:

1
3000 3000 2987 3000
2467 1579 998 1618 1357 2165 1926 860 1375 1176 52 268 1860 2873 1418 2470 66 ...

output:

3000

result:

ok 1 number(s): "3000"

Subtask #6:

score: 35
Accepted

Test #19:

score: 35
Accepted
time: 269ms
memory: 13016kb

input:

20000
10 1000000000 3 1000000000
579022231 974160358 916238000 335408245 905609147 18568878 94851631...

output:

249999999999999997
249999999999999996
249999999999999997
249999999999999996
249999999999999996
24999...

result:

ok 20000 numbers

Test #20:

score: 0
Accepted
time: 264ms
memory: 13016kb

input:

20000
10 1000000000 4 1000000000
592826245 78674384 405780822 141131769 582921763 650893783 35034114...

output:

199999999999999996
199999999999999997
199999999999999997
199999999999999998
199999999999999996
19999...

result:

ok 20000 numbers

Test #21:

score: 0
Accepted
time: 261ms
memory: 13068kb

input:

2000
100 1000000000 30 1000000000
706959573 917853556 339926158 407774192 279323674 960270530 748240...

output:

32258064999999955
32258064999999953
32258064999999951
32258064999999957
32258064999999951
3225806499...

result:

ok 2000 numbers

Test #22:

score: 0
Accepted
time: 174ms
memory: 13068kb

input:

2000
100 1000000000 13 1000000000
467549977 585965482 521809854 595741967 458729064 95285208 2663221...

output:

71428571999999955
71428571999999955
71428571999999956
71428571999999950
71428571999999953
7142857199...

result:

ok 2000 numbers

Test #23:

score: 0
Accepted
time: 282ms
memory: 41680kb

input:

1
200000 1000000 121 1000000000
891005 278822 654128 505498 208457 970237 141411 104733 391594 92964...

output:

8196999698403

result:

ok 1 number(s): "8196999698403"

Test #24:

score: 0
Accepted
time: 423ms
memory: 90132kb

input:

1
200000 999976568 1210 1000000000
642090932 240890376 155277558 882428733 286350032 180672252 96799...

output:

825744999882269

result:

ok 1 number(s): "825744999882269"

Test #25:

score: 0
Accepted
time: 764ms
memory: 89876kb

input:

1
200000 999987679 1232131 1000000000
465561785 304303085 734834193 217572194 764029307 920399750 98...

output:

811999658121

result:

ok 1 number(s): "811999658121"

Test #26:

score: 0
Accepted
time: 848ms
memory: 89584kb

input:

1
200000 999576588 31031231 1000000000
349872441 576165215 659166910 621934286 226126614 746808809 6...

output:

32999613908

result:

ok 1 number(s): "32999613908"

Extra Test:

score: 0
Extra Test Passed