zoukankan      html  css  js  c++  java
  • POJ 3468 A Simple Problem with Integers 线段树

    http://poj.org/problem?id=3468 

    线段树区间操作...和昨天的题差不多...也是lazy操作...

    /********************* Template ************************/
    #include <set>
    #include <map>
    #include <list>
    #include <cmath>
    #include <ctime>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <bitset>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <cassert>
    #include <cstdlib>
    #include <cstring>
    #include <sstream>
    #include <fstream>
    #include <numeric>
    #include <iomanip>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    using namespace std;
    #define EPS         1e-8
    #define DINF        1e15
    #define MAXN        105000
    #define LINF        1LL << 60
    #define MOD         1000000007
    #define INF         0x7fffffff
    #define PI          3.14159265358979323846
    #define lson            l,m,rt<<1
    #define rson            m+1,r,rt<<1|1
    #define BUG             cout<<" BUG! "<<endl;
    #define LINE            cout<<" ------------------ "<<endl;
    #define FIN             freopen("in.txt","r",stdin);
    #define FOUT            freopen("out.txt","w",stdout);
    #define mem(a,b)        memset(a,b,sizeof(a))
    #define FOR(i,a,b)      for(int i = a ; i < b ; i++)
    #define read(a)         scanf("%d",&a)
    #define read2(a,b)      scanf("%d%d",&a,&b)
    #define read3(a,b,c)    scanf("%d%d%d",&a,&b,&c)
    #define write(a)        printf("%d
    ",a)
    #define write2(a,b)     printf("%d %d
    ",a,b)
    #define write3(a,b,c)   printf("%d %d %d
    ",a,b,c)
    #pragma comment         (linker,"/STACK:102400000,102400000")
    template<class T> inline T L(T a)       {return (a << 1);}
    template<class T> inline T R(T a)       {return (a << 1 | 1);}
    template<class T> inline T lowbit(T a)  {return (a & -a);}
    template<class T> inline T Mid(T a,T b) {return ((a + b) >> 1);}
    template<class T> inline T gcd(T a,T b) {return b ? gcd(b,a%b) : a;}
    template<class T> inline T lcm(T a,T b) {return a / gcd(a,b) * b;}
    template<class T> inline T Min(T a,T b) {return a < b ? a : b;}
    template<class T> inline T Max(T a,T b) {return a > b ? a : b;}
    template<class T> inline T Min(T a,T b,T c)     {return min(min(a,b),c);}
    template<class T> inline T Max(T a,T b,T c)     {return max(max(a,b),c);}
    template<class T> inline T Min(T a,T b,T c,T d) {return min(min(a,b),min(c,d));}
    template<class T> inline T Max(T a,T b,T c,T d) {return max(max(a,b),max(c,d));}
    template<class T> inline T exGCD(T a, T b, T &x, T &y){
        if(!b) return x = 1,y = 0,a;
        T res = exGCD(b,a%b,x,y),tmp = x;
        x = y,y = tmp - (a / b) * y;
        return res;
    }
    typedef long long LL;    typedef unsigned long long ULL;
    //typedef __int64 LL;      typedef unsigned __int64 ULL;
    /*********************   By  F   *********************/
    LL sum[MAXN*4];
    LL lazy[MAXN*4];
    void pushup(LL rt){
        sum[rt] = sum[L(rt)] + sum[R(rt)];
    }
    void pushdown(int rt,int val){
        if(lazy[rt]){
            lazy[L(rt)] += lazy[rt];
            lazy[R(rt)] += lazy[rt];
            sum[L(rt)] += (val - (val>>1)) * lazy[rt];
            sum[R(rt)] += (val>>1) * lazy[rt];
            lazy[rt] = 0;
        }
    }
    void build(int l,int r,int rt){
        lazy[rt] = 0;
        if(l == r) {
            scanf("%lld",&sum[rt]);
            return;
        }
        int m = Mid(l,r);
        build(lson);
        build(rson);
        pushup(rt);
    }
    void update(int L,int R,int l,int r,int rt,LL val){
        if(L <= l && R >= r){
            lazy[rt] += val;
            sum[rt] += val * (r-l+1);
            return;
        }
        pushdown(rt,r-l+1);
        int m = Mid(l,r);
        if(L <= m) update(L,R,lson,val);
        if(R > m) update(L,R,rson,val);
        pushup(rt);
    }
    LL query(int L,int R,int l,int r,int rt){
        if(L <= l && R >= r) return sum[rt];
        pushdown(rt,r-l+1);
        LL m = Mid(l,r);
        LL ret = 0;
        if(L <= m) ret += query(L,R,lson);
        if(R > m) ret += query(L,R,rson);
        return ret;
    }
    int main(){
        //FIN;
        //FOUT;
        int n,m,i,j;
        LL k;
        char op[10];
        while(~scanf("%d%d",&n,&m)){
            build(1,n,1);
            while(m--){
                scanf("%s",op);
                if(op[0] == 'C'){
                    scanf("%d%d%lld",&i,&j,&k);
                    update(i,j,1,n,1,k);
                }
                if(op[0] == 'Q'){
                    scanf("%d%d",&i,&j);
                    printf("%lld
    ",query(i,j,1,n,1));
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    洛谷 P2590 [ZJOI2008]树的统计
    如何让文件夹隐藏起来,保护自己的隐私
    SharePoint入门——创建一个网站
    SharePoint 解决管理员密码修改后各种问题的来袭
    .NET创建一个即是可执行程序又是Windows服务的程序
    C#——WebApi 接口参数传参详解
    C#中对Web.Config、App.Config字符串加密与解密的方法
    邮件服务器常用地址和端口信息
    Unity3d 5.x搭载VS2013使用
    系统性能优化- Session丢失
  • 原文地址:https://www.cnblogs.com/Felix-F/p/3339330.html
Copyright © 2011-2022 走看看