Problem Description
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⌋
 
Input
There are multiple test cases, please read till the end of input file.
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.
 
Output
Output the answer for each 'query', each one line.
 
Sample Input
5 12
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

Sample Output
1
1
2
4
4
6

题意

初始a数组为0,给你一个全排列的b数组,q次询问add x y为a数组区间x y增加1,query x y查询a数组整除b数组对应下标的和

题解

区间操作很容易想到线段树

初始每个叶子节点赋值为b[i],维护一个区间最小值min,和区间和sum

对于每个add,区间[X,Y]最小值减1,如果当前区间最小值=1,就继续往下更新,如果更新到叶子节点并且min=1,sum+1

对于每个query,查询区间[X,Y]sum,如果区间min=0,再去暴力更新区间(可以知道一共q次询问,q/1+q/2+q/3+....q/n为调和级数,复杂度O(logn))

总复杂度O(nlog^2 n)

代码

 #include<bits/stdc++.h>
using namespace std; const int N=1e5+; int a[N<<],lazy[N<<],b[N],sum[N<<];
int n;
void PushUp(int rt)
{
a[rt]=min(a[rt<<],a[rt<<|]);
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void PushDown(int rt)
{
if(lazy[rt]==)return;
lazy[rt<<]+=lazy[rt];
lazy[rt<<|]+=lazy[rt];
a[rt<<]-=lazy[rt];
a[rt<<|]-=lazy[rt];
lazy[rt]=;
}
void Build(int l,int r,int rt)
{
lazy[rt]=;sum[rt]=;
if(l==r)
{
a[rt]=b[l];
return;
}
int mid=(l+r)>>;
Build(l,mid,rt<<);
Build(mid+,r,rt<<|);
PushUp(rt);
}
void Update(int L,int R,int l,int r,int rt)
{
if(a[rt]>&&L<=l&&r<=R)
{
lazy[rt]++;
a[rt]--;
return;
}
if(a[rt]==&&l==r)
{
sum[rt]++;
lazy[rt]=;
a[rt]=b[l];
return;
}
int mid=(l+r)>>;
PushDown(rt);
if(L<=mid)Update(L,R,l,mid,rt<<);
if(R>mid)Update(L,R,mid+,r,rt<<|);
PushUp(rt);
}
int Query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)return sum[rt];
if(a[rt]==)Update(L,R,,n,);
int mid=(l+r)>>,ans=;
PushDown(rt);
if(L<=mid)ans+=Query(L,R,l,mid,rt<<);
if(R>mid)ans+=Query(L,R,mid+,r,rt<<|);
PushUp(rt);
return ans;
}
int main()
{
int q,x,y;
char op[];
while(scanf("%d%d",&n,&q)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%d",&b[i]);
Build(,n,);
for(int i=;i<q;i++)
{
scanf("%s%d%d",op,&x,&y);
if(op[]=='a')
Update(x,y,,n,);
else
printf("%d\n",Query(x,y,,n,));
}
}
return ;
}

最新文章

  1. 《30天自制操作系统》12_day_学习笔记
  2. 简单几何(半平面交+二分) LA 3890 Most Distant Point from the Sea
  3. python练习程序(c100经典例16)
  4. Zend Framework 入门(2)—多国语言支持
  5. mysql 支持emoji
  6. Python连接Redis连接配置
  7. R语言 一元线性回归
  8. hdu 4541 Ten Googol
  9. HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP
  10. spring mvc上传下载文件
  11. Linux 用户组及用户管理
  12. 什么是HTML?
  13. JMeter+Ant-自动发送测试结果报告邮件
  14. SSL里的certificate格式资料小结
  15. Codeforces Round #523 (Div. 2) D. TV Shows
  16. EFCodeFirst示例
  17. Spring Boot入门一:在Eclipse中使用Spring boot
  18. C++ STL Maps
  19. NBUT 1221 Intermediary 2010辽宁省赛
  20. centos7.3安装zend guard loader3.3 for php5.6

热门文章

  1. python 网页爬虫,带登陆信息
  2. linux 源码编译php的参数
  3. electron安装到第一个实例
  4. 【技术文档】jeecg3.7.3-maven搭建环境入门
  5. Hive安装 和管理
  6. tp5中ajax方式提交表单
  7. 【383】defaultdict 相关用法
  8. ASP.NET Form身份验证方式详解
  9. IPv4和IPv6的差异;如何实现IPv4和IPv6双协议栈的通信
  10. C#调用非托管dll--路径问题