A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 83822   Accepted: 25942
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

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

Source

题解:线段树区间更新模版,由于mid比较那块出问题了,错了好半天。。。还有就是注意左边的要是x-(x>>1),因为左边可能多一

代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
#define PL(x) printf("%lld",x)
typedef long long LL;
const int INF=0x3f3f3f3f;
#define ll root<<1
#define rr root<<1|1
#define lson ll,l,mid
#define rson rr,mid+1,r
#define S(x) tree[x].sum
#define L(x) tree[x].lazy
const int MAXN=100010;
LL ans=0;
struct Node{
LL lazy,sum;
};
Node tree[MAXN<<2]; void pushup(int root){
S(root)=S(ll)+S(rr);
}
void pushdown(int root,int x){
if(L(root)){
L(ll)+=L(root);
L(rr)+=L(root);
S(ll)+=L(root)*(x-(x>>1));//注意
S(rr)+=L(root)*(x>>1);
L(root)=0;
}
}
void build(int root,int l,int r){
int mid=(l+r)>>1;
L(root)=0;
if(l==r){
SL(S(root));
//printf("%d %d %lld\n",l,r,S(root));
return;
}
build(lson);
build(rson);
pushup(root);
}
void update(int root,int l,int r,int A,int B,int v){
int mid=(l+r)>>1;
if(l>=A&&r<=B){
L(root)+=v;//注意是+=
S(root)+=v*(r-l+1);
return;
}
pushdown(root,r-l+1);
if(mid>=A)update(lson,A,B,v);//注意
if(mid<B)update(rson,A,B,v);//注意
pushup(root);
}
void query(int root,int l,int r,int A,int B){
int mid=(l+r)>>1;
//printf("%d %d %lld\n",l,r,S(root));
// PI(A);P_;PI(B);
if(l>=A&&r<=B){
ans+=S(root);
//printf("%d %d %lld\n",l,r,S(root));
return;
}
pushdown(root,r-l+1);
if(mid>=A)query(lson,A,B);//
if(mid<B)query(rson,A,B);//
}
int main(){
int N,Q;
char s[2];
int A,B,v;
while(~scanf("%d%d",&N,&Q)){
build(1,1,N);
while(Q--){
scanf("%s",s);
if(s[0]=='Q'){
scanf("%d%d",&A,&B);
ans=0;
query(1,1,N,A,B);
printf("%lld\n",ans);
}
else{
scanf("%d%d%d",&A,&B,&v);
update(1,1,N,A,B,v);
}
}
}
return 0;
}

  

extern "C++"{
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef unsigned u;
typedef unsigned long long ull;
#define mem(x,y) memset(x,y,sizeof(x))
void SI(double &x){scanf("%lf",&x);}
void SI(int &x){scanf("%d",&x);}
void SI(LL &x){scanf("%lld",&x);}
void SI(u &x){scanf("%u",&x);}
void SI(ull &x){scanf("%llu",&x);}
void SI(char *s){scanf("%s",s);} void PI(int x){printf("%d",x);}
void PI(double x){printf("%lf",x);}
void PI(LL x){printf("%lld",x);}
void PI(u x){printf("%u",x);}
void PI(ull x){printf("%llu",x);}
void PI(char *s){printf("%s",s);} #define NL puts("");
#define ll root<<1
#define rr root<<1|1
#define lson ll,l,mid
#define rson rr,mid+1,r
const int INF=0x3f3f3f3f;
const int MAXN=;
LL tree[MAXN << ];
LL lazy[MAXN << ];
} void pushup(int root){
tree[root] = tree[ll] + tree[rr];
}
void pushdown(int root,int x){
if(lazy[root]){
lazy[ll] += lazy[root];
lazy[rr] += lazy[root];
// tree[ll]=lazy[root]*(mid-l+1);
// tree[rr]=lazy[root]*(r-mid);
tree[ll] += lazy[root] * (x - (x >> ));
tree[rr] += lazy[root] * (x >> ); lazy[root] = ;
}
}
void build(int root,int l,int r){
int mid = (l + r) >> ;
lazy[root] = ;
if(l == r){
SI(tree[root]);
return ;
}
build(lson);
build(rson);
pushup(root);
}
void update(int root,int l,int r,int L,int R,int C){
if(l >= L && r <= R){
tree[root] += (r - l + ) * C;
lazy[root] += C;
return ;
}
int mid = (l + r) >> ;
pushdown(root,r-l+);
if(mid >= L)
update(lson,L,R,C);
if(mid < R)
update(rson,L,R,C);
pushup(root);
}
LL query(int root,int l,int r,int L,int R){
int mid = (l + r) >> ;
if(l >= L && r <= R){
return tree[root];
}
pushdown(root,r-l+);
LL ans = ;
if(mid >= L)ans += query(lson,L,R);
if(mid < R)ans += query(rson,L,R);
return ans;
}
int main(){
//assert(false);
int N,M;
while(~scanf("%d%d",&N,&M)){
build(,,N);
char s[];
int l,r,c;
while(M--){
SI(s);
SI(l);SI(r);
if(s[]=='Q'){
PI(query(,,N,l,r)),NL
}
else{
SI(c);
update(,,N,l,r,c);
}
}
}
return ;
}

A Simple Problem with Integers(线段树,区间更新)的更多相关文章

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

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

  2. (简单) 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 ...

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

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

  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 3468A Simple Problem with Integers(线段树区间更新)

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

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

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

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

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

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

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

  9. A Simple Problem with Integers(线段树区间更新复习,lazy数组的应用)-------------------蓝桥备战系列

    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...

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

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

随机推荐

  1. linux系统下安全管理

    1.引导程序安全 linux系统的root密码是很容易破解的,当然前提是你没有设置引导程序密码,如GRUB或LILO,为了防止通过引导程序破译root密码,强烈建 议设置GRUB或LILO的引导密码, ...

  2. jenkins中Deploy to container Plugin插件发布配置

    参数详解: 第一项(WAR/EAR files):是war包的相对路径(相对于工作区路径,即在工作区中war包的相对路径.)如我的maven执行完成之后会在工作区的target目录下生成项目.war, ...

  3. MAC OS X 快捷键(自己总结)

    command+space 可以切换键盘输入法:长按可进入输入法列表,并在多个输入法之间切换,输入法列表会根据你最近使用过的输入法自动调整排序. HID:00 00 91 00 00 00 00 00 ...

  4. CC++初学者编程教程(12) 基于rhel6.3的Oracle数据库学习环境搭建

    前言 安装oracle 11g系统最好是1G以上内存,硬盘至少需要4.5G空间. 至少环境在Linux Server release 5.3以上. win安装包 win32_11gR2_databas ...

  5. C++多字节字符转换为宽字符的两种方法

    目前知道有两种方式:可以提供宽字符与ANSI字符之间的转换, 第一种由COM库提供的函数 char*  _com_util::ConvertBSTRToString(BSTR ); BSTR _com ...

  6. Udacity-Artificial Intelligence for Robotics 课程笔记

    Lesson 1 Localization 蒙特卡洛机器人定位模型 sense 贝叶斯模型 move 全概率公式 localization练习 # The function localize take ...

  7. 浅谈css盒模型

    在我们网页上的每一个元素,一个按钮,一段文本,一张图片等等,浏览器都将它们当做一个“盒子”看待,并把这样的盒子称为盒模型(box model).使用Chrome的右键>审查元素对某个网页上的元素 ...

  8. 【带权并查集】【HDU3038】【How Many Answers Are Wrong】d s

    这个题看了2天!!!最后看到这篇题解才有所明悟 转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298091.html   ---by 墨染之樱 ...

  9. js charts去掉logo

    打开js charts 3的源代码搜索关键字"fs.bg",然后会找到 fs.bg.2v(fX),将这句代码删掉就OK了,可能有的版本会是fs.bg.2u(fX) 欢迎加入群:25 ...

  10. 查询EBS在线用户SQL(R12)

    SELECT U.USER_NAME, APP.APPLICATION_SHORT_NAME, FAT.APPLICATION_NAME, FR.RESPONSIBILITY_KEY, FRT.RES ...