POJ3468 A Simple Problem With Integers 树状数组 区间更新区间询问
今天学了很多关于树状数组的技巧。一个是利用树状数组可以简单的实现段更新,点询问(二维的段更新点询问也可以),每次修改只需要修改2个角或者4个角就可以了,另外一个技巧就是这题,原本用线段树做,现在可以用树状数组做的题,只需多维护一个bit即可。具体的思路见下面的链接:
http://hi.baidu.com/billdu/item/053f6a15ca301b0a8ebde400
要理解里面的橙色块求的时候是打竖看的,不是打横看的。
- #pragma warning(disable:4996)
- #include<iostream>
- #include<cstring>
- #include<cstdio>
- #include<vector>
- #include<algorithm>
- #include<cmath>
- #include<string>
- #define ll long long
- #define maxn 100000
- #define lowbit(k) k&(-k)
- using namespace std;
- ll bit[2][maxn + 50];
- int n,q;
- void inc(ll bit[],int i, int m)
- {
- for (; i <= n; i += lowbit(i)) bit[i] += m;
- }
- ll query(ll bit[],int i)
- {
- ll sum = 0;
- for (; i > 0; i -= lowbit(i)){
- sum += bit[i];
- }
- return sum;
- }
- ll sum[maxn + 50];
- int main()
- {
- while (cin >> n >> q)
- {
- memset(bit, 0, sizeof(bit));
- for (int i = 1; i <= n; i++){
- scanf("%lld", sum + i);
- }
- sum[0] = 0;
- for (int i = 1; i <= n; i++) sum[i] += sum[i - 1];
- char str[3];
- int a,b,c;
- for (int i = 0; i < q; i++){
- scanf("%s", str);
- if (str[0] == 'Q'){
- scanf("%d%d", &a, &b);
- ll ans = (query(bit[0], b)*(b + 1) - query(bit[1], b)) - (query(bit[0], a - 1)*a - query(bit[1], a - 1));
- ans += sum[b] - sum[a - 1];
- printf("%lld\n", ans);
- }
- else{
- scanf("%d%d%d", &a, &b, &c);
- inc(bit[0], a, c);
- inc(bit[1], a, c*a);
- inc(bit[0], b + 1, -c);
- inc(bit[1], b + 1, -c*(b + 1));
- }
- }
- }
- return 0;
- }
POJ3468 A Simple Problem With Integers 树状数组 区间更新区间询问的更多相关文章
- HDU 4267 A Simple Problem with Integers --树状数组
题意:给一个序列,操作1:给区间[a,b]中(i-a)%k==0的位置 i 的值都加上val 操作2:查询 i 位置的值 解法:树状数组记录更新值. 由 (i-a)%k == 0 得知 i%k == ...
- A Simple Problem with Integers(树状数组HDU4267)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- POJ3468 A Simple Problem with Interger [树状数组,差分]
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
- A Simple Problem with Integers_树状数组
Problem Description Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operation ...
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- hdu 2642二维树状数组 单点更新区间查询 模板题
二维树状数组 单点更新区间查询 模板 从零开始借鉴http://www.2cto.com/kf/201307/227488.html #include<stdio.h> #include& ...
- poj3468 A Simple Problem with Integers (树状数组做法)
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
- poj3468 A Simple Problem with Integers(线段树/树状数组)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- kuangbin专题七 POJ3468 A Simple Problem with Integers (线段树或树状数组)
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
随机推荐
- 常用的HTML 标签二
<marquee></marquee> 滚动的文字,也称"走马灯" 语法格式 <marquee 属性="属性值">内容< ...
- ZigBee HA示例程序分析
ZigBee协议栈中自带的HomeAutomation例程,虽然也是操作灯泡,但是,是通过ZCL来统一处理的,符合HA profile规范,互连互操作性较好.下面就简要分析以下ZCL的使用. 在任务数 ...
- Cllimbing Stairs [LeetCode 70]
1- 问题描述 You are climbing a stair case. It takes n steps to reach to the top. Each time you can eithe ...
- 必须知道的.NET FrameWork
这部分内容呢,对普通开发者来说,意义不是特别大.但是作为一个有情怀的开发者,应该追根溯源,懂一些自己用的工具, 的粗浅知识.并且这部分内容可以用来个HR吹嘘一下自己的情怀.基本就是这样. .NET生成 ...
- Webserver issues | PHP manager for IIS
4 down vote accepted In order to successfully install the PHP manager for IIS 8, you need the .NET 3 ...
- Color About——First
最近在Android开发学习中遇到TextView背景色以及文字颜色着色问题,在此做个记录. 首先对于颜色的选择,我推荐W3C School,上面有对网页颜色的详尽说明以及实例,还提供了对于,同一种颜 ...
- .NET特性-Attribute
两篇文章有助于学习Attribute特性的概念. http://blog.csdn.net/byondocean/article/details/6802111 http://www.cnblogs. ...
- WIN7右下角的声音图标不见了
有时候电脑启动了,但是声音图标却不见了,造成调试声音相当的麻烦,那么怎么来处理呢? 一:ctrl+shit+Esc键打开任务管理器 二:找到exeplore.exe,结束进程. 三:重新建立进程 上述 ...
- c# 空接合(??)运算符的运用
相信很多人都看到??运算符,但是不一定每个人都知道它是用来做什么的,之前我也看到过很多次,但是因为一直没有去用过,所以也没有了解他的作用,今天又看到了,所以查了的MSDN,原来??运算符叫做空接合运算 ...
- android开发 PopupWindow 设置充满屏幕
View qrcode_view = this.getLayoutInflater().inflate(R.layout.taskdetail_qrcode,null); final PopupWin ...