[Luogu] 送花
https://www.luogu.org/problemnew/show/2073
自己yy,明显错
#include <bits/stdc++.h> using namespace std;
const int N = 1e5 + ;
const int oo = ; #define gc getchar() struct Node{
int w, b, bef;
}flower[N];
int Ans1, Ans2, opt, W, B, Max, Min;
map<int, bool> Map;
int js = , tot = ; inline int read(){
int x = , f = ; char c = gc;
while(c < '' || c > '') {if(c == '-') f = -; c = gc;}
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x * f;
} int main()
{
Max = ;
Min = ;
opt = read();
while(opt != ) opt = read();
flower[Max].w = read(); flower[Max].b = read(); Map[flower[Max].w] = ;
flower[Max].bef = ;
while(true){
opt = read();
if(opt == -) break;
if(opt == ){
flower[++ js].w = read(); flower[js].b = read();
W = flower[js].w; B = flower[js].b;
if(Map[W]) {js --; continue ;} else tot ++, Map[W] = ;
if(W > flower[Max].w || ! flower[Max].w) {flower[js].bef = Max; Max = js;}
if(W < flower[Min].w || ! flower[Min].w) {flower[js].bef = Min; Min = js;}
}
else if(opt == ){
if(tot <= ) continue;
int ma = Max;
Map[flower[Max].w] = ;
flower[Max].w = ; flower[Max].b = ;
Max = flower[ma].bef; flower[ma].bef = ;
tot --;
}
else {
if(tot <= ) continue;
int mi = Min;
Map[flower[Min].w] = ;
flower[Min].w = ; flower[Min].b = ;
Min = flower[mi].bef; flower[mi].bef = ;
tot --;
}
}
for(int i = ; i<= N - ; i ++)
Ans1 += flower[i].w, Ans2 += flower[i].b;
cout << Ans1 << " " << Ans2; return ;
}
/*
1 2 2
1 7 20
3
1 16 3
1 2 16
2
-1 */
以权值为下标建立权值线段树
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ls jd<<1
#define rs jd<<1|1 using namespace std;
const int N = 1e6 + ;
const int inf = 0x7f7f7f7f; int opt;
bool vis[N];//用来判重;
struct Tree {int l, r, sumw, sumc, Min,Max;} T[N<<]; void build(int jd, int ll, int rr) {
T[jd].l = ll, T[jd].r = rr, T[jd].Min = inf;
if(ll == rr) return;
int m = ll + rr >> ;
build(ls, ll, m), build(rs, m + , rr);
} void update(int jd, int pos, int val1, int val2, int val3, int val4) {
if(T[jd].l == pos && T[jd].r == pos) {
T[jd].sumc = val1;
T[jd].sumw = val2;
T[jd].Min = val3;
T[jd].Max = val4;
return;
}
if(T[ls].r >= pos) update(ls, pos, val1, val2, val3, val4);
else update(rs, pos, val1, val2, val3, val4);
T[jd].sumc = T[ls].sumc + T[rs].sumc;
T[jd].sumw = T[ls].sumw + T[rs].sumw;
T[jd].Min = min(T[ls].Min, T[rs].Min);
T[jd].Max = max(T[ls].Max, T[rs].Max);
} int main()
{
int n = N - ;
build(, , n);
while(scanf("%d", &opt) == && opt != -) {
int w, c;
if(opt == ) {
scanf("%d %d", &w, &c);
if(vis[c]) continue;
update(, c, c, w, c, c);
vis[c] = true;
}
if(opt == ) {
if(T[].Min == inf) continue;//此时无花,下同;
vis[T[].Min] = false, update(, T[].Min, , , inf, );
}
if(opt == ) {
if(T[].Max == ) continue;
vis[T[].Max] = false, update(, T[].Max, , , inf, );
}
}
printf("%d %d",T[].sumw, T[].sumc);
}
[Luogu] 送花的更多相关文章
- 【题解】Luogu P2073 送花
原题传送门 这题需要用到Splay 我们用一棵splay维护金钱 考虑c<=1000000 我们珂以把每种价格现在对应的美丽值存在一个a数组中 这样讲有珂能不太清楚qaq,还是对着操作一个一个讲 ...
- luogu P2073 送花 线段树
思路&心路 一眼认定沙比提 写的比较慢,写了1小时吧 开心的交上去 卧槽,只有20? 不服不服,拿着题解的代码去对拍 Emma,<100没问题 100000数据错了,还只是错了一个数据 ...
- [Luogu 2073] 送花
很容易想到的平衡树,加个维护区间和. 只需要插入和删除操作即可. kth其实都不用的,最小和最大可以从根节点log n一直向左/一直向右跑到叶子节点而求得. 记得每插入完一个点一定要更新区间和!!更新 ...
- Luogu P2073 送花 set
这题...一眼set...但是打了一会儿.. 记录一下每个价格对应的美丽度,顺便充当vis数组,如果美丽度不为0,说明set里已经有了... 删除好说,删*s.begin()和*--s.end()就好 ...
- Luogu P2073 送花
权值线段树的模板题 然而AC后才发现,可以用\(\tt{set}\)水过-- 权值线段树类似于用线段树来实现平衡树的一些操作,代码实现还是比较方便的 #include<iostream> ...
- Luogu 魔法学院杯-第二弹(萌新的第一法blog)
虽然有点久远 还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题 沉迷游戏,伤感情 #include <queue> ...
- luogu p1268 树的重量——构造,真正考验编程能力
题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...
- [luogu P2170] 选学霸(并查集+dp)
题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...
- [luogu P2647] 最大收益(贪心+dp)
题目传送门:https://www.luogu.org/problem/show?pid=2647 题目描述 现在你面前有n个物品,编号分别为1,2,3,--,n.你可以在这当中任意选择任意多个物品. ...
随机推荐
- 匹配script标签及内容js代码的正则表达式
<script>[\s\S]+?</script>
- Springboot笔记01——Springboot简介
一.什么是微服务 在了解Springboot之前,首先我们需要了解一下什么是微服务. 微服务是一种架构风格(服务微化),是martin fowler在2014年提出来的.微服务简单地说就是:一个应用应 ...
- centos7上使用git clone出现问题
centos 7 git clone时出现不支持协议版本的问题 unable to access 'https://github.com/baloonwj/TeamTalk.git/': Peer ...
- 基于【 SpringBoot】一 || QQ授权流程
一.准备工作 1.qq开放平台应用申请,获取APP ID和APP Key 2.qq开放平台配置回调地址 二.服务器端生成授权链接 1.请求地址 https://graph.qq.com/oauth2. ...
- VBA事件(十七)
在VBA中,要手动更改单元格或单元格值范围时,可以触发事件驱动的编程. 更改事件可能会使事情变得更容易,但您可以非常快速地结束一个完整的格式化页面.VBA中有两种事件 - 工作表事件 工作簿事件 工作 ...
- Vue指令之`v-for`和`key`属性
2.2.0+ 的版本里,**当在组件中使用** v-for 时,key 现在是必须的. 当 Vue.js 用 v-for 正在更新已渲染过的元素列表时,它默认用 “**就地复用**” 策略.如果数据项 ...
- MVC-Cache-2.应用程序缓存(Cache:1.输出缓存[2].应用程序缓存)
2.应用数据缓存-Cache 1.引入CacheHelper.cs CacheHelper.cs文件源码在下面; 2.介绍用法: //键 string ips = "键"; //值 ...
- Linux 之 用户、用户组以及权限
拥有者(user),拥有组(group),其他人(other) 由于Linux是一个多人多任务的系统,因此经常会出现同一台机器同时有多个人进行操作,为了考虑每个人的隐私权以及每个人喜好的工作环境,所以 ...
- 【BZOJ2324】[ZJOI2011]营救皮卡丘 给定起点最小权K链可相交覆盖
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef ][]; int main() { i ...
- MySQL进阶12-- 数据类型介绍: 数值型/字符型/日期型-- 正负溢出保护/枚举型/set型/时间戳
/*进阶12 SQL 数据类型介绍 数值型: 整数: Tinyint(1b) < mediumint(3b)<smallint(2b) <int(4b) <bigint(8b) ...