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.
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.
InputThe 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.
OutputFor 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.
思路:线段树区间修改(裸的)。需要加lazy数组,延迟标记。
#include <iostream> #include <string.h> #include <string> #include<cstdio> #include <algorithm> #define ll long long using namespace std; ll tree[400010],a[400010],add[400010]; void push_up(ll rt) { tree[rt] = tree[rt*2+1] + tree[rt*2+2]; } void push_down(ll rt,ll m) { if(add[rt]) { add[rt*2+1]=add[rt]; add[rt*2+2]=add[rt]; tree[rt*2+1]=(m-(m/2))*add[rt]; tree[rt*2+2]= (m/2)*add[rt]; add[rt] = 0; } } void bulid_tree(int node,int start,int end) { if(start==end) { tree[node]=1; } else { ll mid=(start+end)/2; ll left_node=2*node+1; ll right_node=2*node+2; bulid_tree(left_node,start,mid); bulid_tree(right_node,mid+1,end); tree[node]=tree[left_node]+tree[right_node]; } } void update_tree(int L, int R, ll key, int l, int r, int rt) //区间更新 { if(L <= l && R >= r) { tree[rt] = (r - l + 1) * key; add[rt] = key; return; } push_down(rt, r - l + 1);//向下更新 ll mid=(l+r)/2; ll left_node=2*rt+1; ll right_node=2*rt+2; if(L <= mid) update_tree(L, R, key,l,mid,left_node); if(R > mid) update_tree(L, R, key,mid+1,r,right_node); push_up(rt);//向上更新 } ll query_tree(int L, int R, int l, int r, int rt) { if(L <= l && R >= r) return tree[rt]; push_down(rt,r-l+1); ll mid=(l+r)/2; ll left_node=2*rt+1; ll right_node=2*rt+2; ll ans = 0; if(L <= mid) ans += query_tree(L, R,l,mid,left_node); if(R > mid) ans += query_tree(L, R,mid+1,r,right_node); return ans; } int main() { int t; cin>>t; int Case=0; while(t--) { int n,m; cin>>n>>m; memset(tree,0,sizeof(tree)); memset(add,0,sizeof(add)); bulid_tree(0,0,n-1); while(m--) { ll a,b,c; scanf("%lld%lld%lld", &a, &b, &c); update_tree(a-1,b-1,c,0,n-1,0); } printf("Case %d: The total value of the hook is ",++Case); cout<<query_tree(0,n-1,0,n-1,0)<<"."<<endl; } return 0; }