Codeforces 1180E Serge and Dining Room
题意:
有\(n\)个菜肴,有\(m\)个小朋友,每个菜肴的价格为\(a_i\),每个小朋友有\(b_i\)元钱,小朋友从\(1 \rightarrow m\)依次购买菜肴,当第\(i\)个小朋友轮到的时候,他会购买他买的起的最贵的,否则就离开。
要求支持修改第\(i\)个菜肴的价格和修改第\(i\)个小朋友的拥有的钱数的两种操作,每次操作完成给出\(m\)个小朋友买完后剩下的最贵的菜肴的价格是多少。
思路:
假设价格大于\(x\)的\(y\)个菜肴都被买了,那么显然拥有钱数\(\geq x\)的小朋友个数一定要\(\geq y\),显然如何购买是无所谓的。
那么就在\(a_i\)处减一,\(b_i\)处加一,每次询问一个最大的\(l\)使得\([l, \infty]\)的最大后缀和\(> 0\)。
线段树维护即可。
代码:
#include <bits/stdc++.h>
using namespace std;
#define N 1000010
#define M 1000000
int n, m, q, a[N], b[N];
struct SEG {
struct node {
int sum, Max;
node() {
sum = Max = 0;
}
node(int sum, int Max) : sum(sum), Max(Max) {}
node operator + (const node &other) const {
node res = node();
res.sum = sum + other.sum;
res.Max = max(other.Max, Max + other.sum);
return res;
}
}t[N << 2];
void init() {
memset(t, 0, sizeof t);
}
void update(int id, int l, int r, int x, int v) {
if (l == r) {
t[id].sum += v;
t[id].Max += v;
return;
}
int mid = (l + r) >> 1;
if (x <= mid) update(id << 1, l, mid, x, v);
else update(id << 1 | 1, mid + 1, r, x, v);
t[id] = t[id << 1] + t[id << 1 | 1];
}
int query(int id, int l, int r, node tmp) {
if (l == r) {
return l;
}
int mid = (l + r) >> 1;
node tmp2 = t[id << 1 | 1] + tmp;
if (tmp2.Max > 0) {
return query(id << 1 | 1, mid + 1, r, tmp);
} else {
return query(id << 1, l, mid, tmp2);
}
}
}seg;
int main() {
while (scanf("%d%d", &n, &m) != EOF) {
seg.init();
for (int i = 1; i <= n; ++i) {
scanf("%d", a + i);
seg.update(1, 1, M, a[i], 1);
}
for (int i = 1; i <= m; ++i) {
scanf("%d", b + i);
seg.update(1, 1, M, b[i], -1);
}
scanf("%d", &q);
int op, x, v;
while (q--) {
scanf("%d%d%d", &op, &x, &v);
switch(op) {
case 1 :
seg.update(1, 1, M, a[x], -1);
seg.update(1, 1, M, a[x] = v, 1);
break;
case 2 :
seg.update(1, 1, M, b[x], 1);
seg.update(1, 1, M, b[x] = v, -1);
break;
default :
assert(0);
}
if (seg.t[1].Max <= 0) {
puts("-1");
} else {
printf("%d\n", seg.query(1, 1, M, SEG::node(0, 0)));
}
}
}
return 0;
}
Codeforces 1180E Serge and Dining Room的更多相关文章
- codeforces 1180E Serge and Dining Room 线段树
题目传送门 题目大意: 给出a序列和b序列,a序列为各种食物的价格,b序列为一列排着队的小朋友拥有的钱,小朋友依次购买食物,每个人都买自己能买的起的最贵的食物,买不起就离开队伍.给出q次操作,操作1是 ...
- E - Serge and Dining Room
https://codeforces.com/contest/1180/problem/E 转载自他人博客 题意:有nn个菜肴,有mm个小朋友,每个菜肴的价格为aiai,每个小朋友有bibi元钱,小朋 ...
- Codeforces Round #569 题解
Codeforces Round #569 题解 CF1179A Valeriy and Deque 有一个双端队列,每次取队首两个值,将较小值移动到队尾,较大值位置不变.多组询问求第\(m\)次操作 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- BZOJ 1711: [Usaco2007 Open]Dining吃饭
1711: [Usaco2007 Open]Dining吃饭 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 902 Solved: 476[Submit ...
- BZOJ 1226: [SDOI2009]学校食堂Dining
1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 730 Solved: 446[Submit][ ...
随机推荐
- 『Python Web框架之Django』第几节: AJAX
一. AJAX简介 AJAX(Asynchronous Javascript And XML)翻译成中文就是“异步的Javascript和XML”.即使用Javascript语言与服务器进行异步交互, ...
- Lieges of Legendre CodeForces - 603C (博弈论,SG找规律)
大意: 给定$n$堆石子, 两人轮流操作, 每次操作两种选择 $(1)$任选非空堆拿走一个石子 $(2)$任选石子数为$2x(x>0)$的一堆, 替换为$k$堆$x$个石子. ($k$给定) 最 ...
- MyCat 插件 的应用
什么是MyCat MyCAT是一款由阿里Cobar演变而来的用于支持数据库,读写分离.分表分库的分布式中间件.MyCAT支持Oracle.MSSQL.MYSQL.PG.DB2关系型数据库,同时也支持M ...
- TensorType
- 【已解决】项目加载失败,Web应用程序项目XX已配置为使用IIS
这个解决方法是我在网上参考了很多方法都不行,因为昨天还好好的,今天就不行,那跟项目没多大关系,跟环境有关. 解决方案: 本地iis和vs自带的iis冲突了,默认用了本地的iis,我删掉本地的就可以了. ...
- InnoDB引擎中的索引与算法9
5.1 InnoDB支持以下几种常见的索引: B+树索引 全文索引 哈希索引(自适应哈希索引) 关于哈希索引的说明: -- 1.InnoDB的哈希索引是自适应的,其根据表的使用情况自动生成哈希索引,不 ...
- 爬虫的新模块pyppeteer的使用
安装 python3 -m pip install pyppeteer 最好是py3.5+ 手动安装 你懂的,天朝网络环境很复杂,如果要用pyppeteer自己绑定的chromium,半天都下载不下来 ...
- 目标检测 — two-stage检测
目前主流的目标检测算法主要是基于深度学习模型,其可以分成两大类:two-stage检测算法:one-stage检测算法.本文主要介绍第一类检测算法,第二类在下一篇博文中介绍. 目标检测模型的主要性能指 ...
- 远程连接服务器数据库报错:Host ‘XXXXXX’ is blocked because of many connection errors
原文:https://blog.csdn.net/li_li_lin/article/details/72764683 一.我遇到的问题描述 使用Navicat for mysql连接公司的服务器数据 ...
- Jupyter Notebook 插件安装
刚才安装notebook插件jupyter_contrib_nbextensions,搜了很多教程都没有作用.直到用了这个命令,一行解决. pip install jupyter_contrib_nb ...