hdu 6351

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

include

include

using namespace std;

const int maxn=(100000+10)*4;

define Min(a,b) ((a)<(b)?(a):(b))

int b[maxn],add[maxn],minn[maxn],sum[maxn],n;

void pushup(int rt){
minn[rt]=Min(minn[rt<<1],minn[rt<<1|1]);
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}

void build(int l,int r,int rt){
sum[rt]=0,add[rt]=0;
if(l==r){
minn[rt]=b[l];
return ;
}
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
pushup(rt);
}

void pushdown(int rt){
if(add[rt]){
add[rt<<1|1]+=add[rt];
add[rt<<1]+=add[rt];
minn[rt<<1|1]-=add[rt];
minn[rt<<1]-=add[rt];
add[rt]=0;
}
}

void change(int l,int r,int rt,int L,int R){
if(l>=L && r<=R && minn[rt]>1){
add[rt]++;
minn[rt]--;
return ;
}
int mid=(l+r)>>1;
if(lr && minn[rt]1){
minn[rt]=b[l];
add[rt]=0;
sum[rt]++;
return ;
}
pushdown(rt);
if(L<=mid) change(l,mid,rt<<1,L,R);
if(R>mid) change(mid+1,r,rt<<1|1,L,R);
pushup(rt);
}

int ask(int l,int r,int rt,int L,int R){
if(l>=L && r<=R){
return sum[rt];
}
int mid=(l+r)>>1;
pushdown(rt);
int ans=0;
if(L<=mid) ans+=ask(l,mid,rt<<1,L,R);
if(R>mid) ans+=ask(mid+1,r,rt<<1|1,L,R);
pushup(rt);
return ans;
}

int main(){
int q,l,r;
while(scanf("%d%d",&n,&q)!=EOF){
for (int i=1;i<=n;i++) scanf("%d",&b[i]);
build(1,n,1);
while(q--){
char c[10];
scanf("%s",c);
if(c[0]=='a') {scanf("%d%d",&l,&r);change(1,n,1,l,r);}
else {scanf("%d%d",&l,&r);printf("%d\n",ask(1,n,1,l,r));}
}
}
return 0;
}

posted @ 2019-02-14 16:03  lmjer  阅读(196)  评论(0编辑  收藏  举报