Naive Operations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 2801 Accepted Submission(s): 1224
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.
Naive Operations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 2801 Accepted Submission(s): 1224
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
思路:看了好多题解才完全理解了。用线段树来维护一个区间最小值,维护一个sum。首先将序列b插入到线段树中,每次add时对区间进行减1操作,当区间最小值减到0时,跟新sum,0重新变为b[i]。
#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
int n,q;
int sum[maxn<<2];
int mi[maxn<<2];
int b[maxn];//题目中的b数组
int lazy[maxn<<2];
char s[10];
int L,R;
void update(int x){
sum[x] = sum[x<<1] + sum[x<<1|1];
mi[x] = min(mi[x<<1] , mi[x<<1|1]);
}
void push(int x){
if(lazy[x]){//如果节点已经被标记,则下放标记
lazy[x<<1] += lazy[x];//下放到左儿子
lazy[x<<1|1] += lazy[x];//下放到右儿子
mi[x<<1] -= lazy[x];
mi[x<<1|1] -= lazy[x];
lazy[x] = 0;//取消上方标记
}
}
void build(int x,int l,int r){
lazy[x] = sum[x] = 0;
if(l == r){
mi[x] = b[l];
return;
}
int mid = (l + r)>>1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
mi[x] = min(mi[x<<1] , mi[x<<1|1]);
}
int query(int x,int l,int r){
if(L <= l&&R >= r)
return sum[x];
int mid = (l+r)>>1,ans=0;
if(L <= mid)
ans += query(x<<1,l,mid);
if(R>mid)
ans += query(vjudgex<<1|1,mid+1,r);
return ans;
}
void change(int x,int l,int r){
if(l == r){
mi[x] = b[l];
sum[x]++;
return;
}
push(x);
int mid = (l+r)>>1;
if(!mi[x<<1])
change(x<<1,l,mid);
if(!mi[x<<1|1])
change(x<<1|1,mid+1,r);
update(x);
}
void modify(int x,int l,int r){
if(L <= l&&R >= r){//如果区间能够完全覆盖
lazy[x]++;//给这个区间打上标记
mi[x]--;
if(!mi[x])//当区间最小值减到0时,我们更新sum并且将0变为b[i];
change(x,l,r);
return;
}
push(x);
int mid = (l+r)>>1;
if(L<=mid)
modify(x<<1,l,mid);
if(R>mid)
modify(x<<1|1,mid+1,r);
update(x);
}
void work(){
for(int i=1;i<=n;i++)
scanf("%d",b+i);
build(1,1,n);
for(int i=0;i<q;i++){
scanf("%s%d%d",s,&L,&R);
if(s[0] == 'q')
printf("%d\n",query(1,1,n));
else
modify(1,1,n);
}
}
int main(){
while(~scanf("%d%d",&n,&q))
work();
return 0;
}