一段时间不写线段树标记,有些生疏了

codeforces 679e Bear and Bad Powers of 42 - CHADLZX - 博客园

关键点是:42的次幂,在long long范围内只有11个

考虑暴力修改

记录每个点距离下一个42次幂的距离,一般是负数

再记录每个点的等级,则有num=mi[lev+1]+val

特别地,当val=0的时候,num就是第lev个42的次幂

假如只有3操作,区间加,如果当前区间最大值大于等于0,

那么暴力下去升级:如果区间最大值大于等于0,就接着升级。如果升级途中,发现一个点的val==0,意味着还要再进行这个操作,flag记录一下

每个数只增不减,而且不会重复加太多次,大概每个点最多会被加O(logn)次,复杂度就是O(nlog^2n)

加上2操作,区间赋值会把很多数“拉回来”,而且一次性增加很多42次幂

但是权值种类会少很多,至少整个区间都是一个数

所以记录区间是否都是一个数,如果是一个数,就不用暴力修改了

增加了常数个42次幂的可能,也不会太多

这一步也就O(nlogn)

1的询问就是O(nlogn)

总复杂度正确

代码:

#include<bits/stdc++.h>
#define reg register int
#define il inline
#define numb (ch^'0')
#define mid ((l+r)>>1)
#define ls (x<<1)
#define rs (x<<1|1)
#define fi first
#define se second
using namespace std;
typedef long long ll;
il void rd(int &x){
char ch;x=;bool fl=false;
while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
for(x=numb;isdigit(ch=getchar());x=x*+numb);
(fl==true)&&(x=-x);
}
namespace Miracle{
const int N=+;
const ll inf=0x3f3f3f3f3f3f3f3f;
int n,q;
int U;
ll a[N];
ll mi[];
pair<ll,ll> zip(ll x){
int p=lower_bound(mi,mi+U+,x)-mi;
--p;
return make_pair(p,x-mi[p+]);
}
ll num(int lev,ll dis){
return mi[lev+]+dis;
}
struct tr{
ll mx; ll ad,chan; int same;
int lev;ll val;
}t[*N]; void pushup(int x){
t[x].mx=max(t[x<<].mx,t[x<<|].mx);
if(t[x<<].same&&t[x<<|].same&&t[x<<].val==t[x<<|].val&&t[x<<].lev==t[x<<|].lev){
t[x].same=;
t[x].val=t[x<<].val;
t[x].lev=t[x<<].lev;
}else{
t[x].same=;
}
}
pair<ll,ll>tmp;
void pro(int &lev,ll &dis){
ll now=num(lev,dis);
tmp=zip(now);
lev=tmp.fi;
dis=tmp.se;
}
void build(int x,int l,int r){
if(l==r){
t[x].same=;
tmp=zip(a[l]);
t[x].mx=t[x].val=tmp.se;
t[x].lev=tmp.fi;
t[x].chan=-inf;//warninig!!!! -inf represented no change
t[x].ad=;
return;
}
t[x].ad=;
t[x].chan=-inf;
build(ls,l,mid);build(rs,mid+,r);
pushup(x);
}
void pushdown(int x){
for(reg i=;i<=;++i){
int son=x<<|i;
if(t[x].chan!=-inf){
ll c=t[x].chan;
t[son].same=;
tmp=zip(c);
t[son].lev=tmp.fi;
t[son].val=tmp.se;
t[son].ad=;
t[son].chan=c;
t[son].mx=t[son].val;
}else if(t[x].ad){
ll c=t[x].ad;
if(t[son].same){
//if(t[x].mx+c==0) c+=c;//warning!!! if(t[son].chan!=-inf){
t[son].chan+=c;
}
else{
t[son].ad+=c;
}
t[son].val+=c;
pro(t[son].lev,t[son].val);
t[son].mx=t[son].val;
}
else{
t[son].mx+=c;
t[son].ad+=c;
}
}
}
t[x].ad=;
t[x].chan=-inf;
}
bool upda(int x,int l,int r){//bao li updaing level
if(l==r){
if(t[x].val==){
return true;
}
return false;
}
else if(t[x].same){
if(t[x].val==) return true;
return false;
}
pushdown(x);
bool ret=false;
if(t[ls].mx>=) ret|=upda(x<<,l,mid);
if(t[rs].mx>=) ret|=upda(x<<|,mid+,r);
pushup(x);//warinnig!!!!
return ret;
} int flag;
void add(int x,int l,int r,int L,int R,ll c){
if(L<=l&&r<=R){
if(t[x].same){
if(t[x].chan!=-inf){
t[x].chan+=c;
}
else{
t[x].ad+=c;
}
t[x].val+=c;
pro(t[x].lev,t[x].val);
t[x].mx=t[x].val; if(t[x].mx==) flag=;//need again
}
else{
t[x].mx+=c;
t[x].ad+=c;
if(t[x].mx>=){
flag|=upda(x,l,r);
// while(fl){
// fl=upda(x,l,r);
// if(fl) {
// t[x].mx+=c;
// t[x].ad+=c;
// }
// if(t[x].mx>=0){
// fl=true;
// }else{
// fl=false;
// }
// }
}
}
return;
}
pushdown(x);
if(L<=mid) add(x<<,l,mid,L,R,c);
if(mid<R) add(x<<|,mid+,r,L,R,c);
pushup(x);
}
void chan(int x,int l,int r,int L,int R,ll c){
if(L<=l&&r<=R){
t[x].same=;
tmp=zip(c);
t[x].lev=tmp.fi;
t[x].val=tmp.se;
t[x].ad=;
t[x].chan=c;
t[x].mx=t[x].val;
return;
}
pushdown(x);
if(L<=mid) chan(x<<,l,mid,L,R,c);
if(mid<R) chan(x<<|,mid+,r,L,R,c);
pushup(x);
}
ll query(int x,int l,int r,int p){
if(l==r){
return num(t[x].lev,t[x].val);
}
pushdown(x);
if(p<=mid) return query(ls,l,mid,p);
else return query(rs,mid+,r,p);
}
int main(){
rd(n);rd(q);
for(reg i=;i<=n;++i){
scanf("%lld",&a[i]);
}
mi[]=;
for(reg i=;i<=;++i) mi[i]=mi[i-]*;
U=;
build(,,n);
int op,l,r,p;
ll c;
while(q--){
rd(op);
if(op==){
rd(p);
printf("%lld\n",query(,,n,p));
}
else if(op==){
rd(l);rd(r);
scanf("%lld",&c);
chan(,,n,l,r,c);
}else{
rd(l);rd(r);
scanf("%lld",&c);
flag=;
add(,,n,l,r,c);
while(flag){
flag=;
add(,,n,l,r,c);
}
}
}
return ;
} }
signed main(){
Miracle::main();
return ;
} /*
Author: *Miracle*
Date: 2019/2/14 20:07:14
*/

CF679E Bear and Bad Powers of 42的更多相关文章

  1. Codeforces679E. Bear and Bad Powers of 42

    传送门 今天子帧的一套模拟题的T3. 考试的时候其实已经想到了正解了,但是一些地方没有想清楚,就没敢写,只打了个暴力. 首先考虑用线段树维护区间信息. 先把每个值拆成两个信息,一是距离他最近的且大于他 ...

  2. codeforces 679e Bear and Bad Powers of 42

    传送门:http://codeforces.com/contest/679/problem/E 题目意思很清晰,给你一个序列,要求你完成以下三个操作: 1.输出A[i] 2.将[a,b]区间的所有数字 ...

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

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

  4. Codeforces 679E - Bear and Bad Powers of 42(线段树+势能分析)

    Codeforces 题目传送门 & 洛谷题目传送门 这个 \(42\) 的条件非常奇怪,不过注意到本题 \(a_i\) 范围的最大值为 \(10^{14}\),而在值域范围内 \(42\) ...

  5. (Problem 29)Distinct powers

    Consider all integer combinations ofabfor 2a5 and 2b5: 22=4, 23=8, 24=16, 25=32 32=9, 33=27, 34=81, ...

  6. Codeforces Round #356 (Div. 2) D. Bear and Tower of Cubes dfs

    D. Bear and Tower of Cubes 题目连接: http://www.codeforces.com/contest/680/problem/D Description Limak i ...

  7. codeforces 680D D. Bear and Tower of Cubes(dfs+贪心)

    题目链接: D. Bear and Tower of Cubes time limit per test 2 seconds memory limit per test 256 megabytes i ...

  8. 【19.05%】【codeforces 680D】Bear and Tower of Cubes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 1142 - Summing up Powers (II)

    1142 - Summing up Powers (II)    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit ...

随机推荐

  1. WPF EventTrigger,BeginStoryboard

    <Window x:Class="WpfApplication2.LoginWind" xmlns="http://schemas.microsoft.com/wi ...

  2. Centos7 定时任务启动python脚本发送邮件

    直接上python脚本: 2.我是把这个脚本放在home文件夹下面 3.在centos命令模式下: crontab -e   命令编辑启动脚本: 4.第一个命令意思是:每天9点到下午5点,每隔一个小时 ...

  3. mfc c++字符串类与 流输出

    一.命名空间 所谓命名空间(namespace),是指标识符的各种可见范围.C++标准程序库中的所有标识符都被定义于一个名为std的命名空间(namespace)中.而我们要使用的string类也是一 ...

  4. LOJ #2135. 「ZJOI2015」幻想乡战略游戏

    #2135. 「ZJOI2015」幻想乡战略游戏 链接 分析: 动态点分治,求加权重心,带修改. 考虑如果知道了一个点s,如何求答案,那么首先可以点分治的思想,求每个联通块内所有点到分治中心距离和,然 ...

  5. CodeForces-1155D Beautiful Array

    Description You are given an array \(a\) consisting of \(n\) integers. Beauty of array is the maximu ...

  6. ILSVRC2016目标检测任务回顾——视频目标检测(VID)

    转自知乎<深度学习大讲堂> 雷锋网(公众号:雷锋网)按:本文作者王斌,中科院计算所前瞻研究实验室跨媒体计算组博士生,导师张勇东研究员.2016年在唐胜副研究员的带领下,作为计算所MCG-I ...

  7. cocos2d-x学习记录5——CCTransition场景过渡

    利用CCTransition能够创建出一系列的场景过渡动画,能够使场景切换时更加绚丽丰富. CCTransition派生出很多过渡动画,传入的参数一般包括过渡时间和创建的场景. MyScene.h内容 ...

  8. ReactJS实用技巧(1):JSX与HTML的那些不同

    在项目中使用ReactJS也已经有大半年了,收获很多也踩过不少坑.不想把这个系列写成抄书似的罗列,旨在总结些常用的技巧及常见的坑,以帮助初心者快速入门,想系统学习的同学还是多阅读文档. JSX本质上与 ...

  9. 如何在内网安装compass

    神器compass是肿么用这里不做介绍,因为我也不清楚,可参考官网:http://compass-style.org.这里主要介绍如何在内网安装compass. 首先介绍一般是如何安装compass的 ...

  10. jmeter:正则表达式的使用

    Jmeter中正则关联的使用是可以提取动态变化数据进行传递:关联的方式和提取器有多种,这篇先讲解正则表达式怎么来关联(?) 在需要获取数据的http请求上添加后置处理器 比如提取百度title值: 正 ...