線段樹·HDU1166 敵兵佈陣·單點更新區間求和

題目大意:

query a, b查詢ab區間的和

add a b單點更新,更新a的值

sub就是減;

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 sum[maxd<<2];
int 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;sum[rt] = 1;
    if(l == r) {

        //cout << "+++" << endl;
        return ;
        //scanf("%d", &sum[rt]);
    }
    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);
}
int query(int L, int R, int l, int r, int rt ) {
    if(L <= l && r <= R) {
        return sum[rt];
    }
    push_down(rt, r - l + 1);
    int m = (l + r) >> 1;
    int res = 0;
    if(L <= m) {
        res += query(L, R, lson);
    }
    if(R > m) {
        res += query(L, R, rson);
    }
    return res;
}

int main() {
    int t;
    cin >>t;
    int kase = 0;
    while(t --) {
        kase ++;
        int n;
        cin >> n;
        build(1, n, 1);
        int m;
        cin >> m;
        while(m --) {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            //cout << "+++" << endl;
            update(a, b, c, 1, n, 1);
        }
        printf("Case %d: The total value of the hook is %d.\n", kase, sum[1]);
    }
}

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