fafu 1100 线段树
单点更新, 区间查询。 这题空间好小....
#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 线段树的更多相关文章
- poj 2155:Matrix(二维线段树,矩阵取反,好题)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17880 Accepted: 6709 Descripti ...
- poj 1195:Mobile phones(二维线段树,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14391 Accepted: 6685 De ...
- POJ 3468 A Simple Problem with Integers (线段树)
题意:给定两种操作,一种是区间都加上一个数,另一个查询区间和. 析:水题,线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024 ...
- HDU 1754 I Hate It (线段树)
题意:略. 析:裸的线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include < ...
- HDU 1166 敌兵布阵 (数状数组,或线段树)
题意:... 析:可以直接用数状数组进行模拟,也可以用线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...
- HDU 5023 A Corrupt Mayor's Performance Art (据说是线段树)
题意:给定一个1-n的墙,然后有两种操作,一种是P l ,r, a 把l-r的墙都染成a这种颜色,另一种是 Q l, r 表示,输出 l-r 区间内的颜色. 析:应该是一个线段树+状态压缩,但是我用s ...
- hdu 1255 覆盖的面积(线段树 面积 交) (待整理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 Description 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. In ...
- UVa 11297 Census (二维线段树)
题意:给定上一个二维矩阵,有两种操作 第一种是修改 c x y val 把(x, y) 改成 val 第二种是查询 q x1 y1 x2 y2 查询这个矩形内的最大值和最小值. 析:二维线段树裸板. ...
- Gym 101201J Shopping (线段树+取模)
题意:给定 n 个物品,然后有 m 个人买东西,他们有 x 元钱,然后从 l - r 这个区间内买东西,对于每个物品都尽可能多的买,问你最少剩下多少钱. 析:对于物品,尽可能多的买的意思就是对这个物品 ...
随机推荐
- jquery 浏览器放大缩小函数resize
<script> $(function(){ $(window).resize(function(){ var _height = $(window).height(); var _con ...
- 《转载》详解 CSS 属性 - 伪类和伪元素的区别
首先,阅读 w3c 对两者的定义: CSS 伪类用于向某些选择器添加特殊的效果. CSS 伪元素用于将特殊的效果添加到某些选择器. 可以明确两点,第一两者都与选择器相关,第二就是添加一些“特殊”的效果 ...
- c++中basic_istream::getline()的返回值何时为真
今天在看primer,17ch中的IO库再探,做课后练习,要求用ifstream.getline(char*, const unsigned, char)读取一个文件,用循环: while(ifs.g ...
- leetcode Linked List Cycle python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- css 里层元素撑不开外层元素
一般是,里面那层做了高度设置,如:height, overflow等 另外可以让里面元素清楚浮动,如:clear:both
- Vim配置 终端背景色配置
在终端下使用vim进行编辑时,默认情况下,编辑的界面上是没有显示行号.语法高亮度显示.智能缩进 等功能的.为了更好的在vim下进行工作,需要手动设置一个配置文件:.vimrc.在启动vim时,当前用户 ...
- 关于用自带摄像机录像无法捕获uri 问题解决
这个 我自己调用,好像并没有出现什么问题. 下面是我的代码.你们可以参照一下 File file = new File(Environment.getExternalStorageDirectory( ...
- javscript上传图片前预览的方法setPreViewImage()
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- IOS版新闻客户端应用源码项目
IOS版新闻客户端应用源码,这个是一款简单的新闻客户端源码,该应用实现没采用任何第三方类库的 ,并且这个应用的UI做得很不错的,值得我们的参考和学习,希望大家可以更加完善这款新闻类的应用吧. 源码下载 ...
- php数组分页类
<?php class ArrayPage{ public $totalPage;//全部页数 public $lists;//每页显示数目 public $arr = array();//分页 ...