[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)\)。

源代码:

#include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
using int64=long long;
constexpr int N=3e5+2,SIZE=1e7;
int n,m,q,lim;
std::vector<int64> v[N];
class SegmentTree {
#define mid ((b+e)>>1)
private:
struct Node {
int val,left,right;
};
Node node[SIZE];
int sz,new_node() {
return ++sz;
}
public:
int root[N];
void modify(int &p,const int &b,const int &e,const int &x) {
p=p?:new_node();
node[p].val++;
if(b==e) return;
if(x<=mid) modify(node[p].left,b,mid,x);
if(x>mid) modify(node[p].right,mid+1,e,x);
}
int query(const int &p,const int &b,const int &e,const int &k) {
if(b==e) return b;
const int tmp=mid-b+1-node[node[p].left].val;
return k<=tmp?query(node[p].left,b,mid,k):query(node[p].right,mid+1,e,k-tmp);
}
#undef mid
};
SegmentTree t;
int64 del2(const int &x,const int64 &y) {
const int tmp=t.query(t.root[n+1],1,lim,x);
t.modify(t.root[n+1],1,lim,tmp);
const int64 ans=tmp<=n?(int64)tmp*m:v[n+1][tmp-n-1];
v[n+1].emplace_back(y?:ans);
return ans;
}
int64 del1(const int &x,const int &y) {
const int tmp=t.query(t.root[x],1,lim,y);
t.modify(t.root[x],1,lim,tmp);
const int64 ans=tmp<m?(int64)(x-1)*m+tmp:v[x][tmp-m];
v[x].emplace_back(del2(x,ans));
return ans;
}
int main() {
n=getint(),m=getint(),q=getint();
lim=std::max(n,m)+q;
for(register int i=0;i<q;i++) {
const int x=getint(),y=getint();
printf("%lld\n",y==m?del2(x,0):del1(x,y));
}
return 0;
}

[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. js_微信分享,监听点击分享,分享成功,取消分享,分享失败回调

    2017-06-13 见代码: function weixinShare() { var links = links = "www.youku.com"; common.get_o ...

  2. 【Git/GitHub学习笔记】基本操作——创建仓库,本地、远程同步等

    近日想分享一些文件,但是用度盘又太麻烦了(速度也很恶心).所以突发奇想去研究了下GitHub的仓库,这篇文章也就是一个最最最基础的基本操作.基本实现了可以在GitHub上存储文本信息与代码. 由于我的 ...

  3. SD卡spi读写流程

    SD卡spi读写流程 1.SD卡的命令格式: SD卡的指令由6字节(Byte)组成,如下: Byte1:0 1 x x x x x x(命令号,由指令标志定义,如CMD39为100111即16进制0x ...

  4. 基于ARM 构架(带MMU)的copy_from_user与copy_to_user详细分析

      [转自:http://blog.chinaunix.net/uid-20543672-id-3195249.html]   在学习Linux内核驱动的时候,一开始就会碰到copy_from_use ...

  5. kernel defconfig

    Some defconfig files are placed on below path. Only one *_defconfig can be selected. android/kernel/ ...

  6. rdpClient

    https://github.com/jean343/RPI-GPU-rdpClient https://github.com/Nullstr1ng/MultiRDPClient.NET https: ...

  7. ogre3d环境配置 SDK安装配置及简单事例教程

    ogre3d环境配置 SDK安装配置及简单事例教程 http://www.cr173.com/html/22594_1.html ogre3d环境配置 SDK安装配置及简单事例教程 http://ww ...

  8. tornado当用户输入的URL无效时转入设定的页面

    今天做web的测验..坑爹的要用tornado...作为一个比较新的用的人还不多的东东...查资料好麻烦.. 下面是当用户输入非法 url时, 显示一个自定义 404 页面提示用户,其访问的页面不存在 ...

  9. HIbernate学习笔记5 之 查询

    一.HQL查询 * 按条件查询,条件中写的是属性名,之后在query对象为添加赋值,如: String hql = " from User where uid=?"; Sessio ...

  10. Leetcode 之Length of Last Word(37)

    扫描每个WORD的长度并记录即可. int lengthOfLast(const char *s) { //扫描统计每个word的长度 ; while (*s) { if (*s++ != ' ')/ ...