POJ 3468 线段树 成段更新 懒惰标记
A Simple Problem with Integers
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<cstring>
- #include<cstdlib>
- #include<cstdio>
- #include<algorithm>
- #include<cmath>
- #include<queue>
- #include<map>
- #define N 100010
- #define M 15
- #define mod 1000000007
- #define mod2 100000000
- #define ll long long
- #define maxi(a,b) (a)>(b)? (a) : (b)
- #define mini(a,b) (a)<(b)? (a) : (b)
- using namespace std;
- int n,q;
- int x[N];
- char ch;
- int a,b,c;
- struct node
- {
- ll mark,sum;
- }tree[*N];
- void update(int l,int r,int i)
- {
- if(!tree[i].mark) return;
- int mid=(l+r)/;
- tree[*i].sum+=tree[i].mark*(ll)(mid-l+);
- tree[*i+].sum+=tree[i].mark*(ll)(r-mid);
- tree[*i].mark+=tree[i].mark;
- tree[*i+].mark+=tree[i].mark;
- tree[i].mark=;
- }
- ll query(int tl,int tr,int l,int r,int i)
- {
- if(tl>r || tr<l)
- return ;
- if(tl<=l && r<=tr)
- return tree[i].sum;
- update(l,r,i);
- int mid=(l+r)/;
- return query(tl,tr,l,mid,*i)+query(tl,tr,mid+,r,*i+);
- }
- void addd(int tl,int tr,int l,int r,int i,int val)
- {
- if(tl>r || tr<l)
- return;
- if(tl<=l && tr>=r){
- tree[i].sum+=val*(ll)(r-l+);
- tree[i].mark+=val;
- return;
- }
- update(l,r,i);
- int mid=(l+r)/;
- addd(tl,tr,l,mid,*i,val);
- addd(tl,tr,mid+,r,*i+,val);
- tree[i].sum=tree[*i].sum+tree[*i+].sum;
- }
- void build_tree(int l,int r,int i)
- {
- if(l==r){
- tree[i].sum=x[l];
- return;
- }
- int mid=(l+r)/;
- build_tree(l,mid,*i);
- build_tree(mid+,r,*i+);
- tree[i].sum=tree[*i].sum+tree[*i+].sum;
- }
- int main()
- {
- int i;
- // freopen("data.in","r",stdin);
- //scanf("%d",&T);
- //for(int cnt=1;cnt<=T;cnt++)
- //while(T--)
- while(scanf("%d%d",&n,&q)!=EOF)
- {
- // printf(" %d %d \n",n,q);
- for(i=;i<=n;i++) scanf("%d",&x[i]);
- // printf(" 1\n");
- memset(tree,,sizeof(tree));
- build_tree(,n,);
- // printf(" 2\n");
- getchar();
- while(q--){
- // printf(" 3\n");
- scanf("%c",&ch);
- // printf(" %c\n",ch);
- if(ch=='C'){
- //printf(" 31\n");
- scanf("%d%d%d",&a,&b,&c);
- getchar();
- addd(a,b,,n,,c);
- }
- else{
- // printf(" 32\n");
- scanf("%d%d",&a,&b);
- getchar();
- ll ans=query(a,b,,n,);
- printf("%I64d\n",ans);
- }
- }
- }
- return ;
- }
POJ 3468 线段树 成段更新 懒惰标记的更多相关文章
- 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- poj 3468 线段树成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 54012 ...
- 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单位之间的 ...
- HDU-1698-Just a Hook-区间更新+线段树成段更新
In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. T ...
随机推荐
- 文本编辑器vim/vi用法完全解读
vi用法 1.启动vim 2.命令模式和输入模式 3.退出vi 4.vi与ex命令 5.移动光标 6.跳转 7.搜索 8.插入文本 9.修改文本 10.替换文本 11.删除文本 12.恢复和撤销改变 ...
- Linux系统GEDIT编译运行C++
作为NOIP第一年强制使用Linux系统的考生,真的很难受,被迫还要学一波Linux系统. 正常的Windows对于较基础的程序员来说非常方便好用,但是对于高级程序员来说就是一个坑,于是就有了Linu ...
- iOS dateformatter设置GMT格式时间--iOS开发系列---项目中成长的知识四
今天在项目中开始接手客户端的签名这个模块,签名这个会在项目结束过后再单独写一下自己的心得! 今天讲讲在签名的过程中我们需要向服务器传送一个Date值,格式要求是格林威治时间,也就是GMT时间! 格式要 ...
- iptables(1)工具详解
一. iptables 查看链表,创建链表,类命令 1. iptables [-t table] -N chain : 创建一条自定义规则的链 1 2 # iptables -t filter ...
- 优化mysql查询
mysql提供了一个特别的explain语句,用来分析查询语句的性能 : explain select ... 1.在所有用于where,order by,group by的列上添加索引 创建索引 添 ...
- 力扣题目汇总(反转字符串中的单词,EXCEL表列序号,旋置矩阵)
反转字符串中的单词 III 1.题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode ...
- python爬虫基础08-selenium大全2/8-Chrome Webdriver启动选项
Selenium笔记(2)Chrome Webdriver启动选项 本文集链接:https://www.jianshu.com/nb/25338984 在Selenium中使用不同的Webdriver ...
- (转)在Xcode 7上直接使用Clang Address Sanitizer
原文地址: http://www.cocoachina.com/ios/20150730/12830.html WWDC 2015上,除了Swift 2.0外,还有一个令人激动的消息:可以直接在Xco ...
- 与LCD_BPP相关的函数
board/freescale/mx6q_sabresd/mx6q_sabresd.c: panel_info.vl_bpix = LCD_BPP; common/lcd.c: off = ...
- Ribbitmq
rittbiMQ: 连接远程rabbitmq server sudo rabbitmqctl addser mihon mihon123 sudo rabbitmqctl set_permission ...