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⌊a/bi⌋∑i=lr⌊a/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
dls实力教我做人,我实在太菜了。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#define maxx 100005
using namespace std;
struct node
{
int add;
int sum;
int m;
}c[maxx<<2];
int b[maxx],n,m;
char s[10];
void pushU(int st)
{
c[st].m=min(c[st<<1].m,c[st<<1|1].m);
c[st].sum=c[st<<1].sum+c[st<<1|1].sum;
}
void setf(int st,int f)
{
c[st].add+=f;
c[st].m+=f;
}
void pushD(int st)
{
if(c[st].add)
{
setf(st<<1,c[st].add);
setf(st<<1|1,c[st].add);
c[st].add=0;
}
}
void build(int st,int l,int r)
{
c[st].add=0;
if(l==r)
{
c[st].m=b[l]-1;
c[st].sum=0;
return;
}
int mid=(l+r)>>1;
build(st<<1,l,mid);
build(st<<1|1,mid+1,r);
pushU(st);
}
int query(int st,int l,int r,int L,int R)
{
if(L<=l&&r<=R)return c[st].sum;
pushD(st);
int mid=(l+r)>>1;
int ans=0;
if(L<=mid) ans+= query(st<<1,l,mid,L,R);
if(R>mid) ans+=query(st<<1|1,mid+1,r,L,R);
return ans;
}
void update(int st,int l,int r,int L,int R)
{
if(L<=l&&r<=R)
{
if(c[st].m>0)
{
c[st].m--;
c[st].add--;
}
else
{
if(l==r)
{
c[st].m=b[l]-1;
c[st].sum++;
}
else
{
pushD(st);
int mid=(l+r)>>1;
if(c[st<<1].m==0)update(st<<1,l,mid,L,R);
else setf(st<<1,-1);
if(c[st<<1|1].m==0) update(st<<1|1,mid+1,r,L,R);
else setf(st<<1|1,-1);
pushU(st);
}
}
return;
}
pushD(st);
int mid=(l+r)>>1;
if(L<=mid)update(st<<1,l,mid,L,R);
if(R>mid)update(st<<1|1,mid+1,r,L,R);
pushU(st);
}
int main()
{
while(scanf("%d%d",&n,&m)==2)
{
for(int i=1;i<=n;i++)
scanf("%d",b+i);
build(1,1,n);
int l,r;
while(m--)
{
scanf("%s%d%d",s,&l,&r);
if(s[0]=='a')
update(1,1,n,l,r);
else
printf("%d\n",query(1,1,n,l,r));
}
}
return 0;
}
本文介绍了一道关于段式树状数组的数据结构题目,通过动态更新区间内的元素值并查询特定区间的累加和来解决问题。文章提供了一个完整的C++实现案例,包括输入输出流程、段式树的构建、更新及查询操作。
2744

被折叠的 条评论
为什么被折叠?



