Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 51731 Accepted Submission(s): 24270
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.
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.
题目大意:
有一把钩子由n节棍子组成,要你求n节棍子的总价值,就是n节棍子之和,默认每截棍子的价值为1;
然后有一种操作 x y z表示在区间[x,y]中将每截棍子的价值换成z。
思路:线段树+lazy_tag模板题。
代码:
#include<stdio.h>
const int maxn=100000+10;
struct node {
int left;
int right;
int sum;
int lazy;
}tree[maxn*4];
int n;
void build(int l, int r, int i) {
tree[i].left = l;
tree[i].right = r;
tree[i].sum = 0;
tree[i].lazy = 0;
if (l == r) {
tree[i].sum = 1;
return;
}
int mid = (l + r) / 2;
build(l, mid, 2 * i);
build(mid + 1, r, 2 * i + 1);
tree[i].sum = tree[i * 2].sum + tree[i * 2 + 1].sum;
}
void pushdown(int i) {
if (tree[i].lazy) {
tree[i * 2].sum = (tree[i * 2].right - tree[i * 2].left + 1)*tree[i].lazy;
tree[i * 2 + 1].sum = (tree[i * 2 + 1].right - tree[i * 2 + 1].left + 1)*tree[i].lazy;
tree[i * 2].lazy = tree[i].lazy;
tree[i * 2 + 1].lazy = tree[i].lazy;
tree[i].lazy = 0;
}
}
void updata(int l, int r, int data, int i)
{
if (tree[i].left == l && tree[i].right == r) {
tree[i].lazy = data;
tree[i].sum = (tree[i].right - tree[i].left + 1)*data;
return;
}
pushdown(i);
int mid = (tree[i].right + tree[i].left) / 2;
if (r <= mid) {
updata(l, r, data, 2 * i);
}
else if (l > mid) {
updata(l, r, data, 2 * i + 1);
}
else {
updata(l, mid, data, 2 * i);
updata(mid + 1, r, data, 2 * i + 1);
}
tree[i].sum = tree[i * 2].sum + tree[i * 2 + 1].sum;
}
int query(int i) {
return tree[i].sum;
}
int main()
{
int ccase;scanf("%d", &ccase);
for (int i = 0; i < ccase; i++) {
int n;scanf("%d", &n);
build(1, n, 1);
int k;scanf("%d",&k);
while(k--) {
int x,y,t;scanf("%d%d%d", &x, &y, &t);
updata(x, y, t, 1);
}
printf("Case %d: The total value of the hook is %d.\n", i+1, query(1));
}
}