HDU 1698Just a Hook

题意:水题线段树,区间更新带上+lazy标记,最后统计即可;

 

#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<cstring>
#include<cstdio>
#define N 100005
using namespace std;
typedef struct node{
	int x;int y;int date;int p;
}node;
node a[N*4];
void built(int root,int first,int end){
	if(first==end){
		a[root].date=1;
		a[root].x=first;
		a[root].y=end;
		a[root].p=0;
		return ;
	}
	int mid=(first+end)/2;
	built(root*2,first,mid);
	built(root*2+1,mid+1,end);
	a[root].x=a[root*2].x;a[root].y=a[root*2+1].y;a[root].date=a[root*2+1].date+a[root*2].date;a[root].p=0;
}
void U(int root,int first,int end,int l,int r,int e){
	if(l<=first&&end<=r){
		a[root].date=(end-first+1)*e;a[root].p=e;
		return ;
	}
	int mid=(first+end)/2;
	if(a[root].p!=0){
		a[root*2+1].date=(end-mid)*a[root].p;
		a[root*2+1].p=a[root].p;
		a[root*2].date=(mid-first+1)*a[root].p;
		a[root*2].p=a[root].p;
		a[root].p=0;
	}
	if(l<=mid)  U(root*2,first,mid,l,r,e);
	if(r>mid)  U(root*2+1,mid+1,end,l,r,e);
	a[root].date=a[root*2].date+a[root*2+1].date;
}
void Q(int root,int first,int end){
	if(first==end){
		return ;
	}
	int mid=(first+end)/2;
		if(a[root].p!=0){
		a[root*2+1].date=(end-mid)*a[root].p;
		a[root*2+1].p=a[root].p;
		a[root*2].date=(mid-first+1)*a[root].p;
		a[root*2].p=a[root].p;
		a[root].p=0;
	}
	Q(root*2,first,mid);
	Q(root*2+1,mid+1,end);
	a[root].date=a[root*2+1].date+a[root*2].date;
}
int main(){
	int m;
	scanf("%d",&m);
	for(int i=1;i<=m;i++){
		int n;
		scanf("%d",&n);
		built(1,1,n);
		int k;
		scanf("%d",&k);
		int b,c,d;
		for(int j=1;j<=k;j++){
			scanf("%d%d%d",&b,&c,&d);
			U(1,1,n,b,c,d);
		}
		Q(1,1,n);
		printf("Case %d: ",i);
		printf("The total value of the hook is %d.\n",a[1].date);
	}
	return 0;
}


 

posted @ 2017-10-03 20:25  wang9897  阅读(79)  评论(0编辑  收藏  举报