\(\mathcal{Description}\)

  Link.

  有 \(n\) 个站台在一个圆环上,顺时针编号 \(1\sim n\),其中 \(1\sim m\) 号站台只能乘坐顺时针转的环线,其他车站只能乘坐逆时针转的环线。给定起点 \(s\) 和参数 \(t\),运动规则为:

  1. 乘坐在 \(s\) 站的环线,坐 \(t\) 站,令 \(s\) 为到达的站点;

  2. 令 \(t\leftarrow t-1\),若 \(t>0\),返回第一步。

  求 \(s\) 最终的值。

  \(3\le n\le10^5\),\(1\le t\le 10^{12}\)。

\(\mathcal{Solution}\)

  想要倍增?参数 \(t\) 每时每刻在变化,不可能直接倍增。

  但注意到运动是在环上,所以 \(t\) 和 \((t\bmod n)\) 在当前一步运动所达到的效果是等价的,而且 \(n\) 的范围能够接受,我们可以暴力模拟直到 \(t\) 成为 \(n\) 的倍数。

  接着再用倍增,我们需要求出从 \(i~(i=1,2,\cdots,n)\) 出发,参数 \(t=n\),运动完成后到达的点。不放令所有点编号 \(-1\) 方便接下来使用 DP,令 \(f(i,j)\) 表示从 \(i\) 出发,参数 \(t=j\),运动完成后到达的点。显然有转移

\[f(i,j)=\begin{cases}
i&j=0\\
f(i+j,j-1)&i\in[0,m)\land j>0\\
f(i-j,j-1)&i\in[m,n)\land j>0
\end{cases}
\]

其中第一维默认取\(\bmod n\) 意义下的非负值。可见,我们仅需要支持对 \(f(i)\) 这一维状态进行整段的提取和拼接,就能“整体 DP”出 \(f(i+1)\) 的状态。

  用可持久化 treap 维护这一过程即可。注意 DP 中提取出的两段区间可能有大面积重叠,需要使用随机合并的方法实现 merge 操作,并且定期重构树并回收掉所有用于持久化的结点。

  求出 \(f(i,n)\) 后,对其处理倍增数组即可回答询问。

  复杂度 \(\mathcal O(n\log t)\),常数较大。

\(\mathcal{Code}\)

/* Clearink */

#include <cstdio>
#include <cassert>
#include <cstdlib> #define rep( i, l, r ) for ( int i = l, rpbound##i = r; i <= rpbound##i; ++i )
#define per( i, r, l ) for ( int i = r, rpbound##i = l; i >= rpbound##i; --i ) typedef long long LL; const int MAXN = 1e5, MAXLG = 39;
int n, m, s, f[MAXN + 5][MAXLG + 5], tf[MAXN + 5];
LL turn; struct PersistentTreap {
static const int MAXND = 4e7;
int node, val[MAXND + 5], ch[MAXND + 5][2], siz[MAXND + 5];
// nodes's id are 0-based. PersistentTreap() { srand( 20120712 ); } inline int crtnd( const int v ) {
int u = node++;
val[u] = v, ch[u][0] = ch[u][1] = -1, siz[u] = 1;
return u;
} inline void copy( const int u, const int v ) {
val[u] = val[v], ch[u][0] = ch[v][0], ch[u][1] = ch[v][1];
siz[u] = siz[v];
} inline void pushup( const int u ) {
siz[u] = 1 + ( ~ch[u][0] ? siz[ch[u][0]] : 0 )
+ ( ~ch[u][1] ? siz[ch[u][1]] : 0 );
} inline bool goleft( const int a, const int b ) {
return rand() % ( a + b ) < a;
} inline int merge( const int u, const int v ) {
if ( !~u || !~v ) return ~u ? u : v;
int w = crtnd( 0 );
if ( goleft( siz[u], siz[v] ) ) {
copy( w, u ), ch[w][1] = merge( ch[w][1], v );
} else {
copy( w, v ), ch[w][0] = merge( u, ch[w][0] );
}
return pushup( w ), w;
} inline void rsplit( const int u, const int k, int& x, int& y ) {
if ( !~u ) return void( x = y = -1 );
if ( !k || ( ~ch[u][0] && k <= siz[ch[u][0]] ) ) {
assert( node < MAXND );
copy( y = crtnd( 0 ), u );
rsplit( ch[y][0], k, x, ch[y][0] );
pushup( y );
} else {
assert( node < MAXND );
copy( x = crtnd( 0 ), u );
rsplit( ch[x][1],
k - ( ~ch[u][0] ? siz[ch[u][0]] : 0 ) - 1, ch[x][1], y );
pushup( x );
}
} inline int rebuild( const int l, const int r, const int* arr ) {
if ( l > r ) return -1;
int mid = l + r >> 1, u = crtnd( arr[mid] );
ch[u][0] = rebuild( l, mid - 1, arr );
ch[u][1] = rebuild( mid + 1, r, arr );
return pushup( u ), u;
} inline void travel( const int u, int& idx, int* arr ) {
if ( !~u ) return ;
travel( ch[u][0], idx, arr );
arr[idx++] = val[u];
travel( ch[u][1], idx, arr );
}
} trp; inline int extract( const int rt, int l, int r ) {
l = ( l % n + n ) % n, r = ( r % n + n ) % n;
int x, y, z;
if ( l <= r ) {
trp.rsplit( rt, l, x, y );
assert( trp.siz[y] >= r - l + 1 );
trp.rsplit( y, r - l + 1, y, z );
return y;
} else {
trp.rsplit( rt, r + 1, x, y );
assert( trp.siz[y] >= l - r - 1 );
trp.rsplit( y, l - r - 1, y, z );
return trp.merge( z, x );
}
} int main() {
scanf( "%d %d %d %lld", &n, &m, &s, &turn ), --s;
for ( ; turn % n; --turn ) {
s = ( s + ( s < m ? turn : -turn ) % n + n ) % n;
}
turn /= n;
int rt = -1;
rep ( i, 0, n - 1 ) rt = trp.merge( rt, trp.crtnd( i ) );
rep ( i, 1, n ) {
rt = trp.merge( extract( rt, i, i + m - 1 ),
extract( rt, m - i, n - i - 1 ) );
if ( !( i % 5000 ) ) {
int tmp = 0;
trp.travel( rt, tmp, tf );
trp.node = 0, rt = trp.rebuild( 0, tmp - 1, tf );
}
}
int tmp = 0;
trp.travel( rt, tmp, tf );
rep ( i, 0, n - 1 ) f[i][0] = tf[i];
for ( int j = 1; 1ll << j <= turn; ++j ) {
rep ( i, 0, n - 1 ) {
f[i][j] = f[f[i][j - 1]][j - 1];
}
}
for ( int j = 39; ~j; --j ) if ( ( turn >> j ) & 1 ) s = f[s][j];
printf( "%d\n", s + 1 );
return 0;
}

Solution -「CF 1056G」Take Metro的更多相关文章

  1. Solution -「CF 1342E」Placing Rooks

    \(\mathcal{Description}\)   Link.   在一个 \(n\times n\) 的国际象棋棋盘上摆 \(n\) 个车,求满足: 所有格子都可以被攻击到. 恰好存在 \(k\ ...

  2. Solution -「CF 1622F」Quadratic Set

    \(\mathscr{Description}\)   Link.   求 \(S\subseteq\{1,2,\dots,n\}\),使得 \(\prod_{i\in S}i\) 是完全平方数,并最 ...

  3. Solution -「CF 923F」Public Service

    \(\mathscr{Description}\)   Link.   给定两棵含 \(n\) 个结点的树 \(T_1=(V_1,E_1),T_2=(V_2,E_2)\),求一个双射 \(\varph ...

  4. Solution -「CF 923E」Perpetual Subtraction

    \(\mathcal{Description}\)   Link.   有一个整数 \(x\in[0,n]\),初始时以 \(p_i\) 的概率取值 \(i\).进行 \(m\) 轮变换,每次均匀随机 ...

  5. Solution -「CF 1586F」Defender of Childhood Dreams

    \(\mathcal{Description}\)   Link.   定义有向图 \(G=(V,E)\),\(|V|=n\),\(\lang u,v\rang \in E \Leftrightarr ...

  6. Solution -「CF 1237E」Balanced Binary Search Trees

    \(\mathcal{Description}\)   Link.   定义棵点权为 \(1\sim n\) 的二叉搜索树 \(T\) 是 好树,当且仅当: 除去最深的所有叶子后,\(T\) 是满的: ...

  7. Solution -「CF 623E」Transforming Sequence

    题目 题意简述   link.   有一个 \(n\) 个元素的集合,你需要进行 \(m\) 次操作.每次操作选择集合的一个非空子集,要求该集合不是已选集合的并的子集.求操作的方案数,对 \(10^9 ...

  8. Solution -「CF 1023F」Mobile Phone Network

    \(\mathcal{Description}\)   Link.   有一个 \(n\) 个结点的图,并给定 \(m_1\) 条无向带权黑边,\(m_2\) 条无向无权白边.你需要为每条白边指定边权 ...

  9. Solution -「CF 599E」Sandy and Nuts

    \(\mathcal{Description}\)   Link.   指定一棵大小为 \(n\),以 \(1\) 为根的有根树的 \(m\) 对邻接关系与 \(q\) 组 \(\text{LCA}\ ...

随机推荐

  1. sqlcl - Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file

    运行sqlcl的命令sql出错 bash-4.2$ sql username/password@hostname:1521:databasename Exception in thread " ...

  2. PPT2010初识

    原文链接:https://www.toutiao.com/i6486689592241029645/ 一.认识PPT PPT是PowerPoint简称,主要用于演示文稿的创建,即幻灯片的制作演示软件. ...

  3. python多环境管理一(venv与virtualenv)

    一.背景 我们经常会遇见这样的场景: 1.各个项目使用的python版本不相同 由于Python的解释器版本众多,各版本之间差异非常大.特别是python2和python3,互不兼容. 有些项目可能用 ...

  4. 关于Vue中根组件与组件树的理解

    在观看了b站的关于Vue3的教学视频后,对Vue的根组件与组件树进行简单的总结下 一.实例化Vue应用 // 此时,app就是一个Vue应用的实例,或者说是一个对象 const app = Vue.c ...

  5. Ubuntu 18.04 server安装+搭建Seacms v10.1网站

    0x00 写在前面 以前我天真的认为,ubuntu Desktop会安装了,server就无所谓了,其实完全不然,server还是有一些坑点的. 之所以选择Seacms搭建网站,是因为这个SeaCMS ...

  6. 【笔记】golang中使用protocol buffers的底层库直接解码二进制数据

    背景 一个简单的代理程序,发现单核QPS达到2万/s左右就上不去了,40%的CPU消耗在pb的decode/encode上面. 于是我想,对于特定的场景,直接从[]byte中取出字段,而不用完全的把整 ...

  7. 2022GDUT寒假专题学习-1 B,F,I,J题

    专题链接:专题学习1 - Virtual Judge (vjudge.net) B - 全排列 题目 思想 这道题可以用DFS进行求解,但是一看到全排列,其实可以立刻想到一个STL函数:next_pe ...

  8. golang中的rpc开发

    golang中实现RPC非常简单,官方提供了封装好的库,还有一些第三方的库 golang官方的net/rpc库使用encoding/gob进行编解码,支持tcp和http数据传输方式,由于其他语言不支 ...

  9. linux文件编辑器快捷方式

    一:文件编辑器快捷方式 7.光标快速移动快捷方式 ①. 快速切换光标到底行 shift + G ②. 快速切换光标到首行 gg ③. 快速跳转到行首 0 ④. 快速跳转到行尾 shift + $ ⑤. ...

  10. Python 使用 Windows10 桌面通知

    前言 Win10 没有提供简单命令行方式来触发桌面通知,所以使用 Python 来写通知脚本. 一番搜索,找到 win10toast .但这开源仓库已无人维护,通过 github fork 的关系图, ...