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