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.
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.
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.
1 10 2 1 5 2 5 9 3
Case 1: The total value of the hook is 24.
题意: 屠夫的钩子由n节构成,一开始都是铜质,铜质的价值为1. 现在屠夫可以更换部分的钩子的材质。 金子的价值为3,银的价值为2,铜的价值为1. 在进行过m次更新后,求钩子的总价值。
分析: 区间更新,不是加减某一个值,而是直接将区间改为一个值。所以当碰到一个结点对应的lazy值不为0时,就将该点对应的segtree的值改为 val*(r-l) 即delta乘以区间长度。(因为一个结点的lazy值不为0的意思就是,该节点所包含的区间内的所有叶子的值都被更改)然后再将标记下方到两个孩子身上。就可以A啦~
#include<iostream>
#include<cstdio>
#include<string.h>
#include<math.h>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<queue>
#include<iomanip>
using namespace std;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
typedef long long ll;
const int maxn = 100000+10;
int segtree[maxn*4],num[maxn*2],lazy[maxn*4];
void build(int l,int r,int pos){
if(r-l == 1){
segtree[pos] = num[l];
return;
}
int mid = (l+r)/2;
build(l,mid,pos*2+1);
build(mid,r,pos*2+2);
segtree[pos] = segtree[pos*2+1] + segtree[pos*2+2];
}
void updaterange(int ul,int ur,int l, int r,int pos,int delta){
if(lazy[pos] != 0){
segtree[pos] = lazy[pos] * (r-l); // pos's value equal delta * (r-l)
if(r-l != 1){
lazy[pos*2+1] = lazy[pos]; // put down the sign
lazy[pos*2+2] = lazy[pos];
}
lazy[pos] = 0;
}
if(ul >= r || ur <= l) return;
if(ul <= l && ur >= r){
segtree[pos] = delta*(r-l); //the same as the previous operation
if(r-l != 1){
lazy[pos*2+1] = delta;
lazy[pos*2+2] = delta;
}
return;
}
int mid = (l+r)/2;
updaterange(ul,ur,l,mid,pos*2+1,delta);
updaterange(ul,ur,mid,r,pos*2+2,delta);
segtree[pos] = segtree[pos*2+1] + segtree[pos*2+2];
}
int queryrange(int ul,int ur,int l,int r,int pos){
if(lazy[pos] != 0){
segtree[pos] = lazy[pos]*(r-l); //same
if(r-l != 1){
lazy[pos*2+1] = lazy[pos];
lazy[pos*2+2] = lazy[pos];
}
lazy[pos] = 0;
}
if(ul >= r||ur <= l) return 0;
if(ul <= l && ur >= r) return segtree[pos]; //attention! it's wrong to return segtree[pos]*(r-l)
int mid = (l+r)/2;
return queryrange(ul,ur,l,mid,pos*2+1)+queryrange(ul,ur,mid,r,pos*2+2);
}
int main()
{
int T;
int cnt = 0;
scanf("%d",&T);
while(T--){
memset(lazy,0,sizeof(lazy));
int m,n,k=1;
scanf("%d%d",&n,&m);
while(k<n) k <<= 1;
for(int i=0;i<2*k-1;i++){
num[i] = 1;
}
build(0,k,0);
int l,r,delta;
for(int i=0;i<m;i++){
scanf("%d%d%d",&l,&r,&delta);
l--;
updaterange(l,r,0,k,0,delta);
}
cnt++;
printf("Case %d: The total value of the hook is %d.\n",cnt,queryrange(0,n,0,k,0));
}
}