Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.

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.
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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
/****************************************/
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
const int N = 100010;
int tree_sum[N<<2], lazy[N<<2];
int A, B, val;
void Up(int rt) {
tree_sum[rt] = tree_sum[rt<<1] + tree_sum[rt<<1|1];
}
void Down(int rt, int len) {
if(lazy[rt]) {//本层有懒惰标记说明有事儿还没完成
lazy[rt<<1|1] = lazy[rt<<1] = lazy[rt];
tree_sum[rt<<1] = (len-(len>>1)) * lazy[rt];//下放标记之后,更新左子树、右子树
tree_sum[rt<<1|1] = (len>>1) * lazy[rt];
lazy[rt] = 0;//本层没事儿了,反正有事儿也是下一层的事儿,不用我管
}
}
void build(int l, int r, int rt) {
tree_sum[rt] = 1;
lazy[rt] = 0;
if(l == r)
return ;
int m = (l+r) >> 1;
build(lson); build(rson);
Up(rt);
}
void update(int l, int r, int rt) {
if(A <= l&&r <= B) {//倘若当前区间在目标区间之内,则更新懒惰标记即可,不会进入下面的懒惰下放及更新
lazy[rt] = val;//设置懒惰标记,表示该段下面所有结点共享的val值
tree_sum[rt] = val * (r-l+1);
return ;
}
//不在目标区间之内,继续向下找
Down(rt, r-l+1);//倘若该点拥有懒惰标记,下放懒惰标记并且更新儿子,不然的话正确数据会被覆盖
int m = (l+r) >> 1;
if(A <= m)
update(lson);
if(B > m)
update(rson);
Up(rt);//向上更新是必须的
}
int main() {
int cas;
scanf("%d", &cas);
for(int i = 1; i <= cas; i++) {
int n, op;
scanf("%d%d", &n, &op);
build(1, n, 1);
while(op--) {
scanf("%d%d%d", &A, &B, &val);
update(1, n, 1);
}
printf("Case %d: The total value of the hook is %d.\n", i, tree_sum[1]);
}
return 0;
}