Naive Operations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 2271 Accepted Submission(s): 987
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
Source
2018 Multi-University Training Contest 2
Recommend
chendu | We have carefully selected several similar problems for you: 6318 6317 6316 6315 6314
Statistic | Submit | Discuss | Note
#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define ll long long
#define maxn 100005
#define ms memset
using namespace std;
/*
题目大意:明显的线段树风格题目,
给定两种操作,在a数组上区间相加,
查询区间和:sigma(ai/bi)(下界)。
初始a数组为零
这种题目比赛时用普通的想法肯定超时,
因为在更新区间答案时采用了遍历。
其实细细分析下,总共的答案修改次数非常之少,
且b数组是1~n的数字的填充。
那么可想而知其复杂度应该可以搞成nlogn(调和级数)。
线段树的节点主要是维护区间性质来代替区间更新对最终答案产生的影响。
那么我们维护b数组的最小值,和a数组的最大值,
可想而知在一个区间中,如果a数组的最大值小于b数组最小值,
则该区间操作对答案更笨没有影响,懒惰标记下推即可,因为相加要继续下放到下面层的区间。
在单点中,如果ai>=bi,则产生影响,更新与b数组关联的最小值(倍增),更新单点的s答案。
通过上推标记和懒惰标记配合,其他的都是线段树的模板
*/
struct node
{
int l,r,fg;
int s,minv,maxv;
node(){}
}tr[maxn<<2];
int a[maxn],b[maxn];
int n,q,l,r;
char c[10];
void pushup(int rt)
{
tr[rt].s=tr[rt<<1].s+tr[rt<<1|1].s;
tr[rt].minv=min(tr[rt<<1].minv,tr[rt<<1|1].minv);
tr[rt].maxv=max(tr[rt<<1].maxv,tr[rt<<1|1].maxv);
}
void pushdown(int rt)
{
int tfg=tr[rt].fg;
if(tfg)
{
tr[rt<<1].fg+=tfg;
tr[rt<<1|1].fg+=tfg;
tr[rt<<1].maxv+=tfg;
tr[rt<<1|1].maxv+=tfg;
tr[rt].fg=0;
}
}
void Build(int l,int r,int rt)
{
tr[rt].l=l,tr[rt].r=r;
tr[rt].fg=0;
if(l==r)
{
tr[rt].maxv=tr[rt].s=0;
tr[rt].minv=b[l];
return ;
}
pushdown(rt);
int mid=(l+r)>>1;
Build(l,mid,rt<<1);
Build(mid+1,r,rt<<1|1);
pushup(rt);
}
void update(int l,int r,int rt,int L,int R)
{
if(L<=l&&r<=R)
{
tr[rt].maxv++;
if(tr[rt].maxv<tr[rt].minv)
{
tr[rt].fg++;///懒惰标记更新,下次遇到再下推,该区间的波动不会引起答案的更新
return ;
}
if(l==r&&tr[rt].maxv>=tr[rt].minv) ///单点更新,如果最大值大于b数组,则引起和的变化,并且迭代b数组
{
tr[rt].s++;
tr[rt].minv+=b[l];
return ;
}
}
pushdown(rt);
int mid=l+r>>1;
if(L<=mid ) update(l,mid,rt<<1,L,R);
if(mid<R) update(mid+1,r,rt<<1|1,L,R);
pushup(rt);
}
int Query(int l,int r,int rt,int L,int R)
{
if(L<=l&&r<=R) return tr[rt].s;
pushdown(rt);
int ans=0,mid=l+r>>1;
if(L<=mid) ans+=Query(l,mid,rt<<1,L,R);
if(mid<R) ans+=Query(mid+1,r,rt<<1|1,L,R);
return ans;
}
int main()
{
while(scanf("%d%d",&n,&q)!=EOF)
{
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++) scanf("%d",&b[i]);
Build(1,n,1);
for(int i=0;i<q;i++)
{
scanf("%s%d%d",c,&l,&r);
if( c[0]=='a' ) update(1,n,1,l,r);
else printf("%d\n",Query(1,n,1,l,r));
}
}
return 0;
}