Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 1899 Accepted Submission(s): 258
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
Statistic | Submit | Clarifications | Back
题意:给出一个长度为n初值为0的数组,以及长度为n的b数组,然后q次操作,add(l,r) 使得区间l~r所有元素+1,或者查询l~r区间a[i]/b[i]的和
思路:维护区间的a的最大值和b的最小值,使用lazy标记就不需要更新到每个点了。每次更新当当前最大值a小于最小的b,那么下面子节点就都不需要查询了(对于查询操作,如果val>0,说明这个区间产生不了贡献,直接返回ans值。如果val<=0,说明这个区间存在ai>=bi,对答案有贡献。继续往下查询,直到叶子节点,ans 表示之前区间ai/bi的和。)
大于时我们暴力找到l==r然后cnt+1,同时分母+上b[l],比如当前是2/2 我们就把它变成 2/4 这样我们还需要增加2次才能到达4/4 再变成4/6.
代码
线段树模板
#include
#define Lson l,m,rt<<1
#define Rson m+1,r,rt<<1|1
#define pll pair
#define mp make_pair
#define ll long long
#define INF 0x3f3f3f3f
const int maxn=1e5+5;
const int INFINITE = INT_MAX;
using namespace std;
int b[maxn];
int q;
struct SegTreeNode
{
int cnt;
int minb;
int maxa;
int addMark;//延迟标记
} segTree[maxn<<2]; //定义线段树
void build(int root,int istart, int iend)
{
segTree[root].addMark = 0;//----设置标延迟记域
if(istart == iend)//叶子节点
{
segTree[root].cnt=segTree[root].maxa=0;
segTree[root].minb = b[istart];
}
else
{
int mid = (istart + iend) / 2;
build(root*2 , istart, mid);//递归构造左子树
build(root*2+1 , mid+1, iend);//递归构造右子树
//根据左右子树根节点的值,更新当前根节点的值
segTree[root].minb = min(segTree[root*2].minb, segTree[root*2+1].minb);
segTree[root].cnt = segTree[root*2].cnt + segTree[root*2+1].cnt;
segTree[root].maxa = max(segTree[root*2].maxa, segTree[root*2+1].maxa);
}
}
void pushDown(int root)
{
if(segTree[root].addMark != 0)
{
//设置左右孩子节点的标志域,因为孩子节点可能被多次延迟标记又没有向下传递
//所以是 “+=”
segTree[root*2].addMark += segTree[root].addMark;
segTree[root*2+1].addMark += segTree[root].addMark;
//根据标志域设置孩子节点的值。因为我们是求区间最小值,因此当区间内每个元
//素加上一个值时,区间的最小值也加上这个值
segTree[root*2].maxa += segTree[root].addMark;
segTree[root*2+1].maxa += segTree[root].addMark;
//传递后,当前节点标记域清空
segTree[root].addMark = 0;
}
}
void update(int root, int nstart, int nend, int ustart, int uend)
{
//更新区间和当前节点区间没有交集
if(ustart > nend || uend < nstart)
return ;
//当前节点区间包含在更新区间内
if(ustart <= nstart && uend >= nend)
{
segTree[root].maxa++;
if(segTree[root].maxa=segTree[root].minb)
{
segTree[root].cnt++;
segTree[root].minb+=b[nstart];
return;
}
}
pushDown(root); //延迟标记向下传递
//更新左右孩子节点
int mid = (nstart + nend) / 2;
if(ustart<=mid)
update(root*2, nstart, mid, ustart, uend);
if(uend>mid)
update(root*2+1, mid+1, nend, ustart, uend);
//根据左右子树的值回溯更新当前节点的值
segTree[root].minb = min(segTree[root*2].minb, segTree[root*2+1].minb);
segTree[root].cnt = segTree[root*2].cnt + segTree[root*2+1].cnt;
segTree[root].maxa = max(segTree[root*2].maxa, segTree[root*2+1].maxa);
}
int query(int root, int nstart, int nend, int qstart, int qend)
{
//查询区间和当前节点区间没有交集
if(qstart > nend || qend < nstart)
return INFINITE;
//当前节点区间包含在查询区间内
if(qstart <= nstart && qend >= nend)
return segTree[root].cnt;
//分别从左右子树查询,返回两者查询结果的较小值
pushDown(root); //----延迟标志域向下传递
int mid = (nstart + nend) / 2;
int ans=0;
return min(query(root*2+1, nstart, mid, qstart, qend),query(root*2+2, mid + 1, nend, qstart, qend));
if(qstart<=mid)
ans+=query(root*2, nstart, mid, qstart, qend);
if(qend>mid)
ans+=query(root*2+1, mid+1, nend, qstart, qend);
return ans;
}
int main()
{
int n,x,y;
while(~scanf("%d %d",&n,&q))
{
for(int i=1; i<=n; i++)
{
scanf("%d",&b[i]);
}
build(1,1,n);
char pp[6];
int l,r;
while(q--)
{
scanf("%s%d%d",pp,&l,&r);
if(pp[0]=='a')
{
update(1,1,n,l,r);
}
else
{
printf("%d\n",query(1,1,n,l,r));
}
}
}
return 0;
}