【树转数组】poj1195
- /*
- 二维的树状数组:
- 更新某个元素时:
- NO.1:c[n1],c[n2],c[n3],....,c[nm];
- 当中n1 = i,n(i+1) = ni+lowbit(ni);
- nm+lowbit(nm)的值应该大于元素个数N。
- NO.2:sum(k)=c[n1]+c[n2]+...+c[nm];
- 当中nm=k,n(i-1)=ni-lowbit(ni);
- n1-lowbit(n1)的值应该小于0
- ----------------------------------------------------------------------
- 用二维的c[i][j]存储长条的和。。。、
- ---------------------------------------
- 元素的值加一个数:
- void update(int x, int y, int a)
- {
- for(int i=x; i<=s; i+=lowbit(i))
- {
- for(int j=y; j<=s; j+=lowbit(j))
- c[i][j] += a;
- }
- }
- ---------------------------------------------------
- int sum(int x, int y)矩阵0到x和0到y的元素和
- {
- int ret = 0;
- for(int i=x; i>0; i-=lowbit(i))
- {
- for(int j=y; j>0; j-=lowbit(j))
- ret += c[i][j];
- }
- return ret;
- }
- ------------------------------------------------
- */
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #define INF 0x3f3f3f3f
- using namespace std;
- int op,s;
- int x,y,a;
- int l,b,r,t;
- int c[1050][1050];
- int lowbit(int x)
- {
- return x & (-x);
- }
- void update(int x, int y, int a)
- {
- for(int i=x; i<=s; i+=lowbit(i))
- {
- for(int j=y; j<=s; j+=lowbit(j))
- c[i][j] += a;
- }
- }
- int sum(int x, int y)
- {
- int ret = 0;
- for(int i=x; i>0; i-=lowbit(i))
- {
- for(int j=y; j>0; j-=lowbit(j))
- ret += c[i][j];
- }
- return ret;
- }
- int main()
- {
- //freopen("input.txt","r",stdin);
- while(1)
- {
- scanf("%d",&op);
- if(op == 0)
- {
- scanf("%d",&s);
- memset(c, 0, sizeof(c));
- }
- if(op == 1)
- {
- scanf("%d%d%d",&x,&y,&a);
- update(x+1, y+1, a);
- }
- if(op == 2)
- {
- scanf("%d%d%d%d",&l, &b,&r,&t);
- l++;
- b++;
- r++;
- t++;
- printf("%d\n",sum(r, t) - sum(r, b-1) - sum(l-1, t) + sum(l-1, b-1) );
- }
- if(op == 3)
- return 0;
- }
- }
----------------------------------
快。。。。。
【树转数组】poj1195的更多相关文章
- 二维树状数组poj1195
题目链接:https://vjudge.net/problem/POJ-1195 题意:一开始输入0和一个s,0代表开始,s代表这是一个s*s的图,接下来会输入1或2,1代表进行单点修改,后面会接3个 ...
- poj1195(二维树状数组)
题目链接:https://vjudge.net/problem/POJ-1195 题意:有s*s的矩阵,初始化为全0,有两种操作,单点修改(x,y)的值,区间查询(x,y)的值(l<=x< ...
- POJ-1195 Mobile phones---裸的二维树状数组(注意下标从1,1开始)
题目链接: https://vjudge.net/problem/POJ-1195 题目大意: 直接维护二维树状数组 注意横纵坐标全部需要加1,因为树状数组从(1,1)开始 #include<c ...
- 二维树状数组(水题) POJ1195
前段时间遇到线段树过不了,树状数组却过了的题.(其实线段树过得了的) 回忆了下树状数组. 主要原理,还是二进制位数,每一项的和表示其为它的前((最后一位1及其后)的二进制数)和,可从二进制图来看.(用 ...
- 【POJ1195】【二维树状数组】Mobile phones
Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...
- poj1195二维树状数组模板
二维树状数组和一维的也差不多,改一下add和query函数即可:即按行修改,行内单点修改即可 /* 二维树状数组,询问一个二维区间内的数之和 */ #include<iostream> # ...
- poj-1195(二维树状数组)
题目链接:传送门 题意:给出操作,按照操作进行. 思路:将树状数组设置为二维的就行了. 注意: (1)每次求出的面积是S(x2,y2)-S(x1-1,y2)-S(x2,y1-1)+S(x1-1,y1- ...
- 【poj1195】Mobile phones(二维树状数组)
题目链接:http://poj.org/problem?id=1195 [题意] 给出一个全0的矩阵,然后一些操作 0 S:初始化矩阵,维数是S*S,值全为0,这个操作只有最开始出现一次 1 X Y ...
- POJ1195(二维树状数组)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 17176 Accepted: 7920 De ...
随机推荐
- Emmet超详细教程
Emmet超详细教程 一.总结 一句话总结:用的时候照着用,能提高效率. 1.快捷键如何使用? 需要敲代码的时候把快捷键放到旁边即可.照着敲. 二.Emmet超详细教程 Emmet的前身是大名鼎鼎的Z ...
- thinkphp 3.2,引入第三方类库的粗略记录
首先用第三方类库我是放到vendor里面的 根目录\ThinkPHP\Library\Vendor\Wxphp 例如创建了一个Wxphp文件夹,里面有个php文件叫做 zll.php 文 ...
- [Recompose] Transform Props using Recompose --mapProps
Learn how to use the 'mapProps' higher-order component to modify an existing component’s API (its pr ...
- C#生成6位随机验证码
private string VerifyCode() { Random random = new Random(); , ).ToString(); }
- [Linux] Use find to search for filename patterns
Learn how to use find to identify filenames matching specified patterns. We'll use find to identify ...
- [Compose] 12. Two rules about Funtors
We learn the formal definition of a functor and look at the laws they obey. Any Functor should follo ...
- Java的面向AOP编程
一. 引言 AOP(Aspect-Oriented Programming,面向切面的编程),是一种新型的编程范式,主张关注软件流程中的一个切面,将相同功能的代码整合打包在一起,减少系统的耦合性,增强 ...
- Android 输入框弹出样式
在androidMainfest.xml文件里 在Activity中设置 [A]stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置 [B]stateU ...
- [GraphQL] Add an Interface to a GraphQL Schema
As we start building out more complex GraphQL schemas, certain fields start to repeat across differe ...
- NOIP模拟 赌博游戏 - 概率dp
题意: 最近西雅图的高中校园里流行这样一个游戏. 我们有一个骰子,这个骰子有M个面,分别写着1..M,并且是个公平的骰子,换句话说,一次投掷时每个面朝上的概率是相同的. 游戏的组织者使用这个骰子进行N ...