2019牛客暑期多校训练营(第七场) - C - Governing sand - 平衡树
5
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
感觉该出14才对,取前k小写成了取前k大。
5
1 5 4
2 5 3
3 5 2
4 5 1
6 5 5
suf=55 res=0
ans=55
suf=40 res=0
ans=40
suf=30 res=15
ans=40
suf=25 res=25
ans=40
suf=0 res=30
ans=30
30
第四次询问为什么是25?明明要砍完前面所有的9棵树的。擦,curnum加的怎么是c。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2005;
int ch[MAXN][2];
int val[MAXN], dat[MAXN];
ll siz[MAXN], cnt[MAXN];
ll sum[MAXN];
int tot, root;
inline void Init() {
tot = 0;
root = 0;
}
inline int NewNode(int v, ll num) {
ch[++tot][0] = ch[tot][1] = 0;
val[tot] = v, dat[tot] = rand();
siz[tot] = cnt[tot] = num;
sum[tot] = 1ll * val[tot] * cnt[tot];
return tot;
}
inline void PushUp(int id) {
siz[id] = siz[ch[id][0]] + siz[ch[id][1]] + cnt[id];
sum[id] = sum[ch[id][0]] + sum[ch[id][1]] + 1ll * val[id] * cnt[id];
}
inline void Rotate(int &id, int d) {
int temp = ch[id][d ^ 1];
ch[id][d ^ 1] = ch[temp][d];
ch[temp][d] = id;
id = temp;
PushUp(ch[id][d]), PushUp(id);
}
inline void Insert(int &id, int v, ll num) {
if(!id)
id = NewNode(v, num);
else {
if(v == val[id])
cnt[id] += num;
else {
int d = v < val[id] ? 0 : 1;
Insert(ch[id][d], v, num);
if(dat[id] < dat[ch[id][d]])
Rotate(id, d ^ 1);
}
PushUp(id);
}
}
ll GetSum(int id, ll k) {
if(!id||k<=0)
return 0;
else {
if(k >= siz[ch[id][0]] + cnt[id])
return GetSum(ch[id][1], k - siz[ch[id][0]] - cnt[id]) + sum[ch[id][0]] + 1ll * val[id] * cnt[id];
else if(k >= siz[ch[id][0]])
return sum[ch[id][0]] + 1ll * val[id] * (k - siz[ch[id][0]]);
else
return GetSum(ch[id][0], k);
}
}
int n;
struct Tree {
int h, c, p;
bool operator<(const Tree& t)const {
return h < t.h;
}
} tr[100005];
const ll INF = 1e18;
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
while(~scanf("%d", &n)) {
Init();
for(int i = 1; i <= n; ++i)
scanf("%d%d%d", &tr[i].h, &tr[i].c, &tr[i].p);
sort(tr + 1, tr + 1 + n);
ll sufcost = 0;
for(int i = 1; i <= n; ++i)
sufcost += 1ll * tr[i].c * tr[i].p;
ll ans = INF;
for(int i = 1, nxt; i <= n; i = nxt) {
ll curnum = 0;
for(nxt = i + 1; nxt <= n && tr[nxt].h == tr[i].h; ++nxt);
for(int j = i; j < nxt; ++j) {
curnum += tr[j].p;
sufcost -= 1ll * tr[j].c * tr[j].p;
}
ll res=GetSum(root, siz[root] - curnum + 1);
ans = min(ans, sufcost + res);
for(int j = i; j < nxt; ++j)
Insert(root, tr[j].c, tr[j].p);
}
printf("%lld\n", ans);
}
}
2019牛客暑期多校训练营(第七场) - C - Governing sand - 平衡树的更多相关文章
- 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)
题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9: 对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可. 后者mod=1e9,5才 ...
- 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)
链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...
- 2019牛客暑期多校训练营(第一场) B Integration (数学)
链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...
- 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...
- 2019牛客暑期多校训练营(第二场)F.Partition problem
链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...
- 2019牛客暑期多校训练营(第一场)A Equivalent Prefixes(单调栈/二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 Two arrays u and v each with m distinct elements ...
- [状态压缩,折半搜索] 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem
链接:https://ac.nowcoder.com/acm/contest/889/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...
- 2019牛客暑期多校训练营(第二场)J-Subarray(思维)
>传送门< 前言 这题我前前后后看了三遍,每次都是把网上相关的博客和通过代码认真看了再思考,然并卵,最后终于第三遍也就是现在终于看懂了,其实懂了之后发现其实没有那么难,但是的的确确需要思维 ...
- 2019牛客暑期多校训练营(第一场)-A (单调栈)
题目链接:https://ac.nowcoder.com/acm/contest/881/A 题意:给定两个长度均为n的数组a和b,求最大的p使得(a1,ap)和(b1,bp)等价,等价的定义为其任意 ...
- 2019牛客暑期多校训练营(第一场)A - Equivalent Prefixes(单调栈)
题意 给定两个$n$个元素的数组$a,b$,它们的前$p$个元素构成的数组是"等价"的,求$p$的最大值."等价"的意思是在其任意一个子区间内的最小值相同. $ ...
随机推荐
- 1.使用kubeadm安装kubernetes
一.环境准备 所有规划主机(一台master,两台node)均需操作 1.关闭防火墙,selinux [root@node1 ~]# systemctl stop firewalld [root@no ...
- MySQL--缓存的使用注意
几个参数 query_cache_type:为ON时开启,为OFF关闭,为DEMAND时则只有查询语句中有sql cache时才使用缓存 query_cache_size: 缓存的内存空间 query ...
- 快照方式备份MySQL数据库及举例
快照方式备份MySQL数据库及举例 作者: 红豆殺 日期: 2011 年 03 月 17 日发表评论7条评论查看评论 一.创建逻辑卷 依照如下连接的文档创建一个逻辑卷 http://www.178 ...
- Hive 笔试题
Hive 笔试题 考试时间: 姓名:____________ 考试成绩:____________ 考试时长:180 分钟 注意事项: 1. 自主答题,不能参考任何除本试卷外的其它资料. 2. 总成绩共 ...
- heroinfo_set.all 函数
如果是 一对多 关系 即使用 heroinfo_set.all 此时关联字段类型通用,即上边的字段通用,但是需要添加many=True的参数heroinfo_set = serializers.Pr ...
- 51nod1730 涂边
题目描述 题解 八级sb题 显然可以想到状压 枚举当前的宽度\(I\),设\(f[s]\)表示在当前的宽度下选的竖边的状态为s 再设\(g[s1][s2]\)表示状态s1转移到s2的方案数,枚举中间横 ...
- BZOJ 3319: 黑白树 并查集 + 离线 + 思维
Description 给定一棵树,边的颜色为黑或白,初始时全部为白色.维护两个操作: 1.查询u到根路径上的第一条黑色边的标号. 2.将u到v 路径上的所有边的颜色设为黑色. Notice:这 ...
- Shell的简单介绍(一)
shell 的分类 Shell 类别 易学性 可移植性 编辑性 快捷性 Bourne Shell (sh) 容易 好 较差 较差 Korn Shell (ksh) 较难 较好 好 较好 Bourne ...
- SpringMVC开发手册
title: SpringMvc -- 开发手册 date: 2018-11-15 22:14:22 tags: SpringMvc categories: SpringMvc #分类名 type: ...
- php部署调优
转自Laravel学院, 作者:学院君 最近刚好看到一些php.ini优化问题处理. 很多文章都是把配置全部翻译. (内容翻译太多和流程结构写的不是很清晰,看起来也头大.但是建议全部内容看几遍了解一 ...