【poj3468】 A Simple Problem with Integers
http://poj.org/problem?id=3468 (题目链接)
题意
给出一个序列,要求维护区间修改与区间求和操作。
Solution
多年以前学习的树状数组区间修改又忘记了→_→。
其实就是用树状数组维护一个差分序列${delta[i]}$,${delta[x]}$记录${[i,n]}$中每一个数的增量,每次修改${[l,r]}$就转化为了${delta[l]+=d,delta[r+1]-=d}$。
对于求和操作${[l,r]}$,其实就是${sum(x)-sum(y)}$,我们这里只讨论${sum(x)}$的求法。
$${sum(x)=s[x]+delta[1]*x+delta[2]*(x-1)+delta[3]*(x-2)+······+delta[x]}$$
其中${s[x]}$表示原数组的前缀和。
$${sum(x)=s[x]+\sum_{i=1}^{x}{delta[i]*(x-i+1)}}$$
$${sum(x)=s[x]+(x+1)*\sum_{i=1}^{x}{delta[i]}-\sum_{i=1}^{x}{i*delta[i]}}$$
于是我们用两个树状数组维护${delta[i]}$与${delta[i]*i}$即可。
细节
更新与求和的时候的下标一定不要打错,没注意到,贡献1Wa→_→。
代码
// poj3468
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define MOD 100000000
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=100010;
LL c1[maxn],c2[maxn],s[maxn];
int n,m; int lowbit(int x) {return x&-x;}
LL query(int x) {
LL res=0;
for (int i=x;i;i-=lowbit(i)) res+=(x+1)*c1[i]-c2[i]; //important
return res;
}
void add(int x,LL val) {
for (int i=x;i<=n;i+=lowbit(i)) c1[i]+=val,c2[i]+=(LL)val*x;
}
int main() {
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) scanf("%lld",&s[i]),s[i]+=s[i-1];
char ch[10];
for (int x,y,i=1;i<=m;i++) {
scanf("%s%d%d",ch,&x,&y);
if (ch[0]=='Q') printf("%lld\n",s[y]-s[x-1]+query(y)-query(x-1));
else {
LL z;
scanf("%lld",&z);
add(x,z);add(y+1,-z);
}
}
return 0;
}
【poj3468】 A Simple Problem with Integers的更多相关文章
- 【poj3468】A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 97008 Accepted: 30285 Case Time Limi ...
- 【线段树成段更新成段查询模板】【POJ3468】A Simple Problem with Integerst
题目大意: 2个操作 A.区间a b 增加 c B 查询a b; 注意事项:1.记住要清除标记 2.查询时要下放标记,但没必要向上更新 线段:自带的,不用建模 区间和性质:sum: /* WA 1次 ...
- 一本通1548【例 2】A Simple Problem with Integers
1548:[例 2]A Simple Problem with Integers 题目描述 这是一道模板题. 给定数列 a[1],a[2],…,a[n],你需要依次进行 q 个操作,操作有两类: 1 ...
- POJ3468:A Simple Problem with Integers(线段树模板)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 149972 ...
- 【POJ3468】【zkw线段树】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
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- 【POJ2761】【fhq treap】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
http://poj.org/problem?id=3468 _(:зゝ∠)_我又活着回来啦,前段时间太忙了写的题没时间扔上来,以后再说. [问题描述] 成段加某一个值,然后询问区间和. [思路] 讲 ...
- 【POJ 3468】 A Simple Problem with Integers
[题目链接] 点击打开链接 [算法] 本题用线段树很容易写,但是,笔者为了练习树状数组,就用树状数组的方法做了一遍 我们不妨引入差分数组c, 则sum(n) = c[1] + (c[1] + c[2] ...
随机推荐
- 利用 Html 元标记控制搜索引擎蜘蛛
摘要:快照不被百度缓存: meta name = Baiduspider content = noarchive 所有搜索引擎,抓取这个页面.爬行链接.禁止快照: meta name = robots ...
- 学习jQuery的on事件
开发asp.net mvc程序,多少是离不开jQuery客户程序.今天Insus.NET学习jQuery的一个on事件驱动. 先在网页视图放一个图片铵钮,用户可以使用mouse对这图片时行over,o ...
- QT 对话框一
标准文件对话框 其函数形式如下:: QString QFileDialog::getOpenFileName ( QWidget * parent=, const QString &capti ...
- 微信小程序 开发 微信开发者工具 快捷键
微信小程序已经跑起来了.快捷键设置找了好久没找到,完全凭感觉.图贴出来.大家看看. 我现在用的是0.10.101100的版本,后续版本更新快捷键也应该不会有什么变化. 现在貌似不能修改.如果有同学找到 ...
- eclipse/intellij idea 远程调试hadoop 2.6.0
很多hadoop初学者估计都我一样,由于没有足够的机器资源,只能在虚拟机里弄一个linux安装hadoop的伪分布,然后在host机上win7里使用eclipse或Intellj idea来写代码测试 ...
- java调用.net asmx / wcf
一.先用asmx与wcf写二个.net web service: 1.1 asmx web服务:asmx-service.asmx.cs using System; using System.Coll ...
- TinyFrame再续篇:整合Spring AOP实现日志拦截
上一篇中主要讲解了如何使用Spring IOC实现依赖注入的.但是操作的时候,有个很明显的问题没有解决,就是日志记录问题.如果手动添加,上百个上千个操作,每个操作都要写一遍WriteLog方法,工作量 ...
- python数字图像处理(16):霍夫圆和椭圆变换
在极坐标中,圆的表示方式为: x=x0+rcosθ y=y0+rsinθ 圆心为(x0,y0),r为半径,θ为旋转度数,值范围为0-359 如果给定圆心点和半径,则其它点是否在圆上,我们就能检测出来了 ...
- 网页中三角切边还半透明,现在的设计师越来越牛,css也要跟上啊
需求 今天在群里看到一个需求,啊这种三角形缺角怎么做啊,还带半透明阴影的. 分析 要实现这个,可以用css做三角,网上找一下代码,像这样. 由于以前没有试过border能不能带透明,所以需要试验一下. ...
- 深入grootJs(进阶教程)
深入grootJs 这篇教程的原则是把grootJs原理讲透,主要真正理解了原理才能用起来随心所欲 mvvm模式简介 grootJs的vm结构 扫描函数sweep 垃圾回收的原理 加载器中的预编 ,控 ...