题:https://codeforces.com/contest/1236/problem/E

粗自:https://www.cnblogs.com/YSFAC/p/11715522.html

#include<bits/stdc++.h>
using namespace std;
#define lson root<<1,l,midd
#define rson root<<1|1,midd+1,r
typedef long long ll;
const int M=1e5+;
ll a[M],tree[M<<],lz[M<<];
ll L[M],R[M];
void build(ll root,ll l,ll r){
if(l==r){
tree[root]=l;
return ;
}
int midd=(l+r)>>;
build(lson);
build(rson);
}
ll query(ll pos,ll root,ll l,ll r){
if(l==r)
return tree[root]+lz[root];
ll midd=(l+r)>>;
if(pos<=midd)
return query(pos,lson)+lz[root];
else
return query(pos,rson)+lz[root];
}
void add(int L,int R,int c,int root,int l,int r){
if(L<=l&&r<=R){
lz[root]+=c;
return ;
}
int midd=(l+r)>>;
if(L<=midd)
add(L,R,c,lson);
if(R>midd)
add(L,R,c,rson);
}
int main(){
ll n,m;
scanf("%I64d%I64d",&n,&m);
for(int i=;i<=m;i++)
scanf("%I64d",&a[i]);
if(n==)
return cout<<"",;
//计算向右最多能走的距离
build(,,n);
for(int i=;i<=m;i++){
ll l=,r=n;
ll qul=,qur=;
while(l<=r){
ll midd=(l+r)>>;
if(query(midd,,,n)<a[i]-i)
qul=midd,l=midd+;
else
r=midd-;
}
qul++,l=,r=n;
while(l<=r){
ll midd=(l+r)>>;
if(query(midd,,,n)<=a[i]-i)
qur=midd,l=midd+;
else
r=midd-;
}
if(qul<=qur)
add(qul,qur,-,,,n);
}
for(int i=;i<=n;i++)
R[i]=min(query(i,,,n)+m+,n);
memset(tree,,sizeof(tree));
memset(lz,,sizeof(lz));
//计算向左最多能走的距离
build(,,n);
for(int i=;i<=m;i++){
ll l=,r=n;
ll qul=,qur=;
while(l<=r){
ll midd=(l+r)>>;
if(query(midd,,,n)<a[i]+i)
qul=midd,l=midd+;
else
r=midd-;
}
qul++,l=,r=n;
while(l<=r){
ll midd=(l+r)>>;
if(query(midd,,,n)<=a[i]+i)
qur=midd,l=midd+;
else
r=midd-;
}
if(qul<=qur)
add(qul,qur,,,,n);
}
for(int i=;i<=n;i++)
L[i]=max(query(i,,,n)-m-,1ll);
ll ans=;
for(int i=;i<=n;i++)
ans+=R[i]-L[i]+;
printf("%I64d",ans);
}

E. Alice and the Unfair Game(推导线段树)的更多相关文章

  1. 线段树(区间树)之区间染色和4n推导过程

    前言 线段树(区间树)是什么呢?有了二叉树.二分搜索树,线段树又是干什么的呢?最经典的线段树问题:区间染色:正如它的名字而言,主要解决区间的问题 一.线段树说明 1.什么是线段树? 线段树首先是二叉树 ...

  2. 【推导】【线段树】hdu5929 Basic Data Structure

    题意: 维护一个栈,支持以下操作: 从当前栈顶加入一个0或者1: 从当前栈顶弹掉一个数: 将栈顶指针和栈底指针交换: 询问a[top] nand a[top-1] nand ... nand a[bo ...

  3. [cf 1236 E] Alice and the Unfair Game

    题意: 给定一个长度为m的序列$A$,你有一个长度为n的棋盘,可以任选一个位置x作为起点. 在时刻$[1,m+1]$你可以向左或向右移动一格. 设时刻i你移动后所在的位置为$B_i$,你需要满足对于任 ...

  4. HDU 5558 Alice's Classified Message(后缀数组+二分+rmq(+线段树?))

    题意 大概就是给你一个串,对于每个\(i\),在\([1,i-1]\)中找到一个\(j\),使得\(lcp(i,j)\)最长,若有多个最大\(j\)选最小,求\(j\)和这个\(lcp\)长度 思路 ...

  5. CH Round #52 还教室[线段树 方差]

    还教室 CH Round #52 - Thinking Bear #1 (NOIP模拟赛) [引子]还记得 NOIP 2012 提高组 Day2 中的借教室吗?时光飞逝,光阴荏苒,两年过去了,曾经借教 ...

  6. 【BZOJ-4515】游戏 李超线段树 + 树链剖分 + 半平面交

    4515: [Sdoi2016]游戏 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 304  Solved: 129[Submit][Status][ ...

  7. SPOJ COT3 Combat on a tree(Trie树、线段树的合并)

    题目链接:http://www.spoj.com/problems/COT3/ Alice and Bob are playing a game on a tree of n nodes.Each n ...

  8. HDU 4614 Vases and Flowers (2013多校2 1004 线段树)

    Vases and Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others ...

  9. 数据结构(树链剖分,线段树):SDOI 2016 游戏

    4515: [Sdoi2016]游戏 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 351  Solved: 157[Submit][Status][ ...

随机推荐

  1. matplotlib画图--Line Color

    1.线形 2.标记 3.颜色

  2. VUE常见的语法

    模版渲染{{msg}} v-html="" v-text="" v-bind:id=""  类似 attr 三元判断 {{ok?'yes': ...

  3. redis(四)----发布订阅

    发布订阅(pub/sub)是一种消息通信模式,主要的目的是解耦消息发布者和消息订阅者之间的耦合.pub /sub不仅仅解决发布者和订阅者直接代码级别耦合,也解决两者在物理部署上的耦合.废话不多说,直接 ...

  4. float 格式化到TCHAR 数组

    <span style="white-space:pre"> </span>float lCount = 123.3; TCHAR tcBalance[MA ...

  5. const成员函数返回*this

    #include <iostream> using namespace std; class A{ public: A &set(char); const A &displ ...

  6. js.console携程近期低价机票信息

    !function(){var city = {"SHA":"上海虹桥","PVG":"上海浦东","YIW& ...

  7. (1)opencv的安装和遇到的问题

    opencv的安装主要是看的这个博客 https://blog.csdn.net/w_chaoqi/article/details/81949738 然后在把代码粘贴上时,出现,不识别我的opencv ...

  8. Vmotion迁移要求

  9. 超级顽固的流方式读取doc,docx乱码问题

    因为工作中需要一个把doc或者docx的office文档内容,需要读取出来,并且也没展示功能.代码中第一考虑可能就是通过读取流方式,结果写了以后,各种乱码,百科的解决方案也是千奇百怪,第一点:可能是文 ...

  10. Python说文解字_杂谈05

    1. isinstance和type: is和==符号,is指的是内存地址,是不是一个对象,ID知否相同 集成链 class A: pass class B(A): pass b = B() prin ...