Naive Operations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 1620 Accepted Submission(s): 689
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;
#define ll long long
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
const int MAX = 1e5 + 7;
int n, m;
int c[MAX], a[MAX << 2], lz[MAX << 2];
int J[MAX];
int lowbit(int x){
return x & -x;
}
void add(int pos, int v){
while(pos <= MAX){
c[pos] += v;
pos += lowbit(pos);
}
}
ll query(int pos){
ll ans = 0;
while(pos){
ans += c[pos];
pos -= lowbit(pos);
}
return ans;
}
void pushup(int rt){
a[rt] = min(a[rt << 1], a[rt << 1 | 1]);
}
void pushdown(int rt, int l, int r){
if(lz[rt] != 0){
a[rt << 1] += lz[rt];
a[rt << 1 | 1] += lz[rt];
lz[rt << 1] += lz[rt];
lz[rt << 1 | 1] += lz[rt];
lz[rt] = 0;
}
}
void build(int rt, int l, int r){
if(l == r){
a[rt] = J[l];
return;
}
int mid = (l + r) >> 1;
build(lson);
build(rson);
pushup(rt);
}
void update(int rt, int l, int r, int x, int y){
if(x <= l && r <= y){
if(a[rt] > 1){
a[rt] -= 1;
lz[rt] -= 1;
}
else{
pushdown(rt, l, r);
if(l == r){
add(l, 1);
a[rt] = J[l];
return;
}
int mid = (l + r) >> 1;
if(a[rt << 1] > 1){
a[rt << 1] -= 1;
lz[rt << 1] -= 1;
}
else
update(lson, x, y);
if(a[rt << 1 | 1] > 1){
a[rt << 1 | 1] -= 1;
lz[rt << 1 | 1] -= 1;
}
else
update(rson, x, y);
pushup(rt);
}
return;
}
pushdown(rt, l, r);
int mid = (l + r) >> 1;
if(x <= mid)
update(lson, x, y);
if(mid < y)
update(rson, x, y);
pushup(rt);
}
int main(){
while(scanf("%d%d", &n, &m) != EOF){
memset(a, 0, sizeof a);
memset(lz, 0, sizeof lz);
memset(c, 0, sizeof c);
for(int i = 1; i <= n; i++)
scanf("%d", &J[i]);
build(1, 1, n);
while(m--){
char op[15];
int l, r;
scanf("%s%d%d", op, &l, &r);
if(op[0] == 'a')
update(1, 1, n, l, r);
else
printf("%lld\n", query(r) - query(l - 1));
}
}
return 0;
}
|