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

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. 20155202《网络对抗》Exp9 web安全基础实践

    20155202<网络对抗>Exp9 web安全基础实践 实验前回答问题 (1)SQL注入攻击原理,如何防御 SQL注入产生的原因,和栈溢出.XSS等很多其他的攻击方法类似,就是未经检查或 ...

  2. 【php增删改查实例】第九节 - 部门管理模块(模糊查询)

    给datagrid添加一个工具栏. 效果: 模糊查询部门名称. 接下来,在工具栏区域画一个input框: <div id="toolbar" style="padd ...

  3. Caffe上手教程

    Caffe上手教程 入门系列FAQ72 在Unbuntu上安装Caffe828 Windows下安装Caffe1.4K Caffe框架上手教程1.2K Caffe编译运行调试462 Caffe 电脑配 ...

  4. 使用nginx很卡之strace命令

    一.strace命令常用参数 strace -tt -T -v -f -e trace= -p -tt 在每行输出的前面,显示毫秒级别的时间 -T 显示每次系统调用所花费的时间 -v 对于某些相关调用 ...

  5. TMS320VC5509驱动LCD1602之奇怪问题和时序图

    1. 最近调试自己板子上LCD1602的时候,看下测试的时序图,因为下面的时序图导致LCD1602无法显示,下面的时序图是有问题的,E的上升沿和下降沿的时候,RW需要低电平 对比下淘宝上买的可以显示的 ...

  6. 微信小程序之缓存——不同页面传递数据

    1. 添加缓存 单个密钥允许存储的最大数据长度为1MB,所有数据存储上限为10MB. // 存储信息到storage // 异步存储 set() { wx.setStorage({ key: 'use ...

  7. SSISDB6:参数和环境变量

    SSISDB 系列随笔汇总: SSISDB1:使用SSISDB管理Package SSISDB2:SSIS工程的操作实例 SSISDB3:Package的执行实例 SSISDB4:当前正在运行的Pac ...

  8. python list的一个面试题

    面试题''' 一个list,里面的数字偶数在左边,奇数在右边,不借助其他列表 ''' def userlist(add_list): if type(add_list)==list: if len(a ...

  9. 高精度减法--C++

    高精度减法--C++ 仿照竖式减法,先对其,再对应位相减. 算法处理时,先比较大小,用大的减小的,对应位再比较大小,用于作为借位符. #include <iostream> #includ ...

  10. 唐雎(jū)不辱使命

    唐雎(jū)不辱使命 ​选自<战国策> 秦王使人谓安陵君曰:“寡人欲以五百里之地易安陵,安陵君其许寡人.”安陵君曰:“大王加惠,以大易小,甚善.虽然,受地于先生,愿终守之,弗敢易.”秦王不 ...