HDU 6351 Naive Operations(线段树)
题目:
http://acm.hdu.edu.cn/showproblem.php?pid=6315
Naive Operations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Others)
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⌋
For each test case, in the first line, two integers n,q, representing the length of a,b and the number of queries.
In the second line, n integers separated by spaces, representing permutation b.
In the following q lines, each line is either in the form 'add l r' or 'query l r', representing an operation.
1≤n,q≤100000, 1≤l≤r≤n, there're no more than 5 test cases.
1 5 2 4 3
add 1 4
query 1 4
add 2 5
query 2 5
add 3 5
query 1 5
add 2 4
query 1 4
add 2 5
query 2 5
add 2 2
query 1 5
#include<bits/stdc++.h>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pqueue priority_queue
#define NEW(a,b) memset(a,b,sizeof(a))
const double pi=4.0*atan(1.0);
const double e=exp(1.0);
const int maxn=3e6+;
typedef long long LL;
typedef unsigned long long ULL;
//typedef pair<LL,LL> P;
const LL mod=1e9+;
using namespace std;
struct node{
LL l,r,sum,g,mi;
LL lazy;
LL mid(){
return (l+r)>>;
}
}a[maxn];
int b[maxn];
void build(int l,int r,int num){
a[num].l=l;
a[num].r=r;
a[num].lazy=;
if(l==r){
a[num].sum=;
a[num].g=;
a[num].mi=b[l];
}
else{
build(l,a[num].mid(),num<<);
build(a[num].mid()+,r,(num<<)|);
a[num].g=a[num<<].g+a[(num<<)|].g;
a[num].mi=min(a[num<<].mi,a[(num<<)|].mi);
}
}
void as(int d)
{
if(a[d].lazy)
{
a[(d<<)].lazy+=a[d].lazy;
a[(d<<|)].lazy+=a[d].lazy;
a[(d<<)].mi-=a[d].lazy;
a[(d<<|)].mi-=a[d].lazy;
a[d].lazy=;
}
}
LL Find(int l,int r,int num){
if(a[num].l==l&&a[num].r==r){
return a[num].g;
}
if(l>a[num].mid()){
return Find(l,r,(num<<)|);
}
else if(r<=a[num].mid()){
return Find(l,r,num<<);
}
else{
return Find(l,a[num].mid(),num<<)+Find(a[num].mid()+,r,(num<<)|);
}
}
void add(int l,int r,int num,LL x){
if(a[num].l==l&&a[num].r==r||x==){
a[num].lazy+=x;
a[num].mi-=x;
if(a[num].mi>){
return ;
}
else if(l!=r){
as(num);
add(l,a[num].mid(),num<<,);
add(a[num].mid()+,r,(num<<)|,);
a[num].mi=min(a[num<<].mi,a[(num<<)|].mi);
a[num].g=a[num<<].g+a[(num<<)|].g;
return;
}
}
if(l==r&&a[num].l==l&&a[num].r==r)
{
if(a[num].mi<=)
{
a[num].mi=a[num].lazy=;
a[num].mi=b[l];
a[num].g++;
}
return;
}
as(num);
if(l>a[num].mid()){
add(l,r,(num<<)|,x);
}
else if(r<=a[num].mid()){
add(l,r,num<<,x);
}
else {
add(l,a[num].mid(),num<<,x);
add(a[num].mid()+,r,(num<<)|,x);
}
a[num].mi=min(a[num<<].mi,a[(num<<)|].mi);
a[num].g=a[num<<].g+a[(num<<)|].g;
//cout<<'a'<<a[num].l<<' '<<a[num].r<<' '<<a[num].g<<endl;
}
int main(){
fio;
int n,m;
string op;
int x,y;
while(cin>>n>>m){
for(int i=;i<=n;i++){
cin>>b[i];
}
build(,n,);
while(m--){
cin>>op>>x>>y;
if(op[]=='a'){
add(x,y,,);
}
else if(op[]=='q'){ add(x,y,,);
cout<<Find(x,y,)<<endl;
}
}
}
}
HDU 6351 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-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 (线段树+思维) 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 ...
- HDU6315 Naive Operations(线段树 复杂度分析)
题意 题目链接 Sol 这题关键是注意到题目中的\(b\)是个排列 那么最终的答案最多是\(nlogn\)(调和级数) 设\(d_i\)表示\(i\)号节点还需要加\(d_i\)次才能产生\(1\)的 ...
- 2018HDU多校二 -F 题 Naive Operations(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6315 In a galaxy far, far away, there are two integer ...
随机推荐
- eclipse聚合工程如何提交SVN,如何从SVN下载
提交: 比如聚合工程为taotao-manager,包含了taotao1,taotao2,taotao3等项目,在提交SVN只需 提交taotao-manager就可以了 1.右键taotao-man ...
- ASP.NET WebApi 图片上传
以下是代码的实现过程: Html页面表单布局: <form id="UpPicture" enctype="multipart/form-data" ac ...
- windows服务器自动删除日志文件
https://blog.csdn.net/u010050174/article/details/72510367 步骤: 1.新建 一个bat脚本 2.添加到window执行计划中,进行每日执行. ...
- a标签自执行点击事件
//html <a href='http://www.baidu.com' ><button id='sss'>百度</button></a> //原生 ...
- vue-i18n
安装 npm install vue-i18n 初始化 import VueI18n from 'vue-i18n' Vue.use(VueI18n) const messages = { zh: { ...
- Linux 设置IP地址,并能连接外网
1,如果是 centos6,请修改 vi /etc/sysconfig/network-scripts/ifcfg-eth0 2,如果是 centos7,请修改 => vi /etc/sysc ...
- 关于sql链接超时的问题
也许你会说,我在连接字符串中已经 设置了 Connect Timeout=80000 ,并且数据库中超时连接也是设置的值是一个很大的值.为啥到了30秒,仍然超时了呢?? 这是因为: ...
- mysql每天凌晨0点准时启动taskeng.exe如何关闭
MySQL弹出一个taskeng.exe. 内容如下:=====================Start Initialization====================mysql Instal ...
- mac下shell给文件名批量加前缀
用rename命令 如果没装的话执行下面这个命令安装rename brew install rename rename 's/^/logo_/' *.png
- nodejs 热更新插件
键入命令: npm -g install supervisor supervisor必须安装到全局 可以用supervisor 来启动服务 命令supervisor app.js