题目大意:

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. tcpdump 抓包让wireshark来分析

    在linux下面用tcpdump 抓包非常方便, 但是抓的包要提取出来进行分析, 还是得用wireshark来过滤分析比较方便. 下面先介绍一下 TCPDUMP 的使用 例:tcpdump host ...

  2. uva12096 The SetStack Computer By sixleaves

    代码     typedef  map<Set,  vector<Set> Setcache;                  stack<               ci ...

  3. hdu 1166 敌兵布阵_线段树

    题意:略 思路:这题是单点更新,如果是减少的话,直接把数据变成负加上去就行了. #include <iostream> #include<cstdio> #include< ...

  4. Hibernate映射1

    Hibernet映射 集合映射: 类的属性字段是集合的. set: <set name=”属性字段” table=“属性字段的表名”> <key 外键 column=“”> & ...

  5. Linux 程序启停脚本

    start.sh #!/bin/sh java -jar ./program.jar & echo $! > /var/run/program.pid stop.sh #!/bin/sh ...

  6. R-plot

    颜色.图例和线 在散点图中添加信息.图例以及回归线. 模拟数据 #模拟数据 dat <- data.frame(X = runif(100,-2,2),T1 = gl(n=4,k=25,labe ...

  7. 【MFC初学】

    void CMy322Dlg::OnButton1() { UpdateData(TRUE); m_crypt=m_plaintxt; for(int i=0;i<m_plaintxt.GetL ...

  8. java工厂类与反射机制

    java 简单工厂类 2012-04-22 15:44:07|  分类: java |  标签:java工厂类  简单工厂类  |举报|字号 订阅     简单工厂模式需要由以下角色组成: 接口    ...

  9. pythonj基础之 多线程

    多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进 ...

  10. Hadoop数据操作系统YARN全解析

    “ Hadoop 2.0引入YARN,大大提高了集群的资源利用率并降低了集群管理成本.其在异构集群中是怎样应用的?Hulu又有哪些成功实践可以分享? 为了能够对集群中的资源进行统一管理和调度,Hado ...