Flower[hdu4325][线段树][离散化]

传送门

离散化 线段树区间修改+单点查询就可以了


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#define N 100050
using namespace std;
int s[N],t[N],b[N<<2],a[N],n,m,T;
int val[N<<2],tag[N<<2],ret;
int read(){
	int cnt=0;char ch=0;
	while(!isdigit(ch))ch=getchar();
	while(isdigit(ch))cnt=cnt*10+(ch-'0'),ch=getchar();
	return cnt;
}
void Init(){
	ret=0;
	memset(val,0,sizeof(val));
	memset(tag,0,sizeof(val));
}
void Pushup(int x){val[x] = val[x<<1] + val[x<<1|1];}
void Pushdown(int x){
	if(tag[x]){
		val[x<<1] += tag[x];
		val[x<<1|1] += tag[x];
		tag[x<<1] += tag[x];
		tag[x<<1|1] += tag[x];
		tag[x]=0;
	}
}
void Update(int x,int l,int r,int L,int R){
	if(L<=l && r<=R){
		val[x]++ , tag[x]++; return;
	}
	Pushdown(x);
	int mid = (l+r)>>1;
	if(L<=mid) Update(x<<1,l,mid,L,R);
	if(R>mid) Update(x<<1|1,mid+1,r,L,R);
	Pushup(x);
}
int Quary(int x,int l,int r,int pos){
	if(l==r) return val[x];
	Pushdown(x);
	int mid = (l+r)>>1;
	if(pos<=mid) return Quary(x<<1,l,mid,pos);
	else return Quary(x<<1|1,mid+1,r,pos);
}
int main(){ 
	T=read(); for(int Case=1;Case<=T;Case++){
		Init();
		n=read(),m=read(); 
		for(int i=1;i<=n;i++){
			s[i] = b[++ret] = read();
			t[i] = b[++ret] = read();
		}
		for(int i=1;i<=m;i++){
			a[i] = b[++ret] = read(); 
		}
		sort(b+1,b+ret+1);
		int siz = unique(b+1,b+n*2+m+1) - (b+1);
		for(int i=1;i<=n;i++){
			int L=lower_bound(b+1,b+siz+1,s[i])-b;
			int R=lower_bound(b+1,b+siz+1,t[i])-b;
			Update(1,1,siz,L,R);
		}
		printf("Case #%d:\n",Case);
		for(int i=1;i<=m;i++){
			int x=lower_bound(b+1,b+siz+1,a[i])-b;
			printf("%d\n",Quary(1,1,siz,x));
		}
	}return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

FSYo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值