HDU 1698 Just a Hook 线段树

来源:互联网 发布:淘宝怎么买伟哥 编辑:程序博客网 时间:2024/06/07 01:26

http://acm.hdu.edu.cn/showproblem.php?pid=1698

Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


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
11021 5 25 9 3
 

Sample Output
Case 1: The total value of the hook is 24.
没看博客自己把线段树写出来了,虽然代码不精简,做出来就不错了

AC代码:

#include<cstdio>#include<iostream>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<vector>#include<map>#include<string>#define LL long long#define eps 1e-8using namespace std;const int mod2 = 1e9+7;const int INF = 1e8;const int inf = 0x3f3f3f3f;const int maxx = 100010;struct nodee{    int op,e,sum,l,r;    int len()    {        return r-l+1;    }    int mid()    {        return (l+r)>>1;    }}tree[maxx*4];void build(int node,int l,int r){    tree[node].op=0;    tree[node].e=0;    tree[node].l=l;    tree[node].r=r;    tree[node].sum=tree[node].len();    if(l==r)        return ;    build(node<<1,l,tree[node].mid());    build(node<<1|1,tree[node].mid()+1,r);}void down(int node){    int k=node,k1=k<<1,k2=k<<1|1;    if(tree[k].op&&tree[k].l!=tree[k].r)    {        tree[k1].op=tree[k2].op=1;        tree[k].op=0;        tree[k2].e=tree[k1].e=tree[k].e;        tree[k1].sum=tree[k1].len()*tree[k].e;        tree[k2].sum=tree[k2].len()*tree[k].e;    }}void Insert(int node,int l,int r,int add){    down(node);    if(tree[node].l==l&&tree[node].r==r)    {        tree[node].sum=tree[node].len()*add;        tree[node].op=1;        tree[node].e=add;        return ;    }    if(r<=tree[node].mid())        Insert(node<<1,l,r,add);    else if(l>tree[node].mid())        Insert(node<<1|1,l,r,add);    else    {        Insert(node<<1,l,tree[node].mid(),add);        Insert(node<<1|1,tree[node].mid()+1,r,add);    }    tree[node].sum=tree[node<<1].sum+tree[node<<1|1].sum;}int main(){    int t,cas=1;    scanf("%d",&t);    while(t--)    {        int n,m,a,b,c;        scanf("%d%d",&n,&m);        build(1,1,n);        while(m--)        {           scanf("%d%d%d",&a,&b,&c);           Insert(1,a,b,c);        }        printf("Case %d: The total value of the hook is %d.\n",cas++,tree[1].sum);    }}