HDU

来源:互联网 发布:php截取字符串第一位 编辑:程序博客网 时间:2024/06/03 04:28

题目链接: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)
Total Submission(s): 34559    Accepted Submission(s): 16876


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.
 

区间更新操作 储存区间和 最后的结果其实是根结点储存的值

#include <bits/stdc++.h>using namespace std;#define maxn 100000+5struct xjy{    int left;    int right;    int target;    int lazy;    int sum;};xjy tree[maxn<<2];int a[maxn];int ans=0;void show(int i,int left,int right){    if(left==right)    {        printf("%d %d %d %d %d\n",tree[i].left,tree[i].right,tree[i].sum,tree[i].lazy,tree[i].target);        return ;    }    int mid=(left+right)>>1;    show(i<<1,left,mid);    show(i<<1|1,mid+1,right);    printf("%d %d %d %d %d\n",tree[i].left,tree[i].right,tree[i].sum,tree[i].lazy,tree[i].target);}void build(int i,int left ,int right){    if(left==right)    {        tree[i].left=left;        tree[i].right=right;        tree[i].sum=a[left];        return ;    }    int mid=(left+right)>>1;    build(i<<1,left,mid);    build (i<<1|1,mid+1,right);    tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;    tree[i].left=left;    tree[i].right=right;}void updateregion(int i,int left,int right,int val){    if(tree[i].lazy)    {        if(tree[i].left==tree[i].right)        {            tree[i].lazy=0;            tree[i].target=0;        }        else         {            tree[i].lazy=0;            tree[i<<1].lazy=tree[i<<1|1].lazy=1;            tree[i<<1].target=tree[i<<1|1].target=tree[i].target;            tree[i<<1].sum=(tree[i<<1].right-tree[i<<1].left+1)*tree[i].target;            tree[i<<1|1].sum=(tree[i<<1|1].right-tree[i<<1|1].left+1)*tree[i].target;            tree[i].target=0;        }    }    if(tree[i].left==left&&tree[i].right==right)    {        tree[i].lazy=1;        tree[i].target=val;        tree[i].sum=(right-left+1)*val;        return;    }    int mid=(tree[i].left+tree[i].right)>>1;    if(right<=mid)        updateregion(i<<1,left,right,val);    else if(left>mid)        updateregion(i<<1|1,left,right,val);    else    {        updateregion(i<<1,left,mid,val);        updateregion(i<<1|1,mid+1,right,val);    }    tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;}int main(){    int n,m;    int t;    scanf("%d",&t);    for(int step=1;step<=t;step++)    {        for(int i=1;i<maxn;i++)        {            tree[i].lazy=0;            tree[i].left=0;            tree[i].right=0;            tree[i].sum=0;            tree[i].target=0;            a[i]=0;        }        scanf("%d",&n);        for(int i=1;i<=n;i++)            a[i]=1;        build(1,1,n);        //show(1,1,n);        scanf("%d",&m);        for(int i=1;i<=m;i++)        {            int l,r,type;            scanf("%d%d%d",&l,&r,&type);            updateregion(1,l,r,type);            //show(1,1,n);        }        ans=0;        printf("Case %d: The total value of the hook is %d.\n",step,tree[1].sum);    }}




原创粉丝点击