poj 3468 线段树成段更新
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 54012 | Accepted: 16223 | |
| 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<cstdio>
using namespace std; #define lson left,mid,i<<1
#define rson mid+1,right,i<<1|1 typedef __int64 LL;
int a[]; struct IntervalTree
{
int left,right;
LL sum,add;
}f[]; void pushdown(int i)
{
if(f[i].add != )
{
f[i<<].add+=f[i].add;
f[i<<|].add+=f[i].add;
f[i<<].sum+=(f[i<<].right-f[i<<].left+)*f[i].add;
f[i<<|].sum+=(f[i<<|].right-f[i<<|].left+)*f[i].add;
f[i].add=;
}
} void pushup(int i)
{
f[i].sum=f[i<<].sum+f[i<<|].sum;
} void bulid(int left,int right,int i)
{
f[i].left=left,f[i].right=right,f[i].add=;
if(left==right)
{
f[i].sum=a[left];return ;
}
int mid=(left+right)>>;
bulid(lson);
bulid(rson);
pushup(i);
return ;
} void update(int left,int right,int add,int i)
{
if(f[i].left==left && f[i].right==right)
{
f[i].add+=add;
f[i].sum+=(right-left+)*add;
return ;
}
pushdown(i);
if(f[i<<].right>=right) update(left,right,add,i<<);
else if(f[i<<|].left<=left) update(left,right,add,i<<|);
else { update(left,f[i<<].right,add,i<<);update(f[i<<|].left,right,add,i<<|);}
pushup(i);
return ;
} LL query(int left,int right,int i)
{
if(f[i].left==left && f[i].right==right) return f[i].sum;
pushdown(i);
if(f[i<<].right>=right) return query(left,right,i<<);
else if (f[i<<|].left<=left) return query(left,right,i<<|);
else return query(left,f[i<<].right,i<<)+query(f[i<<|].left,right,i<<|);
} int main()
{
int i,n,m,ai,bi,d;
char ch[];
while(~scanf("%d %d",&n,&m))
{
for(i=;i<=n;i++) scanf("%d",a+i);
bulid(,n,);
while(m--)
{
scanf("%s",ch);
if(strcmp(ch,"C")==)
{
scanf("%d %d %d",&ai,&bi,&d);
update(ai,bi,d,);
}
else
{
scanf("%d %d",&ai,&bi);
printf("%I64d\n",query(ai,bi,));
}
}
}
return ;
}
poj 3468 线段树成段更新的更多相关文章
- POJ 3468 线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- poj 3648 线段树成段更新
线段树成段更新需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候.延迟标记的意思是:这个区间的左右儿子都需要被更新,但是当 ...
- poj 3468 线段树 成段增减 区间求和
题意:Q是询问区间和,C是在区间内每个节点加上一个值 Sample Input 10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4Sample O ...
- poj 3669 线段树成段更新+区间合并
添加 lsum[ ] , rsum[ ] , msum[ ] 来记录从左到右的区间,从右到左的区间和最大的区间: #include<stdio.h> #define lson l,m,rt ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- ACM: Copying Data 线段树-成段更新-解题报告
Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
随机推荐
- python-下拉框处理
在自动化中python对下拉框的处理网上相对实例比较少,其它前辈写的教程中对下拉也仅仅是相对与教程来说的,比如下面: m=driver.find_element_by_id("Shippin ...
- oracle centos 重启后报错ORA-12514, TNS:listener does not currently know of service requested in connect descriptor
oracle centos 重启后报错ORA-12514, TNS:listener does not currently know of service requested in connect d ...
- JavaScript中数组的使用
---恢复内容开始--- 创建数组 1,通过 var arr1 = [1,2,3] 2通过使用 var arr2 = new Array(1,2,3) 在这里创造的数组实际上都是一个对象,然后把对象的 ...
- Windows平台下使用vs code来调试python代码(2)
背景:上篇文章我们介绍了怎么搭建相关的环境,文章链接:https://www.cnblogs.com/yahuian/p/10507467.html,这篇文章来介绍怎么调试我们的程序. 1.Debug ...
- 【最大流】bzoj1711: [Usaco2007 Open]Dining吃饭
正在网络流入门(原来这种题用网络流做) Description 农夫JOHN为牛们做了很好的食品,但是牛吃饭很挑食. 每一头牛只喜欢吃一些食品和饮料而别的一概不吃.虽然他不一定能把所有牛喂饱,他还是想 ...
- 了解swagger
https://blog.csdn.net/i6448038/article/details/77622977 随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染,变成了:前端渲染.先后端分离 ...
- PHP redis使用命令
很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/owlient/phpredis(支持redis 2.0.4) Redis::__constru ...
- 《linux设备驱动开发详解》笔记——14 linux网络设备驱动
14.1 网络设备驱动结构 网络协议接口层:硬件无关,标准收发函数dev_queue_xmit()和netif_rx(); 注意,netif_rx是将接收到的数据给上层,有时也在驱动收到数据以后调用 ...
- python数据类型之列表(list)和其常用方法
列表是python常用数据类型之一,是可变的,可由n = []创建,也可由n = list()创建,第一种方法更常用. 常用方法总结: # 创建方法 n = [] 或者 n = list() # in ...
- PAT Basic 1081
1081 检查密码 本题要求你帮助某网站的用户注册模块写一个密码合法性检查的小功能.该网站要求用户设置的密码必须由不少于6个字符组成,并且只能有英文字母.数字和小数点 .,还必须既有字母也有数字. 输 ...