A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 69589 | Accepted: 21437 | |
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 <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long LL;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=; LL sum[MS];
LL C[][MS];
int N,Q; int lowbit(int x)
{
return x&(-x);
} void updata(int no,int x,LL value)
{
while(x<=N)
{
C[no][x]+=value;
x+=lowbit(x);
}
} LL getsum(int no,int x)
{
LL res=;
while(x>)
{
res+=C[no][x];
x-=lowbit(x);
}
return res;
} void solve()
{
scanf("%d%d",&N,&Q);
sum[]=;
for(int i=;i<=N;i++)
{
scanf("%lld",&sum[i]);
sum[i]+=sum[i-];
}
memset(C,,sizeof(C));
int l,r;
LL c;
char cmd[];
for(int i=;i<=Q;i++)
{
scanf("%s",cmd);
if(cmd[]=='Q')
{
scanf("%d%d",&l,&r);
LL ans=sum[r]-sum[l-]+(getsum(,r)-getsum(,r)*(N-r))-(getsum(,l-)-getsum(,l-)*(N-l+));
printf("%lld\n",ans);
}
else
{
scanf("%d%d%lld",&l,&r,&c);
updata(,l,c*(N-l+));
updata(,r+,(N-r)*(-c)); updata(,l,c);
updata(,r+,-c);
}
}
} int main()
{
solve();
return ;
}
A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。的更多相关文章
- C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)
参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; stru ...
- A Simple Problem with Integers(100棵树状数组)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 4267 A Simple Problem with Integers 多个树状数组
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
- A Simple Problem with Integers POJ - 3468 (线段树)
思路:线段树,区间更新,区间查找 #include<iostream> #include<vector> #include<string> #include< ...
- A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询
//add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...
- A Simple Problem with Integers~POJ - 3468
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- A Simple Problem with Integers POJ - 3468 (分块)
题目链接:https://cn.vjudge.net/problem/POJ-3468 题目大意:区间加减+区间查询操作. 具体思路:本来是一个线段树裸题,为了学习分块就按照分块的方法做吧. 分块真的 ...
- C - A Simple Problem with Integers - poj 3468(区间更新)
题意:有一个比较长的区间可能是100000.长度, 每个点都有一个值(值还比较大),现在有一些操作,C abc, 把区间a-b内全部加上c, Qab,求区间ab的值. 分析:很明显我们不可能对区间的每 ...
- POJ 3468(树状数组的威力)
之前说过这是线段树的裸题,但是当看了http://kenby.iteye.com/blog/962159 这篇题解后我简直震惊了,竟然能如此巧妙地转化为用树状数组来处理,附上部分截图(最好还是进入原网 ...
随机推荐
- 第三百四十三天 how can I 坚持
今天又莫名其妙的烦起来了,好没劲. 现在还在看电视机<太阳的后裔>,晚上也没怎么吃饭,干吃了两个馒头,老干妈+生洋葱,好凄惨. 上班看了好长时间会,乱七八糟的. 坚决不跳槽,但得坚持自己的 ...
- Latex 横排图片
\begin{figure} \begin{minipage}[t]{0.5\linewidth} \centering \includegraphics[width=2.2in]{figure/an ...
- sql server转移tempdb数据库的物理存放位置
转移前将原来的文件备份一下 将 tempdb 从其在磁盘上的当前位置移动到其他磁盘位置.由于每次启动 MSSQLSERVER 服务时都会重新创建 tempdb,因此不需要从物理意义上移动数据和日志 ...
- Spring中使用Hibernate
在context中定义DataSource,创建SessionFactoy,设置参数: DAO类继承HibernateDaoSupport,实现具体接口,从中获得HibernateTemplate进行 ...
- WCF再学习小结
http://www.cnblogs.com/jillzhang/archive/2010/04/04/1704388.html http://leelei.blog.51cto.com/856755 ...
- Oracle中的NVL,NVL2,NULLIF以及COALESCE函数使用
首先注意空(null)值,空值加任何值都是空值,空值乘任何值也都是空值,依此类推. 1.NVL函数 NVL函数的格式如下:NVL(expr1,expr2) 含义是:如果oracle第一个参数为空那么显 ...
- 理解MFC 文档、视图、框架[转]
理解文档/视图框架 出处.雷神 了解文档和视图的相互作用关系是编写MFC程序的基本功.但是MFC的应用程序框架把文档和视图之间 ...
- 事件委托&jQuery on
例如: <h2>Great Web resources</h2> <ul id="resources"> <li><a hre ...
- 关于以DataTable形式批量写入数据的案例
void IDataAccess.CommandDataTable(DataTable dt, string ProcedureName, System.Data.Common.DbParameter ...
- 一种基于PTP 协议的局域网高精度时钟同步方法(转)
原文地址 http://www.dzsc.com/data/html/2011-1-17/88338.html 1 引言 在分布式系统中, 常常需要一个全局时间, 用来确定系统中各种事件发生的先后.协 ...