A Simple Problem with Integers(线段树)
F - A Simple Problem with Integers
Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu
Description
给出了一个序列,你需要处理如下两种询问。
"C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000)。
"Q a b" 询问[a, b]区间中所有值的和。
Input
第一行包含两个整数N, Q。1 ≤ N,Q ≤ 100000.
第二行包含n个整数,表示初始的序列A (-1000000000 ≤ Ai ≤ 1000000000)。
接下来Q行询问,格式如题目描述。
Output
对于每一个Q开头的询问,你需要输出相应的答案,每个答案一行。
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
//自己写,又超时了,网上看了别人的才过的,在类里面要增加一个积累量这个数据,这样,增加命令的时候只要将特定区间积累量加起来并且num+总和,不必遍历到每一个叶节点,但是在查询时,还是要释放,因为,可能查的区间,在积累的这个区间只占一部分。
这样节约了时间,好像叫lazy的思想
#include <stdio.h>
#include <string.h> struct
{
int l;
int r;
double num;
double Inc;
}shu[]; void Init (int left,int right,int k)
{
shu[k].l=left;
shu[k].r=right;
shu[k].Inc=;
if(left==right)
{
scanf("%lf",&shu[k].num);
return;
}
int mid=(left+right)/;
Init(left,mid,*k);
Init(mid+,right,*k+);
shu[k].num=shu[*k].num+shu[*k+].num;
} void insert(int left,int right,double c,int k)
{
if (shu[k].l==left&&shu[k].r==right)//到某个区间
{
shu[k].Inc+=c;
return;
}
shu[k].num+=(right-left+)*c;
int mid=(shu[k].l+shu[k].r)/;
if (right<=mid)
insert(left,right,c,*k);
else if (left>mid)
insert (left,right,c,*k+);
else
{
insert(left,mid,c,*k);
insert(mid+,right,c,*k+);
}
}
double query(int left,int right,int k)
{
if (shu[k].l==left&&shu[k].r==right)//
{ return shu[k].num+(shu[k].r-shu[k].l+)*shu[k].Inc;
}
int mid=(shu[k].l+shu[k].r)/;
if(shu[k].Inc!=)//将这个区间的积累量释放
{
shu[k].num+=(shu[k].r-shu[k].l+)*shu[k].Inc;//自己先加上
insert(shu[k].l,mid,shu[k].Inc,*k);
insert(mid+,shu[k].r,shu[k].Inc,*k+);
shu[k].Inc=;
}
if (left>mid) return query(left,right,*k+);
else if (right<=mid) return query(left,right,*k);
else
return query(left,mid,*k)+query(mid+,right,*k+); } int main()
{
int Q;
int all_p,a,b;
double c;
char comend;
while (scanf("%d%d",&all_p,&Q)!=EOF)
{
Init(,all_p,);
while (Q--)
{
getchar();
scanf("%c",&comend);
scanf("%d%d",&a,&b); if (comend=='Q') printf("%.0lf\n",query(a,b,));
if (comend=='C')
{
scanf("%lf",&c);
insert(a,b,c,);
}
}
}
return ;
}
A Simple Problem with Integers(线段树)的更多相关文章
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...
- Poj 3468-A Simple Problem with Integers 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- A Simple Problem with Integers(线段树,区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 83822 ...
- POJ A Simple Problem with Integers 线段树 lazy-target 区间跟新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 105742 ...
随机推荐
- 打造你爱不释手的编辑器sublime3
首先去官网下载你的sublime3 让后安装好package control 去package control官网 安装好package control 安装emmet,和格式化工具 接着安装一个好主 ...
- opencv提取surf特征点出现的错误
opencv实现surf特征的提取.本来是一个非常easy的代码,结果我执行时却出现了各种错误,以下来谈谈我出现的错误及问题的解决过程. 首先,我把提取surf特征的过程整合成了一个函数,我单独建立一 ...
- python——深刻理解Python中的元类(metaclass)
译注:这是一篇在Stack overflow上 很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍然觉 ...
- Linux下使用Fastboot给手机刷ROM
前言 一直在刷机.失败.刷机.失败中,还好今天有个任务能够使用fastboot刷机.好开心,最终不用切换系统了.(话说好久没有写代码了,身为一个互联网程序猿,不写代码我easy紧张). 开发环境 Ub ...
- SSH——增删改的实现二
二.批量删除 逻辑删除取派员,将取派员的deltag改为“1” 1. 为“作废”按钮绑定事件 //批量删除取派员 function doDelete(){ //获得选中的行 var rows = $( ...
- 用Darwin开发RTSP级联server(拉模式转发)(附源代码)
源代码下载地址:https://github.com/EasyDarwin orwww.easydarwin.org 在博客 在Darwin进行实时视频转发的两种模式 中,我们描写叙述了流媒体serv ...
- ubuntu 环境变量PATH的修改
sudo nano /etc/environment 环 境变量是和Shell紧密相关的,用户登录系统后就启动了一个Shell.对于Linux来说一般是bash,但也可以重新设定或切换到其它的 She ...
- OSI与TCP/IP你了解多少?
刚开始接触计算机的时候,非常好奇数据是怎样传输的,通过后期的学习逐步了解了计算机的通信原理.相信很多人在学习计算机的时候,首先入门的第一门课就是计算机的通信原理,而通信原理又离不开两大参考模型:OSI ...
- 使用pycharm手动搭建python语言django开发环境(三) 使用django的apps应用 添加应用静态文件
1)在django 工程目录中使用cmd命令行 敲入"python manage.py startapp app名称(例子为blog)" 2)在django工程目录中应该生成了bl ...
- codeforces #364a Cards
cf的a题没什么好说到,100的量级,每个人给2张牌,使每个人手中的牌点数相等.保证有一种分配方案. 对每个人,先计算出手中的牌的点数,然后循环两遍拿牌就可以. A. Cards time lim ...