Persistent Bookcase

题目链接http://codeforces.com/contest/707/problem/D

注释:略。


题解

发现虽然$q\le 10^5$但是网格是$1000\times 1000$的,而且每次操作只会操作一行。

故此我们考虑按照行来搞。

想到每次暴力重新建一行,但是空间开不下,我们用$bitset$即可。

但是我们又面临一个问题,即:回到某一个时刻。

这个很难弄,最简单的支持可持久化的数据结构是主席树,所以我们对行建主席树。

每次修改操作我们都新开一个$bitset$,主席树的第$i$个叶子表示的是第$i$行对应哪一个$bitset$。

时间复杂度为$O(\frac{np}{32} +plogn)$。

代码

#include <bits/stdc++.h>

#define setIO(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout) 

#define N 400010 

using namespace std;

bitset <1010 > b[N], mdl;

char *p1, *p2, buf[100000];

#define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1 ++ )

int rd() {
int x = 0, f = 1;
char c = nc();
while (c < 48) {
if (c == '-')
f = -1;
c = nc();
}
while (c > 47) {
x = (((x << 2) + x) << 1) + (c ^ 48), c = nc();
}
return x * f;
} int cnt = 0, rt[N]; struct Node {
int v;
int ls, rs;
}a[N * 30]; void update(int x, int val, int l, int r, int &p, int pre) {
p = ++cnt;
a[p] = a[pre];
if (l == r) {
a[p].v = val;
return;
}
int mid = (l + r) >> 1;
if (x <= mid) {
update(x, val, l, mid, a[p].ls, a[pre].ls);
}
else {
update(x, val, mid + 1, r, a[p].rs, a[pre].rs);
}
} int query(int x, int l, int r, int p) {
if (l == r) {
return a[p].v;
}
int mid = (l + r) >> 1;
if (x <= mid) {
return query(x, l, mid, a[p].ls);
}
else {
return query(x, mid + 1, r, a[p].rs);
}
} int ans[N]; void build(int l, int r, int &p) {
if (!p) {
p = ++cnt;
}
if (l == r) {
a[p].v = l;
return;
}
int mid = (l + r) >> 1;
build(l, mid, a[p].ls), build(mid + 1, r, a[p].rs);
} int main() {
// setIO("now");
int n = rd(), m = rd(), q = rd();
for (int i = 1; i <= m; i ++ ) {
// 1 << (i - 1)
mdl.set(i);
}
build(1, n, rt[0]);
int tmp = n;
for (int i = 1; i <= q; i ++ ) {
int opt = rd();
ans[i] = ans[i - 1];
if (opt == 1) {
int x = rd(), y = rd();
int z = query(x, 1, n, rt[i - 1]);
b[ ++ tmp] = b[z];
b[tmp].set(y);
ans[i] += b[tmp].count() - b[z].count();
update(x, tmp, 1, n, rt[i], rt[i - 1]);
}
else if (opt == 2) {
int x = rd(), y = rd();
int z = query(x, 1, n, rt[i - 1]);
b[ ++ tmp] = b[z];
b[tmp].reset(y);
ans[i] += b[tmp].count() - b[z].count();
update(x, tmp, 1, n, rt[i], rt[i - 1]);
}
else if (opt == 3) {
int x = rd();
int z = query(x, 1, n, rt[i - 1]);
b[ ++ tmp] = b[z];
b[tmp].flip();
b[tmp] = b[tmp] & mdl;
ans[i] += b[tmp].count() - b[z].count();
update(x, tmp, 1, n, rt[i], rt[i - 1]);
}
else {
int x = rd();
ans[i] = ans[x];
rt[i] = rt[x];
}
printf("%d\n", ans[i]);
}
return 0;
}

[CF707D]Persistent Bookcase_主席树_bitset的更多相关文章

  1. POJ 2104 K-th Number(主席树——附讲解)

    Description You are working for Macrohard company in data structures department. After failing your ...

  2. BZOJ.3489.A simple rmq problem(主席树 Heap)

    题目链接 当时没用markdown写,可能看起来比较难受...可以复制到别的地方看比如DevC++. \(Description\) 给定一个长为n的序列,多次询问[l,r]中最大的只出现一次的数.强 ...

  3. HDU2665Kth number (主席树+离散)

    Give you a sequence and ask you the kth big number of a inteval. InputThe first line is the number o ...

  4. 静态区间第k大(主席树)

    POJ 2104为例(主席树入门题) 思想: 可持久化线段树,也叫作函数式线段树,也叫主席树(高大上). 可持久化数据结构(Persistent data structure):利用函数式编程的思想使 ...

  5. CF707D Persistent Bookcase

    CF707D Persistent Bookcase 洛谷评测传送门 题目描述 Recently in school Alina has learned what are the persistent ...

  6. bzoj3207--Hash+主席树

    题目大意: 给定一个n个数的序列和m个询问(n,m<=100000)和k,每个询问包含k+2个数字:l,r,b[1],b[2]...b[k],要求输出b[1]~b[k]在[l,r]中是否出现. ...

  7. bzoj1901--树状数组套主席树

    树状数组套主席树模板题... 题目大意: 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]--a[ ...

  8. BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]

    3626: [LNOI2014]LCA Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2050  Solved: 817[Submit][Status ...

  9. BZOJ 1146: [CTSC2008]网络管理Network [树上带修改主席树]

    1146: [CTSC2008]网络管理Network Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 3522  Solved: 1041[Submi ...

随机推荐

  1. Til the Cows Come Home ( POJ 2387) (简单最短路 Dijkstra)

    problem Bessie is out in the field and wants to get back to the barn to get as much sleep as possibl ...

  2. Dubbo——基础

    一.分布式基础理论 1.1 什么是分布式系统? “分布式系统是若干独立计算机的集合,这些计算机对于用户来说就像单个相关系统” 分布式系统(distributed system)是建立在网络之上的软件系 ...

  3. Android studio 导入项目报 Error:Cause: peer not authenticated 异常

    修改build.gradle文件(project级的) 一.dependencies { classpath 'com.android.tools.build:gradle:1.0.1'}将class ...

  4. nginx 配置 nodejs 反向代理

    upstream my_nodejs_upstream { server ; keepalive ; } server { listen ; server_name www.my-website.co ...

  5. PG数据库创建并执行存储过程批量插入数据

    记录一下PG数据库创建并执行存储过程批量插入数据的SQL: create or replace function addId() returns boolean AS $BODY$ declare i ...

  6. ubuntu 无法访问windows使用的磁盘

    安装双系统的电脑,正常情况下Ubuntu是可以访问windows下使用的磁盘的, 当出现如下图所示问题时: Windows没有正常关闭. 解决方法: sudo apt-get install ntfs ...

  7. amm与tmpfs和swap

    /dev/shm对Oracle 11g的影响: ORACLE 从11g版本开始,引入了一个自动内存管理(Automatic Memory Management)特性,该特性需要更多的共享内存(/dev ...

  8. [工具]tcping检查开放的端口

    tcping小工具是一款用于tcp监控的软件.tcping小工具可以时刻监控服务器的网络情况,包括ping值和端口状态,可以突破机房和服务器的禁用设置,是一款十分实用的网络分析小工具. 下载地址:ht ...

  9. 阶段5 3.微服务项目【学成在线】_day04 页面静态化_22-页面静态化-静态化测试-静态化程序测试

    测试service内些的静态化的方法 先新建一个测试类 模板的id 放到下拉的静态数据里面 这样这条数据 就是用用的轮播图005这个模板 把这条数据静态化 进入到断点里面.先获取数据模型 获取模板时 ...

  10. CPU 上下文切换及案例分析

    什么是CPU 上下文 我们都知道,Linux是一个多任务操作系统,它远支持大于CPU数量的任务同时运行,当然,这些任务实际上并不是真的在同时运行,而是因为系统在很短时间内,将CPU轮流分配给他们,造成 ...