太神辣 treap的随机键值竟然能派上用场。。

要用不旋转的treap来进行维护区间信息

 #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream> using namespace std; template<typename Q> Q &read(Q &x) {
static char c, f;
for(f = ; c = getchar(), !isdigit(c); ) if(c == '-') f = ;
for(x = ; isdigit(c); c = getchar()) x = x * + c - '';
if(f) x = -x; return x;
}
template<typename Q> Q read() {
static Q x; read(x); return x;
} typedef long long LL;
const int N = + ; LL S(LL x) {
return x * (x + ) >> ;
} struct Node *null, *pis;
struct Node {
int sz, h, tag;
LL ans;
Node *ch[]; Node() {}
Node(int h) : h(h) {
sz = , ans = tag = ;
ch[] = ch[] = null;
} void add(int d) {
if(this == null) return;
h += d, tag += d;
} void maintain() {
sz = ch[]->sz + ch[]->sz + ;
ans = ;
for(int c = ; c < ; c++) {
ans += ch[c]->ans + S(ch[c]->sz) * (ch[c]->h - h);
}
} void down() {
ch[]->add(tag);
ch[]->add(tag);
tag = ;
} void *operator new(size_t) {
return pis++;
}
}pool[N]; Node *merge(Node *l, Node *r) {
if(l == null) return r;
if(r == null) return l;
if(l->h < r->h) {
l->down();
l->ch[] = merge(l->ch[], r);
return l->maintain(), l;
}else {
r->down();
r->ch[] = merge(l, r->ch[]);
return r->maintain(), r;
}
} typedef pair<Node *, Node *> pnn;
pnn split(Node *o, int k) {
if(o == null) return pnn(null, null);
pnn res; o->down();
if(o->ch[]->sz >= k) {
res = split(o->ch[], k);
o->ch[] = res.second;
res.second = o;
}else {
res = split(o->ch[], k - o->ch[]->sz - );
o->ch[] = res.first;
res.first = o;
}
return o->maintain(), res;
} pair<int, int> p[ + ]; int main() {
#ifdef DEBUG
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif int r, c, n;
scanf("%d%d%d", &r, &c, &n);
for(int i = ; i < n; i++) {
scanf("%d%d", &p[i].first, &p[i].second);
}
sort(p, p + n); pis = pool, null = new Node();
null->sz = ;
Node *root = null;
for(int i = ; i <= c; i++) {
root = merge(root, new Node());
} LL ans = S(r) * S(c);
for(int i = , j = ; i <= r; i++) {
root->add();
while(j < n && p[j].first == i) {
int x = p[j++].second;
pnn r1 = split(root, x - );
pnn r2 = split(r1.second, );
r2.first->h = ;
root = merge(merge(r1.first, r2.first), r2.second);
}
ans -= root->ans + S(root->sz) * root->h;
}
cout << ans << endl; return ;
}

fhq treap

谁说一定要用fhq treap?

用普通的treap就好了 而且常数小!

注意建树最好$O(n)$建一下,每次insert有可能退化成$O(n^2)$的。

 #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream> using namespace std; template<typename Q> Q &read(Q &x) {
static char c, f;
for(f = ; c = getchar(), !isdigit(c); ) if(c == '-') f = ;
for(x = ; isdigit(c); c = getchar()) x = x * + c - '';
if(f) x = -x; return x;
}
template<typename Q> Q read() {
static Q x; read(x); return x;
} typedef long long LL;
const int N = + ; LL S(LL x) {
return x * (x + ) >> ;
} struct Node *null, *pis, *bin[N];
int top;
struct Node {
int sz, v, h, tag;
LL ans;
Node *ch[]; Node() {}
Node(int v, int h) : v(v), h(h) {
sz = , ans = tag = ;
ch[] = ch[] = null;
} void add(int d) {
if(this == null) return;
h += d, tag += d;
} void maintain() {
sz = ch[]->sz + ch[]->sz + ;
ans = ;
for(int c = ; c < ; c++) {
ans += ch[c]->ans + S(ch[c]->sz) * (ch[c]->h - h);
}
} void down() {
ch[]->add(tag);
ch[]->add(tag);
tag = ;
} void *operator new(size_t) {
return top ? bin[--top] : pis++;
} void operator delete(void *p) {
bin[top++] = (Node *) p;
} int cmp(int x) const {
if(x == v) return -;
return x < v ? : ;
}
}pool[N]; void rotate(Node *&o, int d) {
Node *t = o->ch[d];
o->ch[d] = t->ch[d ^ ];
t->ch[d ^ ] = o;
o->maintain();
(o = t)->maintain();
} void modify(Node *&o, int x, int w) {
o->down();
int d = o->cmp(x);
if(d == -) return o->h = w, o->maintain(), void();
modify(o->ch[d], x, w);
if(o->ch[d]->h < o->h) rotate(o, d);
else o->maintain();
} pair<int, int> p[ + ]; void build(Node *&o, int l, int r) {
if(l > r) return;
int mid = (l + r) >> ;
o = new Node(mid, );
build(o->ch[], l, mid - );
build(o->ch[], mid + , r);
o->maintain();
} int main() {
#ifdef DEBUG
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif int r, c, n;
scanf("%d%d%d", &r, &c, &n);
for(int i = ; i < n; i++) {
read(p[i].first), read(p[i].second);
}
sort(p, p + n); pis = pool, null = new Node(, );
null->sz = ;
Node *root;
build(root, , c); LL ans = S(r) * S(c);
for(int i = , j = ; i <= r; i++) {
root->add();
while(j < n && p[j].first == i) {
modify(root, p[j++].second, );
}
ans -= root->ans + S(root->sz) * root->h;
}
cout << ans << endl; return ;
}

普通treap

bzoj2658: [Zjoi2012]小蓝的好友(mrx)的更多相关文章

  1. 【BZOJ2658】[Zjoi2012]小蓝的好友(mrx) 平衡树维护笛卡尔树+扫描线

    [BZOJ2658][Zjoi2012]小蓝的好友(mrx) Description 终于到达了这次选拔赛的最后一题,想必你已经厌倦了小蓝和小白的故事,为了回馈各位比赛选手,此题的主角是贯穿这次比赛的 ...

  2. 【BZOJ2658】[Zjoi2012]小蓝的好友(mrx) (扫描线,平衡树,模拟)

    题面 终于到达了这次选拔赛的最后一题,想必你已经厌倦了小蓝和小白的故事,为了回馈各位比赛选手,此题的主角是贯穿这次比赛的关键人物--小蓝的好友. 在帮小蓝确定了旅游路线后,小蓝的好友也不会浪费这个难得 ...

  3. @bzoj - 2658@ [Zjoi2012]小蓝的好友(mrx)

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 终于到达了这次选拔赛的最后一题,想必你已经厌倦了小蓝和小白的故事 ...

  4. BZOJ2658 ZJOI2012 小蓝的好友(treap)

    显然转化为求不包含关键点的矩形个数.考虑暴力,枚举矩形下边界,求出该行每个位置对应的最低障碍点高度,对其建笛卡尔树,答案即为Σhi*(slson+1)*(srson+1),即考虑跨过该位置的矩形个数. ...

  5. 洛谷 P2611 [ZJOI2012]小蓝的好友 解题报告

    P2611 [ZJOI2012]小蓝的好友 题目描述 终于到达了这次选拔赛的最后一题,想必你已经厌倦了小蓝和小白的故事,为了回馈各位比赛选手,此题的主角是贯穿这次比赛的关键人物--小蓝的好友. 在帮小 ...

  6. [ZJOI2012]小蓝的好友

    https://www.luogu.org/problemnew/show/P2611 题解 \(n\times m\)肯定过不去.. 我们把给定的点看做障碍点,考虑先补集转化为求全空矩阵. 然后我们 ...

  7. BZOJ 2658 小蓝的好友

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2658 题意:给出一个n*m的格子.某些格子中有障碍.求包含至少一个障碍的矩形有多少 ...

  8. P2611-[ZJOI2012]小蓝的好友【Treap,扫描线】

    正题 题目链接:https://www.luogu.com.cn/problem/P2611 题目大意 \(r*c\)的网格上有\(n\)个标记点,然后求有多少个矩形包含至少一个标记点. \(1\le ...

  9. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

随机推荐

  1. SGU 246. Black & White(数论)

    题意: 有2*n-1个黑色和白色的珠子组成的环形项链,求至少需要多少颗黑色珠子才能使任意排列的项链中都存在两个黑珠间有n个珠子. (2*n-1<=2^31-1); Solution: 先分析n= ...

  2. 使用微信api接口开发的框架

    <?php/** * 微信公众平台API */class WeixinChat{ private $token; private $appid; private $appsecret; priv ...

  3. sharepoint读取站点下列表

    前言 还是自己做着练习,也算对这个代码的一个认识吧.东西没什么. 过程 这是后台的一下代码,其中我会对标注的地方解释一下. 标注1:是获取的列表中的某一个列名,标注2:是这个列表下的数据列的名称,我们 ...

  4. php5.4安装ecshopphp5.4问题及解决

    includes/cls_template.php line422 将 $tag_sel = array_shift(explode(" ", $tag)); 这句话拆开为两句. $tag_exp = ...

  5. 帝国cms灵动标签调用tags

    这个语法用来调用[指定分类][指定条件]的所有tags [e:loop={"select * from [!db.pre!]enewstags order by num desc limit ...

  6. Ubuntu 下 安装QQ 截图工具

    1.由于ubuntu下是没有dll动态链接库的,所以需要安装一个软件wine,有这个东西之后,以后在ubuntu下就可以运行exe文件了.(wine是一款优秀的Linux系统平台下的模拟器软件,用来将 ...

  7. X-window

    X-Window(也常称为X11或X)系统是一种以位图方式显示的软件视窗系统,最初是1984年麻省理工学院的研究,之后变成UNIX.类UNIX. 以及OpenVMS等操作系统所一致适用的标准化软件工具 ...

  8. mac os x 10.9.1 安装 Homebrew软件包管理工具及brew安装maven3.1.1

    Mac OSX上的软件包管理工具,安装软件或者卸载软件. 打开终端输入(如不行,可参考homebrew官网): ruby -e "$(curl -fsSL https://raw.githu ...

  9. Python: 设计模式 之 工厂模式例(1)

    #!/usr/bin/env python #coding=utf-8 # # 工厂模式一例 # 版权所有 2014 yao_yu (http://blog.csdn.net/yao_yu_126) ...

  10. LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法

    LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...