HDU 6315.Naive Operations-线段树(两棵树合并)(区间单点更新、区间最值、区间求和)+思维 (2018 Multi-University Training Contest 2 1007)
6315.Naive Operations
题意很好理解,但是因为区间求和求的是向下取整的a[i]/b[i],所以直接分数更新区间是不对的,所以反过来直接当a[i]==b[i]的时候,线段树对应的位置更新+1操作是可取的,但是怎样才能在合适的时候+1操作呢?一开始智障想的是只要单点是b[i]的倍数就可以啊,但是这样就相当于单点查询的操作,铁定超时到上天,但是反过来就可以啊,直接一开始给一个数组赋值为b[i]的值,区间更新的时候所有的都更新,然后区间查询一下最小值,有0就说明有的已经正好减完b[i]个,然后tree数组进行+1操作就可以了,然后变为0的数组重新赋值相应的b[i]的值就可以了。因为b[i]是1-n的全排列,所以这种操作是可行的。然后我倒着思路写就过了,mdzz。。。
dls的思路也是这样的,贴一下dls的思路的题解:
代码:
//1007-6315-线段树-其实是两个线段树,合到一起
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
typedef long long ll;
using namespace std;
const int maxn=1e5+;
const int inf=0x3f3f3f3f;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 int tree[maxn<<],cnt[maxn<<],col[maxn<<],b[maxn],n,m;//tree建树求和,col延时标记,cnt暂时标记增加的数量 void pushup(int rt)
{
cnt[rt]=min(cnt[rt<<],cnt[rt<<|]);
tree[rt]=tree[rt<<]+tree[rt<<|];
} void pushdown(int rt)
{
if(col[rt]){
cnt[rt<<]-=col[rt];
cnt[rt<<|]-=col[rt];
col[rt<<]+=col[rt];
col[rt<<|]+=col[rt];
col[rt]=;
}
} void build(int l,int r,int rt)
{
if (l==r){
cnt[rt]=b[l];
tree[rt]=col[rt]=;
return;
} int m=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
} void update(int L,int R,int temp,int l,int r,int rt)
{
if(temp==){
if(l==r){
cnt[rt]=b[l];
tree[rt]+=;
return ;
} pushdown(rt);
int m=(l+r)>>;
if (L<=m) update(L,R,cnt[rt<<]==,lson);
if (R> m) update(L,R,cnt[rt<<|]==,rson);
}
else{
if(L<=l&&r<=R){
cnt[rt]-=;
col[rt]+=;
return ;
} pushdown(rt);
int m=(l+r)>>;
if (L<=m) update(L,R,,lson);
if (R> m) update(L,R,,rson);
}
pushup(rt);
} int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R){
return tree[rt];
} pushdown(rt);
int m=(l+r)>>;
int ret=;
if(L<=m) ret+=query(L,R,lson);
if(R> m) ret+=query(L,R,rson);
return ret;
} int main()
{
while(~scanf("%d%d",&n,&m)){
memset(cnt,inf,sizeof(cnt));
memset(tree,,sizeof(tree));
memset(col,,sizeof(col));
for(int i=;i<=n;i++)
scanf("%d",&b[i]);
build(,n,);
char s[];int l,r;
for(int i=;i<m;i++){
scanf("%s%d%d",s,&l,&r);
if(s[]=='a'){
update(l,r,cnt[]==,,n,);
}
else{
printf("%d\n",query(l,r,,n,));
}
}
}
return ;
}
讲道理,这个题写的我已经没有脾气了,因为我敲的时候,手抖把int m=(l+r)>>1,敲成int m=(l+r)<<1了,然后找了一晚上+一上午的错,最后快要撞墙的的时候,瞄了一眼代码,然后,就发现,哎哟我勒个去,我这个是不是写反了,mdzz,我去撞墙。。。
溜了。
HDU 6315.Naive Operations-线段树(两棵树合并)(区间单点更新、区间最值、区间求和)+思维 (2018 Multi-University Training Contest 2 1007)的更多相关文章
- 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 线段树变形
Naive Operations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Other ...
- 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 (线段树+思维) 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(线段树+复杂度均摊)
发现每次区间加只能加1,最多全局加\(n\)次,这样的话,最后的答案是调和级数为\(nlogn\),我们每当答案加1的时候就单点加,最多加\(nlogn\)次,复杂度可以得当保证. 然后问题就是怎么判 ...
- 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 ...
- HDU 6315: Naive Operations
Naive Operations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Other ...
- HDU 6315 Naive Operations(线段树+区间维护)多校题解
题意:a数组初始全为0,b数组题目给你,有两种操作: 思路:dls的思路很妙啊,我们可以将a初始化为b,加一操作改为减一,然后我们维护一个最小值,一旦最小值为0,说明至少有一个ai > bi,那 ...
- HDU 6315 Naive Operations 【势能线段树】
<题目链接> 题目大意: 给出两个序列,a序列全部初始化为0,b序列为输入值.然后有两种操作,add x y就是把a数组[x,y]区间内全部+1,query x y是查询[x,y]区间内∑ ...
随机推荐
- [剑指Offer] 11.二进制中1的个数
[思路]如果一个整数不为0,那么这个整数至少有一位是1.如果我们把这个整数减1,那么原来处在整数最右边的1就会变为0,原来在1后面的所有的0都会变成1(如果最右边的1后面还有0的话).其余所有位将不会 ...
- 【bzoj3653】谈笑风生 DFS序+树状数组
题目描述 给出一棵以1为根的有根树,q次询问,每次询问给出a和k,求点对 (b,c) 的数目,满足:a.b.c互不相同,b与a距离不超过k,且a和b都是c的祖先. 输入 输入文件的第一行含有两个正整数 ...
- [Leetcode] Balanced binary tree平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- HZOI String STL的正确用法
String 3s 512 MB描述硬盘中里面有n ...
- Asp.net MVC Combres的简单用法
第一步:添加nuget包 [1]添加 nuget包后,会自动在 webconfig里面 添加配置文件(不用改) <section name="dotless" type=&q ...
- @Resource注解完成自动装配
@Resource注解是通过名字来自动装配的.在spring中自动装配的模式如果是通过名字来自动装配那么必须保证bean的名字和pojo 的属性名一直. 下面是详细代码:说明了@Resource注解是 ...
- eclipse关闭错误警告提示
- mapreduce出现大量task被KILLED_UNCLEAN的3个原因
Request received to kill task 'attempt_201411191723_2827635_r_000009_0' by user ------- Task has bee ...
- java摘要
**idea 注册 Licensed to ilanyu License Server: http://idea.iteblog.com/key.php 1.文件上传下载 http://blog.cs ...
- idea使用(一)
基本上正式开发的常用工具基本都集成了,而且基本都在你非常容易触到的位置.说说我比较常用的: 1.ant 你懂的 2.maven你也懂的 3.SVN相比之下,IDEA的SVN的提交提供了更多的选项和功能 ...