题目大意:

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. 充分利用CPU高速缓存,提高程序效率(原理篇)

    提高程序效率应该充分利用CPU的高速缓存.要想编写出对CPU缓存友好的程序就得先明白CPU高速缓存的运行机制. i5-2400S: 1.有三级缓存分别为 32k(数据.指令缓存分开,分为32k),25 ...

  2. poj 3230 Travel(dp)

    Description One traveler travels among cities. He has to pay for this while he can get some incomes. ...

  3. 不使用TNS直连数据库的三种方式

    1.在当前目录下新建tnsnames.ora文件 如windows环境下,在C:\Users\Administrator目录下新建tnsnames.ora文件,内容如下:test =(descript ...

  4. 【C++学习笔记1】

    几个比较容易忘记的东西....... 移动构造函数: Vector(Vector &&copy) //移动构造函数 { if(copy.A!=NULL) { A=copy.A; cop ...

  5. Linux学习2——文件与目录

    一.写在前面  在本节将介绍Linux下文件与目录的一些基本概念以及一些基本操作. 二.完成目标 1.了解文件和目录的一些基本概念 2.操作文件和目录的相关命令 3.文件内容查阅命令 4.文件查询命令 ...

  6. .net程序员转战android第二篇---牛刀小试

    上篇说道如何搭建android的开发环境,这一篇我们将牛刀小试一下, 完成我们第一个android APP应用. 我就从新建项目说起吧. 首先打开Eclipse,选择顶部的File(文件)——new( ...

  7. SQL查询练习题目

    SQL查询练习题目 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表(四)所示 ...

  8. C# 根据年月获得此月第一天和最后一天,并计算工作日

    string str = "2015年3月"; ); ); , secondIndex - firstIndex - ); , ); DateTime dt = DateTime. ...

  9. JS高级——闭包

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  10. 绝对好文C#调用C++DLL传递结构体数组的终极解决方案

    C#调用C++DLL传递结构体数组的终极解决方案 时间 2013-09-17 18:40:56 CSDN博客相似文章 (0) 原文  http://blog.csdn.net/xxdddail/art ...