Naive Operations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 1878 Accepted Submission(s): 810
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
[题意]
给定: 全排列 b数组, a数组为0;
两个操作 add(l,r) a [L,R] +1 , query(l,r) 查询 [L,R] sum of a[i]/b[i] (floor) 向下取整
[思路]
a[i]/b[i] 向下取整, 当 a[i] == b[i] 时 对整个区间才 贡献一个 1
所以 我们 用线段树区间维护(lazy标记), 维护 一个 m,和 sum , 初始 m = b[i] , 每次 a[i] add 就让 m -- , 当 m = 0 说明, 贡献了1 , 就更新和.
区间最小值,但是有多个最小值,(0) 即由多个 0 的时候, 就 需要遍历[L,R] 区间内 所有为0 的位置 进行 update.
遍历[L,R] 可以用 dfs
[代码]
#include <bits/stdc++.h>
#include <stdlib.h>
#include <utility>
#include<ext/rope>
#define findx(x,b,n) lower_bound(b+1,b+1+n,x)-b
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
#define SHUT ios_base::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(20); cout.tie(nullptr); cin.tie(nullptr);
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#pragma comment(linker, "/STACK:1024000000,1024000000") // 扩栈
//next_permutation(a+1,a+x) 全排列
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
using namespace __gnu_cxx;
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const double PI=acos(-1.0);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MOD=1e9+7;
const int mod=1e9+7;
/*********************************head************************/
const int N = 100100;
struct node{
int fg,mi,s;
// fg lasy标记, s->sum mi -> m 变化
}node1[4*N];
char str[20];
int b[N],n,m,l,r;
void upd(int rt)
{
node1[rt].mi =min(node1[rt<<1].mi,node1[rt<<1|1].mi);
node1[rt].s = node1[rt<<1].s + node1[rt<<1|1].s;
}
void build(int rt,int l,int r)
{
node1[rt].fg = 0;
if(l==r)
{
node1[rt].mi = b[l]-1;
node1[rt].s = 0;
} else {
int mid = (l+r)>>1;
build(lson);
build(rson);
upd(rt);
}
}
void push(int rt)//lazy
{
if(node1[rt].fg)
{
node1[rt<<1].fg += node1[rt].fg;
node1[rt<<1|1].fg += node1[rt].fg;
node1[rt<<1].mi += node1[rt].fg;
node1[rt<<1|1].mi += node1[rt].fg;
node1[rt].fg = 0;
}
}
int query(int rt,int l,int r,int L,int R)
{
if( l ==L && r == R)
return node1[rt].s;
else
{
push(rt);
int mid =(l+r)>>1;
if( R <= mid)
return query(rt<<1,l,mid,L,R);
else if( L > mid)
return query(rt<<1|1,mid+1,r,L,R);
else
return query(rt<<1,l,mid,L,mid)+query(rt<<1|1,mid+1,r,mid+1,R);
}
}
void add(int rt,int l,int r,int L,int R)
{
if( L > R) return;
if( l == L && r == R)
{
if( node1[rt].mi > 0) // if is not zero go on
{
node1[rt].mi --;
node1[rt].fg --;
} else{// if mi is zero update
if(L == R) // if L,R -> node : sum +1 , mi ->initialize
{
node1[rt].mi = b[l]-1;
node1[rt].s++;
} else{ // if not go on dfs
push(rt);
int mid =(l+r)>>1;
if( node1[rt<<1].mi == 0)
add(rt<<1,l,mid,L,mid);
else {
node1[rt<<1].fg -=1;
node1[rt<<1].mi -=1;
}
if(node1[rt<<1|1].mi ==0)
add(rt<<1|1,mid+1,r,mid+1,R);
else {
node1[rt<<1|1].fg -=1;
node1[rt<<1|1].mi -=1;
}
upd(rt);
}
}
}else{ // go on dfs
push(rt);
int mid =(l+r)>>1;
if( R <= mid)
add(rt<<1,l,mid,L,R);
else if( L > mid)
add(rt<<1|1,mid+1,r,L,R);
else
add(rt<<1,l,mid,L,mid),add(rt<<1|1,mid+1,r,mid+1,R);
upd(rt);
}
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
rep(i,1,n+1)
scanf("%d",&b[i]);
build(1,1,n);
rep(i,0,m)
{
scanf("%s %d %d",str,&l,&r);
if(str[0]=='a')
add(1,1,n,l,r);
else {
printf("%d\n",query(1,1,n,l,r));
}
}
}
return 0;
}