Solution -「CF 1056G」Take Metro
\(\mathcal{Description}\)
Link.
有 \(n\) 个站台在一个圆环上,顺时针编号 \(1\sim n\),其中 \(1\sim m\) 号站台只能乘坐顺时针转的环线,其他车站只能乘坐逆时针转的环线。给定起点 \(s\) 和参数 \(t\),运动规则为:
乘坐在 \(s\) 站的环线,坐 \(t\) 站,令 \(s\) 为到达的站点;
令 \(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\),运动完成后到达的点。显然有转移
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的更多相关文章
- Solution -「CF 1342E」Placing Rooks
\(\mathcal{Description}\) Link. 在一个 \(n\times n\) 的国际象棋棋盘上摆 \(n\) 个车,求满足: 所有格子都可以被攻击到. 恰好存在 \(k\ ...
- Solution -「CF 1622F」Quadratic Set
\(\mathscr{Description}\) Link. 求 \(S\subseteq\{1,2,\dots,n\}\),使得 \(\prod_{i\in S}i\) 是完全平方数,并最 ...
- Solution -「CF 923F」Public Service
\(\mathscr{Description}\) Link. 给定两棵含 \(n\) 个结点的树 \(T_1=(V_1,E_1),T_2=(V_2,E_2)\),求一个双射 \(\varph ...
- Solution -「CF 923E」Perpetual Subtraction
\(\mathcal{Description}\) Link. 有一个整数 \(x\in[0,n]\),初始时以 \(p_i\) 的概率取值 \(i\).进行 \(m\) 轮变换,每次均匀随机 ...
- Solution -「CF 1586F」Defender of Childhood Dreams
\(\mathcal{Description}\) Link. 定义有向图 \(G=(V,E)\),\(|V|=n\),\(\lang u,v\rang \in E \Leftrightarr ...
- Solution -「CF 1237E」Balanced Binary Search Trees
\(\mathcal{Description}\) Link. 定义棵点权为 \(1\sim n\) 的二叉搜索树 \(T\) 是 好树,当且仅当: 除去最深的所有叶子后,\(T\) 是满的: ...
- Solution -「CF 623E」Transforming Sequence
题目 题意简述 link. 有一个 \(n\) 个元素的集合,你需要进行 \(m\) 次操作.每次操作选择集合的一个非空子集,要求该集合不是已选集合的并的子集.求操作的方案数,对 \(10^9 ...
- Solution -「CF 1023F」Mobile Phone Network
\(\mathcal{Description}\) Link. 有一个 \(n\) 个结点的图,并给定 \(m_1\) 条无向带权黑边,\(m_2\) 条无向无权白边.你需要为每条白边指定边权 ...
- Solution -「CF 599E」Sandy and Nuts
\(\mathcal{Description}\) Link. 指定一棵大小为 \(n\),以 \(1\) 为根的有根树的 \(m\) 对邻接关系与 \(q\) 组 \(\text{LCA}\ ...
随机推荐
- 读取 properties 配置文件含有中文的value内容 导致中文乱码 的解决办法
1.前言 因为装系统的时候把中文写在了系统路径,现在我想把这个路径写在properties里面来读取,可是 发现java 读取会导致中文乱码成 问号????的乱码 ,百度找了好多博客,基本都是一摸一 ...
- vert.x框架-使用spring注解功能
1.前言 习惯了spring注解风格,方便好用,现在用vert.x框架,怎么使用spring注解呢? 2.maven安装依赖包 <!--spring注解依赖包--> <depende ...
- 使用NSSM把任意exe程序注册成Windows服务
1.登录NSSM官网下载最新版本: http://www.nssm.cc/download 2.解压之后可以看到win32和win64位的nssm.exe,根据自己的电脑选择使用 3.拷贝一份nssm ...
- Visual Studio 2015 MFC之Button颜色变化-断点调试(Debug)
软件开发,对自己的程序进行调试很重要,本次文章在上一边随笔的基础上,介绍一下Button控件做显示灯的用法,Button控件的添加和变量设置等可以参考下面的的链接:Visaul Studio 2015 ...
- 同步gitlab与github
1.找到setting 2.找到左下方的developer setting 3.按标好的数字依次执行 4.填写note并勾选repo 5.在最左下方勾选 6.保存生成的新的token到其他地方,之后你 ...
- 【pwn】V&N2020 公开赛 simpleHeap
[pwn]V&N2020 公开赛 simpleHeap 1.静态分析 首先libc版本是ubuntu16的2.23版本,可以去buu的资源处下载 然后checksec一下,保护全开 拖入IDA ...
- winform全局异常处理
static void Main() { //设置应用程序处理异常方式:ThreadException处理 Application.SetUnhandledExceptionMode(Unhandle ...
- 个人作业2-6.4-Python爬取顶会信息
1.个人作业2 数据爬取阶段 import requestsfrom lxml import etreeimport pymysqldef getdata(url): # 请求CVPR主页 page_ ...
- 2月10日 体温APP开发总结
1.Java代码 1.user package bean;public class User { private String name; private String riqi; private S ...
- 遇到奇怪的问题:web.py 0.40中使用web.input(),出现一堆奇怪的错误
有的请求很正常,有的请求就出现了500错误. 这里使用POST请求,然后在web.input()中出现了很长很长的错误. 猜测是这个机器上安装了python2.7 / python 3.6 / pyt ...