您的位置:首页 > 其它

线段树·HDU1166 敌兵布阵·单点更新区间求和

2018-03-23 11:46 375 查看
题目大意:
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", ka
4000
se, sum[1]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: