打感叹号处为傻逼处

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long //#define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin); #else #define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std; const int N = 100007; long long ansCost, ansBeauty; struct Treap{
int ch[2], fa, val, beauty, siz;
}t[N];
int root, treeIndex;
inline void Pushup(int rt){
t[rt].siz = t[t[rt].ch[0]].siz + t[t[rt].ch[1]].siz;
}
inline int Ident(int x){
return t[t[x].fa].ch[1] == x;
}
inline void Rotate(int x){
int y = t[x].fa, z = t[y].fa, k = Ident(x);
t[z].ch[Ident(y)] = x, t[x].fa = z; // !
t[y].ch[k] = t[x].ch[k ^ 1], t[t[x].ch[k ^ 1]].fa = y; // !
t[x].ch[k ^ 1] = y, t[y].fa = x;
Pushup(y), Pushup(x);
}
inline void Splay(int x, int pos){
while(t[x].fa != pos){
int y = t[x].fa, z = t[y].fa;
if(z != pos){
Ident(x) == Ident(y) ? Rotate(y) : Rotate(x);
}
Rotate(x);
}
if(!pos) root = x;
}
inline void Find(int x){
int u = root;
if(!u) return;
while(t[u].ch[x > t[u].val] && t[u].val != x) u = t[u].ch[x > t[u].val];
Splay(u, 0);
}
inline void Insert(int x, int beauty){
int u = root, fa = 0;
while(u && t[u].val != x){
fa = u;
u = t[u].ch[x > t[u].val];
}
if(u){
return;
}
else{
ansCost += x;
ansBeauty += beauty;
u = ++treeIndex;
t[u].ch[0] = t[u].ch[1] = 0;
t[u].fa = fa;
t[u].siz = 1;
t[u].val = x;
t[u].beauty = beauty;
if(fa) t[fa].ch[x > t[fa].val] = u; // !
}
Splay(u, 0);
}
inline int Next(int x, int type){
Find(x);
int u = root;
if(t[u].val > x && type) return u;
if(t[u].val < x && !type) return u;
u = t[u].ch[type];
while(t[u].ch[type ^ 1]) u = t[u].ch[type ^ 1];
return u;
}
inline void Delete(int x){
int pre = Next(x, 0), nxt = Next(x, 1);
Splay(pre, 0), Splay(nxt, pre);
t[nxt].ch[0] = 0;
} inline void Calc(int u){
if(!u) return;
if(t[u].val == 2147483647 || t[u].val == -2147483647) return;
Calc(t[u].ch[0]);
Calc(t[u].ch[1]);
ansCost += t[u].val;
ansBeauty += t[u].beauty;
}
int main(){
int opt;
Insert(2147483647, 0);
Insert(-2147483647, 0);
while(scanf("%d", &opt) && opt != -1){
if(opt == 1){
int beauty, x;
io >> beauty >> x;
Insert(x, beauty);
}
else if(opt == 2){
int x = Next(2147483647, 0);
if(t[x].val != -2147483647){
ansCost -= t[x].val;
ansBeauty -= t[x].beauty;
Delete(t[x].val);
}
}
else{
int x = Next(-2147483647, 1);
if(t[x].val != 2147483647){
ansCost -= t[x].val;
ansBeauty -= t[x].beauty;
Delete(t[x].val);
}
}
} //Calc(root); printf("%lld %lld\n", ansBeauty, ansCost); return 0;
}

WA得红红火火(20pts)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long //#define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin); #else #define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std; const int N = 100007; struct Treap{
int ch[2], fa, val, beauty, siz;
}t[N];
int root, treeIndex;
inline void Pushup(int rt){
t[rt].siz = t[t[rt].ch[0]].siz + t[t[rt].ch[1]].siz;
}
inline int Ident(int x){
return t[t[x].fa].ch[1] == x;
}
inline void Rotate(int x){
int y = t[x].fa, z = t[y].fa, k = Ident(x);
t[z].ch[Ident(y)] = x, t[x].fa = z; // !
t[y].ch[k] = t[x].ch[k ^ 1], t[t[x].ch[k ^ 1]].fa = y; // !
t[x].ch[k ^ 1] = y, t[y].fa = x;
Pushup(y), Pushup(x);
}
inline void Splay(int x, int pos){
while(t[x].fa != pos){
int y = t[x].fa, z = t[y].fa;
if(z != pos){
Ident(x) == Ident(y) ? Rotate(y) : Rotate(x);
}
Rotate(x);
}
if(!pos) root = x;
}
inline void Find(int x){
int u = root;
if(!u) return;
while(t[u].ch[x > t[u].val] && t[u].val != x) u = t[u].ch[x > t[u].val];
Splay(u, 0);
}
inline void Insert(int x, int beauty){
int u = root, fa = 0;
while(u && t[u].val != x){
fa = u;
u = t[u].ch[x > t[u].val];
}
if(u) return;
else{
u = ++treeIndex;
t[u].ch[0] = t[u].ch[1] = 0;
t[u].fa = fa;
t[u].siz = 1;
t[u].val = x;
t[u].beauty = beauty;
if(fa) t[fa].ch[x > t[fa].val] = u; // !
}
Splay(u, 0);
}
inline int MAXX(){
int u = root;
while(t[u].ch[1]) u = t[u].ch[1];
return t[u].fa;
}
inline int MINN(){
int u = root;
while(t[u].ch[0]) u = t[u].ch[0];
return t[u].fa;
}
inline int Next(int x, int type){
Find(x);
int u = root;
if(t[u].val > x && type) return u;
if(t[u].val < x && !type) return u;
u = t[u].ch[type];
while(t[u].ch[type ^ 1]) u = t[u].ch[type ^ 1];
return u;
}
inline void Delete(int x){
int pre = Next(x, 0), nxt = Next(x, 1);
Splay(pre, 0), Splay(nxt, pre);
t[nxt].ch[0] = 0;
} long long ansCost, ansBeauty;
inline void Calc(int u){
if(!u) return;
if(t[u].val == 2147483647 || t[u].val == -2147483647) return;
Calc(t[u].ch[0]);
Calc(t[u].ch[1]);
ansCost += t[u].val;
ansBeauty += t[u].beauty;
}
int main(){
int opt;
Insert(2147483647, 0);
Insert(-2147483647, 0);
int tot = 0;
while(scanf("%d", &opt) && opt != -1){
if(opt == 1){
int beauty, x;
io >> beauty >> x;
Insert(x, beauty);
++tot;
}
else if(opt == 2){
if(tot == 0) continue;
int x = MAXX();
Delete(t[x].val);
--tot;
}
else{
if(tot == 0) continue;
int x = MINN();
Delete(t[x].val);
--tot;
}
} Calc(root); printf("%lld %lld\n", ansBeauty, ansCost); return 0;
}

Luogu2073 送花 (平衡树)的更多相关文章

  1. [Luogu2073]送花

    题面 题目背景 小明准备给小红送一束花,以表达他对小红的爱意.他在花店看中了一些花,准备用它们包成花束. 题目描述 这些花都很漂亮,每朵花有一个美丽值W,价格为C. 小明一开始有一个空的花束,他不断地 ...

  2. Splay详解

    平衡树实际很简单的 以下讲解都以Luogu P3369 [模板]普通平衡树为例 我不会带指针的Splay,所以我就写非指针型的Splay Splay是基于二叉查找树(bst)实现的 什么是二叉查找树呢 ...

  3. P2073 送花

    P2073 送花 题目背景 小明准备给小红送一束花,以表达他对小红的爱意.他在花店看中了一些花,准备用它们包成花束. 题目描述 这些花都很漂亮,每朵花有一个美丽值W,价格为C. 小明一开始有一个空的花 ...

  4. [洛谷P2073] 送花

    送花 题目背景 小明准备给小红送一束花,以表达他对小红的爱意.他在花店看中了一些花,准备用它们包成花束. 题目描述 这些花都很漂亮,每朵花有一个美丽值W,价格为C. 小明一开始有一个空的花束,他不断地 ...

  5. [BZOJ3223]Tyvj 1729 文艺平衡树

    [BZOJ3223]Tyvj 1729 文艺平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区 ...

  6. [BZOJ3224]Tyvj 1728 普通平衡树

    [BZOJ3224]Tyvj 1728 普通平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个) ...

  7. BZOJ3223: Tyvj 1729 文艺平衡树 [splay]

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3595  Solved: 2029[Submit][Sta ...

  8. [普通平衡树treap]【学习笔记】

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 9046  Solved: 3840[Submit][Sta ...

  9. BZOJ 3224: Tyvj 1728 普通平衡树

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 9629  Solved: 4091[Submit][Sta ...

随机推荐

  1. Change Buffer 只适用于非唯一索引页?错

    最近在网上看到一些文章里说:"change buffer 只适用于非唯一索引页."其实这个观点是错的,先来看看官方文档对 change buffer 的介绍: 文档地址:https ...

  2. Spring Authorization Server 0.3.0 发布,官方文档正式上线

    基于OAuth2.1的授权服务器Spring Authorization Server 0.3.0今天正式发布,在本次更新中有几大亮点. 文档正式上线 Spring Authorization Ser ...

  3. 使用Rclone将Onedirve挂载到Linux本地

    1. centos挂载onedrive时, 需要安装fuse. # 安装fuse yum -y install fuse 2. 安装完fuse后使用rclone进行挂载 #创建挂载目录 mkdir - ...

  4. Linux系统下运行.sh文件

    在Linux系统下运行.sh文件有两种方法,比如我在root目录下有个vip666.sh文件 #chmod +x *.sh的文件名 #./*.sh的文件名 第一种(这种办法需要用chmod使得文件具备 ...

  5. 2021.05.05【NOIP提高B组】模拟 总结

    T1 给你一棵树,要求增加最少的边权是的从根到每一个叶子的长度相等 不能改变原有的最大长度 这是一个贪心:尽可能往深度小的边增加 先预处理出 \(mx_i\) 表示从 \(i\) 到叶子的最大长度 然 ...

  6. 2020.09.12【NOIP提高组&普及组】模拟赛C组 总结

    总结:这次比赛成绩并不理想,虽然策略得当 \(P.S.\):太多题有多组数据,但是样例只有一个数据 各题题解和改题情况 T1 匹配 题面 描述 给你一个由{a,b-z,A,B-.Z}组成的字符串,我们 ...

  7. 使用Group By子句的时候,一定要记住下面的一些规则

    使用Group By子句的时候,一定要记住下面的一些规则:(1)不能Group By非标量基元类型的列,如不能Group By text,image或bit类型的列(2)Select指定的每一列都应该 ...

  8. .NET中如何在同步代码块中调用异步方法

    更新记录 本文迁移自Panda666原博客,原发布时间:2021年7月2日. 在同步代码块中调用异步方法,方法有很多. 一.对于有返回值的Task 在同步代码块中直接访问 Task 的 Result ...

  9. python亲密数设计

    '''亲密数 (如果a的所有正因子和等于b,b的所有正因子和等于a,因子包括1但不包括本身,且a不等于b,则称a,b为亲密数对.一般通过叠代编程求出相应的亲密数对)'''n = 3000def fun ...

  10. VisonPro · 视觉定位工具包示例

    一.概述 视觉定位工具包一般包含: 1.相机取像: 2.图像九点标定: 3.Mark点粗定位: 4.建立粗定位坐标系: 5.Mark点精定位 6.输出Mark点坐标,角度等信息. 二.分类 1.单特征 ...