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 - 平衡树的更多相关文章

  1. 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)

    题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9:  对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可.     后者mod=1e9,5才 ...

  2. 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)

    链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...

  3. 2019牛客暑期多校训练营(第一场) B Integration (数学)

    链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...

  4. 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...

  5. 2019牛客暑期多校训练营(第二场)F.Partition problem

    链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...

  6. 2019牛客暑期多校训练营(第一场)A Equivalent Prefixes(单调栈/二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 Two arrays u and v each with m distinct elements ...

  7. [状态压缩,折半搜索] 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem

    链接:https://ac.nowcoder.com/acm/contest/889/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...

  8. 2019牛客暑期多校训练营(第二场)J-Subarray(思维)

    >传送门< 前言 这题我前前后后看了三遍,每次都是把网上相关的博客和通过代码认真看了再思考,然并卵,最后终于第三遍也就是现在终于看懂了,其实懂了之后发现其实没有那么难,但是的的确确需要思维 ...

  9. 2019牛客暑期多校训练营(第一场)-A (单调栈)

    题目链接:https://ac.nowcoder.com/acm/contest/881/A 题意:给定两个长度均为n的数组a和b,求最大的p使得(a1,ap)和(b1,bp)等价,等价的定义为其任意 ...

  10. 2019牛客暑期多校训练营(第一场)A - Equivalent Prefixes(单调栈)

    题意 给定两个$n$个元素的数组$a,b$,它们的前$p$个元素构成的数组是"等价"的,求$p$的最大值."等价"的意思是在其任意一个子区间内的最小值相同. $ ...

随机推荐

  1. Python图谱

    Reference: https://time.geekbang.org/column/article/94311

  2. Apach Hadoop 与 CDH 区别

    1.Apache Hadoop 不足之处 • 版本管理混乱 • 部署过程繁琐.升级过程复杂 • 兼容性差 • 安全性低 2.Hadoop 发行版 • Apache Hadoop • Cloudera’ ...

  3. 【leetcode】1108. Defanging an IP Address

    题目如下: Given a valid (IPv4) IP address, return a defanged version of that IP address. A defanged IP a ...

  4. 微信小程序 上拉刷新/下拉加载

    小程序项目中上拉刷新下拉加载是比较常见的需求,官方文档也提供了相当友好的API,但是因为API隐藏的比较深,文档描述也比较模糊所以也折腾了一番(官方文档),在此记录一下使用方式 onPullDownR ...

  5. HDU 6620 Just an Old Puzzle

    Time limit 2000 ms Memory limit 262144 kB OS Windows 解题过程 感觉搜索不可行,状态太多了,120步,判断状态是否重复时,即使用std::map也太 ...

  6. es之java搜索文档

    1:搜索文档数据(单个索引) @Test public void getSingleDocument(){ GetResponse response = client.prepareGet(" ...

  7. python中的实例属性和类属性

    在python中,类属性和实例属性的区别是什么? 我认为是作用域的不同,实例对象可以访问类属性,类对象不可以访问实例属性.(类的概念本身就是作用域的概念,你不能让一只猫会飞,猫属于猫类,这一类都不会飞 ...

  8. Matlab 中 函数circshift()的用法

    a = [ ; ; ]; b = [- - -; - - -;- - -]; c = [ ; ; ]; Hist(:,:,) = a; Hist(:,:,) = b; Hist(:,:,) = c; ...

  9. 使用tushare获取股票历史数据和实时分笔数据

    使用tushare获取股票历史数据和实时分笔数据   财经数据接口包tushare的使用(一) Tushare是一款开源免费的金融数据接口包,可以用于获取股票的历史数据.年度季度报表数据.实时分笔数据 ...

  10. URL里的分号';'一定要编码为%3b!!!!

    http://en.wikipedia.org/wiki/Query_string The series of pairs is separated by the ampersand, '&' ...