太神辣 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. C++自定义异常处理

    自定义异常类 class MyException { public: MyException() { } MyException(char* str) { msg = str; } MyExcepti ...

  2. ACM HDU 2044 一只小蜜蜂

    Problem Description 有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数. 其中,蜂房的结构如下所示. Input 输入数据的第一 ...

  3. C++的函数重载 转

    ——每个现象后面都隐藏一个本质,关键在于我们是否去挖掘 写在前面: 函数重载的重要性不言而明,但是你知道C++中函数重载是如何实现的呢(虽然本文谈的是C++中函数重载的实现,但我想其它语言也是类似的) ...

  4. PHP 编译问题PEAR package PHP_Archive not installed的解决

    php 的编译时需要依赖pear package ,目前的问题错误"PEAR package PHP_Archive not installed",已经明显报出这个问题. 因此编译 ...

  5. js 中对象--对象结构(原型链基础解析)

    对于本篇对于如何自定义对象.和对象相关的属性操作不了解的话,可以查我对这两篇博客.了解这两篇可以更容易理解本篇文章 用构造函数创建了一个对象  obj对象的本身创建了两个属性  x=1   ,y=2 ...

  6. CentOS6.5使用本地光盘做yum源 (参考:http://www.jb51.net/os/RedHat/43343.html)

    一.使用光盘做yum源安装软件 mkdir /media/CentOS  #新建CentOS挂载目录 mount -t auto /dev/cdrom /media/CentOS #挂载CentOS光 ...

  7. Android PackageManager packages.xml文件格式

    packages.xml文件存放在/data/system目录下    该文件记录了系统中所有应用程序的包管理相关信息    PmS根据该文件进行包管理的各种操作 标签名称 所包含的值举例 last- ...

  8. Word神器使用

    调整自动换行符的换行起始点: 拖拽下面的三角部分(三角下面的四边形部分不要碰),就可以调整自动编号的换行后的其实点在哪里.

  9. ARCH Linux pacman 包管理器出错总结

    最在使用ARCH的时候使用命令: sudo pacman -S Ruby 终端报错: error: could not open file /var/lib/pacman/sync/apricity- ...

  10. Palindrome

    poj3974:http://poj.org/problem?id=3974 题意:求给定长度最长回文串的长度. 题解:直接套manacher,搞定. #include<iostream> ...