HDU:1698 Just a Hook

线段树区间更新,区间求和。模版题。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#define ll  long long
#define INF 2139062143
#define inf -2139062144
#define MOD 20071027
#define MAXN  200005
#define LEN 111111<<2
using namespace std;
struct Line_Tree
{
private:
    int data[LEN],sumv[LEN];
    int sz,sv,ql,qr;
    void Push_up(int o)
    {
        sumv[o]=sumv[o<<1]+sumv[o<<1|1];
    }
    void Push_down(int o,int m)
    {
        if(data[o])
        {
            data[o<<1]=data[o];
            data[o<<1|1]=data[o];
            sumv[o<<1]=data[o]*(m-(m>>1));
            sumv[o<<1|1]=data[o]*(m>>1);
            data[o]=0;
        }

    }
    void myupdate(int o,int L,int R)
    {
        if(ql<=L&&R<=qr)
        {
            data[o]=sv;
            sumv[o]=sv*(R-L+1);
            return ;
        }
        Push_down(o,R-L+1);
        int M=(L+R)>>1;
        if(ql<=M) myupdate(o<<1,L,M);
        if(M<qr) myupdate(o<<1|1,M+1,R);
        Push_up(o);
    }
public:
    void init(int n)
    {
        sz=n;
    }
    void build(int o,int L,int R)
    {
        data[o]=1;
        sumv[o]=R-L+1;
        if(L==R) return ;
        int M=(L+R)>>1;
        build(o<<1,L,M);
        build(o<<1|1,M+1,R);
        Push_up(o);
    }
    void update(int v,int L,int R)
    {
        ql=L;
        qr=R;
        sv=v;
        myupdate(1,1,sz);
    }
    int query()
    {
        return sumv[1];
    }
};
Line_Tree tree;
int main()
{
    int T;
    int kase=0;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        tree.init(n);
        tree.build(1,1,n);
        int m;
        scanf("%d",&m);
        int x,y,z;
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&z);
            tree.update(z,x,y);
        }
        printf("Case %d: The total value of the hook is %d.\n",++kase,tree.query());
    }
    return 0;
}


 

你可能感兴趣的:(线段树)