HDU-DuoXiao第二场hdu 6315 Naive Operations 线段树
题意:对于一个数列a,初始为0,每个a[ i ]对应一个b[i],只有在这个数字上加了b[i]次后,a[i]才会+1。
有q次操作,一种是个区间加1,一种是查询a的区间和。
思路:线段树,一开始没用lazy,TLE了,然后开始想lazy的标记,这棵线段树的特点是,每个节点维护 :一个区间某个a 要增加1所需个数的最小值,一个区间已加上的mx的最大值标记,还有就是区间和sum。 (自己一开始没有想到mx标记,一度想把lazy传回去。
(思路差一点就多开节点标记。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <cstdlib>
#include <iterator>
#include <cmath>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <stack>
#pragma comment(linker, "/STACK:102400000,102400000") //c++
using namespace std; #define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii; #define fi first
#define se second #define OKC ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i) const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
// #define _DEBUG; //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*----------------------show time----------------------*/
int n,q;
const int maxn = ;
int a[maxn],b[maxn];
int sum[maxn*];
int lazy[maxn*];
int mx[maxn*];
int nd[maxn*]; void pushup(int rt){
sum[rt] = sum[rt<<] + sum[rt<<|];
nd[rt] = min(nd[rt<<] , nd[rt<<|]);
mx[rt] = max(mx[rt<<], mx[rt<<|]);
// lazy[rt] = lazy[rt<<1] + lazy[rt<<1|1];
// lazy[rt<<1] = lazy[rt<<1|1] = 0;
} void pushdown(int rt){
if(lazy[rt]){
lazy[rt<<]+= lazy[rt];
lazy[rt<<|] += lazy[rt];
mx[rt<<] +=lazy[rt];
mx[rt<<|] +=lazy[rt];
lazy[rt] = ;
}
}
void build(int l,int r,int rt){
if(l==r){
nd[rt] = b[l];
lazy[rt] = mx[rt] = sum[rt] = ;
return;
}
int mid = (l+r)/;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
pushup(rt);
} void update(int l,int r,int rt,int L,int R,int k){
if(l>=L && r<=R)
{
mx[rt] ++;
if(mx[rt] < nd[rt]){
lazy[rt]++;
return;
}
if(l==r){
sum[rt]++;
nd[rt]+=b[l];
lazy[rt] = ;
return;
} }
int mid = (l+r)/;
pushdown(rt);
if(mid >= L)update(l,mid,rt<<,L,R,k);
if(mid<R)update(mid+,r,rt<<|,L,R,k);
pushup(rt);
} ll query(int l,int r,int rt,int L,int R){
if(l>=L&&r<=R){
return sum[rt];
}
int mid = (l+r)/;
// pushdown(rt);
ll ans = ;
if(mid >= L)ans += query(l,mid,rt<<,L,R);
if(mid < R)ans += query(mid+,r,rt<<|,L,R);
return ans;
}
int main(){ while(~scanf("%d%d", &n, &q)){ for(int i=; i<=n; i++){
scanf("%d", &b[i]);
}
char s[];
build(,n,);
for(int i=; i<=q; i++){
int l,r;
scanf("%s%d%d", s, &l, &r);
if(s[]=='a'){
update(,n,,l,r,);
// debug(a[5]);
}
else {
ll ans = query(,n,,l,r);
printf("%lld\n",ans);
}
}
} return ;
}
HDU 6315
HDU-DuoXiao第二场hdu 6315 Naive Operations 线段树的更多相关文章
- 杭电多校第二场 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(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 Naive Operations 线段树
题目大意 题目链接Naive Operations 题目大意: 区间加1(在a数组中) 区间求ai/bi的和 ai初值全部为0,bi给出,且为n的排列,多组数据(<=5),n,q<=1e5 ...
- 2018HDU多校二 -F 题 Naive Operations(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6315 In a galaxy far, far away, there are two integer ...
- 51nod“省选”模测第二场 C 小朋友的笑话(线段树 set)
题意 题目链接 Sol 直接拿set维护\(li\)连续段.因为set内的区间互不相交,而且每个线段会被至多加入删除一次,所以复杂度是对的. #include<bits/stdc++.h> ...
- HDU6315 Naive Operations(线段树 复杂度分析)
题意 题目链接 Sol 这题关键是注意到题目中的\(b\)是个排列 那么最终的答案最多是\(nlogn\)(调和级数) 设\(d_i\)表示\(i\)号节点还需要加\(d_i\)次才能产生\(1\)的 ...
随机推荐
- ASP.NET Core on K8S深入学习(1)K8S基础知识与集群搭建
在上一个小系列文章<ASP.NET Core on K8S学习初探>中,通过在Windows上通过Docker for Windows搭建了一个单节点的K8S环境,并初步尝试将ASP.NE ...
- CF803D 题解
题面 正解:一道二分大水题! A:为什么我得不到满分? B : 评测的系统不一样啊! A : 蛤? 正常情况下我们日常练习均使用的是windows系统,在windows下,string 本身是可以存储 ...
- UIRefreshControl 问题
这两天在学UIRefreshControl,主要参照的是github上的一个Demo(网址 https://github.com/evgeniymikholap/UIRefreshControlExa ...
- unity3d立方体碰撞检测(c#代码实现)
由于unity自带的碰撞组件特别耗费性能,网上的unity物体碰撞的c#代码实现比较少,没有适合的,只能自己写一个来用: using System; using System.Collections. ...
- BrowserSync,自动刷新,解放F5,去掉更新提示
BrowserSync虽然这个技术不算新,但是依然有用.略微介绍下 没有安装node,先安装node,这里不再做介绍 安装 npm install -g browser-sync 全局安装,方便在任 ...
- S3 介绍
S3 是ceph rgw的基础,在学习RGW之前,先了解S3.
- 8.源码分析---从设计模式中看SOFARPC中的EventBus?
我们在前面分析客户端引用的时候会看到如下这段代码: // 产生开始调用事件 if (EventBus.isEnable(ClientStartInvokeEvent.class)) { EventBu ...
- 转载 | SVG向下兼容优雅降级方法
本文引自:http://www.zhangxinxu.com/wordpress/2013/09/svg-fallbacks/ 1.svg image标签降级技术 <svg width=&quo ...
- 简易数据分析 10 | Web Scraper 翻页——抓取「滚动加载」类型网页
这是简易数据分析系列的第 10 篇文章. 友情提示:这一篇文章的内容较多,信息量比较大,希望大家学习的时候多看几遍. 我们在刷朋友圈刷微博的时候,总会强调一个『刷』字,因为看动态的时候,当把内容拉到屏 ...
- SpringBoot第二十三篇:安全性之Spring Security
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11350255.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言 系统的安全 ...