UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#194705#2038. bwosile1004761ms61380kbC++111.9kb2023-10-17 09:08:442023-10-17 12:02:14

answer

#include<bits/stdc++.h>
#define lowbit(x) (x&(-x))
using namespace std;
int n;
struct query{
	int l,r,id,k;
	int ans;
	bool operator <(const query &o)const{
		return id<o.id;
	}
}q[1000005],b[1000005];
int tl[1000005],tr[1000005];
int tot=0,len;
int tmp[2000005];
int c[2000005];
void add(int x,int v){
//	printf("add %d %d\n",x,v);
	while(x<=len){
		c[x]+=v;
		x+=lowbit(x);
	}
}
int sum(int x){
	int ans=0;
	while(x){
		ans+=c[x];
		x-=lowbit(x);
	}
	return ans;
}
void cdq(int l,int r){
//	printf("cdq %d %d\n",l,r);
	if(l==r)return;
	int mid=(l+r)/2;
	cdq(l,mid);
	cdq(mid+1,r);
	int pos=l;
//	printf("cdq %d %d\n",l,r);
//	for(int i=l;i<=r;i++)printf("%d %d %d\n",q[i].l,q[i].r,q[i].k);
	for(int i=mid+1;i<=r;i++){
		while(pos<=mid && q[pos].l>=q[i].l){
			add(q[pos].r,q[pos].k);
			pos++;
		}
		q[i].ans+=sum(q[i].r);
//		printf("%d %d %d add %d\n",q[i].l,q[i].r,q[i].k,sum(q[i].r));
	}
	while(pos>l){
		pos--;
		add(q[pos].r,-q[pos].k);
	}
	int pl=l,pr=mid+1;
	for(int i=l;i<=r;i++){
		if(pl>mid)b[i]=q[pr++];
        else if(pr>r)b[i]=q[pl++];
        else if(q[pl].l>=q[pr].l)b[i]=q[pl++];
        else b[i]=q[pr++];
	}
	for(int i=l;i<=r;i++)q[i]=b[i];
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&q[i].k);
		if(q[i].k==0)q[i].k=1;
		else q[i].k=-1;
		q[i].id=i;
		q[i].ans=0;
		if(q[i].k==1){
			scanf("%d",&q[i].l);
			++tot;
			tl[tot]=q[i].l;
			tr[tot]=q[i].r=tl[tot]+tot-1;
		}
		else{
			int x;
			scanf("%d",&x);
			q[i].l=tl[x];
			q[i].r=tr[x];
		}
	}
	for(int i=1;i<=tot;i++)tmp[i+i-1]=tl[i],tmp[i+i]=tr[i];
	sort(tmp+1,tmp+tot+tot+1);
	len=unique(tmp+1,tmp+tot+tot+1)-tmp-1;
	for(int i=1;i<=n;i++)q[i].l=lower_bound(tmp+1,tmp+len+1,q[i].l)-tmp,q[i].r=lower_bound(tmp+1,tmp+len+1,q[i].r)-tmp;
	cdq(1,n);
	sort(q+1,q+n+1);
	for(int i=1;i<=n;i++)if(q[i].k==1)printf("%d\n",q[i].ans);
	return 0;
}

详细

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

Test #1:

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

input:

666
0 -389
0 -952
0 143
1 1
0 6
0 179
0 -923
0 -20
0 302
0 -109
1 8
0 894
0 -573
0 494
0 -385
0 -873...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
1
0
0
0
0
0
0
1
1
0
0
0
0
0
0
0
1
1
0
2
0
0
0
0
1
0
0
...

result:

ok 593 numbers

Test #2:

score: 10
Accepted
time: 1ms
memory: 1288kb

input:

1000
0 588
1 1
0 -628
0 -956
0 -665
0 765
0 -190
0 -576
0 -813
0 -292
0 274
1 10
0 651
0 -205
0 -574...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
1
0
0
0
0
0
0
0
1
0
0
0
1
0
0
0
1
...

result:

ok 888 numbers

Test #3:

score: 10
Accepted
time: 58ms
memory: 4044kb

input:

50000
0 -7094
0 -8664
0 -9115
1 3
0 -9755
0 -3119
0 4344
0 1734
1 5
0 -3462
1 7
0 7843
0 5728
0 -562...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
...

result:

ok 44900 numbers

Test #4:

score: 10
Accepted
time: 84ms
memory: 5164kb

input:

70000
0 -5155
1 1
0 9088
0 6886
0 -5081
0 1938
0 -959
0 -8158
0 414
1 4
0 6503
0 2048
0 9571
0 -9902...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 63042 numbers

Test #5:

score: 10
Accepted
time: 104ms
memory: 5996kb

input:

85000
0 -8442
0 7059
0 2716
0 307
1 1
0 1173
0 -2796
1 2
0 -3617
0 5458
0 -1083
0 -3482
1 5
0 2832
0...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 76519 numbers

Test #6:

score: 10
Accepted
time: 122ms
memory: 6816kb

input:

100000
0 -3400
0 -3786
0 -4616
0 -3125
0 -3813
0 9233
1 1
0 32
0 -5027
0 -6531
0 -9932
0 -8502
0 -31...

output:

0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 90009 numbers

Test #7:

score: 10
Accepted
time: 458ms
memory: 19284kb

input:

300000
0 -578222155
1 1
0 -464946128
0 -503954765
0 -579608591
0 -147391629
1 2
0 -140936373
1 4
0 -...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 270045 numbers

Test #8:

score: 10
Accepted
time: 835ms
memory: 31308kb

input:

500000
0 -879047197
0 22865740
0 -579471564
0 -455491333
0 -632783600
0 -345134153
0 -341714454
0 -7...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 450086 numbers

Test #9:

score: 10
Accepted
time: 1262ms
memory: 43332kb

input:

700000
0 -106097647
0 -135803415
0 -694029768
0 -407027902
1 2
1 3
0 -534078466
0 -317401273
0 -2520...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 629728 numbers

Test #10:

score: 10
Accepted
time: 1837ms
memory: 61380kb

input:

1000000
0 -759131017
0 -632412881
0 -712627012
0 -353714850
0 -207626084
0 -545766331
0 -423546500
0...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 899925 numbers

Extra Test:

score: 0
Extra Test Passed