Luogu 4602 [CTSC2018]混合果汁
BZOJ 5343
福利题。
对于每一个询问可以二分$d$,然后把满足条件的果汁按照$p$从小到大排序贪心地取$L$升看看满不满足价格的条件。
那么按照$p$建立权值主席树,$chk$的时候在主席树上走一走算出价格即可。
当然也可以整体二分。
时间复杂度都是$O(nlog^2n)$。
Code:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll; const int N = 1e5 + ;
const ll inf = 1LL << ; int n, qn, tot = , pos[N], buc[N]; struct Item {
int d, cost, lim; inline Item(int D = , int Cost = , int Lim = ) {
d = D, cost = Cost, lim = Lim;
} friend bool operator < (const Item x, const Item y) {
return x.d > y.d;
} } a[N]; namespace Fread {
const int L = << ; char buffer[L], *S, *T; inline char Getchar() {
if(S == T) {
T = (S = buffer) + fread(buffer, , L, stdin);
if(S == T) return EOF;
}
return *S++;
} template <class T>
inline void read(T &X) {
char ch; T op = ;
for(ch = Getchar(); ch > '' || ch < ''; ch = Getchar())
if(ch == '-') op = -;
for(X = ; ch >= '' && ch <= ''; ch = Getchar())
X = (X << ) + (X << ) + ch - '';
X *= op;
} } using namespace Fread; namespace Fwrite {
const int L = << ; char buf[L], *pp = buf; void Putchar(const char c) {
if(pp - buf == L) fwrite(buf, , L, stdout), pp = buf;
*pp++ = c;
} template<typename T>
void print(T x) {
if(x < ) {
Putchar('-');
x = -x;
}
if(x > ) print(x / );
Putchar(x % + '');
} void fsh() {
fwrite(buf, , pp - buf, stdout);
pp = buf;
} template <typename T>
inline void write(T x, char ch = ) {
print(x);
if (ch != ) Putchar(ch);
fsh();
} } using namespace Fwrite; namespace SegT {
struct Node {
int lc, rc;
ll sum, cnt;
} s[N * ]; int root[N], nodeCnt = ; #define lc(p) s[p].lc
#define rc(p) s[p].rc
#define sum(p) s[p].sum
#define cnt(p) s[p].cnt
#define mid ((l + r) >> 1) void ins(int &p, int l, int r, int x, int v, int pre) {
s[p = ++nodeCnt] = s[pre];
cnt(p) += 1LL * v, sum(p) += 1LL * v * buc[x];
if (l == r) return; if (x <= mid) ins(lc(p), l, mid, x, v, lc(pre));
else ins(rc(p), mid + , r, x, v, rc(pre));
} ll query(int p, int l, int r, ll cur) {
if (l == r) return cur > cnt(p) ? inf : cur * buc[l];
ll now = cnt(lc(p));
if (cur <= now) return query(lc(p), l, mid, cur);
else return sum(lc(p)) + query(rc(p), mid + , r, cur - now);
} #undef mid } using namespace SegT; inline bool chk(int mid, ll x, ll y) {
if (cnt(root[pos[a[mid].d]]) < y) return ;
ll now = query(root[pos[a[mid].d]], , tot, y);
return now <= x;
} int main() {
#ifndef ONLINE_JUDGE
freopen("Sample.txt", "r", stdin);
#endif read(n), read(qn);
for (int i = ; i <= n; i++) {
read(a[i].d), read(a[i].cost), read(a[i].lim);
buc[++tot] = a[i].cost;
} sort(buc + , buc + + tot);
tot = unique(buc + , buc + + tot) - buc - ;
for (int i = ; i <= n; i++)
a[i].cost = lower_bound(buc + , buc + + tot, a[i].cost) - buc; sort(a + , a + + n);
for (int i = ; i <= n; i++) {
ins(root[i], , tot, a[i].cost, a[i].lim, root[i - ]);
pos[a[i].d] = i;
} for (ll x, y; qn--; ) {
read(x), read(y);
/* if (y > cnt(root[n])) {
puts("-1");
continue;
} */ int ln = , rn = n, mid, res = ;
for (; ln <= rn; ) {
mid = (ln + rn) / ;
if (chk(mid, x, y)) rn = mid - , res = mid;
else ln = mid + ;
} write((!res) ? - : a[res].d, '\n');
} return ;
}
Luogu 4602 [CTSC2018]混合果汁的更多相关文章
- Luogu P4062 [CTSC2018]混合果汁 (主席树)
二分$d$, 转为判断判断是否能取到$Lj$升, 再可持久化一下就好了 #include <iostream> #include <algorithm> #include &l ...
- Luogu P4602 [CTSC2018]混合果汁
题目 把果汁按美味度降序排序,以单价为下标插入主席树,记录每个节点的\(sum\)果汁升数和\(val\)果汁总价. 每次询问二分最小美味度,查询美味度大于等于\(mid\)的总体积为\(L\)的最低 ...
- BZOJ5343[Ctsc2018]混合果汁——主席树+二分答案
题目链接: CTSC2018混合果汁 显然如果美味度高的合法那么美味度低的一定合法,因为美味度低的可选方案包含美味度高的可选方案. 那么我们二分一个美味度作为答案然后考虑如何验证? 选择时显然要贪心的 ...
- [Bzoj]5343: [Ctsc2018]混合果汁
5343: [Ctsc2018]混合果汁 题目描述 小 R 热衷于做黑暗料理,尤其是混合果汁. 商店里有 \(n\) 种果汁,编号为 \(0,1,\cdots,n-1\) .\(i\) 号果汁的美味度 ...
- BZOJ_5343_[Ctsc2018]混合果汁_二分答案+主席树
BZOJ_5343_[Ctsc2018]混合果汁_二分答案+主席树 题意:给出每个果汁的价格p,美味度d,最多能放的体积l.定义果汁混合后的美味度为果汁的美味度的最小值. m次询问,要求花费不大于g, ...
- BZOJ5343 & 洛谷4602 & LOJ2555:[CTSC2018]混合果汁——题解
https://www.luogu.org/problemnew/show/P4602 https://loj.ac/problem/2555 https://www.lydsy.com/JudgeO ...
- [bzoj5343][Ctsc2018]混合果汁_二分答案_主席树
混合果汁 bzoj-5343 Ctsc-2018 题目大意:给定$n$中果汁,第$i$种果汁的美味度为$d_i$,每升价格为$p_i$,每次最多添加$l_i$升.现在要求用这$n$中果汁调配出$m$杯 ...
- [CTSC2018]混合果汁
题目连接:https://www.luogu.org/problemnew/show/P4602 因为题中说是让最小值最大,所以自然想到二分答案.对于每一个二分的值,判断是否合法,若合法,在右区间二分 ...
- 洛谷P4602 [CTSC2018]混合果汁(主席树)
题目描述 小 R 热衷于做黑暗料理,尤其是混合果汁. 商店里有 nn 种果汁,编号为 0,1,\cdots,n-10,1,⋯,n−1 . ii 号果汁的美味度是 d_idi ,每升价格为 p_ipi ...
随机推荐
- [linux]df 磁盘100%Used告警,du显示目录状态良好的故障排查
1.回顾: 某在线主机深夜连续接到告警系统的disk Used 超限告警. 登陆主机查看却遇到了困惑:在检查磁盘使用量 df –h 出来的磁盘使用量确实和告警信息一样,已经被100%占用,但是查看目录 ...
- python3 openpyxl基本操作
#coding:utf-8 import xlrd import xlwt # 读写2007 excel import openpyxl import sys #读取设备sn # def readSN ...
- 《DSP using MATLAB》Problem 3.2
1.用x1序列的DTFT来表示x2序列的DTFT 2.代码: %% ------------------------------------------------------------------ ...
- 在Mac上安装anaconda,在命令行中输入conda,提示不是有效命令的解决办法
原链接:https://stackoverflow.com/questions/18675907/how-to-run-conda
- 把CDLinux制作成U盘启动
因为用下了CDlinux,本来想在虚拟机上运行的.发现虚拟机跑的时候无法识别集成的笔记本网卡,坑爹啊.后来想刻碟的,发现手头上还没有现成的东西,光驱是只读的,又要用到光驱,于是想到了了用U盘,正好手上 ...
- BeagleBoneBlack Linux开发相关链接收藏
ubuntu挂载vdi文件 官方linux代码地址 官方devicetree代码地址 [转]使用BBB的device tree和cape(重新整理版) iio: input: ti_am335x_ad ...
- 使用SharpZip压缩与解压缩
使用SharpZip压缩与解压缩 编写人:左丘文 2015-4-11 大家在做项目时,相信会经常性的会遇到要对数据流或dataset byte[] 或文件进行压缩和解压缩,比如:利用webservic ...
- ruby的代码风格
http://stylesror.github.io/ 大部分同意,有小部分,不敢苟同.
- appium -ios-安卓 获取元素时 配置参数的方法
iPhone_5S{ "automationName": "XCUITest", "platformName": "iOS&quo ...
- Bootstrap-CL:面板
ylbtech-Bootstrap-CL:面板 1.返回顶部 1. Bootstrap 面板(Panels) 本章将讲解 Bootstrap 面板(Panels).面板组件用于把 DOM 组件插入到一 ...