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 ;
}
最新文章
- Android WIFI 分析(一)
- HTML5第一讲
- 初试钓鱼工具Weeman+DNS欺骗的使用
- Beetl 1.25 发布,java模板引擎
- Spring MVC + MyBatis整合(IntelliJ IDEA环境下)
- CentOS mysql硬盘满了挂载阿里云硬盘
- U盘安装RedHat 5.3
- 数据库SQLiteDatabase
- 基于WCF大型分布式系统的架构设计
- Activiti5.16.4数据库表结构
- spring boot admin + spring boot actuator + erueka 微服务监控
- 【Qt编程】基于Qt的词典开发系列<;四>;--无边框窗口的缩放与拖动
- Pandas基本操作
- [蓝桥杯]PREV-22.历届试题_国王的烦恼
- [Laravel] 04 - Blade templates
- Python中列表(list)、字典(dict)排序的程序
- redis 基本性能测试说明
- 批量改名的多种方法stu_3_finished.jpg 去掉finished,stu_{1..20}_finished.jpg
- 浅析linux内核中timer定时器的生成和sofirq软中断调用流程(转自http://blog.chinaunix.net/uid-20564848-id-73480.html)
- Codeforces Beta Round #5 B. Center Alignment 模拟题
热门文章
- HDU 6069 Counting Divisors(区间素数筛法)
- Python-OpenCV:cv2.imread(),cv2.imshow(),cv2.imwrite()
- PyCharm如何配置断点调试功能
- shell脚本,awk如何处理文件中上下关联的两行。
- javase(14)_java基础增强
- java在线聊天项目 swt可视化窗口Design 重新设计好友列表窗口 增加菜单栏
- java在线聊天项目0.8版 实现把服务端接收到的信息返回给每一个客户端窗口中显示功能
- UIControlEvent
- 2018 CCF NOIP提高组&;&;普及组答案
- initcall机制