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 ...
随机推荐
- Linux——.bash_profile和.bashrc的区别(如何设置生效)
/etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置./etc/bashrc:为每一个运 ...
- Session机制详细介绍
Session机制详细介绍
- mac下配置adb
博主近期搞了台macbook用,搞android开发爽多了.程序编译那个速度确实让我感到非常爽.尤其是在之前用windows时动辄启动eclipse几分钟,编译又花非常久的情况下,可是用了mac发现a ...
- Redis 3.2.8 集群模式+Sentinel多Master部署
环境准备CentOS 7.3redis1 172.18.1.101:7001 masterredis2 172.18.1.102:7002 masterredis3 172.18.1.103:7003 ...
- Nginx-1.12.1安装配置
Nginx介绍 Nginx官网 nginx.org,最新版1.13,最新稳定版1.12 Nginx应用场景:web服务.反向代理.负载均衡 Nginx著名分支,淘宝基于Nginx开发的Tengine, ...
- Failed to load http://localhost:8080/team.php: Request header field x-jwt-header is not allowed by Access-Control-Allow-Headers in preflight response.
axios 加入header之后,请求出现 Failed to load http://localhost:8080/team.php: Request header field x-jwt-head ...
- string 的函数
string 有一个很好用到函数:substr(index). 去掉前index个字符.
- js基础系列框架:JS重要知识点(转载)
这里列出了一些JS重要知识点(不全面,但自己感觉很重要).彻底理解并掌握这些知识点,对于每个想要深入学习JS的朋友应该都是必须的. 讲解还是以示例代码搭配注释的形式,这里做个小目录: JS代码预解析原 ...
- iOS开发UITableViewCell的选中时的颜色设置(转)
iOS开发UITableViewCell的选中时的颜色设置 1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSelectionStyle ...
- G - Harmonic Number (II) 找规律--> 给定一个数n,求n除以1~n这n个数的和。n达到2^31 - 1;
/** 题目:G - Harmonic Number (II) 链接:https://vjudge.net/contest/154246#problem/G 题意:给定一个数n,求n除以1~n这n个数 ...