codeforce 121E - Lucky Array

  10^4以内只由4和7构成的数字只有31种,那么做法就很简单了,求出每个数字与其最接近的幸运数的差值,然后建立线段树,线段树维护区间最小值和最小值个数,如果操作过程中最小值<0,那么就去对差值进行暴力修改,直到区间差值>=0,很明显线段树每个叶子节点不会被修改超过31次,询问操作的话差值=0的数字就是幸运数了。复杂度O(31nlogn)

  代码

 #include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <bitset>
using namespace std;
const int N = ;
int n,m,a[N],b[N],tot,i,j,id[N],v[N];
int s[N],cnt[N];
char str[];
int q1,q2,q3;
void dfs(int x)
{
if (x>=) return;
if (x!=) b[++tot]=x;
dfs(x*+);
dfs(x*+);
}
void updata(int x)
{
if (s[*x]<s[*x+])
{
s[x]=s[*x];
cnt[x]=cnt[*x];
}
else
if (s[*x]>s[*x+])
{
s[x]=s[*x+];
cnt[x]=cnt[*x+];
}
else
{
s[x]=s[*x];
cnt[x]=cnt[*x]+cnt[*x+];
}
}
void build(int x,int l,int r)
{
if (r-l==)
{
s[x]=a[r];
cnt[x]=;
}
else
{
int m=(l+r)>>;
build(*x,l,m);
build(*x+,m,r);
updata(x);
}
}
void clean(int x)
{
if (v[x])
{
s[x]-=v[x];
v[*x]+=v[x];
v[*x+]+=v[x];
v[x]=;
}
}
int query(int x,int a,int b,int l,int r)
{
clean(x);
if ((a<=l)&&(r<=b))
{
if (s[x]==) return cnt[x];else return ;
}
int m=(l+r)>>,ans=;
if (a<m) ans+=query(*x,a,b,l,m);
if (m<b) ans+=query(*x+,a,b,m,r);
return ans;
}
void gao(int x,int l,int r)
{
clean(x);
if (s[x]>=) return;
if (r-l==)
{
while (s[x]<)
{
s[x]+=b[id[r]+]-b[id[r]];
id[r]++;
}
}
else
{
int m=(l+r)>>;
gao(*x,l,m);
gao(*x+,m,r);
updata(x);
}
}
void change(int x,int a,int b,int l,int r,int c)
{
clean(x);
if ((a<=l)&&(r<=b))
{
v[x]+=c;
gao(x,l,r);
return;
}
int m=(l+r)>>;
if (a<m) change(*x,a,b,l,m,c);
if (m<b) change(*x+,a,b,m,r,c);
clean(*x);clean(*x+);
updata(x);
}
int main()
{
scanf("%d%d",&n,&m);
for (i=;i<=n;i++)
scanf("%d",&a[i]);
dfs();
sort(b+,b++tot);b[tot+]=;
for (i=;i<=n;i++)
{
for (j=;j<=tot;j++)
if (b[j]>=a[i]) break;
id[i]=j;
a[i]=b[j]-a[i];
}
build(,,n);
for (i=;i<=m;i++)
{
scanf("%s",str);
if (str[]=='c')
{
scanf("%d%d",&q1,&q2);
printf("%d\n",query(,q1-,q2,,n));
}
else
{
scanf("%d%d%d",&q1,&q2,&q3);
change(,q1-,q2,,n,q3);
}
}
}
上一篇:验证码倒计时


下一篇:Js 中对 Json 数组的常用操作