题意:

两个序列a和b,初始a[i]=0,b[i]给定且为一个1到n的排列,要求维护以下两种操作:
1.区间[L,R]内a[i]加1

2.询问[L,R]内a[i]/b[i](下取整)之和

n,q<=1e5

思路:

实际上a[i]/b[i]的线段树可以改为树状数组,因为只需要支持单点修改和前缀区间求和

 #include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair
const int N=;
int ad[N<<],mn[N<<],T[N],b[N],n,m,cas; void pushdown(int p)
{
if(ad[p])
{
ad[p<<]+=ad[p];
mn[p<<]+=ad[p];
ad[p<<|]+=ad[p];
mn[p<<|]+=ad[p];
ad[p]=;
}
} void pushup(int p)
{
mn[p]=min(mn[p<<],mn[p<<|]);
} void build(int l,int r,int p)
{
ad[p]=;
if(l==r)
{
mn[p]=b[l];
return;
}
int mid=(l+r)>>;
build(l,mid,p<<);
build(mid+,r,p<<|);
pushup(p);
} void modify(int x)
{
for(int i=x;i<=n;i+=i&-i) T[i]++;
} int ask(int x)
{
int ans=;
for(int i=x;i;i-=i&-i) ans+=T[i];
return ans;
} void update(int l,int r,int x,int y,int p)
{
if((x<=l)&&(r<=y))
{
ad[p]--;
mn[p]--;
return;
}
pushdown(p);
int mid=(l+r)>>;
if(x<=mid) update(l,mid,x,y,p<<);
if(y>mid) update(mid+,r,x,y,p<<|);
pushup(p);
} void clear(int l,int r,int p)
{
if(l==r)
{
if(!mn[p])
{
mn[p]=b[l];
modify(l);
}
return;
}
pushdown(p);
int mid=(l+r)>>;
if(!mn[p<<]) clear(l,mid,p<<);
if(!mn[p<<|]) clear(mid+,r,p<<|);
pushup(p);
} int main()
{ while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++) scanf("%d",&b[i]);
build(,n,);
for(int i=;i<=n;i++) T[i]=;
for(int i=;i<=m;i++)
{
char ch[];
scanf("%s",ch);
if(ch[]=='a')
{
int l,r;
scanf("%d%d",&l,&r);
update(,n,l,r,);
clear(,n,);
}
else
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",ask(r)-ask(l-));
}
}
}
return ;
}

【HDOJ6315】Naive Operations(线段树,树状数组)的更多相关文章

  1. hdu Naive Operations 线段树

    题目大意 题目链接Naive Operations 题目大意: 区间加1(在a数组中) 区间求ai/bi的和 ai初值全部为0,bi给出,且为n的排列,多组数据(<=5),n,q<=1e5 ...

  2. 杭电多校第二场 hdu 6315 Naive Operations 线段树变形

    Naive Operations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Other ...

  3. 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,另一种操 ...

  4. HDU 6315 Naive Operations(线段树区间整除区间)

    Problem DescriptionIn a galaxy far, far away, there are two integer sequence a and b of length n.b i ...

  5. 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, ...

  6. HDU6315 Naive Operations(线段树 复杂度分析)

    题意 题目链接 Sol 这题关键是注意到题目中的\(b\)是个排列 那么最终的答案最多是\(nlogn\)(调和级数) 设\(d_i\)表示\(i\)号节点还需要加\(d_i\)次才能产生\(1\)的 ...

  7. HDU 6315 Naive Operations(线段树+复杂度均摊)

    发现每次区间加只能加1,最多全局加\(n\)次,这样的话,最后的答案是调和级数为\(nlogn\),我们每当答案加1的时候就单点加,最多加\(nlogn\)次,复杂度可以得当保证. 然后问题就是怎么判 ...

  8. HDU-DuoXiao第二场hdu 6315 Naive Operations 线段树

    hdu 6315 题意:对于一个数列a,初始为0,每个a[ i ]对应一个b[i],只有在这个数字上加了b[i]次后,a[i]才会+1. 有q次操作,一种是个区间加1,一种是查询a的区间和. 思路:线 ...

  9. 2018HDU多校二 -F 题 Naive Operations(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6315 In a galaxy far, far away, there are two integer ...

  10. HDU6315 Naive Operations 线段树

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门  原题目描述在最下面. Solution ...

随机推荐

  1. AJPFX关于面向对象之封装,继承,多态 (下)

    (3)private: 对于对于成员来说:只能在该成员隶属于的类中访问. 对于类来说:类不可以声明为private. 4)protected: 对于对于成员来说:相同包中的类可以访问(包访问权限):基 ...

  2. 【转】javap -c命令详解

    javap -c命令详解 一直在学习Java,碰到了很多问题,碰到了很多关于i++和++i的难题,以及最经典的String str = "abc" 共创建了几个对象的疑难杂症. 知 ...

  3. Maven项目中War包的打包及依赖方式

    两个web项目之间的依赖引用方式.Web项目之间,通过war包的方式进行引用的.例如,有两个项目,puzzle-web和puzzle-web-demo,两个均是web项目,puzzle-web-dem ...

  4. IDEA下MyBatis错误总结

    1. Pom.xml配置 语法顺序 <properties resource="config.properties"> </properties> < ...

  5. 《Python基础教程》 读书笔记 第五章(下)循环语句

    5.5.1while循环 x=1 while x<=100: print x x+=1 确保用户输入了名字: name="" while not name: name=raw ...

  6. IOS颜色块设置

    + (UIImage *)imageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIG ...

  7. zc.buildout构建项目时报错‘AttributeError: '_NamespacePath' object has no attribute 'sort'’

    在使用zc.buildout构建项目时如果碰到‘AttributeError: '_NamespacePath' object has no attribute 'sort'’报错: An inter ...

  8. npm与cnpm

    npm介绍 说明:npm(node package manager)是nodejs的包管理器,用于node插件管理(包括安装.卸载.管理依赖等) 使用npm安装插件:命令提示符执行npm instal ...

  9. Python小记-- 读取当前目录下所有文件名

    # -*- coding: utf-8 -*- import os def file_name(file_dir): with open("SelectAllFiles.txt", ...

  10. Python基础1 介绍、基本语法 、 流程控制-DAY1

    本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...