题意:n个点。m个操作。两种操作类型。C X Y K 表示区间[x,y]上每一个点值加k。Q X Y 求区间[x,y]的和



分析:线段树区间求和,裸模板



注意:结果会超int,要用long long 表示,假设是在hust上交结果要用%I64d。poj的话则用%lld



代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include<vector>
#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
const int maxn = 1e5+5;
long long sum[maxn*4];
long long lazy[maxn*4];
int num[maxn];
void up(int rt){
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void down(int l,int r,int rt){
if(!lazy[rt]) return;
int mid=(l+r)/2;
lazy[rt<<1]+=lazy[rt];
lazy[rt<<1|1]+=lazy[rt];
sum[rt<<1]+=lazy[rt]*(mid-l+1);
sum[rt<<1|1]+=lazy[rt]*(r-mid);
lazy[rt]=0;
}
void build(int l,int r,int rt){
lazy[rt]=sum[rt]=0;
if(l==r){
sum[rt]=num[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
up(rt);
}
void update(int L,int R,int k,int l,int r,int rt){
if(L<=l&&r<=R){
lazy[rt]+=k;
sum[rt]+=(r-l+1)*k;
return;
}
down(l,r,rt);
int mid=(l+r)>>1;
if(L<=mid) update(L,R,k,l,mid,rt<<1);
if(R>mid) update(L,R,k,mid+1,r,rt<<1|1);
up(rt);
}
long long query(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R) return sum[rt];
down(l,r,rt);
int mid=(l+r)>>1;
long long ret = 0;
if(L<=mid) ret+=query(L,R,l,mid,rt<<1);
if(R>mid) ret+=query(L,R,mid+1,r,rt<<1|1);
up(rt);
return ret;
}
int main()
{
int n,q;
while(scanf("%d%d",&n,&q)!=EOF){
for(int i=1;i<=n;i++) scanf("%d",&num[i]);
build(1,n,1);
while(q--){
char c;
int x,y,k;
scanf(" %c",&c);
if(c=='C'){
scanf("%d%d%d",&x,&y,&k);
update(x,y,k,1,n,1);
}
else{
scanf("%d%d",&x,&y);
printf("%lld\n",query(x,y,1,n,1));
}
}
}
}

线段树专题 POJ3468 A Simple Problem with Integers的更多相关文章

  1. 【成端更新线段树模板】POJ3468-A Simple Problem with Integers

    http://poj.org/problem?id=3468 _(:зゝ∠)_我又活着回来啦,前段时间太忙了写的题没时间扔上来,以后再说. [问题描述] 成段加某一个值,然后询问区间和. [思路] 讲 ...

  2. POJ 3468 (线段树 区间增减) A Simple Problem with Integers

    这题WA了好久,一直以为是lld和I64d的问题,后来发现是自己的pushdown函数写错了,说到底还是因为自己对线段树理解得不好. 因为是懒惰标记,所以只有在区间分开的时候才会将标记往下传递.更新和 ...

  3. (线段树模板)A Simple Problem with Integers --POJ--3468

    链接: http://poj.org/problem?id=3468 代码: #include<stdio.h> #include<algorithm> #include< ...

  4. 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和

    poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...

  5. poj3468 A Simple Problem with Integers (线段树区间最大值)

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

  6. poj3468 A Simple Problem with Integers (树状数组做法)

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

  7. POJ3468 A Simple Problem with Integers 【段树】+【成段更新】

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

  8. poj------(3468)A Simple Problem with Integers(区间更新)

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

  9. poj3468 A Simple Problem with Integers(线段树区间更新)

    https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...

随机推荐

  1. 跨站脚本攻击XXS(Cross Site Scripting)修复方案

    今天突然发现,网站被主页莫名奇妙的出现了陌生的广告. 通过排查发现是跨站脚本攻击XXS(Cross Site Scripting).以下为解决方案. 漏洞类型: Cross Site Scriptin ...

  2. 关于c++11中static类对象构造函数线程安全的验证

    在c++11中,static静态类对象在执行构造函数进行初始化的过程是线程安全的,有了这个特征,我们可以自己动手轻松的实现单例类,关于如何实现线程安全的单例类,请查看c++:自己动手实现线程安全的c+ ...

  3. POJ_1088_(dp)(记忆化搜索)

    滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 95792   Accepted: 36322 Description ...

  4. HDU_1556_线段树区间更新

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  5. 干货分享--iOS及Mac开源项目和学习资料【超级全面】

    原文出处:codecloud http://www.kancloud.cn/digest/ios-mac-study/84557

  6. unity问题笔记

    拖放在预制体中的图片等资源,他们的加载需要我们控制吗?我觉得不需要控制,但是如果按照现在的这种方式保存资源到非标准的resources文件下,那怎么加载?ulua的规则是这样查找资源的吗?猜想:客户端 ...

  7. Python学习第二阶段Day2(json/pickle)、 shelve、xml、PyYAML、configparser、hashlib模块

    1.json/pickle   略. 2.shelve模块 import shelve # shelve 以key value的形式序列化,value为对象 class Foo(object): de ...

  8. xfce 设在分辨率1920 1080

    #自定义cvt 1920 1080 #查看系统显示器名称xrandr #设置分辨率xrandr --newmode "1920x1080_60.00" 173.00 1920 20 ...

  9. 利用postman进行接口测试并发送带cookie请求的方法

    做web测试的基本上都用用到postman去做一些接口测试,比如测试接口的访问权限,对于某些接口用户A可以访问,用户B不能访问:比如有时需要读取文件的数据.在postman上要实现这样测试,我们就必要 ...

  10. Python面向对象之私有属性和方法

    私有属性与私有方法 应用场景 在实际开发中,对象的某些属性或者方法 可能只希望在对象的内部被使用,而不希望在外部被访问到: 私有属性 就是对象不希望公开的属性: 私有方法 就是对象不希望公开的方法: ...