HDU 1698(線段樹)

ProblemDescription

In the gameof DotA, Pudge’s meat hook is actually the most horrible thing for most of theheroes. The hook is made up of several consecutive metallic sticks which are ofthe same length.

 

Now Pudgewants to do some operations on the hook.

Let us number the consecutive metallic sticks of the hook from 1 to N. For eachoperation, Pudge can change the consecutive metallic sticks, numbered from X toY, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallicsticks. More precisely, the value for each kind of stick is calculated asfollows:

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 theoperations.
You may consider the original hook is made up of cupreous sticks.

 

Input

The inputconsists of several test cases. The first line of the input is the number ofthe cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, whichis the number of the sticks of Pudge’s meat hook and the second line containsan 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 toY into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 representsthe silver kind and Z=3 represents the golden kind.

 

Output

For eachcase, print a number in a line representing the total value of the hook afterthe operations. Use the format in the example.

 

SampleInput

1

10

2

15 2

59 3

 

SampleOutput

Case1: The total value of the hook is 24.

 

Source

2008 “Sunline Cup” National Invitational Contest

 

Recommend

wangye

 

代碼:

//其實挺簡單的

#include<cstdio>
using namespace std;
struct point {
    int x,y,s,bj;
};
point a[100000*4];
void build(int x,int y,int z) {
    a[z].x=x;
    a[z].y=y;
    a[z].bj=0;
    if(x==y) {
        a[z].s=1;
        return;
    }
    int mind=(x+y)/2;
    build(x,mind,z*2);
    build(mind+1,y,z*2+1);
    a[z].s=a[z*2].s+a[z*2+1].s;
}
void up_sum(int x,int y,int z,int w) {
    if(a[z].bj) {
        if(a[z].x!=a[z].y) {
            a[z*2].s=(a[z*2].y-a[z*2].x+1)*a[z].bj;
            a[z*2].bj=a[z].bj;
            a[z*2+1].s=(a[z*2+1].y-a[z*2+1].x+1)*a[z].bj;
            a[z*2+1].bj=a[z].bj;
        }
        a[z].bj=0;
    }
    if(x==a[z].x&&y==a[z].y) {
        a[z].s=w*(a[z].y-a[z].x+1);
        a[z].bj=w;
        return;
    }
    int mind=(a[z].x+a[z].y)/2;
    if(y<=mind)up_sum(x,y,z*2,w);
    else if(x>mind)up_sum(x,y,z*2+1,w);
    else {
        up_sum(x,mind,z*2,w);
        up_sum(mind+1,y,z*2+1,w);
    }
    a[z].s=a[z*2].s+a[z*2+1].s;
}
int main() {
    int T;
    scanf("%d",&T);
    for(int i=1; i<=T; i++) {
        int n,m,x,y,z;
        scanf("%d%d",&n,&m);
        build(1,n,1);
        for(int j=1; j<=m; j++) {
            scanf("%d%d%d",&x,&y,&z);
            up_sum(x,y,1,z);
        }
        printf("Case %d: The total value of the hook is %d.\n",i,a[1].s);
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章