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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
const int maxn=1e5+10;
const int mod=1e9+7;
struct Tr{
int l,r;
int b,mi;
int lazy,tag;
int sum;
}tr[maxn<<2];
/*
当时以为这个permutation应该是有用的,然后,果断放弃了线段树这个思想,自己简直SB
难点:区间更新+(阀值)点更新
也就是说,在下面会出现1的情况下,我们才去更新下面的点;否则,更新到相应的区间即可
做法:
设置一个阀值,也就是,下面的最少还需要多少,就可以得到一个新的1了。如果更新,之后变成0了,我们就去更新对应的点。
*/
void pushup(int o){
tr[o].sum=tr[ls(o)].sum+tr[rs(o)].sum;
tr[o].mi=min(tr[ls(o)].mi,tr[rs(o)].mi);
}
void pushdown(int o){
tr[ls(o)].lazy=tr[rs(o)].lazy=1;
tr[ls(o)].tag+=tr[o].tag;
tr[rs(o)].tag+=tr[o].tag;
tr[ls(o)].mi-=tr[o].tag;
tr[rs(o)].mi-=tr[o].tag;
tr[o].lazy=0;
tr[o].tag=0;
}
void build(int l,int r,int o){
tr[o].l=l,tr[o].r=r;
tr[o].lazy=0,tr[o].tag=0;
if(l==r){
scanf("%d",&tr[o].b);
tr[o].mi=tr[o].b;
tr[o].sum=0;
return ;
}
int mid=(l+r)>>1;
build(l,mid,ls(o));
build(mid+1,r,rs(o));
pushup(o);
}
void update(int l,int r,int o){
// printf("l:%d r:%d\n",tr[o].l,tr[o].r);
int mid=(tr[o].l+tr[o].r)>>1;
if(l<=tr[o].l&&tr[o].r<=r){
if(tr[o].mi){
tr[o].lazy=1;
tr[o].tag++;
tr[o].mi--;//也就是差值
//if(tr[o].tag<tr[o].mi)return;
}
if(tr[o].l==tr[o].r){
int add=tr[o].tag/tr[o].b;
tr[o].sum+=add;
tr[o].tag-=add*tr[o].b;
tr[o].mi+=add*tr[o].b;
//printf("id:%d sum:%d tag:%d add:%d\n",tr[o].l,tr[o].sum,tr[o].tag,add);
return;
}
if(!tr[o].mi){
// printf("tr[o].l:%d tr[o].r:%d mi:%d tag:%d\n",tr[o].l,tr[o].r,tr[o].mi,tr[o].tag);
if(tr[o].lazy)pushdown(o);
if(!tr[ls(o)].mi) update(tr[o].l,mid,ls(o));
if(!tr[rs(o)].mi) update(mid+1,tr[o].r,rs(o));
pushup(o);
return;
}
return;
}
if(tr[o].lazy)pushdown(o);
if(r<=mid)
update(l,r,ls(o));
else if(l>mid)
update(l,r,rs(o));
else{
update(l,mid,ls(o));
update(mid+1,r,rs(o));
}
pushup(o);
}
int query(int l,int r,int o){
if(l<=tr[o].l&&tr[o].r<=r){
return tr[o].sum;
}
int mid=(tr[o].l+tr[o].r)>>1;
if(r<=mid)
return query(l,r,ls(o));
else if(l>mid)
return query(l,r,rs(o));
else{
int ans1=query(l,mid,ls(o));
int ans2=query(mid+1,r,rs(o));
return ans1+ans2;
}
}
/*
5 12
1 5 2 4 3
add 1 4
add 2 5
add 3 5
add 2 4
add 2 5
add 2 2
*/
void show(){
rep(i,1,9+1){
printf("tr[o].l:%d tr[o].r:%d tag:%d sum:%d mi:%d\n",tr[i].l,tr[i].r,tr[i].tag,tr[i].sum,tr[i].mi);
}
}
char str[10];
int main(){
int n,m;
while(scanf("%d %d",&n,&m)==2){
build(1,n,1);
rep(i,0,m){
int l,r;
scanf(" %s %d %d",str,&l,&r);
if(str[0]=='a'){
update(l,r,1);
// show();
}
else{
printf("%d\n",query(l,r,1));
}
}
}
return 0;
}