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

作为一个初二蒟蒻要考提高组,先做一下17年的题目

我们发现进行一次操作相当于

  • 把第 x 行的第 y 个弹出记为 a,其余向左移 = splay 中弹出第 y 个
  • 把第 m 列的第 x 个弹出记为 b,其余向上移 = splay 中弹出第 x 个
  • 把 b 插到第 x 行末尾 = splay 在队尾插入 b
  • 把 a 插到第 m 列末尾 = splay 在队尾插入 a

然后对于没有用到的节点先合并着,要用就拆开

#include <bits/stdc++.h>
#define int long long
using namespace std; typedef unsigned long long ull;
typedef long long ll; template <typename _T>
inline void read(_T &f) {
f = 0; _T fu = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') fu = -1; c = getchar();}
while(c >= '0' && c <= '9') {f = (f << 3) + (f << 1) + (c & 15); c = getchar();}
f *= fu;
} const int N = 300000 + 10; struct Node {
int l, r, size, id;
Node *ch[2];
Node (int a, int b, int c, int d, Node *e, Node *f) {
l = a, r = b, size = c, id = d;
ch[0] = e, ch[1] = f;
}
}*root[N], *null; int w[N];
int n, m, q; void update(Node *u) {
u -> size = u -> ch[0] -> size + u -> ch[1] -> size + u -> r - u -> l + 1;
} void rotate(Node *&u, int d) {
Node *tmp = u -> ch[d];
u -> ch[d] = tmp -> ch[d ^ 1];
tmp -> ch[d ^ 1] = u;
update(u); update(tmp);
u = tmp;
} void splay(Node *&u, int k) {
int ltree = u -> ch[0] -> size;
if(ltree < k && (ltree + (u -> r - u -> l + 1) >= k)) return;
int d = k > ltree;
int k2 = d ? k - ltree - (u -> r - u -> l + 1) : k;
int ltree2 = u -> ch[d] -> ch[0] -> size;
if(ltree2 >= k2 || (ltree2 + u -> ch[d] -> r - u -> ch[d] -> l + 1) < k2) {
int d2 = k2 > ltree2;
splay(u -> ch[d] -> ch[d2], d2 ? k2 - ltree2 - (u -> ch[d] -> r - u -> ch[d] -> l + 1) : k2);
if(d == d2) rotate(u, d2);
else rotate(u -> ch[d], d2);
}
rotate(u, d);
} Node *build(int l, int r) {
if(l > r) return null;
if(l == r) return new Node(w[l], w[l], 1, w[l], null, null);
int mid = (l + r) >> 1;
return new Node(w[mid], w[mid], r - l + 1, w[mid], build(l, mid - 1), build(mid + 1, r));
} void merge(Node *&a, Node *&b) {
if(a == null) a = b, b = null;
if(b == null) return;
splay(a, a -> size);
a -> ch[1] = b;
update(a);
} int split(Node *&u, int k) {
splay(u, k);
int ltree = u -> ch[0] -> size;
int K = k - ltree, l = u -> l, r = u -> r;
if(K != 1) {
Node *tmp = new Node(l, l + K - 2, K - 1, l, u -> ch[0], null);
u -> ch[0] = tmp; u -> l = l + K - 1; update(u -> ch[0]);
}
if(K != r - l + 1) {
Node *tmp = new Node(l + K, r, r - l - K + 1, l + K, null, u -> ch[1]);
u -> ch[1] = tmp; u -> r = l + K - 1; update(u -> ch[1]);
}
Node *t2 = u -> ch[1]; u -> ch[1] = null; u -> id = u -> l;
int ans = u -> id; u = u -> ch[0]; merge(u, t2); return ans;
} void ins(Node *&u, int x) {
if(u == null) u = new Node(x, x, 1, x, null, null);
else {
splay(u, u -> size);
u -> ch[1] = new Node(x, x, 1, x, null, null);
update(u);
}
} signed main() {
null = new Node(0, -1, 0, 0, 0, 0);
read(n); read(m); read(q);
for(int i = 1; i <= n; i++) {
root[i] = new Node(1 + (i - 1) * m, m - 1 + (i - 1) * m, m - 1, 1 + (i - 1) * m, null, null);
}
for(int i = 1; i <= n; i++) w[i] = i * m;
root[0] = build(1, n);
for(int i = 1; i <= q; i++) {
int a, b;
read(a); read(b);
if(b == m) {
int ans = split(root[0], a);
printf("%lld\n", ans);
ins(root[0], ans);
continue;
}
int ans = split(root[a], b);
printf("%lld\n", ans);
int tmp = split(root[0], a);
ins(root[a], tmp);
ins(root[0], ans);
}
return 0;
}

luoguP3690 列队的更多相关文章

  1. 关于PHP堆栈与列队

    在PHP中数组常被当作堆栈(后进先出:LIFO)与队列(先进先出:FIFO)结构来使用.PHP提供了一组函数可以用于push与pop(堆栈)还有shift与unshift(队列)来操作数组元素.堆栈与 ...

  2. 在PHP中如何使用消息列队

    /** * 消息列队服务 * @author zhou.tingze * @example * -----------------------------------Create----------- ...

  3. BZOJ2720: [Violet 5]列队春游

    2720: [Violet 5]列队春游 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 173  Solved: 125[Submit][Status] ...

  4. [NOIp 2017]列队

    Description Sylvia 是一个热爱学习的女孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有$n \times m$名学生, ...

  5. NOIP2017D2T3 列队—Treap

    NOIP2017列队 Description Sylvia 是一个热爱学习的女孩子.  前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia所在的方阵中有n × m ...

  6. P2649 - 【NOIP2017】列队

    Description Sylvia 是一个热爱学习的女孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有 n×m 名学生,方阵的行数为 ...

  7. WebForm应用log4net记录错误日志——使用线程列队写入

    我的项目结构如下图: 日志帮助类库需要log4net包:工具—NuGet包管理器—管理解决方案NuGet程序包 线程日志帮助类 FlashLogger.cs 代码 using System; usin ...

  8. 【洛谷P3960】列队题解

    [洛谷P3960]列队题解 题目链接 题意: Sylvia 是一个热爱学习的女孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有 n×m ...

  9. [NOIP]2017列队——旋转treap/非旋转treap

    Sylvia 是一个热爱学习的女孩子.  前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia所在的方阵中有n × m名学生,方阵的行数为 n,列数为m.  为了便 ...

随机推荐

  1. Python中常见的异常处理

    异常和错误 part1:程序中难免出现错误,而错误分成两种 1. 语法错误(这种错误,根本过不了Python解释器的语法检测,必须在程序执行前就改正) # 语法错误示范一 if # 语法错误示范二 d ...

  2. LinqHelper连接数据库配置

    LinqHelper连接数据库配置/// <summary> /// Linq通用数据访问类 /// 指定TDataBase来代替后面要使用的数据上下文(指代) /// where:说明指 ...

  3. 每天一个Linux命令 - 【groupadd】

    [命令]:grouadd [语法]:groupadd [选项]   [参数] [功能介绍]:groupadd 命令勇于创建新的工作组,新工作组的信息将被添加的系统文件中. [选项说明]: -g < ...

  4. jQuery Validate自定义验证方法实现方式

    对应调用函数: ( 可以在内部写js/或者外部引入-我是外部引入的文件 )  validate.expand.js // JavaScript Document //检测手机号是否正确 jQuery. ...

  5. 代码查看import的类是出自哪个jar包的方法(转)

    import java.security.ProtectionDomain; import java.security.CodeSource; public static void main(Stri ...

  6. 【总结整理】关于GrowingIO、友盟、google analysis等数据分析

    作者:纯银V链接:https://www.jianshu.com/p/394ec6c96c98來源:简书简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处. 埋点主要分为四步:第一步是 ...

  7. 459. Repeated Substring Pattern 判断数组是否由重复单元构成

    [抄题]: Given a non-empty string check if it can be constructed by taking a substring of it and append ...

  8. SpringMVC 课程第一天

    SpringMVC第一天   框架课程 1. 课程计划 第一天 1.SpringMVC介绍 2.入门程序 3.SpringMVC架构讲解 a) 框架结构 b) 组件说明 4.SpringMVC整合My ...

  9. mybatis学习笔记 spring与mybatis整合

    转载自http://blog.csdn.net/naruto_Mr/article/details/48239357 1.创建web工程,导入spring依赖包与mybatis依赖包,还需要mybat ...

  10. c语言学习笔记 多级else if 和switch case有什么区别

    ; ) { dosth(); } ) { dosth2(); } else if(opion==) { dosth3(); } else dosth4(); 如果给option的一个值是2的话,那么程 ...