hdu 4325

#include<stdio.h>//数据弱线段树延迟更新水过
#define N 100100
struct node {
int x,y,yanchi,num;
}a[N*4];
void build(int t,int x,int y) {
a[t].x=x;
a[t].y=y;
a[t].yanchi=0;
a[t].num=0;
if(x==y)
return ;
int temp=t<<1;
int mid=(x+y)/2;
build(temp,x,mid);
build(temp+1,mid+1,y);
}
void update(int t,int x,int y) {
if(a[t].x==x&&a[t].y==y) {
a[t].yanchi++;
return ;
}
a[t].num=a[t].num+y-x+1;
int temp=t<<1;
int mid=(a[t].x+a[t].y)/2;
if(mid<x)
update(temp+1,x,y);
else
if(mid>=y)
update(temp,x,y);
else {
update(temp,x,mid);
update(temp+1,mid+1,y);
}
return;
}
int qury(int t,int h) {
if(a[t].x==h&&a[t].y==h) 
return a[t].num+a[t].yanchi;
a[t].num=a[t].num+a[t].yanchi*(a[t].y-a[t].x+1);
int temp=t<<1;
a[temp].yanchi+=a[t].yanchi;
a[temp+1].yanchi+=a[t].yanchi;
a[t].yanchi=0;
int mid=(a[t].x+a[t].y)/2;
if(mid<h)
return qury(temp+1,h);
else
return qury(temp,h);
}
int main() {
int t,n,m,x,y,i,k,count=0;
scanf("%d",&t);
while(t--) {
scanf("%d%d",&n,&m);
printf("Case #%d: ",++count);
build(1,1,100000);
for(i=0;i<n;i++) {
scanf("%d%d",&x,&y);
update(1,x,y);
}
while(m--) {
scanf("%d",&k);
printf("%d ",qury(1,k));
}
}
return 0;
}
原文地址:https://www.cnblogs.com/thefirstfeeling/p/4410923.html