A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 85851   Accepted: 26685
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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

The sums may exceed the range of 32-bit integers.

Source

 
题意:给一串数字,每次区间加上一个数,或询问区间和。
 
题解:
线段树水过,打个lazy标记就好。。。
线段树程序:
 #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define LL long long
#define MAXN 100010
struct node
{
LL left,right,sum,tag;
}tree[MAXN*];
LL A[MAXN];
LL read()
{
LL s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
void Pushup(LL k)
{
tree[k].sum=tree[k*].sum+tree[k*+].sum;
}
void Pushdown(LL k,LL l,LL r)
{
if(tree[k].tag!=)
{
tree[k*].tag+=tree[k].tag;
tree[k*+].tag+=tree[k].tag;
LL mid=(l+r)/;
tree[k*].sum+=tree[k].tag*(mid-l+);
tree[k*+].sum+=tree[k].tag*(r-mid);
tree[k].tag=;
}
}
void Build(LL k,LL l,LL r)
{
tree[k].left=l;tree[k].right=r;
if(l==r)
{
tree[k].sum=A[l];
return;
}
LL mid=(l+r)/;
Build(k*,l,mid);Build(k*+,mid+,r);
Pushup(k);
}
void Add(LL k,LL ql,LL qr,LL add)
{
//Pushdown(k,tree[k].left,tree[k].right);
if(ql<=tree[k].left&&tree[k].right<=qr){tree[k].tag+=add;tree[k].sum+=(tree[k].right-tree[k].left+)*add;return;}
Pushdown(k,tree[k].left,tree[k].right);
LL mid=(tree[k].left+tree[k].right)/;
if(qr<=mid)Add(k*,ql,qr,add);
else if(ql>mid)Add(k*+,ql,qr,add);
else {Add(k*,ql,mid,add);Add(k*+,mid+,qr,add);}
Pushup(k);
}
LL Sum(LL k,LL ql,LL qr)
{
//Pushdown(k,tree[k].left,tree[k].right);
if(ql<=tree[k].left&&tree[k].right<=qr)return tree[k].sum;
Pushdown(k,tree[k].left,tree[k].right);
LL mid=(tree[k].left+tree[k].right)/;
if(qr<=mid)return Sum(k*,ql,qr);
else if(ql>mid)return Sum(k*+,ql,qr);
else return Sum(k*,ql,mid)+Sum(k*+,mid+,qr);
}
int main()
{
LL N,Q,i,a,b,c;
char fh[];
N=read();Q=read();
for(i=;i<=N;i++)A[i]=read();
Build(,,N);
for(i=;i<=Q;i++)
{
scanf("\n%s",fh);
if(fh[]=='Q')
{
a=read();b=read();
printf("%lld\n",Sum(,a,b));
}
else
{
a=read();b=read();c=read();
Add(,a,b,c);
}
}
return ;
}

树状数组不会。。。

先附上fhq神犇的题解:

上次NOI集训的时候,一位福建女神牛和我探讨过这题能不能用BIT,我当时
的答复是可以,因为“扩展树状数组”这个东西理论上可以实现一般线段树
可以实现的东西,且空间上的常数好一点。但是对于“扩展树状数组”,这
个东西是我一时兴起想到的玩意,没有进行更多的研究,没查到任何的资料
,更没有想过如何把线段树著名的lazy思想照搬上去,以及动态开内存的解
决方案。 权衡利弊,我想了一个使用两棵BIT的替代方法来解决这题,并且成功地将
内存使用做到了1728K。这恐怕是带标记的扩展树状数组达不到的。 记录两个BIT,
设数列为A[i],BIT1的每个元素B1[i]=A[i]*i,
BIT2的每个元素B2[i]=A[i] 则:sum{A[i]|i<=a}=sum{B1[i]|i<=a}+(sum{B2[i]|1<=i<=N}-sum{B2[i]|i<=a})*a
sum{A[i]|a<=i<=b}=sum{A[i]|i<=b}-sum{A[i]|i<a}
这样就十分巧妙的解决了! 关键代码:
int N;
struct BIT{
long long a[NMax];
void ins(int x,long long k){
for (;x<N;x+=((x+1)&-(x+1)))a[x]+=k;
}
long long ask(int x){
long long ret;
for (ret=0;x>=0;x-=((x+1)&-(x+1)))ret+=a[x];
return ret;
}
}B1,B2;
long long B2S;
void Add(int a,long long x){
//printf("Add %d %I64d\n",a,x);
B1.ins(a,x*((long long)a+1));
B2.ins(a,x);B2S+=x;
}
void Add(int a,int b,long long x){
Add(b,x);
if (a)Add(a-1,-x);
}
long long Ask(int a){
//printf("Ask %d\n",a);
long long ret=B1.ask(a)+(B2S-B2.ask(a))*(long long)(a+1);
//printf("=%I64d\n",ret);
return ret;
}
long long Ask(int a,int b){
long long ret;
ret=Ask(b);
if (a)ret-=Ask(a-1);
return ret;
}

填坑中。。。。。。

Poj 3468-A Simple Problem with Integers 线段树,树状数组的更多相关文章

  1. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  2. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  3. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

    A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...

  4. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  5. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  6. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  7. POJ 3468 A Simple Problem with Integers //线段树的成段更新

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59046   ...

  8. poj 3468 A Simple Problem with Integers 线段树加延迟标记

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  9. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  10. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 67511   ...

随机推荐

  1. C# url 路径转换 相对路径 转换为 绝对路径

    用C#写爬虫时候,比较实用的一项技巧. /// <summary> /// 格式化URL函数 urlX 传入相对URL objurl 传入绝对基URL 基URL 一定要带HTTP:// / ...

  2. mvc5 + ef6 + autofac搭建项目(四)

    在列表页面,点击新增,弹出窗口实现视屏上传,这里存在一个问题,就是大文件上传的问题,iis出于安全问题,有限制,当然这不是大问题,解决也很容易: 见截图: 请忽略视屏文件,看得懂的请装作不懂. 源码 ...

  3. A题笔记(5)

    No. 1385 挤牛奶问题 Tips: 查找之前对数据进行一下排列会比较好; 两个“最长”放在一趟遍历里查找. class LT { public: int bt; int ct; int dura ...

  4. linux下安装svn(基于编码的方式)

    svn是什么,相信能看到这里的同学应该不会有这个问题了,费话不多说,开始: 1.创建目录 mkdir /home/svn/ 2.获取安装svn所需源文件(svn的官方网址是http://subvers ...

  5. C++自定义异常处理

    自定义异常类 class MyException { public: MyException() { } MyException(char* str) { msg = str; } MyExcepti ...

  6. 水题(素数表)NYOJ素数距离

                描述 现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度.如果左右有等距离长度素数,则输出左侧的值及相应距离. 如果输入的整数本身就是素数,则输 ...

  7. PHP设计模式之:工厂模式

    工厂模式: 由工厂类根据参数来决定创建出哪一种产品类的实例: 工厂类是指包含了一个专门用来创建其他对象的方法的类.所谓按需分配,传入参数进行选择,返回具体的类.工厂模式的最主要作用就是对象创建的封装. ...

  8. Python函数式编程初级学习

    函数式编程即函数可以作为参数传入函数,也可以返回函数. 1.高阶函数     函数可以作为参数传入函数.     def add(x,y,f):         return f(x)+f(y)   ...

  9. Django国际化注意事项

    涉及两部分内容: py/html文件国际化.外部js文件国际化 步骤 1. settings.py 激活相应的配置 2. 针对py文件,需要注意被翻译代码的编写方式 3. 针对html文件,需要注意被 ...

  10. 使用awstats统计分析tengine日志访问量及各种http网站数据

    下载awstats最新安装包并解压 cd /usr/local/src wget http://www.awstats.org/files/awstats-7.3.tar.gz tar -zxvf a ...