HDU1698 Just a Hook 线段树区间更新
Just a HookTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 42281 Accepted Submission(s): 20345 Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.Now Pudge wants to do some operations on the hook. Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks. The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows: For each cupreous stick, the value is 1. For each silver stick, the value is 2. For each golden stick, the value is 3. Pudge wants to know the total value of the hook after performing the operations. You may consider the original hook is made up of cupreous sticks.
Input The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
Output For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
Sample Input 1 10 2 1 5 2 5 9 3
Sample Output Case 1: The total value of the hook is 24. |
写的时候main内部query写错了,真是无语....
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define lson l,m,k<<1
#define rson m+1,r,k<<1|1
using namespace std;
int data[100005];
long long sum=0;
int L,R;
struct forma
{
int l,r,flag;
}stu[400005];
void build(int left,int right,int root){
stu[root].l=left,stu[root].r=right,stu[root].flag=1;
if(left==right){
stu[root].flag=1;
return ;
}
int mid=(left+right)>>1;
build(left,mid,root<<1);
build(mid+1,right,root<<1|1);
}
void down(int root){
stu[root<<1].flag=stu[root].flag;
stu[root<<1|1].flag=stu[root].flag;
stu[root].flag=0;
}
void updata(int left,int right,int newcolor,int root){
if(left>=L&&right<=R){
stu[root].flag=newcolor;
return ;
}
if(stu[root].flag)
down(root);
int mid=(left+right)>>1;
if(L<=mid)
updata(left,mid,newcolor,root<<1);
if(R>mid)
updata(mid+1,right,newcolor,root<<1|1);
}
void query(int left,int right,int root){
if(stu[root].flag){
sum+=(stu[root].r-stu[root].l+1)*stu[root].flag;
return ;
}
int mid=(left+right)>>1;
query(left,mid,root<<1);
query(mid+1,right,root<<1|1);
}
int main()
{
int testnum;
scanf("%d",&testnum);
for(int i=1;i<=testnum;i++){
sum=0;
int num,qnum;
scanf("%d%d",&num,&qnum);
build(1,num,1);
while(qnum--){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
L=a,R=b;
updata(1,num,c,1);
}
query(1,num,1);
printf("Case %d: The total value of the hook is %lld.\n",i,sum);
}
}
推荐阅读