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 ...
随机推荐
- mui 选项卡
方法一:通过css 实现选项卡 <div id="slider" class="mui-slider"> <div id="slid ...
- html checkbox 实现全选/取消全选
html checkbox 实现全选/取消全选 <html> <body> <table border="1"> <tr> < ...
- ubuntu cp(copy) command
e.g. #cp –p –R /opt/lampp/drv ~/test Ubuntu_Linux命令:cp(copy)复制文件或目录 功能: 复制文件或目录 www.2cto.com 说 ...
- JavaScript | 基础(变量/引用/转换/函数)
———————————————————————————————————————————— 变量 全局变量:在函数体外声明,全局可以使用 局部变量:通过关键字var来声明 变量类型 <script ...
- C# DateTime的11种构造函数
别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Globalization; using Syste ...
- WPF入门教程系列一
WPF入门教程 一. 前言 公司项目基于WPF开发,最近项目上线有点空闲时间写一篇基于wpf的基础教材,WPF也是近期才接触,学习WPF也是在网上查资料与微软的MSDN进行学习,写本博客的目为了温 ...
- >/dev/null 2>&1的作用
1 表示stdout标准输出,系统默认值是1,所以">/dev/null"等同于"1>/dev/null" 2 表示stderr标准错误 &am ...
- socket编程之实现简单的ssh
服务器代码: #-*- coding:utf-8 -*- #edited by python3.6 # import socket,os ''' 创建socket对象 ''' server = soc ...
- sqlite3 PC安装及使用
sqlite3使用 1. 安装sqlite3 sudo apt-get install sqlite3 sudo apt-get install libsqlite3-dev 2. sqlite常用命 ...
- php 批量插入字段
foreach ($_POST as $key => $value){ $array[] = "add ".$key." varchar(220),"; ...