HDU-6315:Naive Operations(线段树+思维)
题意:
In a galaxy far, far away, there are two integer sequence a and b of length n.
b is a static permutation of 1 to n. Initially a is filled with zeroes.
There are two kind of operations:
1. add l r: add one for al,al+1...ar
2. query l r: query ∑ri=l⌊ai/bi⌋
题解:每次更新时,直接将 b 数组的对应区间都减一,如果发现有零,就将对应位置的答案加一并重置回原b数组对应的数。
#include <bits/stdc++.h>
using namespace std; const double EPS = 1e-;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
const int maxn = 1e5 + ;
int n, m, x, y;
int a[maxn];
char q[maxn];
struct Node{int mi, sum, add;} T[maxn<<]; void Build(int l, int r, int rt)
{
if(l == r){
T[rt].mi = a[l];
T[rt].sum = ;
T[rt].add = ;
return ;
} int mid = (l + r) >> ; Build(l, mid, rt<<);
Build(mid + , r, rt<<|); T[rt].mi = min(T[rt<<].mi, T[rt<<|].mi);
T[rt].sum = T[rt<<].sum + T[rt<<|].sum;
T[rt].add = T[rt<<].add + T[rt<<|].add;
} void Pushdown(int rt, int l, int r)
{
if(!T[rt].add) return; int mid = (l + r) >> ; T[rt<<].mi += T[rt].add;
T[rt<<|].mi += T[rt].add; T[rt<<].add += T[rt].add;
T[rt<<|].add += T[rt].add; T[rt].add = ;
} void Update(int L, int R, int l, int r, int rt, bool ok)
{
if(L <= l && r <= R){
if(ok){
T[rt].add--;
T[rt].mi--;
}
if(T[rt].mi > ) return;
if(l == r){
if(T[rt].mi == ) T[rt].sum++, T[rt].mi = a[l];
return;
}
ok = false;
}
Pushdown(rt, l, r); int mid = (l + r) >> ; if(L <= mid) Update(L, R, l, mid, rt<<, ok);
if(R > mid) Update(L, R, mid + , r, rt<<|, ok); T[rt].mi = min(T[rt<<].mi, T[rt<<|].mi);
T[rt].sum = T[rt<<].sum + T[rt<<|].sum;
} int Query(int L, int R, int l, int r, int rt)
{
if(L <= l && r <= R) return T[rt].sum; int ans = , mid = (l + r) >> ; if(L <= mid) ans += Query(L, R, l, mid, rt<<);
if(R > mid) ans += Query(L, R, mid + , r, rt<<|); return ans;
} int main()
{
while(scanf("%d%d", &n, &m) != EOF){
for(int i = ; i <= n; i++) scanf("%d", &a[i]); Build(, n, ); while(m--){
scanf("%s%d%d", q, &x, &y);
if(q[] == 'a') Update(x, y, , n, , true);
else printf("%d\n", Query(x, y, , n, ));
}
} return ;
}
HDU-6315:Naive Operations(线段树+思维)的更多相关文章
- HDU - 6315 Naive Operations (线段树+思维) 2018 Multi-University Training Contest 2
题意:数量为N的序列a和b,a初始全为0,b为给定的1-N的排列.有两种操作:1.将a序列区间[L,R]中的数全部+1:2.查询区间[L,R]中的 ∑⌊ai/bi⌋(向下取整) 分析:对于一个位置i, ...
- 杭电多校第二场 hdu 6315 Naive Operations 线段树变形
Naive Operations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Other ...
- HDU-DuoXiao第二场hdu 6315 Naive Operations 线段树
hdu 6315 题意:对于一个数列a,初始为0,每个a[ i ]对应一个b[i],只有在这个数字上加了b[i]次后,a[i]才会+1. 有q次操作,一种是个区间加1,一种是查询a的区间和. 思路:线 ...
- HDU 6315 Naive Operations(线段树区间整除区间)
Problem DescriptionIn a galaxy far, far away, there are two integer sequence a and b of length n.b i ...
- HDU 6315 Naive Operations(线段树+复杂度均摊)
发现每次区间加只能加1,最多全局加\(n\)次,这样的话,最后的答案是调和级数为\(nlogn\),我们每当答案加1的时候就单点加,最多加\(nlogn\)次,复杂度可以得当保证. 然后问题就是怎么判 ...
- HDU 6315.Naive Operations-线段树(两棵树合并)(区间单点更新、区间最值、区间求和)+思维 (2018 Multi-University Training Contest 2 1007)
6315.Naive Operations 题意很好理解,但是因为区间求和求的是向下取整的a[i]/b[i],所以直接分数更新区间是不对的,所以反过来直接当a[i]==b[i]的时候,线段树对应的位置 ...
- HDU - 6315(2018 Multi-University Training Contest 2) Naive Operations (线段树区间操作)
http://acm.hdu.edu.cn/showproblem.php?pid=6315 题意 a数组初始全为0,b数组为1-n的一个排列.q次操作,一种操作add给a[l...r]加1,另一种操 ...
- HDU 6315 Naive Operations(线段树+区间维护)多校题解
题意:a数组初始全为0,b数组题目给你,有两种操作: 思路:dls的思路很妙啊,我们可以将a初始化为b,加一操作改为减一,然后我们维护一个最小值,一旦最小值为0,说明至少有一个ai > bi,那 ...
- hdu Naive Operations 线段树
题目大意 题目链接Naive Operations 题目大意: 区间加1(在a数组中) 区间求ai/bi的和 ai初值全部为0,bi给出,且为n的排列,多组数据(<=5),n,q<=1e5 ...
- hdu 6315 Naive Operations (2018 Multi-University Training Contest 2 1007)
Naive Operations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Other ...
随机推荐
- Gradle Goodness: Renaming Files while Copying
With the Gradle copy task we can define renaming rules for the files that are copied. We use the ren ...
- WebSocket的原理,以及和Http的关系 (转载)
一.WebSocket是HTML5中的协议,支持持久连接:而Http协议不支持持久连接. 首先HTMl5指的是一系列新的API,或者说新规范,新技术.WebSocket是HTML5中新协议.新API. ...
- 我告诉你 ,一个 window免费系统下载的网站!
一个 window免费系统下载的网站! https://msdn.itellyou.cn/
- 浅谈JS异步轮询和单线程机制
单线程特点执行异步操作 js是单线程语言,浏览器只分配给js一个主线程,用来执行任务(函数),但一次只能执行一个任务,这些任务就会排队形成一个任务队列排队等候执行.一般而已,相对耗时的操作是要通过异步 ...
- Vue中使用webpack别名的方法
在工作中,我们经常会写出这种代码: import MHeader from '../../components/m-header/m-header' @import "../../commo ...
- ubuntu 如何进行文件、夹删除等操作
rm [选项] 文件-f, --force 强力删除,不要求确认-i 每删除一个文件或进入一个子目录都要求确认-I 在删除超过三个文件或者递归删除前要求确认-r, -R 递归删除子目录-d, --di ...
- jQuery之修改li下样式和图片
<script type="text/javascript"> $(document).ready(function(){ $('li').click(function ...
- 遍历collection是否会出现重复遍历?
在处理一次线上问题时,需要遍历一张玩家信息表,看单个account是否存在多个entity.使用aid_playerid_dict建立aid到playerid的映射,遍历过程中,发现同一个aid会出现 ...
- docker入门——安装(CentOS)、镜像、容器
Docker简介 什么是docker 官方解释: Docker is the company driving the container movement and the only container ...
- 深入了解Linux(一)
Linux的各个文件夹 每次当我使用linux的时候我都被一个个文件夹整懵逼,那么多文件夹到底是怎么分类的呢.今天终于有时间好好整理一下 /boot: 引导文件存放目录,内核文件(vmlinuz),引 ...