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...aral,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≤1000001≤n,q≤100000, 1≤l≤r≤n1≤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
题意:给定b数组,b数组是1~n的一个排列,然后a数组,长度也是n,而且所有元素起始都为0,接下来有两种针对a数组的操作:
1、 add l r: a[l] 到 a[r] 之间每个元素的值 + 1
2、query l r: 求 (i = 从l到r)∑ (int)(a[i]/b[i])
线段树维护a % b - b的最大值m,和2操作的答案sum,m起始值为-b,当线段树更新到某个位置的m为0的时候,立即向下传递lazy标记,然后再向上更新sum,完了就恢复m为起始值-b。
AC代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
const int maxn = 1e5 + 5;
int depot[maxn],add[maxn<<2],n,q;
char op[10];
struct node
{
int l,r,minn,contri;
}nde[maxn<<2];
void pushup(int id)
{
nde[id].contri=nde[id<<1].contri+nde[id<<1|1].contri;
nde[id].minn=min(nde[id<<1].minn,nde[id<<1|1].minn);
}
void pushdown(int id)
{
if(add[id])
{
nde[id<<1].minn-=add[id];
nde[id<<1|1].minn-=add[id];
add[id<<1]+=add[id];
add[id<<1|1]+=add[id];
add[id]=0;
}
}
void build(int l,int r,int id)
{
nde[id].l=l,nde[id].r=r;
if(l==r)
{
nde[id].minn=depot[l];
nde[id].contri=0;
return;
}
int mid=(l+r)>>1;
build(l,mid,id<<1);
build(mid+1,r,id<<1|1);
pushup(id);
}
void modify(int l,int r,int id)
{
if(nde[id].l==l&&nde[id].r==r&&nde[id].minn>1)
{
nde[id].minn-=1;
add[id]+=1;
return;
}
if(nde[id].l==nde[id].r)
{
nde[id].minn-=1;
add[id]+=1;
if(nde[id].minn<=0)
{
nde[id].minn=depot[l];
nde[id].contri+=1;
}
return;
}
pushdown(id);
int mid=(nde[id].l+nde[id].r)>>1;
if(r<=mid)
modify(l,r,id<<1);
else if(mid<l)
modify(l,r,id<<1|1);
else
{
modify(l,mid,id<<1);
modify(mid+1,r,id<<1|1);
}
pushup(id);
}
int query(int l,int r,int id)
{
if(nde[id].l==l&&nde[id].r==r)
{
return nde[id].contri;
}
pushdown(id);
int mid=(nde[id].l+nde[id].r)>>1;
int ret=0;
if(r<=mid)
ret=query(l,r,id<<1);
else if(mid<l)
ret=query(l,r,id<<1|1);
else
ret=query(l,mid,id<<1)+query(mid+1,r,id<<1|1);
pushup(id);
return ret;
}
int main()
{
while(scanf("%d%d",&n,&q)==2)
{
memset(add,0,sizeof add );
for(int i=1; i<=n; ++i)
{
scanf("%d",&depot[i]);
}
build(1,n,1);
for(int i=0; i<q; ++i)
{
int l,r;
scanf("%s %d %d",op,&l,&r);
if(op[0]=='a')
{
modify(l,r,1);
}
else
{
int ans=query(l,r,1);
printf("%d\n",ans);
}
}
}
return 0;
}