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. 【原】PHP初体验

    1. 相关内容介绍 1>互联网开发 互联网:传统互联网.移动互联网 互联网开发:前端开发(前台).后台开发(后端.服务端) 前端开发:视觉展示(用户界面).用户交互.采集输入信息 后台开发:管理 ...

  2. Connect the Cities[HDU3371]

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...

  3. Main 程序的入口要做哪些事情

    Main 程序的入口要做哪些事: 1.从主类中实例化程序(UIApplication)对象 2.如果有委托的话,从给定的类实例化委托和设置程序(UIApplication) 的代理. 3.开启主事件的 ...

  4. 爬虫, 获取登录者的外网IP

    笔者学习了一下用爬虫, 获取登录者的外网IP. 首先导入Jsoup的jar包 public class RetrivePage { private static String url="ht ...

  5. TotalCommander 之 日常使用技巧

    一. 常用操作 常用的操作如查看.复制.移动.删除退出已经在Total Commander下方列出,选择好文件后单击相应的按钮或是按下相应的快捷键(F3~F7)就可以完成操作.也可以像Windows中 ...

  6. .net core 1.0 中的asp.net identity 基本使用(一)

    1.修改密码强度:打开Startup.cs,在public class Startup{}内找public void ConfigureServices(IServiceCollection serv ...

  7. Yii源码阅读笔记(三十一)

    Widget类中开始,获取视图对象,获取widget ID,渲染视图,获取路径方法注释: private $_id; /** * Returns the ID of the widget. * 返回插 ...

  8. iOS圆饼图和圆环的绘制,并且添加引线

    在开发中经常遇到统计之类的需求,特此封装了一个简单的圆饼图和圆环图,效果图如下 代码下载地址:https://github.com/minyahui/MYHCricleView.git

  9. mysql_索引原理及优化

    思考: 我们知道mysql最好的数据存储量级是百万级别,是的往往在百万级别或者几十万级别就会出现慢查询(我对慢查询的定义是大于1秒),几年前我所在的一个做pos机支付的联机交易的核心系统组,当时就做过 ...

  10. TiD大会学习心得之沟通交流

    沟通交流是敏捷成功的关键要素,据相关调查阻碍敏捷的主要原因都与人有关:例如缺乏领导支持.团队缺乏协作精神.公司文化与敏捷相冲突等等:同时沟通交流也是团队建设.教练自身成长的重要支柱.下面结合在< ...