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\n",a)
#define write2(a,b)     printf("%d %d\n",a,b)
#define write3(a,b,c)   printf("%d %d %d\n",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\n",query(i,j,1,n,1));
            }
        }
    }
    return 0;
}

 

posted @ 2013-09-27 15:38  Felix_F  阅读(164)  评论(0编辑  收藏  举报