https://www.luogu.org/problemnew/show/P3960

如果 x = 1,相当于维护一条链,每次取出第 k 个数放在序列末尾
假设有 n + m + q 个位置,每个位置有数为 1 ,没有数为 0,
取出后不前移,那么第 k 个数就是第 k 个 1 的位置
初始时 1 ~ n + m - 1 为 1,其他位置为 0,
用线段树维护区间和即可
类似对于这道题把每行看做一条链,最后一列看做一条链
一次操作可以看成对两条链的操作
套用上面的方法即可
注意要动态开点

#include <bits/stdc++.h>

using namespace std;
const int N = 3e5 + ; int n, m, Q, N_;
int size[N * ], lson[N * ], rson[N * ];
int root[N], jdjs;
int ins[N];
int now_root; #define LL long long LL val[N * ];
LL Ans1; #define gc getchar() inline int read() {
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} int Calc_size(int l, int r) {
if(now_root != n + ) {
if(r <= m - ) return r - l + ;
else {
if(l <= m - ) return (m - ) - l + ;
else return ;
}
} else {
if(r <= n) return r - l + ;
else {
if(l <= n) return n - l + ;
else return ;
}
} } void Sec_A(int l, int r, int & jd, int x) {
if(!jd) {
jd = ++ jdjs;
size[jd] = Calc_size(l, r);
if(l == r) {
if(now_root <= n) val[jd] = (LL) 1ll * (now_root - ) * m + l;
else val[jd] = (LL) 1ll * l * m;
}
}
size[jd] --;
if(l == r) {Ans1 = val[jd]; return ;}
int mid = (l + r) >> ;
if((!lson[jd] && x <= mid - l + ) || x <= size[lson[jd]]) Sec_A(l, mid, lson[jd], x);
else {
if(!lson[jd]) x -= (mid - l + );
else x -= size[lson[jd]];
Sec_A(mid + , r, rson[jd], x);
}
} void Ins(int l, int r, int & jd, int whe, LL Num) {
if(!jd) {
jd = ++ jdjs;
size[jd] = Calc_size(l, r);
if(l == r) val[jd] = Num;
}
size[jd] ++;
if(l == r) return ;
int mid = (l + r) >> ;
if(whe <= mid) Ins(l, mid, lson[jd], whe, Num);
else Ins(mid + , r, rson[jd], whe, Num);
} int main() {
n = read(), m = read(), Q = read();
N_ = max(n, m) + Q;
while(Q --) {
int x = read(), y = read();
if(y == m) now_root = n + , Sec_A(, N_, root[now_root],x);
else now_root = x, Sec_A(, N_, root[now_root], y);
cout << Ans1 << endl;
now_root = n + ; Ins(, N_, root[n + ], n + (++ ins[n + ]), Ans1);
if(y != m) {
now_root = n + ; Sec_A(, N_, root[now_root], x);
now_root = x; Ins(, N_, root[x], m - + (++ ins[x]), Ans1);
}
}
return ;
}

最新文章

  1. C++线程池
  2. JDK6环境下升级项目到springframework4.x和tomcat7.x
  3. NYOJ题目1051simone牌文本编辑器
  4. 多个$(document).ready()函数的执行顺序问题,(未解决)
  5. Toward Scalable Systems for Big Data Analytics: A Technology Tutorial (I - III)
  6. Investigating Your RAM Usage
  7. Linux与Windows共享文件夹之samba的安装与使用(Ubuntu为例)
  8. DisJSet:Find them, Catch them(POJ 1703)
  9. javascript类的类比详解-大白话版
  10. android中的命令安装与卸载
  11. Android 定时器TimerTask 简单使用
  12. Lua table pair和ipair区别
  13. WebLogic(12C)——Server
  14. [ Swift框架 ] # SwiftyJSON
  15. FFMPEG结构体分析:AVIOContext
  16. centos7.4系统升级kernel内核
  17. matplotlib图例-【老鱼学matplotlib】
  18. phpstudy添加redis扩展
  19. python基础知识点四
  20. ERROR in Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime (64)

热门文章

  1. 莫比乌斯反演--HDU模板题
  2. php7和PHP5对比的新特性和性能优化
  3. django的admin密码忘记了怎么办?一分钟解决!!
  4. xpath定位器
  5. Python面向对象中的继承、多态和封装
  6. matplotlib 画图中的basemap安装问题
  7. 复习最短路 spfa+dijstra堆优化
  8. 安装HAXM报错:failed to configure driver unknown error. failed to open driver 并提示数字签证不可用
  9. PLSQL PL/SQL Developer Oracle 使用技巧 常用设置 卡顿问题 病毒防范( 附带:配置文件)
  10. JS — 事件的相关概念和DOM