線段樹·POJ-3468·區間加減區間求和

題目大意:兩種操作:

C a b c  a-b區間加上c;

Q查詢區間和;

解題思路:

基本的線段樹模板,唯一要提的就是和HDU-1698相比,lazy標記的處理不一樣,這題需要區間加減,所以向下傳遞的時候需要加點,而HDU-1698是區間替換,只需要用lazy標記替換下一個即可;

AC代碼:

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <bitset>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <iostream>
#include <algorithm>
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;
#define   maxn          1010
#define lson l , m , rt << 1

#define rson m + 1 , r , rt << 1 | 1
#define   ms(x,y)      memset(x,y,sizeof(x))
#define   rep(i,n)      for(int i=0;i<(n);i++)
#define   repf(i,a,b)   for(int i=(a);i<=(b);i++)
#define   PI           pair<int,int>
//#define   mp            make_pair
#define   FI            first
#define   SE            second
#define   IT            iterator
#define   PB            push_back
#define   Times         10
typedef   long long     ll;
typedef   unsigned long long ull;
typedef   long double   ld;
typedef   pair<int ,int > P;
//#define N 100
const double eps = 1e-10;
const double  pi = acos(-1.0);
const  ll    mod = 1e9+7;
const  int   inf = 0x3f3f3f3f;
const  ll    INF = (ll)1e18+300;
const int   maxd = 101000 + 10;

int ac[maxd];
ll sum[maxd<<2];
ll add[maxd<<2];
void push_up(int rt) {
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void push_down(int rt, int m) {
    if(add[rt]) {
        add[rt<<1] += add[rt];
        add[rt<<1|1] += add[rt];
        sum[rt<<1] += add[rt]*(m - (m>>1));
        sum[rt<<1|1] += add[rt] * (m>>1);
        add[rt] = 0;
    }
}
void build(int l, int r, int rt) {
    add[rt] = 0;
    if(l == r) {
        //cin >> sum[rt];
        scanf("%lld", &sum[rt]);
        return;
    }
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
    push_up(rt);
}
void update(int L, int R, int c, int l, int r, int rt) {
    if(L <= l && r <= R) {
        add[rt] += c;
        sum[rt] += (ll)c * (r - l + 1);
        return ;
    }
    push_down(rt, r - l + 1);
    int m = (l + r) >> 1;
    if(L <= m) {
        update(L, R, c, lson);
    }
    if(R > m) {
        update(L, R, c, rson);
    }
    push_up(rt);
}
ll query(int L, int R, int l , int r, int rt) {
    ll res = 0;
    if(L <= l && r <= R) {
        return sum[rt];
    }
    push_down(rt, r - l + 1);
    int m = (l + r) >> 1;
    if(L <= m) {
        res += query(L, R, lson);
    }
    if(R > m) {
        res += query(L, R, rson);
    }
    return res;
}

int main() {
    int n, q;
    scanf("%d%d", &n, &q);
    //cin >> n >> q;
    //getchar();
    //cin >> n >> q;
    build(1, n, 1);
    getchar();
    for (int i = 0; i < q; i++){
        char c[2];
        ll a, b;
        scanf("%s%lld%lld", &c, &a, &b);
        //cout <<"+**-/*-/  " << c << "   ++--*//*-" << a << " " << b << endl;
        //cin >> c >> a >> b;
        if(c[0] == 'Q') {
            cout << query(a, b, 1, n, 1) << endl;
        }
        else {
            ll cc;
            scanf("%lld", &cc);
            //cin >> cc;
            update(a, b, cc, 1, n, 1);
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章