poj3468 A Simple Problem with Integers (树状数组做法)
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 142198 | Accepted: 44136 | |
Case Time Limit: 2000MS |
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
Hint
Source
#include<iostream>
#include<string.h>
using namespace std;
typedef long long ll;
#define MAX 100005
int n,q;
int a[MAX];
ll bit0[MAX],bit1[MAX];
void updata(ll *b,int i,int val)
{
while(i<=n)
{
b[i]+=val;
i+=i&-i;
}
}
ll query(ll *b,int i)
{
ll res=;
while(i>)
{
res+=b[i];
i-=i&-i;
}
return res;
}
int main()
{
ios_base::sync_with_stdio();
cin.tie();
cout.tie();
while(cin>>n>>q)
{
memset(bit0,,sizeof(bit0));
memset(bit1,,sizeof(bit1));
for(int i=;i<=n;i++)
{
cin>>a[i];
updata(bit0,i,a[i]);
}
while(q--)
{
int l,r,x;
char ch;
cin>>ch;
cin>>l>>r;
if(ch=='C')
{
cin>>x;
updata(bit0,l,-x*(l-));
updata(bit1,l,x);
updata(bit0,r+,x*r);
updata(bit1,r+,-x);
}
else
{
ll sum=;
sum+=query(bit0,r)+query(bit1,r)*r;
sum-=query(bit0,l-)+query(bit1,l-)*(l-);
cout<<sum<<endl;
}
}
}
}
poj3468 A Simple Problem with Integers (树状数组做法)的更多相关文章
- POJ3468 A Simple Problem With Integers 树状数组 区间更新区间询问
今天学了很多关于树状数组的技巧.一个是利用树状数组可以简单的实现段更新,点询问(二维的段更新点询问也可以),每次修改只需要修改2个角或者4个角就可以了,另外一个技巧就是这题,原本用线段树做,现在可以用 ...
- 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 ...
- 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_树状数组
Problem Description Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operation ...
- poj3468 A Simple Problem with Integers(线段树/树状数组)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- 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 ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
随机推荐
- 手模手配置Eslint,看懂脚手架中的Eslint
使用ESLint前:eslint是干嘛的,我这样写有什么问题,怎么还报错了,太麻烦想去掉这个插件,脚手架中关于eslint文件里的配置是什么意思?怎么设置配置项和规则达到自己想要的检测效果呢?怎么集成 ...
- java截取某个字符之前或者之后的字符串
String str = lly://enterVideoList?result={jsonString}; 截取?之前字符串 String str1=str.substring(0, str.ind ...
- org.mybatis.spring.mapper.MapperScannerConfigurer 类作用
1. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property nam ...
- facenet 人脸识别(二)——创建人脸库搭建人脸识别系统
搭建人脸库 选择的方式是从百度下载明星照片 照片下载,downloadImageByBaidu.py # coding=utf-8 """ 爬取百度图片的高清原图 &qu ...
- 源码分析--HashMap(JDK1.8)
在JDK1.8中对HashMap的底层实现做了修改.本篇对HashMap源码从核心成员变量到常用方法进行分析. HashMap数据结构如下: 先看成员变量: 1.底层存放数据的是Node<K,V ...
- 2018-8-10-win10-UWP-发邮件
title author date CreateTime categories win10 UWP 发邮件 lindexi 2018-08-10 19:17:19 +0800 2018-2-13 17 ...
- python爬虫 1.自己定义类实现scrapy框架的基本功能
1.定义调度器,引擎,下载器,爬虫器,管道器 # 调度器,下载器,爬虫器,引擎,管道类 class Scheduler(): """调度器类""&qu ...
- python之 matplotlib模块之基本三图形(直线,曲线,直方图,饼图)
matplotlib模块是python中一个强大的绘图模块 安装 pip install matplotlib 首先我们来画一个简单的图来感受它的神奇 import numpy as np impo ...
- 【leetcode】892. Surface Area of 3D Shapes
题目如下: 解题思路:对于v = grid[i][j],其表面积为s = 2 + v*4 .接下来只要在判断其相邻四个方向有没有放置立方体,有的话减去重合的面积即可. 代码如下: class Solu ...
- Python读取文件时出现UnicodeDecodeError 'gbk' codec can't decode byte 0x80 in position x
Python在读取文件时 with open('article.txt') as f: # 打开新的文本 text_new = f.read() # 读取文本数据出现错误: UnicodeDecode ...