Naive Operations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 2506 Accepted Submission(s): 1100
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
题意:给两个数组,一个数组初始值为0,另一个有题目输入,数组大小一样。然后有一系列操作,分别是add和query,add是在定区间里每个数组+1,query是求得区间里每个ai/bi的和。开一个线段树,用mi存取b[]数组的值,然后每有一个add操作覆盖到区间,就-1,判断如果mi<=0则g+1(表示ai/bi的值),最后在重新初始化mi的值。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
struct node{
int l,r,g,mi;
int lazy;
int mid(){
return (l+r)>>1;
}
}a[400000];
int b[400000];
void build(int l,int r,int num){
a[num].l=l;
a[num].r=r;
a[num].lazy=0;
if(l==r){
a[num].g=0;
a[num].mi=b[l];
}
else{
build(l,a[num].mid(),num<<1);
build(a[num].mid()+1,r,(num<<1)|1);
a[num].g=a[num<<1].g+a[(num<<1)|1].g;
a[num].mi=min(a[num<<1].mi,a[(num<<1)|1].mi);
}
}
void pudo(int d){
if(a[d].lazy){
a[d<<1].lazy+=a[d].lazy;
a[(d<<1)|1].lazy+=a[d].lazy;
a[(d<<1)].mi-=a[d].lazy;
a[(d<<1)|1].mi-=a[d].lazy;
a[d].lazy=0;
}
}
int find(int l,int r,int num){
if(a[num].l==l&&a[num].r==r){
return a[num].g;
}
if(l>a[num].mid()){
return find(l,r,(num<<1)|1);
}
else if(r<=a[num].mid()){
return find(l,r,num<<1);
}
else{
return find(l,a[num].mid(),num<<1)+find(a[num].mid()+1,r,(num<<1)|1);
}
}
void add(int l,int r,int num,int x){
if(a[num].l==l&&a[num].r==r||x==0){
a[num].lazy+=x;
a[num].mi-=x;
if(a[num].mi>0){
return ;
}
else if(l!=r){
pudo(num);
add(l,a[num].mid(),num<<1,0);
add(a[num].mid()+1,r,(num<<1)|1,0);
a[num].mi=min(a[num<<1].mi,a[(num<<1)|1].mi);
a[num].g=a[num<<1].g+a[(num<<1)|1].g;
return ;
}
}
if(l==r&&a[num].l==l&&a[num].r==r){
if(a[num].mi<=0){
a[num].g++;
a[num].lazy=0;
a[num].mi=b[l];
}
return ;
}
pudo(num);
if(l>a[num].mid()){
add(l,r,(num<<1)|1,x);
}
else if(r<=a[num].mid()){
add(l,r,num<<1,x);
}
else{
add(l,a[num].mid(),(num<<1),x);
add(a[num].mid()+1,r,(num<<1)|1,x);
}
a[num].mi=min(a[num<<1].mi,a[(num<<1)|1].mi);
a[num].g=a[num<<1].g+a[(num<<1)|1].g;
}
int main(){
int n,m;
while(scanf("%d%d",&m,&n)!=EOF){
for(int i=1;i<=m;i++)
scanf("%d",&b[i]);
build(1,m,1);
int a,b;
char str[6];
for(int i=0;i<n;i++){
scanf("%s %d %d",str,&a,&b);
if(str[0]=='a'){
add(a,b,1,1);
}
else{
add(a,b,1,0);
printf("%d\n",find(a,b,1));
}
}
}
}