HDU 1698-Just a Hook(线段树 成段更新)

来源:互联网 发布:中国援助朝鲜 知乎 编辑:程序博客网 时间:2024/05/16 11:38

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

11021 5 25 9 3
 

Sample Output

Case 1: The total value of the hook is 24.

                                                                                     

思路:线段树,成段更新,求总和。

------------

Sotifish_DAccepted2352 KB1000 msG++1910 B2014-07-31 01:31:01------------

#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <string>#include <cstring>#include <queue>#include <stack>#include <vector>#include <set>#include <map>const int inf=0xfffffff;typedef long long ll;using namespace std;const int M=100005;int sumv[M<<2],setv[M<<2];int N;void build(int k,int l,int r){    setv[k]=0;    if(l==r) sumv[k]=1;    else{        int mid=(l+r)/2;        build(k*2,l,mid);        build(k*2+1,mid+1,r);        sumv[k]=sumv[2*k]+sumv[2*k+1];    }}void maintain(int pos){    sumv[pos]=sumv[pos*2]+sumv[pos*2+1];}void PushDown(int pos,int l,int r){        setv[pos*2]=setv[pos*2+1]=setv[pos];        int mid=(l+r)/2;        sumv[pos*2]=(mid-l+1)*setv[pos];        sumv[pos*2+1]=(r-mid)*setv[pos];        setv[pos]=0;}void update(int l,int r,int x,int y,int pos,int v){    if(x<=l&&y>=r) {        setv[pos]=v;        sumv[pos]=v*(r-l+1);    }    else{        if(setv[pos]>0) PushDown(pos,l,r);        int mid=(l+r)/2;        if(x <= mid) update(l,mid,x,y,pos*2,v);        if(y > mid) update(mid+1,r,x,y,pos*2+1,v);        maintain(pos);    }}int query(int pos,int l,int r,int x,int y){    if(x<=l&&y>=r) return sumv[pos];    else{        PushDown(pos,l,r);        int mid=(l+r)/2;        int ans=0;        if(x<=mid) ans += query(pos*2,l,mid,x,y);        if(y > mid) ans += query(pos*2+1,mid+1,r,x,y);        return ans;    }}int main(){    //freopen("in.in","r",stdin);    int T;    scanf("%d",&T);    for(int t_=1;t_<=T;t_++){        scanf("%d",&N);        build(1,1,N);        int m;        scanf("%d",&m);        for(int n=1;n<=m;n++){            int vv;            int xx,yy;            scanf("%d%d%d",&xx,&yy,&vv);            update(1,N,xx,yy,1,vv);        }        printf("Case %d: The total value of the hook is %d.\n",               t_,sumv[1]);    }    return 0;}


0 0
原创粉丝点击