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 ...
随机推荐
- float过后 高度无法自适应的解决方法
float过后 高度无法自适应的解决方法: 在float层最外面包一层div即可.
- annex-b格式
annex-b格式 总的来说H264的码流的打包方式有两种,一种为annex-b byte stream format的格式,这个是绝大部分编码器的默认输出格式,就是每个帧的开头的3~4个字节是H26 ...
- python abstractmethod 对象比较
from functools import total_ordering from abc import ABCMeta,abstractmethod @total_ordering class Sh ...
- 访问vector元素方法的效率比较(转)
LInux下: gcc 4.47,red hat6 #include<iostream> #include<vector> #include<time.h> usi ...
- Service(1)
服务是一个应用组件,能够在后运行耗时的操作,不提供一个用户界面.(由于不提供界面,所以能够耗时运行,和活动最大的不同).还有一个应用组件能够启动一个服务,服务会继续在后台运行及时用户切换到还有一个应用 ...
- .NET Core环境安装
.Net Core可从https://www.microsoft.com/net/download下载 如果你使用自己喜欢的命令行工具或使用Visual Studio Code,你需要下载.NET C ...
- Android 使用handler实现线程间发送消息 (主线程 与 子线程之间)、(子线程 与 子线程之间)
keyword:Android 使用handler实现线程间发送消息 (主线程 与 子线程之间).(子线程 与 子线程之间) 相信大家平时都有使用到异步线程往主线程(UI线程)发送消息的情况. 本文主 ...
- Django学习之全局变量
首先说说什么叫全局变量,我们经常在html中使用{{ var }}这样的模板变量,这些变量是我们在视图函数中 提前定义好的变量,通过render()等方法传递到模板中. 但是,还有一类变量,我们并没有 ...
- codeblocks设置当前行高亮
默认是不开启当前行高亮的. 如果想打开,选择:Settings>Editor>Editor Settings>Other options> 勾选Highlight line u ...
- Step By Step(Lua调用C函数)
原文: http://www.cnblogs.com/stephen-liu74/archive/2012/07/23/2469902.html Lua可以调用C函数的能力将极大的提高Lua的可扩展性 ...