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]混合果汁的更多相关文章

  1. Luogu P4062 [CTSC2018]混合果汁 (主席树)

    二分$d$, 转为判断判断是否能取到$Lj$升, 再可持久化一下就好了 #include <iostream> #include <algorithm> #include &l ...

  2. Luogu P4602 [CTSC2018]混合果汁

    题目 把果汁按美味度降序排序,以单价为下标插入主席树,记录每个节点的\(sum\)果汁升数和\(val\)果汁总价. 每次询问二分最小美味度,查询美味度大于等于\(mid\)的总体积为\(L\)的最低 ...

  3. BZOJ5343[Ctsc2018]混合果汁——主席树+二分答案

    题目链接: CTSC2018混合果汁 显然如果美味度高的合法那么美味度低的一定合法,因为美味度低的可选方案包含美味度高的可选方案. 那么我们二分一个美味度作为答案然后考虑如何验证? 选择时显然要贪心的 ...

  4. [Bzoj]5343: [Ctsc2018]混合果汁

    5343: [Ctsc2018]混合果汁 题目描述 小 R 热衷于做黑暗料理,尤其是混合果汁. 商店里有 \(n\) 种果汁,编号为 \(0,1,\cdots,n-1\) .\(i\) 号果汁的美味度 ...

  5. BZOJ_5343_[Ctsc2018]混合果汁_二分答案+主席树

    BZOJ_5343_[Ctsc2018]混合果汁_二分答案+主席树 题意:给出每个果汁的价格p,美味度d,最多能放的体积l.定义果汁混合后的美味度为果汁的美味度的最小值. m次询问,要求花费不大于g, ...

  6. BZOJ5343 & 洛谷4602 & LOJ2555:[CTSC2018]混合果汁——题解

    https://www.luogu.org/problemnew/show/P4602 https://loj.ac/problem/2555 https://www.lydsy.com/JudgeO ...

  7. [bzoj5343][Ctsc2018]混合果汁_二分答案_主席树

    混合果汁 bzoj-5343 Ctsc-2018 题目大意:给定$n$中果汁,第$i$种果汁的美味度为$d_i$,每升价格为$p_i$,每次最多添加$l_i$升.现在要求用这$n$中果汁调配出$m$杯 ...

  8. [CTSC2018]混合果汁

    题目连接:https://www.luogu.org/problemnew/show/P4602 因为题中说是让最小值最大,所以自然想到二分答案.对于每一个二分的值,判断是否合法,若合法,在右区间二分 ...

  9. 洛谷P4602 [CTSC2018]混合果汁(主席树)

    题目描述 小 R 热衷于做黑暗料理,尤其是混合果汁. 商店里有 nn 种果汁,编号为 0,1,\cdots,n-10,1,⋯,n−1 . ii 号果汁的美味度是 d_idi​ ,每升价格为 p_ipi ...

随机推荐

  1. Documentation/filesystems/sysfs.txt 文档翻译--sysfs

    sysfs - 用于导出内核对象的文件系统. 1.sysfs是一个基于ram的文件系统,最初基于ramfs. 它提供了一种方法,可以将内核数据结构,它们的属性以及它们之间的链接导出到用户空间.sysf ...

  2. GNU Radio: Multiple USRP configurations 配置多个USRP设备

    Introduction 引言 Some USRP devices are capable of being grouped to form a single, virtual device. A s ...

  3. Python笔试题&面试题总结

    黑色加粗的是笔试题,蓝色是面试题 1.什么是GIL 2.Python中的@staticmethod和@classmethod的区别 (**) 3.Python里面如何拷贝一个对象,并解析深浅拷贝 4. ...

  4. php变量的实现

    1.php变量的实现 变量名 zval ,变量值 zend_value,php7的变量内存管理的引用计数 在zend_value结构上,变量的操作也都是zend_value实现的. //zend_ty ...

  5. 【转】Java 字节流与字符流的区别

    字节流与和字符流的使用非常相似,两者除了操作代码上的不同之外,是否还有其他的不同呢?实际上字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的,而字符流在操作时使用了缓冲区,通过缓冲区再操作 ...

  6. 简易的RPC调用框架(大神写的)

    RPC,即 Remote Procedure Call(远程过程调用),说得通俗一点就是:调用远程计算机上的服务,就像调用本地服务一样. RPC 可基于 HTTP 或 TCP 协议,Web Servi ...

  7. 精《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #5 使用checkpatch.pl检查补丁的格式

    HACK #5 使用checkpatch.pl检查补丁的格式 本节介绍发布前检查补丁格式的方法.Linux内核是由多个开发者进行开发的.因此,为了保持补丁评估与源代码的可读性,按照统一的规则进行编写是 ...

  8. [ML] CostFunction [Octave code]

    function J = computeCostMulti(X, y, theta) m = length(y); % number of training examples J = 0; for i ...

  9. wampserver中php版本的升级

    以php5.3.10到5.4.31版本为例: 1.  停止WAMP服务器. 2.  去网站windows.php.net 下载php-5.4.31-Win32-VC9-x86.zip. 不要下载THE ...

  10. oracle创建新数据库

    oracle创建新数据库 look here http://www.cnblogs.com/phoenixzq/p/3510854.html windows start menu>Oracle& ...