题目链接

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

#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;
}
}
}
}

fafu 1100 线段树的更多相关文章

  1. poj 2155:Matrix(二维线段树,矩阵取反,好题)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17880   Accepted: 6709 Descripti ...

  2. poj 1195:Mobile phones(二维线段树,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14391   Accepted: 6685 De ...

  3. POJ 3468 A Simple Problem with Integers (线段树)

    题意:给定两种操作,一种是区间都加上一个数,另一个查询区间和. 析:水题,线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024 ...

  4. HDU 1754 I Hate It (线段树)

    题意:略. 析:裸的线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include < ...

  5. HDU 1166 敌兵布阵 (数状数组,或线段树)

    题意:... 析:可以直接用数状数组进行模拟,也可以用线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...

  6. HDU 5023 A Corrupt Mayor's Performance Art (据说是线段树)

    题意:给定一个1-n的墙,然后有两种操作,一种是P l ,r, a 把l-r的墙都染成a这种颜色,另一种是 Q l, r 表示,输出 l-r 区间内的颜色. 析:应该是一个线段树+状态压缩,但是我用s ...

  7. hdu 1255 覆盖的面积(线段树 面积 交) (待整理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 Description 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.   In ...

  8. UVa 11297 Census (二维线段树)

    题意:给定上一个二维矩阵,有两种操作 第一种是修改 c x y val 把(x, y) 改成 val 第二种是查询 q x1 y1 x2 y2 查询这个矩形内的最大值和最小值. 析:二维线段树裸板. ...

  9. Gym 101201J Shopping (线段树+取模)

    题意:给定 n 个物品,然后有 m 个人买东西,他们有 x 元钱,然后从 l - r 这个区间内买东西,对于每个物品都尽可能多的买,问你最少剩下多少钱. 析:对于物品,尽可能多的买的意思就是对这个物品 ...

随机推荐

  1. bootstrap的导航改造

    在使用bootstrap制作后台时用到了响应式导航条,其中dropdown组件更是用的比较多,用的多需要点击的就多,dropdown默认鼠标左键单击才展开,如果使用鼠标放上去(hover)就展开则会省 ...

  2. [译] 使用Using Data Quality Services (DQS) 清理用户数据

    SQL Server 2012 Data Quality Services (DQS)  允许你使用自己的知识库来清洗数据. 在本文中我会展示一个简单示例. 使用DQS清理步骤如下: A. 建立DQS ...

  3. java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=Aict/listPagedAict.action

    原因:请求的URL地址不完整,没有找到host. 排查解决:发现HTTP请求的URL少加了项目名,导致URL地址不完整.

  4. U3D学习使用笔记(一)

    1.在使用NGUI控件时出现两种回调函数,一种是直接给回调函数赋值,一种是使用EventDelegate. UISlider.onDragFinished = functionName; EventD ...

  5. 写一个jq插件

    本文章摘自博客园的http://www.cnblogs.com/JustinYoung/archive/2010/03/30/jquery-chajian.html,写此文章只是方便自己记载技术 一个 ...

  6. Android studio libs目录

    Android studio libs目录: 关于Android studio libs目录,Android studio 已经为我们自动生成了,如果默认 是看不到默认Libs目录的,点击红色按钮地方 ...

  7. (转)设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)

    转自http://unmi.cc/uilable-uitextfield-padding-insets 主要是理解下UIEdgeInsets在IOS UI里的意义.靠,这货其实就是间隔,起个名字这么让 ...

  8. 【java】静态方法声明与调用习题

    public class dengd { static int getTt(int table[]) { for(int b=0; b<table.length;b++) { System.ou ...

  9. SQL Server 输出受影响的行

    前期准备: create table Nums(X int); create table T(X int); go 目的:把对表Nums的insert | delete | update 反映到T表中 ...

  10. 6. java.lang.IllegalArgumentException

    方法的参数错误 比如g.setColor(int red,int green,int blue)这个方法中的三个值,如果有超过255的也会出现这个异常,因此一旦发现这个异常,我们要做的,就是赶紧去检查 ...