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

描述

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.

输入

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.

输出

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


前几次提交的时候没有吧node[u].add清零,一直WA
第一道线段树的题,当晚没有调出来,第二天才想出来,不错
 
 
#include<cstdio>
#include<iostream>
#define LL long long
#define R(u) (u<<1|1)
#define L(u) (u<<1)
using namespace std;
const int maxx=100005;
LL a[maxx];
int n,m;
struct Node{
int r,l;
LL add,sum;
}node[maxx<<2];
void Pushup(int u)
{
node[u].sum=node[L(u)].sum+node[R(u)].sum;
return;
}
void Pushdown(int u)
{
node[L(u)].add+=node[u].add;
node[R(u)].add+=node[u].add; node[L(u)].sum+=(node[L(u)].r-node[L(u)].l+1)*node[u].add; node[R(u)].sum+=(node[R(u)].r-node[R(u)].l+1)*node[u].add;
node[u].add=0; //一定记得清零
}
void Build(int u,int left,int right)
{
node[u].l=left,node[u].r=right;
node[u].add=0;
if(left==right)
{
node[u].sum=a[left];
return;
}
int mid=(left+right)>>1;
Build(L(u),left,mid);
Build(R(u),mid+1,right);
Pushup(u);
}
void update(int u,int left,int right,LL val)
{
if(left==node[u].l&&node[u].r==right)
{
node[u].add+=val;
node[u].sum+=(node[u].r-node[u].l+1)*val;
return;
}
node[u].sum+=(right-left+1)*val;// 当更新区间小于这段
int mid=(node[u].r+node[u].l)>>1;
if(mid>=right) update(L(u),left,right,val);//在左边
else if(mid<left) update(R(u),left,right,val);
else {
update(L(u),left,mid,val);
update(R(u),mid+1,right,val);
}
//Pushup(u);前面已经直接算出sum后面不用再Pushup了
}
LL Qurey(int u,int left,int right)
{
if(left==node[u].l&&node[u].r==right)
return node[u].sum;
if(node[u].add)Pushdown(u);
int mid=(node[u].r+node[u].l)>>1;
if(mid>=right) Qurey(L(u),left,right);
else if(mid<left) Qurey(R(u),left,right);
else return (Qurey(L(u),left,mid)+Qurey(R(u),mid+1,right));
//Pushup(u);
}
int main()
{
cin>>n>>m;
LL c;
for(int i=1;i<=n;i++)
scanf("%I64d",a+i);
Build(1,1,n);
while(m--)
{char x;int ai,an;
scanf("%c %d %d",&x,&ai,&an);
cin>>x>>ai>>an;
if(x=='C')
{
scanf("%I64d",&c);
update(1,ai,an,c);
}
else
printf("%I64d\n",Qurey(1,ai,an));
}
return 0;
}

  

poj3468 A Simple Problem with Integers (线段树区间最大值)的更多相关文章

  1. POJ3468 A Simple Problem with Integers —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...

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

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

  3. POJ 3468A Simple Problem with Integers(线段树区间更新)

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

  4. 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 ...

  5. 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 ...

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

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

  7. (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

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

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

  9. A Simple Problem with Integers 线段树 区间更新 区间查询

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 115624   Accepted: 35897 Case Time Lim ...

随机推荐

  1. nginx_tomcat负载均衡环境

    Nginx+Tomcat搭建 版本 操作系统版本 Centos 6.4 Nginx版本 nginx-1.3.15.tar.gz JDK版本 jdk-7u71-linux-i586 //jdk1.7 T ...

  2. DCOM中的APPID的用处,以及RemoteServerName的传递问题

      DCOM中的APPID的用处,以及RemoteServerName的传递问题  

  3. 开源GIS简介.学习

    开发者都希望自己的软件能够运行在尽可能多的计算机上.然而事与愿违,摆在 GIS开发者面前的仍然是对峙的平台.J2EE随着Java5.0的发布,已经正式更名为JavaEE,而微软也正式发布了.NET2. ...

  4. 用window.showModelDialog() 打开的页面的返回值

    有两个页面也个 Default1.aspx   另外一个是 Default2.aspx Default1.aspx 有个按钮是用来打开Default2.aspx页面的 按钮的js代码是 var win ...

  5. Linux/Unix双机建立信任教程

    Linux/Unix双机建立信任教程 一 需要建立信任关系的2台主机都执行生成密钥输入ssh-keygen -t rsa之后全部默认回车,这样就会在/root/.ssh下生成密钥文件 [root@pl ...

  6. cms替换主页的步骤

    cms替换主页的步骤 .先做好静态页面: .在D:\wamp\www\phpcms\install_package\phpcms\templates文件夹下建新的文件夹tianqiwangluo(项目 ...

  7. quartus ii13.0~16.0 调用uedit (转载http://blog.sina.com.cn/s/blog_6d5560f00102vax6.html)

    转自 http://blog.sina.com.cn/s/blog_6d5560f00102vax6.html Quartus II 中的文本编辑软件不好用,比较习惯与UE(Uedit32/ultra ...

  8. a==null和a.equals("null")的区别

    equals 是值比较,==是比较内存 A==B,比较句柄,就是比较变量A,B的地址存放的东西,比如int A=0;String B="bbbb";那么变量A的地址方的就是0,B的 ...

  9. eclipse新建项目,报错“Error: workspace\appcompat_v7\res\values-v21\styles_base.xml No resource found that matches the given name”

    新建项目报错,不知道为什么,以前从未出现过的错误,把sdk更新之后,出现莫名错误,自己也是一知半解,在网上找了好久的错误,终于在一个english网站找到了解决方法,soga,从未觉得english如 ...

  10. rhel7防止开机破密码

    在/etc/grub.d/00_header文件结尾加入  cat <<EOF set  superusers="qin"#用户名称可以更加自身编辑 password ...