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 ...
随机推荐
- uva12546. LCM Pair Sum
uva12546. LCM Pair Sum One of your friends desperately needs your help. He is working with a secret ...
- 【BO】安装BO服务器时,oracle服务端安装ora-12514和12541的问题
今天在安装BO服务器,oracle数据库时,出现了这样一个问题,描述如下: 首先安装oracle10g Server 32位版.安装ORCL数据库之后,使用10gServer下的NET MANAGER ...
- 【原】iOS学习之极光推送
一.极光推送工程端 1.下载SDK 极光推送是一个推送消息的第三方,SDK下载:https://www.jpush.cn/common/products 集成压缩包内容:包名为JPush-iOS-SD ...
- CodeForces 279D The Minimum Number of Variables 题解
题目大意: 有一组n个不相同的数字组成数串:a1,a2,a3-an. 1.一个数组b. 2.第一个操作我们将b0的值赋为a1.之后我们有n-1个操作,第k次操作我们将by=bi+bj(y,i,j可能相 ...
- FLEX各种特效集合
http://www.noupe.com/adobe/flex-developers-toolbox-free-components-themes-and-tutorials.html经典中的经典 h ...
- 查看IIS连接数
如果要想知道确切的当前网站IIS连接数的话,最有效的方法是通过windows自带的系统监视器来查看. 一.运行-->输入“perfmon.msc”. 二.在“系统监视器”图表区域里点击右键,然后 ...
- 怎样去除织梦版权信息中的Power by DedeCms
用织梦建站时,网站底部调用的版权信息最后总会多出一个Power by DedeCms链接,此链接是织梦系统中默认的指向织梦官网的外链.本文就介绍两种去除这个外链的方法. 1.为什么要去除Power b ...
- sphinx通过增量索引实现近实时更新
一.sphinx增量索引实现近实时更新设置 数据库中的已有数据很大,又不断有新数据加入到数据库中,也希望能够检索到.全部重新建立索引很消耗资源,因为我们需要更新的数据相比较而言很少. 例如.原来的数据 ...
- Lua字符串库
1. 基础字符串函数: 字符串库中有一些函数非常简单,如: 1). string.len(s) 返回字符串s的长度: 2). string.rep(s,n) 返回字符串s重复n次的结 ...
- 基于Jquery的页面过渡效果(原创)
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...