Luogu2073 送花 (平衡树)
2024-09-05 17:12:08
打感叹号处为傻逼处
#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;
}
最新文章
- Java ArrayList和Vector、LinkedList与ArrayList、数组(Array)和列表集合(ArrayList)的区别
- 在Angular1.X中使用CSS Modules
- SQL 高效分页查询
- 无法更新 EntitySet“SoreInfo_Table”,因为它有一个 DefiningQuery,而 <;ModificationFunctionMapping>; 元素中没有支持当前操作的 <;InsertFunction>; 元素。
- 【转】 使用Redis的Pub/Sub来实现类似于JMS的消息持久化
- 工作流学习——Activiti流程变量五步曲 (zhuan)
- IT公司100题-4-在二元树中找出和为某一值的所有路径
- iOS页面间传值的方式 (Delegate/NSNotification/Block/NSUserDefault/单例)
- JS中常用的几种时间格式处理-【笔记整理】
- python数据结构链表之单向链表
- 201521123051《Java程序设计》第七周学习总结
- windows环境Caffe安装配置步骤(无GPU)及mnist训练
- 使用dom4j 解析xml文件
- <;Android基础>; (六) 数据存储 Part 3 SQLite数据库存储
- C#控制台或应用程序中两个多个Main()方法的设置
- Mybatis Annotation使用小结
- 【LeetCode】区间合并
- poj1093
- sqlite的一个Unable to Open database file的坑爹错误
- [APIO2018] Duathlon 铁人两项 圆方树,DP
热门文章
- Hadoop: 单词计数(Word Count)的MapReduce实现
- Link-Cut-Tree(1)
- vue大型电商项目尚品汇(前台篇)day05
- Python 多道技术以及进程、线程和协程
- c++ 平衡树
- 【Spring】事务的执行原理(二)
- Linux文本查看工具
- ShardingSphere-proxy-5.0.0企业级分库分表、读写分离、负载均衡、雪花算法、取模算法整合(八)
- C语言输出九九乘法表
- Unsupervised Person Re-identification by Soft Multilabel Learning