线段树区间更新 Just a Hook

来源:互联网 发布:一建试题软件 编辑:程序博客网 时间:2024/05/22 06:47
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 <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=100005;
int sum[maxn*4];
struct node
{
    int l,r,val,mark;
}tree[maxn*4];
void build(int i,int l,int r)
{
    tree[i].mark=0;
    tree[i].val=1;
    tree[i].l=l,tree[i].r=r;
    //cout<<l<<" "<<r<<endl;
    if(l==r)return;


    int m=(l+r)/2;
    build(i*2,l,m);
    build(i*2+1,m+1,r);
}
void pushdown(int i,int v)
{
     if(tree[i].mark!=0)
    {
    tree[i*2].mark=tree[i].mark;
    tree[i*2+1].mark=tree[i].mark;


    tree[i*2+1].val=tree[i].mark*(v/2);    //区间中的个数
    tree[i*2].val=tree[i].mark*(v-v/2);


    tree[i].mark=0;
    }
}
void update(int i,int l,int r,int add)
{


    if(tree[i].l==l&&r==tree[i].r)    //要更新的区间比i所含的区间大 全部包含
    {
        tree[i].mark=add;
        tree[i].val=add*(tree[i].r-tree[i].l+1);
        //cout<<tree[i].l<<" "<<tree[i].r<<" "<<tree[i].mark<<" "<<tree[i].val<<endl;
        return;
    }
    pushdown(i,tree[i].r-tree[i].l+1);
    //cout<<tree[i].l<<" "<<tree[i].r<<" "<<tree[i].mark<<" "<<tree[i].val<<endl;
    int m=(tree[i].l+tree[i].r)/2;
    if(r<=m)update(i*2,l,r,add);
    else if(l>m)update(i*2+1,l,r,add);
    else
    {
        update(i*2,l,m,add);
        update(i*2+1,m+1,r,add);
    }
    tree[i].val=tree[i*2].val+tree[i*2+1].val;


}
/*int query(int i,int l,int r)
{
    cout<<tree[i].l<<" "<<tree[i].r<<" "<<tree[i].mark<<" "<<tree[i].val<<endl;
    if(tree[i].l>=l&&tree[i].r<=r)
    return tree[i].val;
    pushdown(i,tree[i].r-tree[i].l+1);
    int ans=0;
    int m=(tree[i].l+tree[i].r)/2;
    if(l<=m)ans+=query(i*2,l,r);
    if(r>m)ans+=query(i*2+1,l,r);
    return ans;


}*/
int main()
{
    int t;
    cin>>t;
    for(int i=1;i<=t;i++)
    {
        int n;
        cin>>n;
        build(1,1,n);
        int q;
        cin>>q;
        int x,y,z;
        while(q--)
        {
            scanf("%d%d%d",&x,&y,&z);
            update(1,x,y,z);
        }
        //for(int i=1;i<100;i++)
        //cout<<tree[i].l<<" "<<tree[i].r<<" "<<tree[i].val<<endl;
        printf("Case %d: The total value of the hook is %d.\n",i,tree[1].val);
    }
    return 0;
}
原创粉丝点击