2018年湖南省第十四届大学生计算机程序设计竞赛 CSU 2170: 千万别用树套树

10 篇文章 0 订阅
2 篇文章 0 订阅

题目传送门
比赛时想的解法,整体二分+树状数组
具体看代码吧

代码:

#include<bits/stdc++.h>
using namespace std;

const int maxn=100000+100;

struct Node{
	
	int l,r;
	int type,id;
};
int n,q;
Node node[maxn],a[maxn],b[maxn];
int tree[maxn],user[maxn],tot;
int ans[maxn];

void Add(int x){
	
	while(x<=n){
		
		if(user[x]!=tot) user[x]=tot,tree[x]=0;
		tree[x]++;
		x+=(x&(-x));
	}
}

int getsum(int x){
	
	int sum=0;
	while(x){
		
		if(user[x]!=tot) user[x]=tot,tree[x]=0;
		sum+=tree[x];
		x-=(x&(-x));
	} 
	return sum;
}

void kaven(int l,int r,int L,int R){

    tot++;
	if(L==R){
		
		for(int i=l;i<=r;i++){
			
			if(node[i].type==1) Add(node[i].r);
			else ans[node[i].id]+=(getsum(n)-getsum(node[i].r-1));
		}
		return ;
	}
	int mid=(L+R)>>1;
	int atot=0,btot=0;
	for(int i=l;i<=r;i++){
		
		if(node[i].type==1){
			
			if(node[i].l<=mid) Add(node[i].r),a[atot++]=node[i];
			else b[btot++]=node[i];
		}
		else{
			
			if(node[i].l>mid) ans[node[i].id]+=(getsum(n)-getsum(node[i].r-1)),b[btot++]=node[i];
			else if(node[i].l==mid) ans[node[i].id]+=(getsum(n)-getsum(node[i].r-1));
			else a[atot++]=node[i];
		}
	}
	for(int i=0;i<atot;i++) node[l+i]=a[i];
	for(int i=0;i<btot;i++) node[l+atot+i]=b[i];
	if(atot) kaven(l,l+atot-1,L,mid);
	if(btot) kaven(l+atot,l+atot+btot-1,mid+1,R);
}

int main(){
	
	while(scanf("%d%d",&n,&q)==2){
		
		int Id=0;
		tot=0;
		memset(tree,0,sizeof(tree));
		memset(user,0,sizeof(user));
		memset(ans,0,sizeof(ans));
		for(int i=1;i<=q;i++){
			
			scanf("%d%d%d",&node[i].type,&node[i].l,&node[i].r);
			if(node[i].type==2) node[i].id=++Id;
			else node[i].id=0;
		}
		kaven(1,q,1,n);
		for(int i=1;i<=Id;i++) printf("%d\n",ans[i]);
	}
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ITKaven

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值