1 条题解
-
0
题意
维护一个序列,支持向尾部插入一个数和替换全局的 为 ,求多次操作以后的最终序列。
题解
首先离线,并记录在序列的某个位置做了对后缀有影响的映射操作。
考虑维护映射关系,由于替换是后效传递的,所以考虑倒着往前扫,就知道了某个位置在后续所有变换的叠加效果(有点类似并查集从根往下传信息)。const int maxn = 5e5 + 10; int buc[maxn], top = 0; vector< pair<int,int> > swp[maxn]; int f[maxn]; int main() { int q; scanf("%d", &q); int opt, x, y; rep(qr,1,q) { scanf("%d", &opt); if(opt == 1) { scanf("%d", &x); buc[ ++ top ] = x; } else { scanf("%d %d", &x, &y); swp[ top ].push_back({x, y}); } } rep(i,1,5e5) f[i] = i; per(i,top,1) { for(auto it = swp[i].rbegin(); it != swp[i].rend(); ++ it) f[ it -> first ] = f[it -> second]; buc[i] = f[buc[i]]; } rep(i,1,top) printf("%d ", buc[i]); puts(""); return 0; }
- 1
信息
- ID
- 7544
- 时间
- 2000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 2
- 已通过
- 1
- 上传者