poj3468 线段树+lazy标记
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 92921 | Accepted: 28910 | |
Case Time Limit: 2000MS |
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
Hint
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn=+;
typedef long long ll;
ll tree[*maxn];
ll lazy[*maxn];
ll num[maxn];
void build (int p,int l,int r)
{
if(l==r) {tree[p]=num[l];return;}
int mid=(l+r)>>;
build(p<<,l,mid);
build((p<<)|,mid+,r);
tree[p]=tree[p<<]+tree[(p<<)|];
}
void pushdown(int p,int m)//把lazy标记下放到儿子节点
{
if(lazy[p])
{
lazy[p<<]+=lazy[p];
lazy[(p<<)|]+=lazy[p];
tree[p<<]+=(m-(m>>))*lazy[p];
tree[(p<<)|]+=(m>>)*lazy[p];
lazy[p]=;
}
}
void update(int p,int l,int r,int x,int y,int v)
{
if(x<=l&&y>=r)
{
lazy[p]+=v;
tree[p]+=(ll)v*(r-l+);//区间长度
return;
}
pushdown(p,r-l+);
int mid=(l+r)>>;
if(y<=mid) update(p<<,l,mid,x,y,v);
else if(x>mid) update((p<<)|,mid+,r,x,y,v);
else {update(p<<,l,mid,x,mid,v),update((p<<)|,mid+,r,mid+,y,v);}
tree[p]=tree[p<<]+tree[(p<<)|];
}
ll find(int p,int l,int r,int x,int y)
{
if(x<=l&&y>=r){return tree[p];}
pushdown(p,r-l+);
int mid=(l+r)>>;
ll ans=;
if(y<=mid) ans=find(p<<,l,mid,x,y);
else if(x>mid) ans=find((p<<)|,mid+,r,x,y);
else ans=find(p<<,l,mid,x,mid)+find((p<<)|,mid+,r,mid+,y);
return ans;
}
int main()
{
int n,q;
char s[];
int a,b,c;
while(~scanf("%d%d",&n,&q))
{
memset(tree,,sizeof(tree));
memset(lazy,,sizeof(lazy));
for(int i=;i<=n;i++)
scanf("%lld",&num[i]);
build(,,n);
while(q--)
{
scanf("%s",s);
if(s[]=='Q')
{
scanf("%d%d",&a,&b);
printf("%lld\n",find(,,n,a,b));
}
else
{
scanf("%d%d%d",&a,&b,&c);
update(,,n,a,b,c);
}
}
}
}
poj3468 线段树+lazy标记的更多相关文章
- POJ3237 Tree(树剖+线段树+lazy标记)
You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edges are numbe ...
- 线段树+lazy标记 2019年8月10日计蒜客联盟周赛 C.小A的题
题目链接:https://nanti.jisuanke.com/t/40852 题意:给定一个01串s,进行m次操作,|s|<=1e6,m<=5e5 操作有两种 l r 0,区间[l,r] ...
- HDU_1698 Just a Hook(线段树+lazy标记)
pid=1698">题目请点我 题解: 接触到的第一到区间更新,须要用到lazy标记.典型的区间着色问题. lazy标记详情请參考博客:http://ju.outofmemory.cn ...
- POJ 3225 线段树+lazy标记
lazy写崩了--. 查了好久 /* U-> [l,r]–>1 I-> [1,l-1] [r+1,+无穷] –>0 D-> [l,r]–>0 C-> [1,l ...
- 线段树+Lazy标记(我的模版)
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ...
- C++-POJ2777-Count Color[线段树][lazy标记][区间修改]
分析:https://www.bilibili.com/read/cv4777102 #include <cstdio> #include <algorithm> using ...
- 线段树lazy标记??Hdu4902
Nice boat Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) To ...
- hdu-3397 Sequence operation 线段树多种标记
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3397 题目大意: 0 a b表示a-b区间置为0 1 a b表示a-b区间置为1 2 a b表示a- ...
- HDU 3468:A Simple Problem with Integers(线段树+延迟标记)
A Simple Problem with Integers Case Time Limit: 2000MS Description You have N integers, A1, A2, ... ...
随机推荐
- memcache memcached 区别
.目前大多数php环境里使用的都是不带d的memcache版本,这个版本出的比较早,是一个原生版本,完全在php框架内开发的.与之对应的带d的memcached是建立在libmemcached的基础上 ...
- 文成小盆友python-num15 - JavaScript基础
一.JavaScript简介 JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的 ...
- COJ 0026 汉诺塔问题
汉诺塔问题 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 经典的汉诺塔游戏相信很多同学都会玩的,规则就不用赘述,百科一下就OK ...
- Linux系统编程(27)——线程控制
进程在各自独立的地址空间中运行,进程之间共享数据需要用mmap或者进程间通信机制,那么如何在一个进程的地址空间中执行多个线程呢.有些情况需要在一个进程中同时执行多个控制流程,这时候线程就派上了用场,比 ...
- HDU_2012——判断表达式是否都为素数
Problem Description 对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数. I ...
- Create a simple REST web service with Python--转载
今日尝试用python建立一个restful服务. 原文地址:http://www.dreamsyssoft.com/python-scripting-tutorial/create-simple-r ...
- windows server2012域服务器降级的方法
最近在一个新的网络环境里建立windows域服务器,准备建立主.备两台域服务器.在一台新服务器上面安装了windows2012R2并配置了DC服务.域和林的级别设置为Windows2012R2级别.在 ...
- 简单CSS定位瀑布流实现方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 在jsp页面上直接打开PDF文件
1.在不需要使用插件,直接打开通过链接方式打开 <%@ page language="java" import="java.util.*,java.io.*&quo ...
- POJ 2513 Colored Sticks - from lanshui_Yang
题目大意:给定一捆木棒,每根木棒的每个端点涂有某种颜色.问:是否能将这些棒子首位项链,排成一条直线,且相邻两根棍子的连接处的颜色一样. 解题思路:此题是一道典型的判断欧拉回路或欧拉通路的问题,以木棍的 ...