fafu 1100 线段树

时间:2021-09-21 14:37:48

题目链接

单点更新, 区间查询。 这题空间好小....

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = ;
int sum[maxn*][maxn*], n;
void pushUp(int pos, int rt) {
sum[pos][rt] = sum[pos][rt<<]+sum[pos][rt<<|];
}
void sub_update(int sign, int pos, int p, int l, int r, int rt, int val) {
if(l == r) {
if(!sign) {
sum[pos][rt] += val;
} else {
sum[pos][rt] = sum[pos<<][rt] + sum[pos<<|][rt];
}
return ;
}
int m = l+r>>;
if(p<=m)
sub_update(sign, pos, p, lson, val);
else
sub_update(sign, pos, p, rson, val);
pushUp(pos, rt);
}
void update(int x, int y, int l, int r, int rt, int val) {
if(l == r) {
sub_update(, rt, y, , n, , val);
return ;
}
int m = l+r>>;
if(x<=m)
update(x, y, lson, val);
else
update(x, y, rson, val);
sub_update(, rt, y, , n, , val);
}
int sub_query(int pos, int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
return sum[pos][rt];
}
int m = l+r>>, ret = ;
if(L<=m)
ret += sub_query(pos, L, R, lson);
if(R>m)
ret += sub_query(pos, L, R, rson);
return ret;
}
int query(int lx, int rx, int ly, int ry, int l, int r, int rt) {
if(lx<=l&&rx>=r) {
return sub_query(rt, ly, ry, , n, );
}
int m = l+r>>, ret = ;
if(lx<=m)
ret += query(lx, rx, ly, ry, lson);
if(rx>m)
ret += query(lx, rx, ly, ry, rson);
return ret;
}
int main()
{
int sign, lx, ly, val, rx, ry, tmp;
while(~scanf("%*d%d", &n)) {
mem(sum);
while(scanf("%d", &sign)) {
if(sign == )
break;
if(sign == ) {
scanf("%d%d%d", &lx, &ly, &val);
lx++, ly++;
update(lx, ly, , n, , val);
} else {
scanf("%d%d%d%d", &lx, &ly, &rx, &ry);
lx++, ly++, rx++, ry++;
int ans = query(lx, rx, ly, ry, , n, );
cout<<ans<<endl;
}
}
}
}