Just a Hook HDU - 1698
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.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
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.
//线段树区间更新
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
struct node
{
int l,r;
long long int sum;
}tree[100000000];
long long int add[1000090];
void pushdown(int rate,int m)//lazy区间向下更新,m是区间范围,rate是当前节点
{
if(add[rate])
{
add[rate<<1]=add[rate];
add[rate<<1|1]=add[rate];
tree[rate<<1].sum=add[rate]*(m-(m>>1));
tree[rate<<1|1].sum=add[rate]*(m>>1);
add[rate]=0;
}
}
void update(int l,int r,int w,int rate)//lazy区间更新,l为左端点r为右端点,w为区间内每个元素增加的值,rate为当前节点
{
if(tree[rate].l==l&&tree[rate].r==r)//lazy思想
{
add[rate]=w;
tree[rate].sum=(long long int)w*(r-l+1);
return ;
}
if(tree[rate].l==tree[rate].r) return ;
pushdown(rate,tree[rate].r-tree[rate].l+1);
int mid=((tree[rate].l+tree[rate].r)>>1);
if(r<=mid)update(l,r,w,rate<<1);
else if(l>mid)update(l,r,w,rate<<1|1);
else
{
update(l,mid,w,rate<<1);
update(mid+1,r,w,rate<<1|1);
}
tree[rate].sum=tree[rate<<1].sum+tree[rate<<1|1].sum;
}
long long int Sum(int l,int r,int rate)//区间求和
{
if(l==tree[rate].l&&r==tree[rate].r)
{
return tree[rate].sum;
}
pushdown(rate,tree[rate].r-tree[rate].l+1);
int mid=(tree[rate].l+tree[rate].r)>>1;
long long int ans=0;
if(r<=mid) ans+=Sum(l,r,rate<<1);
else if(l>mid)ans+=Sum(l,r,rate<<1|1);
else
{
ans+=Sum(l,mid,rate<<1);
ans+=Sum(mid+1,r,rate<<1|1);
}
return ans;
}
void build(int rate,int l,int r)
{
tree[rate].l=l;
tree[rate].r=r;
if(l==r)
{
tree[rate].sum=1;
return ;
}
int mid=(l+r)>>1;
build(rate<<1,l,mid);
build(rate<<1|1,mid+1,r);
tree[rate].sum=tree[rate<<1].sum+tree[rate<<1|1].sum;
}
int main()
{
int t,n,q,i,l,r,w,cas=0;
scanf("%d",&t);
while(t--)
{
cas++;
memset(add,0,sizeof(add));
scanf("%d",&n);
build(1,1,n);
scanf("%d",&q);
for(i=1;i<=q;i++)
{
scanf("%d %d %d",&l,&r,&w);
update(l,r,w,1);
}
printf("Case %d: The total value of the hook is %lld.\n",cas,Sum(1,n,1));
}
return 0;
}
上一篇: HDU1698Just a Hook