题意:

给你n个数和2个操作,C操作是将一个区间内的每个数都加上k,Q操作是询问一个区间的和

链接:http://poj.org/problem?id=3468

思路:

线段树区间修改+区间查询

代码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int MAXN=2e5+;
typedef long long ll;
ll tree[MAXN<<];
ll lazy[MAXN<<]; //lazy数组为区间修改时引进的一个标记数组
void push_up(ll node)
{
tree[node]=tree[node<<]+tree[node<<|];
}
void build(ll node,ll l, ll r)
{
if(l==r)
{
scanf("%lld",&tree[node]);
return ;
}
ll mid=(l+r)>>;
build(node<<,l,mid);
build(node<<|,mid+,r);
push_up(node);
} /*
push_down函数用来下传lazy标记,左右儿子的lazy等于父亲节点的lazy,
左儿子的区间和改变的数值为左儿子的区间长度*lazy[node]的值,右儿子同理,
下传结束后将父亲节点的lazy[node]变为0.
*/ void push_down(int node,int l,int r,int mid)
{ lazy[node<<]+=lazy[node];
lazy[node<<|]+=lazy[node];
tree[node<<]+=(mid-l+)*lazy[node];
tree[node<<|]+=(r-mid)*lazy[node];
lazy[node]=; }
void update(int node,int l,int r,int x,int y,int k) //区间修改函数
{
if(x<=l&&y>=r) //如果l,r在范围内,该区间的和直接增加(r-l+1)*k,打上lazy标记
{
tree[node]+=(r-l+)*k;
lazy[node]+=k;
return;
}
ll mid=(l+r)>>;
if(lazy[node]) //如果有lazy标记,则下传
push_down(node,l,r,mid);
if(x<=mid)
update(node<<,l,mid,x,y,k);
if(y>mid)
update(node<<|,mid+,r,x,y,k);
push_up(node); //修改他父亲区间的和
}
ll query(ll node,ll l,ll r,ll x,ll y)
{
if(x<=l&&y>=r)
return tree[node];
ll mid=(l+r)>>;
if(lazy[node])push_down(node,l,r,mid);
ll ans=;
if(x<=mid)
ans+=query(node<<,l,mid,x,y);
if(y>mid)
ans+=query(node<<|,mid+,r,x,y);
return ans;
}
int main()
{
ll n,q;scanf("%lld%lld",&n,&q);
build(,,n);
for(int i=;i<=q;i++)
{
char str[];
scanf("%s",str);
ll x,y;
if(str[]=='Q')
{
scanf("%lld%lld",&x,&y);
printf("%lld\n",query(,,n,x,y));
}
else
{
ll k;
scanf("%lld%lld%lld",&x,&y,&k);
update(,,n,x,y,k);
}
}
return ;
}

A Simple Problem with Integers-POJ3468 区间修改+区间查询的更多相关文章

  1. POJ 3468 A Simple Problem with Integers (区间更新+区间查询)

    题目链接 Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operation ...

  2. poj3468 A Simple Problem with Integers(zkw区间修改模板)

    此题是一道线段树的裸题,这里只是为了保存我的zkw线段树模板 #include <cstdio> #include <cstring> #include <iostrea ...

  3. A Simple Problem with Integers 循环节 修改 平方 找规律 线段树

    A Simple Problem with Integers 这个题目首先要打表找规律,这个对2018取模最后都会进入一个循环节,这个循环节的打表要用到龟兔赛跑. 龟兔赛跑算法 floyed判环算法 ...

  4. POJ_3468 A Simple Problem with Integers 【线段树区间查询+修改】

    一.题目 POJ3468 二.分析 裸的线段树区间查询+修改. 三.AC代码 #include <cstdio> #include <iostream> #include &l ...

  5. POJ3468A Simple Problem with Integers(区间加数求和 + 线段树)

    题目链接 题意:两种操作:一是指定区间的数全都加上一个数,二是统计指定区间的和 参考斌神的代码 #include <iostream> #include <cstring> # ...

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

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

  7. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

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

  8. A Simple Problem with Integers 多树状数组分割,区间修改,单点求职。 hdu 4267

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

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

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

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

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

随机推荐

  1. JavaScript中的typeof 和instanceof

    Js中的instanceof 和typeof的区别 演示1 var v5=new Number("22"); document.write(typeof v5+"< ...

  2. CSS-禁止文本被选中

    pc端: .not-select{ -moz-user-select:none; /*火狐*/ -webkit-user-select:none; /*webkit浏览器*/ -ms-user-sel ...

  3. 关于SQL

    set nocount on 作用 阻止在结果集中返回显示受t-sql语句影响的行计数信息 set nocount on 不返回计数,set nocount off 返回计数 即使当set nocou ...

  4. Executor、Executors、ExecutorService多线程操作

    Executor:一个接口,其定义了一个接收Runnable对象的方法executor,其方法签名为executor(Runnable command),该方法接收一个Runable实例,它用来执行一 ...

  5. 吴裕雄--天生自然Numpy库学习笔记:NumPy 位运算

    bitwise_and() 函数对数组中整数的二进制形式执行位与运算. import numpy as np print ('13 和 17 的二进制形式:') a,b = 13,17 print ( ...

  6. RTU:EvalRightToUse License for feature adventerprise 1.0 will transition to RightToUse in 10 days. UDI ASR1002-X:JAE2100XXXX

    关于这个log:[Hostname] EvalRightToUse License for feature adventerprise 1.0 will transition to RightToUs ...

  7. Update(stage3):第1节 redis组件:7、持久化

    7.redis的持久化 由于redis是一个内存数据库,所有的数据都是保存在内存当中的,内存当中的数据极易丢失,所以redis的数据持久化就显得尤为重要,在redis当中,提供了两种数据持久化的方式, ...

  8. 「Luogu P5080 Tweetuzki 爱序列」

    题目大意 给出一些数,需要求出 \(\frac{a_{i+1}}{3}=a_i\) 或 \(a_{i+1}=2 \times a_i\) 时最长的序列 \(a\). 分析 可以发现符合条件的序列 \( ...

  9. ArrayStack(栈)

    顺序栈即数组型的栈.什么是栈呢?简单来说就像一个刚好装的下乒乓球大小的球筒,假设不能暴力打开球筒且只有一端有出口,那你放入或取出里面的球的操作都只能在一端进行,并且把球放进去或取出来都是由顺序决定的, ...

  10. Git三招

    一.Git提交指令 git init git第一次使用在当前文件夹初始化一个git仓库,第二次不需要 git add . 把当前文件夹所有文件添加到缓存区中. 可以选特定的文件夹或文件.将后面的.改变 ...