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.
Source
思路:这道题不能用往常的 tree[i].sum = tree[i * 2].sum + tree[i * 2 + 1].sum,虽然思路没问题,但是比较耗时,会T。要从plz(懒惰标记)下手,ans = tree[i].plz * (tree[i].r - tree[i].l +1)
#include <cstdio> #include <iostream> #include <cmath> #include <string> #include <cstring> #include <algorithm> #include <queue> #include <vector> #include <map> using namespace std; #define ll long long #define eps 1e-9 ll t, n, q, a[1000000+8], ans, x, y, z; struct node { ll l, r, plz; }tree[4000000+8]; void build(ll i, ll l, ll r)///建树 { tree[i].l = l; tree[i].r = r; tree[i].plz = 1; if(tree[i].l == tree[i].r) return; ll mid = (tree[i].l + tree[i].r) / 2; build(i * 2, l, mid); build(i * 2 + 1, mid + 1, r); } void push_down(ll i)///向下标记左儿子右儿子(表示值改变成了什么) { tree[i * 2].plz = tree[i * 2 + 1].plz = tree[i].plz; tree[i].plz = 0; } void pls(ll i, ll l, ll r, ll k)///区间赋值 { if(tree[i].l >= l && tree[i].r <= r)///如果这个区间在要找的区间范围内 { tree[i].plz = k;///就把自己的值改变为k return ; } if(tree[i].plz) push_down(i); ll mid = (tree[i].l + tree[i].r) / 2; if(l <= mid)///如果要查找的区间中点在左边 pls(i*2, l, r, k);///往左儿子查找 if(mid < r)///如果要查找的区间中点在右边 pls(i * 2 + 1, l, r, k);///往右边找 } void search(ll i, ll l, ll r)///寻找从1~n的和 { if(tree[i].plz)///如果这个节点或者这个区间被标记了 { ans += tree[i].plz * (tree[i].r - tree[i].l + 1);///就让ans加上他 return ;///返回上一层,层层递归 } if(tree[i].plz)///往下标记自己的左儿子和右儿子,知道所有叶子结点被标记完 push_down(i); ll mid = (tree[i].l + tree[i].r) / 2; search(i * 2, l, mid);///往左儿子那边搜寻根节点,一个一个让ans加上 search(i * 2 + 1, mid + 1, r);///往右儿子那边搜寻根节点,一个一个让ans加上 } int main() { scanf("%lld", &t); ll miao = t; while(t--) { ans = 0; scanf("%lld%lld", &n, &q); build(1, 1, n); for(ll i = 0; i<q; i++) { scanf("%lld%lld%lld", &x, &y, &z); pls(1, x, y, z); } search(1, 1, n); printf("Case %lld: The total value of the hook is %lld. ", miao - t, ans); } return 0; }