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);
}
}
}
codeforce 121E - Lucky Array的更多相关文章
- CF 121E Lucky Array 【树状数组】
这个题目的数据感觉不能更水了.从复杂度上计算,肯定有极限数据可以卡掉暴力方法的么. 总之,暴力的做法就是树状数组了,对于区间更新,就挨个更新就是了.当然,判断是否是Lucky Number的话,可以用 ...
- 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 ...
- 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 ...
- Lucky Array CodeForces - 121E (线段树,好题)
题目链接 题目大意: 定义只含数字$4,7$的数字为幸运数, 给定序列, 区间加正数, 区间询问多少个幸运数 题解: 对于每一个数, 求出它和第一个比它大的幸运数之差, 则问题转化为区间加,查询$0$ ...
- Lucky Array Codeforces - 121E && Bear and Bad Powers of 42 Codeforces - 679E
http://codeforces.com/contest/121/problem/E 话说这题貌似暴力可A啊... 正解是想出来了,结果重构代码,调了不知道多久才A 错误记录: 1.线段树搞混num ...
- CodeForces 122G Lucky Array(一脸懵逼的树状数组)
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal re ...
- codeforces 121 E. Lucky Array
time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...
- Codeforce 1175 D. Array Splitting
新鲜热乎的题 Codeforce 1175 D. 题意:给出一个长度为$n$的序列$a$,你需要把它划分为$k$段,每一个元素都需要刚好在其中一段中.分好之后,要计算$\sum_{i=1}^{n} ( ...
- Codeforce 1155D Beautiful Array(DP)
D. Beautiful Array You are given an array aa consisting of nn integers. Beauty of array is the maxim ...
随机推荐
- [转]netty对http协议解析原理
本文主要介绍netty对http协议解析原理,着重讲解keep-alive,gzip,truncked等机制,详细描述了netty如何实现对http解析的高性能. 1 http协议 1.1 描述 标示 ...
- 字典树 - A Poet Computer
The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...
- maven 项目打包 及window下部署到tomcat
1.maven项目打包 2.将war文件拷贝到tomcat目录webapps下(不要再建目录)3.将必要的jar文件拷贝到tomcat目录libx下 war包 或jar 包 会生成到项目所在路径 的t ...
- C#自定义控件属性显示在属性面板中操作
private Color controleColor; [Browsable(true)] [Description("控件颜色"), Category("自定义&qu ...
- Mongoose 的实例方法中访问静态方法
方法比较简单,也比较粗糙和丑陋,就是通过构造函数来访问静态方法,大致如下: 123456789 WorkSpaceSchema.methods.getPrice = function(startTim ...
- initWithCoder: 与initWithFrame:的区别
从nib中加载对象实例时,init:或initWithFrame:都不会调用.而是调用initWithCoder:怎么理解: 有时候,知道initWithFrame方法如何用,但是么有弄明白ini ...
- O(n) 筛法求素数
var tot,i,j,k,m,n:longint; prime:array[0..100000] of boolean; p:array[0..100000] of longint;begin re ...
- MVC5 视图 不显示 Styles.Render Scripts.Render 问题解决
第一步:安装 WebGrease 使用 nuget 安装 WebGrease 安装依赖 第二步:修改配置文件 <configSections> <!-- For more infor ...
- <把时间当做朋友>读书笔记
这本书我早就看过,还想再来一遍 开始这一行动是看李萌在朋友圈晒101计划,每天健身,读书半小时之类的,我也想做点啥,那就每天睡前读书半小时吧,怎么坚持下去呢? 我不想晒到朋友圈里,那就晒给玉玉看吧, ...
- Nodejs:fs模块 & rimraf模块
模块fs:可以通过他管理文件系统,文件的写入,删除等操作 模块rimraf: 递归删除文件的node插件,在项目的文件编译之前,可以清除dist文件夹里的内容 API样例: var fs = ...