CF679E Bear and Bad Powers of 42
一段时间不写线段树标记,有些生疏了
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的更多相关文章
- Codeforces679E. Bear and Bad Powers of 42
传送门 今天子帧的一套模拟题的T3. 考试的时候其实已经想到了正解了,但是一些地方没有想清楚,就没敢写,只打了个暴力. 首先考虑用线段树维护区间信息. 先把每个值拆成两个信息,一是距离他最近的且大于他 ...
- codeforces 679e Bear and Bad Powers of 42
传送门:http://codeforces.com/contest/679/problem/E 题目意思很清晰,给你一个序列,要求你完成以下三个操作: 1.输出A[i] 2.将[a,b]区间的所有数字 ...
- Lucky Array Codeforces - 121E && Bear and Bad Powers of 42 Codeforces - 679E
http://codeforces.com/contest/121/problem/E 话说这题貌似暴力可A啊... 正解是想出来了,结果重构代码,调了不知道多久才A 错误记录: 1.线段树搞混num ...
- Codeforces 679E - Bear and Bad Powers of 42(线段树+势能分析)
Codeforces 题目传送门 & 洛谷题目传送门 这个 \(42\) 的条件非常奇怪,不过注意到本题 \(a_i\) 范围的最大值为 \(10^{14}\),而在值域范围内 \(42\) ...
- (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, ...
- 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 ...
- 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 ...
- 【19.05%】【codeforces 680D】Bear and Tower of Cubes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 1142 - Summing up Powers (II)
1142 - Summing up Powers (II) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit ...
随机推荐
- 20155305乔磊《网络对抗》逆向及Bof基础
20155305乔磊<网络对抗>逆向及Bof基础 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何 ...
- 20155306 白皎 0day漏洞——漏洞的复现
一.Ubuntu16.04 (CVE-2017-16995) 1.漏洞概述 Ubuntu最新版本16.04存在本地提权漏洞,该漏洞存在于Linux内核带有的eBPF bpf(2)系统调用中,当用户提供 ...
- 使用FindAncestor查找方式绑定且不需要使用datacontext
原文:使用FindAncestor查找方式绑定且不需要使用datacontext <UserControl x:Class="QuJiao.AnimationVideoPlayer&q ...
- Android开发——JobScheduler机制
年Google开发大会上指出,如果每个APP都使用这个API,那么可以节约15%到20%的电量. 2. JobScheduler拥有更多的触发选项 JobScheduler比AlarmManager ...
- Oracle出现与并行相关的ORA-00600时的调查方法
出现了 ORA-00600[kxfpqsod_qc_sod], 如何调查呢? 例如:从trace 文件的 Call Stack,可以看到 Error: ORA-600 [kxfpqsod_qc_sod ...
- 使用pandas,7行代码实现朴素贝叶斯
作者:hhh5460 大抵分成两类 一.离散的.标签化的数据 原文没有使用pandas,我使用pandas重新实现了朴素贝叶斯算法,看起来非常简洁.清爽. import pandas as pd '' ...
- P2463 [SDOI2008]Sandy的卡片
写一种\(O(nm)\)的做法,也就是\(O(\sum 串长)\)的. 先通过差分转化,把每个数变成这个数与上一个数的差,第一个数去掉,答案就是最长公共子串+1 按照套路把所有串拼起来,中间加一个分隔 ...
- js 边写边出现刚写的内容
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- BCompare破解方法
1.删除 BCUnrar.dll 文件,重启软件. 备注:使用everything搜索BCUnrar.dll
- win10安装tensorflow-gpu
1.安装anaconda (最好使用清华源下载) 2.打开cmd conda create -n tensorflow pip python=3.6 activate tensorflow pip i ...