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.
题解:在dota游戏中,pudge的钩子是他最厉害的武器,众多的钩子组成了连续的一列,有铜钩子,银钩子,金钩子,分别用1,2,3表示,有两种操作,一种要求你把某一区间的钩子统统变为另一种钩子,第二种操作询问你某一区间钩子的总和。典型的线段树区间更新,区间求和问题,用了lazy—tag思想。
代码如下:
#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <ctime>
#define maxn 55557
//#define N 100005
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define eps 0.000000001
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define Debug(x) cout<<x<<" "<<endl
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
//typedef long long ll;
const int N = 1e5 + 10;
typedef long long LL;
typedef struct Node
{
int l;
int r;
int mid()
{
return (l + r) / 2.0;
}
} Node;
LL ans;
Node node[N << 2];//为什么乘4
LL add[N << 2];// 延迟标记数组
LL sum[N << 2];//每个节点的sum
void PushUp(int i);
#define lson l,m,i << 1
#define rson m + 1,r,i << 1 | 1
void build(int l,int r,int i)//建树并完成初始化
{
add[i] = 0;
if(l == r)
{
sum[i]=1;//在内部输入,为叶子节点
return ;//一定要写return!!!
}
int m = (l+r)/2;
build(lson);//只想下更新sum
build(rson);
PushUp(i);//递归回去完成区间的更新.
}
void PushUp(int i) // 建树递归返回时更新。
{
sum[i] = sum[i << 1] + sum[i << 1 | 1];
}
void PushDown(int i,int L) // L为区间长度 (延迟标记)
{
if(add[i])
{
add[i << 1] = add[i];
add[i << 1 | 1] = add[i];
sum[i << 1] = add[i] * (L - (L >> 1)); // 易错
sum[i << 1 | 1] = add[i] * (L >> 1);
add[i] = 0;
}
}
void update(int L,int R,int v,int l,int r,int i)//更新操作,区间【l,r】加值v
{
if(L <= l && R >= r)//当找到目标区间时,
{
sum[i] = (LL)v * (r - l + 1);//更新当前节点的值,
add[i] = v;//延迟标记+V
return ;
}
PushDown(i,r - l + 1);// 因为没有匹配到合适的区间,向下寻找,检查这个区间是否有标记.
int m = (l+r)/2;//如果有标记,说明应该先把之前的标记量给计算出,再进行寻找.
if(L <= m)
{
update(L,R,v,lson);
}
if(m<R)
update(L,R,v,rson);
PushUp(i);//递归返回
}
int query(int L,int R,int l,int r,int i) //查询
{
if(r == l)
{
return sum[i];
}
PushDown(i,r - l + 1);
int m = (l+r)/2;
int ans=0;
if(L <= m)
ans+=query(L,R,lson);
if(m<R)
ans+=query(L,R,rson);
PushUp(i);
return ans;
}
int main()
{
ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
int t,cnt=1;
cin>>t;
while(t--)
{
cout<<"Case "<<cnt++<<":"<<" The total value of the hook is ";
int n,m;
cin >> n >> m;
build(1,n,1);
while(m--)
{
int a,b,c;
cin >> a >> b >> c;
update(a,b,c,1,n,1);
}
cout << query(1,n,1,n,1) << "." << endl;
}
}