[NOIp2017提高组]列队

题目大意

一个\(n\times m(n,m\le3\times10^5)\)的方阵,每个格子里的人都有一个编号。初始时第\(i\)行第\(j\)列的编号为\((i-1)*m+j\)。

\(q(q\le3\times10^5)\)次事件,每次在\((x,y)\)位置上的人离队。剩下的人向左、向前填补空位,然后离队的人在\((n,m)\)处归队。

求每次离队事件中的人的编号。

思路:

对于每一行\(1\sim m-1\)列建一棵线段树,对于最后一列也建一棵线段树。开同样数量的vector

\((x,y)\)离队时,在第\(x\)棵线段树上找到第\(y\)个未移动的值在vector中的位置,再从最后一列的线段树中找到第\(x\)个未移动的值加入第\(x\)个vector末尾,最后将答案加入最后一列对应vector末尾即可。

时间复杂度\(\mathcal O(q\log n)\)。

源代码:

  1. #include<cstdio>
  2. #include<cctype>
  3. #include<vector>
  4. inline int getint() {
  5. register char ch;
  6. while(!isdigit(ch=getchar()));
  7. register int x=ch^'0';
  8. while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
  9. return x;
  10. }
  11. using int64=long long;
  12. constexpr int N=3e5+2,SIZE=1e7;
  13. int n,m,q,lim;
  14. std::vector<int64> v[N];
  15. class SegmentTree {
  16. #define mid ((b+e)>>1)
  17. private:
  18. struct Node {
  19. int val,left,right;
  20. };
  21. Node node[SIZE];
  22. int sz,new_node() {
  23. return ++sz;
  24. }
  25. public:
  26. int root[N];
  27. void modify(int &p,const int &b,const int &e,const int &x) {
  28. p=p?:new_node();
  29. node[p].val++;
  30. if(b==e) return;
  31. if(x<=mid) modify(node[p].left,b,mid,x);
  32. if(x>mid) modify(node[p].right,mid+1,e,x);
  33. }
  34. int query(const int &p,const int &b,const int &e,const int &k) {
  35. if(b==e) return b;
  36. const int tmp=mid-b+1-node[node[p].left].val;
  37. return k<=tmp?query(node[p].left,b,mid,k):query(node[p].right,mid+1,e,k-tmp);
  38. }
  39. #undef mid
  40. };
  41. SegmentTree t;
  42. int64 del2(const int &x,const int64 &y) {
  43. const int tmp=t.query(t.root[n+1],1,lim,x);
  44. t.modify(t.root[n+1],1,lim,tmp);
  45. const int64 ans=tmp<=n?(int64)tmp*m:v[n+1][tmp-n-1];
  46. v[n+1].emplace_back(y?:ans);
  47. return ans;
  48. }
  49. int64 del1(const int &x,const int &y) {
  50. const int tmp=t.query(t.root[x],1,lim,y);
  51. t.modify(t.root[x],1,lim,tmp);
  52. const int64 ans=tmp<m?(int64)(x-1)*m+tmp:v[x][tmp-m];
  53. v[x].emplace_back(del2(x,ans));
  54. return ans;
  55. }
  56. int main() {
  57. n=getint(),m=getint(),q=getint();
  58. lim=std::max(n,m)+q;
  59. for(register int i=0;i<q;i++) {
  60. const int x=getint(),y=getint();
  61. printf("%lld\n",y==m?del2(x,0):del1(x,y));
  62. }
  63. return 0;
  64. }

[NOIp2017提高组]列队的更多相关文章

  1. [NOIP2017 提高组] 列队

    考虑我们需要维护的是这样一个东西. 即可能变化的只有每一行前\(m - 1\)个,和最后一列. 我们考虑对每一行开一个权值线段树,记录原本序列的第\(x\)个是否被一出,且用一个\(vector\)记 ...

  2. 【题解】NOIP2017 提高组 简要题解

    [题解]NOIP2017 提高组 简要题解 小凯的疑惑(数论) 不讲 时间复杂度 大力模拟 奶酪 并查集模板题 宝藏 最优解一定存在一种构造方法是按照深度一步步生成所有的联通性. 枚举一个根,随后设\ ...

  3. JZOJ 5196. 【NOIP2017提高组模拟7.3】B

    5196. [NOIP2017提高组模拟7.3]B Time Limits: 1000 ms  Memory Limits: 262144 KB  Detailed Limits   Goto Pro ...

  4. JZOJ 5197. 【NOIP2017提高组模拟7.3】C

    5197. [NOIP2017提高组模拟7.3]C Time Limits: 1000 ms  Memory Limits: 262144 KB  Detailed Limits   Goto Pro ...

  5. JZOJ 5195. 【NOIP2017提高组模拟7.3】A

    5195. [NOIP2017提高组模拟7.3]A Time Limits: 1000 ms  Memory Limits: 262144 KB  Detailed Limits   Goto Pro ...

  6. JZOJ 5184. 【NOIP2017提高组模拟6.29】Gift

    5184. [NOIP2017提高组模拟6.29]Gift (Standard IO) Time Limits: 1000 ms  Memory Limits: 262144 KB  Detailed ...

  7. JZOJ 5185. 【NOIP2017提高组模拟6.30】tty's sequence

    5185. [NOIP2017提高组模拟6.30]tty's sequence (Standard IO) Time Limits: 1000 ms  Memory Limits: 262144 KB ...

  8. NOIP2017提高组 模拟赛15(总结)

    NOIP2017提高组 模拟赛15(总结) 第一题 讨厌整除的小明 [题目描述] 小明作为一个数学迷,总会出于数字的一些性质喜欢上某个数字,然而当他喜欢数字k的时候,却十分讨厌那些能够整除k而比k小的 ...

  9. NOIP2017提高组 模拟赛13(总结)

    NOIP2017提高组 模拟赛13(总结) 第一题 函数 [题目描述] [输入格式] 三个整数. 1≤t<10^9+7,2≤l≤r≤5*10^6 [输出格式] 一个整数. [输出样例] 2 2 ...

随机推荐

  1. python作业三级菜单day1(第一周)

    一.作业需求: 1. 运行程序输出第一级菜单 2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单 3. 菜单数据保存在文件中 4. 让用户选择是否要退出 5. 有返回上一级菜单的功能 二三级菜单文 ...

  2. hdu 3790 最短路径问题(双重权值,dijkstra算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790 题目大意:题意明了,输出最短路径及其花费. 需要注意的几点:(1)当最短路径相同时,输出最小花费 ...

  3. C++学习之路(八):关于C++提供的强制类型转换

    C语言中提供了旧式的强制类型转换方法.比如: int a  =1; char *p = (char *)&a; 上述将a的地址单元强制转换为char类型的指针.这里暂且不说上述转换结果是否合理 ...

  4. Linux汇编教程01: 基本知识

    在我们开始学习Linux汇编之前,需要简单的了解一下计算机的体系结构.我们不需要特别深入的了解,理解了一些基本概念对与我们理解程序会很有帮助.现在计算机的结构体系都是采用冯诺依曼体系结构的基础上发展过 ...

  5. xxx_initcall相关知识

    参考文件include/linux/init.h /* * Early initcalls run before initializing SMP. * * Only for built-in cod ...

  6. HDU 6115 Factory LCA,暴力

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6115 题意:中文题面 分析:直接维护LCA,然后暴力枚举集合维护答案即可. #include < ...

  7. SAE如何使用Git

    了解Git及远程git仓库 请先看博文<Git入门及上传项目到github中>,弄懂了之后我相信我下面说的就相当于废话了. SAE的git远程仓库就相当于github. 向SAE的远程仓库 ...

  8. git学习笔记三

    1.每个分支的历史版本维护信息位置是.git/logs/refs/heads/master,这个位置的信息是文本文件,不是引用. harvey@harvey-Virtual-Machine:~/dem ...

  9. [Jsoi2011]柠檬

    Description Flute 很喜欢柠檬.它准备了一串用树枝串起来的贝壳,打算用一种魔法把贝壳变成柠檬.贝壳一共有 N (1 ≤ N ≤ 100,000) 只,按顺序串在树枝上.为了方便,我们从 ...

  10. django 上传图片、使用PIL制作缩略图并保存到sea的storage

    上传图片解析: SAE的设置指引如下: 处理用户上传文件 在setttings.py中添加以下配置. # 修改上传时文件在内存中可以存放的最大size为10m FILE_UPLOAD_MAX_MEMO ...