题目大意:

2个操作

A.区间a b 增加 c

B 查询a b;

注意事项:1.记住要清除标记

2.查询时要下放标记,但没必要向上更新

线段:自带的,不用建模

区间和性质:sum;


/*
WA 1次 以为不要LONG LONG
*/
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
const int maxn=100000+5;
using namespace std;
int N,Q;
LL tree[maxn*4];
LL col[maxn*4];
void PushUp(int rt)
{
tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
int build(int l,int r,int rt)
{
col[rt]=0;
if(l==r){scanf("%lld",&tree[rt]);return 0;}
int m=(l+r)>>1;
build(lson);
build(rson);
PushUp(rt);
}
void Pushdown(int rt,int k) //k是长度
{
if(col[rt])
{
col[rt<<1]+=col[rt];
col[rt<<1|1]+=col[rt];
tree[rt<<1]+=(k-(k>>1))*col[rt];
tree[rt<<1|1]+=(k>>1)*col[rt];
col[rt]=0;
}
}
int update(int L,int R,int c,int l,int r,int rt) {
if (L<=l&&r<= R) {
col[rt]+=c; //不同题目处理方式不同 ,
tree[rt]+=(LL)c*(r-l+1);
return 0;
}
Pushdown(rt,r-l+1); //Lazy 下放
int m=(l+r)>>1; //下面与以往一样
if (L<=m) update(L,R,c,lson);
if (R>m) update(L,R,c,rson);
PushUp(rt);
}
LL query(int L,int R,int l,int r,int rt)
{
LL temp=0;
if(L<=l&&r<=R){return tree[rt];}
Pushdown(rt,r-l+1);
int m=(l+r)>>1;
if(L<=m) temp+=query(L,R,lson);
if(R>m) temp+=query(L,R,rson);
return temp;
}
void solve()
{
char temp;
int a,b,c;
getchar();
for(int i=1;i<=Q;i++)
{
scanf("%c",&temp);
if(temp=='Q')
{
scanf("%d%d",&a,&b);
printf("%lld\n",query(a,b,1,N,1));
}
else if(temp=='C')
{
scanf("%d%d%d",&a,&b,&c);
update(a,b,c,1,N,1);
}
getchar();
}
}
void init()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
}
int main()
{
// init();
while(cin>>N>>Q)
{
build(1,N,1);
solve();
}
return 0;
}

全long long 形式

/*
WA 1次 以为不要LONG LONG
WA 2次 全LONG LONG为妙
WA 3次 m 忘记改成long long 了
*/
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
const int maxn=100000+5;
using namespace std;
int N,Q;
LL tree[maxn*4];
LL col[maxn*4];
void PushUp(int rt)
{
tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
int build(LL l,LL r,int rt)
{
col[rt]=0;
if(l==r){scanf("%lld",&tree[rt]);return 0;}
LL m=(l+r)>>1;
build(lson);
build(rson);
PushUp(rt);
}
void Pushdown(int rt,LL k) //k是长度
{
if(col[rt])
{
col[rt<<1]+=col[rt];
col[rt<<1|1]+=col[rt];
tree[rt<<1]+=(k-(k>>1))*col[rt];
tree[rt<<1|1]+=(k>>1)*col[rt];
col[rt]=0;
}
}
int update(int L,int R,LL c,LL l,LL r,int rt) {
if (L<=l&&r<= R) {
col[rt]+=c; //不同题目处理方式不同 ,
tree[rt]+=c*(r-l+1);
return 0;
}
Pushdown(rt,r-l+1); //Lazy 下放
LL m=(l+r)>>1; //下面与以往一样
if (L<=m) update(L,R,c,lson);
if (R>m) update(L,R,c,rson);
PushUp(rt);
}
LL query(int L,int R,LL l,LL r,int rt)
{
LL temp=0;
if(L<=l&&r<=R){return tree[rt];}
Pushdown(rt,r-l+1);
LL m=(l+r)>>1;
if(L<=m) temp+=query(L,R,lson);
if(R>m) temp+=query(L,R,rson);
return temp;
}
void solve()
{
char temp;
LL a,b;
LL c;
getchar();
for(int i=1;i<=Q;i++)
{
scanf("%c",&temp);
if(temp=='Q')
{
scanf("%lld%lld",&a,&b);
printf("%d\n",query(a,b,1,N,1));
}
else if(temp=='C')
{
scanf("%lld%lld%lld",&a,&b,&c);
update(a,b,c,1,N,1);
}
getchar();
}
}
void init()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
}
int main()
{
// init();
while(cin>>N>>Q)
{
build(1,N,1);
solve();
}
return 0;
}

【线段树成段更新成段查询模板】【POJ3468】A Simple Problem with Integerst的更多相关文章

  1. hdu3308LCIS(线段树,点更新,段查寻,查寻时一定要注意跨越时如何计算)

    Problem Description Given n integers. You have two operations: U A B: replace the Ath number by B. ( ...

  2. 线段树 (区间更新,区间查询) poj http://poj.org/problem?id=3468

    题目链接 #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...

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

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

  4. poj3468A Simple Problem with Integers(线段树的区域更新)

    http://poj.org/problem?id=3468 真心觉得这题坑死我了,一直错,怎么改也没戏,最后tjj把q[rt].lz改成了long long 就对了,真心坑啊. 线段树的区域更新. ...

  5. hdu 1556:Color the ball(线段树,区间更新,经典题)

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

  6. hdu 1698:Just a Hook(线段树,区间更新)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. UVA 12436-Rip Van Winkle's Code(线段树的区间更新)

    题意: long long data[250001]; void A( int st, int nd ) { for( int i = st; i \le nd; i++ ) data[i] = da ...

  8. hdu1698线段树的区间更新区间查询

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. hdu1754线段树的单点更新区间查询

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  10. HDUOJ---1754 I Hate It (线段树之单点更新查区间最大值)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

随机推荐

  1. OpenStack core components CLI快速调用API

    1,openStack core components CLI 使用自身参数执行;

  2. 将常见对象转换成json字符串

    public class JsonUtil { public static String objectTojson(Object obj) { StringBuilder json = new Str ...

  3. android 读取txt文件内容

    Android的res文件夹是用来存储资源的,可以在res文件夹下建立一个raw文件夹,放置在raw文件夹下的内容会被原样打包,而不会被编译成二进制文件,并且可以通过R文件进行很方便地访问. 比如我们 ...

  4. 为什么VS提示SurfFeatureDetector不是cv的成员函数

    surf和sift算法都是在头文件#include <opencv2/features2d/features2d.hpp>中,但在新的opencv版本出来后,如果仍然使用这个头文件就会出现 ...

  5. Java Math的 floor,round和ceil的总结

    floor 返回不大于的最大整数 round 则是4舍5入的计算,入的时候是到大于它的整数round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下 ...

  6. Android学习之简单的数据存储

    在Android中,数据存储是开发人员不可以避免的.Android为开发者提供了很多的存储方法,在前面的博客中,已经讲述了sqlite存储数据.今天将介绍用SharedPreferences来存储数据 ...

  7. winform使用xml作为数据源

    1.新建窗体应用程序 2.拖放DataGridView 3.在bin\Debug中放入XML文件 using System; using System.Collections.Generic; usi ...

  8. jQuery 随滚动条滚动效果 (适用于内容页长文章)

    直接入题! 当内容页比较长的时候,网站右侧一直是空白,不如放点有用的东西给用户看,最好不要放广告,因为那样很邪恶,你懂的. 好吧,昨天写了这个东西,jQuery滚动随动区块,代码如下: //侧栏随动 ...

  9. your local changes would be overwritten by merge. commit stash or revert them to proceed. view them

    error log: your local changes would be overwritten by merge. commit stash or revert them to proceed. ...

  10. php抽象类与接口的区别

    1.对接口的使用是通过关键字implements.对抽象类的使用是通过关键字extends.当然接口也可以通过关键字extends继承. 2.接口中不可以声明成员变量(包括类静态变量),但是可以声明类 ...