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 ;
}

[Luogu] 列队的更多相关文章

  1. [LUOGU] [NOIP2017] P3960 列队

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

  2. luogu 3960 列队

    noip2017 D2T3 列队 某zz选手当时直接放弃了写了50还写错了 题目大意: 有一个n行m列的方阵,第i行j列的点编号为(i-1)m+j 每次把第x行y列的点拿出来,然后把这一行它之后的点都 ...

  3. Luogu P3960 列队(动态开点线段树)

    P3960 列队 题意 题目描述 Sylvia 是一个热爱学习的女孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia所在的方阵中有\(n \times m ...

  4. luogu P3960 列队

    传送门 因为\(Splay\)可以\(O(logn)\)维护区间,所以直接对每一行维护第一个元素到倒数第二个元素的\(Splay\),最后一列维护一个\(Splay\),每次把选出来的点删掉,然后把那 ...

  5. Luogu 3960 [NOIP2017] 列队 - splay|线段树

    题解 是我从来没有做过的裂点splay... 看的时候还是很懵逼的QAQ. 把最后一列的$n$个数放在一个平衡树中, 有 $n$ 个点 剩下的$n$行数, 每行都开一个平衡树,开始时每棵树中仅有$1$ ...

  6. Luogu P3960 列队 线段树

    题面 线段树入门题. 我们考虑线段树来维护这个矩阵. 首先我们先定n+1棵线段树前n棵维护每行前m-1个同学中没有离队过的同学,还有一棵维护第m列中没有离队过的同学.再定n+1棵线段树前n棵线段树维护 ...

  7. LUOGU P3960 列队 (noip2017 day2T3)

    传送门 解题思路 记得当时考试我还是个孩子,啥也不会QAQ.现在回头写,用动态开点的线段树,在每行和最后一列开线段树,然后对于每次询问,把x行y列的删去,然后再把x行m列的元素加入x行这个线段树,然后 ...

  8. [luogu P3960] [noip2017 d2t3] 队列

    [luogu P3960] [noip2017 d2t3] 队列 题目描述 Sylvia 是一个热爱学习的女♂孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Syl ...

  9. Luogu 魔法学院杯-第二弹(萌新的第一法blog)

    虽然有点久远  还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题  沉迷游戏,伤感情 #include <queue> ...

随机推荐

  1. Ural 1248 Sequence Sum 题解

    目录 Ural 1248 Sequence Sum 题解 题意 题解 程序 Ural 1248 Sequence Sum 题解 题意 给定\(n\)个用科学计数法表示的实数\((10^{-100}\s ...

  2. Thinking In Java 4th Chap5 初始化和清理

    类的构造器名必须与类名一致,且无返回类型,通过参数类型的不同(即使顺序不同也行)可以重载构造器,也可以以此技巧重载方法 this关键字:表示对“调用方法的那个对象的引用”,也可将当前对象传递给其他方法 ...

  3. Python基础总结之第七天开始【认识函数的参数以及返回】(新手可相互督促)

    周日的早上,吃的饱饱,刷刷抖音,开始学习新一天的知识了~~~ 函数的参数: 昨天的笔记中,我们已经使用了参数,在案例中的name和sex 就是参数. 一般的函数都是有参数的,函数的参数都是放在函数定义 ...

  4. Python字符串常用的方法——真心觉得比java,c好用

    # Strings have many methods wo can use rand_string=" life is a beautiful struggle " print( ...

  5. Introduction to Deep Learning Algorithms

    Introduction to Deep Learning Algorithms See the following article for a recent survey of deep learn ...

  6. 【web安全】浅谈web安全之XSS

    XSS定义 XSS, 即为(Cross Site Scripting), 中文名为跨站脚本, 是发生在目标用户的浏览器层面上的,当渲染DOM树的过程成发生了不在预期内执行的JS代码时,就发生了XSS攻 ...

  7. ‘mysql’不是内部或外部命令,也不是可运行的程序--解决方法

    一.场景 在cmd命令窗口下操作mysql时,提示mysql不是内部或外部命令,也不是可运行的程序. 二.原因 有3种原因: 1.没有装mysql 2.没有配置mysql环境变量 3.cmd命令窗口没 ...

  8. Newtonsoft.Json基本用法

    1.将一个 Object 序列化成 JSON: DataSet detail = sqlDB.GetDataSet(string.Format("select * from student ...

  9. MySQL开启binlog无法启动ct 10 21:27:31 postfix/pickup[4801]: warning: 6BD991A0039: message has been queue

    1 详细异常 ct 10 21:27:31 postfix/pickup[4801]: warning: 6BD991A0039: message has been queue Oct 10 21:2 ...

  10. 【异常】azkaban.executor.ExecutorManagerException: No active executors found

    1 azkaban启动异常 没有找到活动的executors,需在MySQL数据库里设置端口为12321的executors表的active为1   update azkaban.executors ...