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);
}
}
}

codeforce 121E - Lucky Array的更多相关文章

  1. CF 121E Lucky Array 【树状数组】

    这个题目的数据感觉不能更水了.从复杂度上计算,肯定有极限数据可以卡掉暴力方法的么. 总之,暴力的做法就是树状数组了,对于区间更新,就挨个更新就是了.当然,判断是否是Lucky Number的话,可以用 ...

  2. Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array 分块

    E. Lucky Array time limit per test 4 seconds memory limit per test 256 megabytes input standard inpu ...

  3. Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array

    E. Lucky Array Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers w ...

  4. Lucky Array CodeForces - 121E (线段树,好题)

    题目链接 题目大意: 定义只含数字$4,7$的数字为幸运数, 给定序列, 区间加正数, 区间询问多少个幸运数 题解: 对于每一个数, 求出它和第一个比它大的幸运数之差, 则问题转化为区间加,查询$0$ ...

  5. Lucky Array Codeforces - 121E && Bear and Bad Powers of 42 Codeforces - 679E

    http://codeforces.com/contest/121/problem/E 话说这题貌似暴力可A啊... 正解是想出来了,结果重构代码,调了不知道多久才A 错误记录: 1.线段树搞混num ...

  6. CodeForces 122G Lucky Array(一脸懵逼的树状数组)

    Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal re ...

  7. codeforces 121 E. Lucky Array

    time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...

  8. Codeforce 1175 D. Array Splitting

    新鲜热乎的题 Codeforce 1175 D. 题意:给出一个长度为$n$的序列$a$,你需要把它划分为$k$段,每一个元素都需要刚好在其中一段中.分好之后,要计算$\sum_{i=1}^{n} ( ...

  9. Codeforce 1155D Beautiful Array(DP)

    D. Beautiful Array You are given an array aa consisting of nn integers. Beauty of array is the maxim ...

随机推荐

  1. java读取xml文件

    public ArrayList getMessage(){ String xmlFileName = null; List list = new ArrayList(); MessageBean m ...

  2. .NET 二维码生成(ThoughtWorks.QRCode)

    引用ThoughtWorks.QRCode.dll (源代码里有) 1.简单二维码生成及解码代码: //生成二维码方法一 private void CreateCode_Simple(string n ...

  3. dedecms中调用制定栏目

    {dede:type typeid='5'} <li> <a href="[field:typelink/]" target="_blank" ...

  4. Servlet生命周期

    初始化:正常情况下,一个Servlet程序在第一次运行时才进行初始化. 刷新只会刷新服务,并没有初始化 销毁:1,容器关闭    2,一个servlet长期不适用 3,开发过程中的reload操作 对 ...

  5. 【emWin】例程四:显示文本

    实验指导书及代码包下载: http://pan.baidu.com/s/1jHOYdqm

  6. true_kb

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 总结-eclipse

    1.eclipse的workspace历史记录 打开eclipse/configuration/.settings/org.eclipse.ui.ide.prefs,把RECENT_WORKSPACE ...

  8. JetBrains注册码计算(IntelliJ IDEA 15.0注册码激活)

    安装前修改时间为2099年等很多年后的时间点,安装后选择免费试用30天,然后退出idea,把时间修改回来,这样就可以成功注册了 如果之前使用过授权服务器等激活方式,不弹出试用界面,请到C:\Users ...

  9. Source Insight 3.X 插件新loader发布

    [前言] 大约一年多没更新SI插件了,主要原因是我从SI换到了sublime,使用SI少了,插件也就停止更新了.不过看到园子里这么多网友的留言,觉得还是有必要更新一下,算是给还在用SI的朋友们一个交代 ...

  10. CentOS 6安装PostgreSQL

    https://zh.wikipedia.org/wiki/PostgreSQL PostgreSQL是自由的对象-关系型数据库服务器(数据库管理系统),在灵活的BSD-风格许可证下发行.它在其他开放 ...