题意:

你有一个点集,有三种操作:

  • 往集合里插入一个点\((x, y)\)
  • 从集合中删除第\(i\)次操作插入的点
  • 对于给出的\(q\),询问点集中\(x \cdot q + y\)的最大值

分析:

先不考虑插入删除操作,对于一个给定的点集,如何寻找\(x \cdot q + y\)最大值

这是一个线性规划的问题,只是可行域变成了离散的点。

设\(x \cdot q + y = z\),其中\(z\)是优化目标。

\(y = -q \cdot x + z\),使得经过点\((x, y)\)斜率为\(-q\)的直线的截距最大。

那么作为最优解的点一定在点集的凸包上,所以可以用单调栈求出凸包,然后三分求最大值。

因为每次操作都可能导致点集发生变化,不可能每次都求一遍凸包。

每个节点对应一个生存期\([L, R]\),即在第\(L\)次操作到第\(R\)次操作中点集中有该点。

然后把它插入到一个线段树的区间中,这样线段树的每个点都对应一段操作区间的一个点集。

用这个点集的凸包更新所有这个区间的询问。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <vector>
  5. using namespace std;
  6. typedef long long LL;
  7. const int maxn = 300000 + 10;
  8. const LL INF = 1LL << 61;
  9. struct Point
  10. {
  11. LL x, y;
  12. Point(LL x = 0, LL y = 0): x(x), y(y) {}
  13. void read() { scanf("%lld%lld", &x, &y); }
  14. bool operator < (const Point& t) const {
  15. return x < t.x || (x == t.x && y < t.y);
  16. }
  17. Point operator + (const Point& t) const {
  18. return Point(x + t.x, y + t.y);
  19. }
  20. Point operator - (const Point& t) const {
  21. return Point(x - t.x, y - t.y);
  22. }
  23. };
  24. LL Cross(const Point& A, const Point& B) {
  25. return A.x * B.y - A.y * B.x;
  26. }
  27. LL Dot(const Point& A, const Point& B) {
  28. return A.x * B.x + A.y * B.y;
  29. }
  30. int type[maxn], top;
  31. Point p[maxn], S[maxn];
  32. vector<Point> v[maxn * 4];
  33. bool del[maxn], empty[maxn];
  34. LL ans[maxn];
  35. void insert(int o, int L, int R, int qL, int qR, int v) {
  36. if(qL <= L && R <= qR) {
  37. ::v[o].push_back(p[v]);
  38. return;
  39. }
  40. int M = (L + R) / 2;
  41. if(qL <= M) insert(o<<1, L, M, qL, qR, v);
  42. if(qR > M) insert(o<<1|1, M+1, R, qL, qR, v);
  43. }
  44. void query(int x) {
  45. int L = 1, R = top;
  46. while(R - L >= 3) {
  47. int mid1 = (L * 2 + R) / 3;
  48. int mid2 = (L + R * 2) / 3;
  49. if(Dot(p[x], S[mid1]) < Dot(p[x], S[mid2])) L = mid1;
  50. else R = mid2;
  51. }
  52. for(int i = L; i <= R; i++)
  53. ans[x] = max(ans[x], Dot(p[x], S[i]));
  54. }
  55. void solve(int o, int L, int R) {
  56. if(L < R) {
  57. int M = (L + R) / 2;
  58. solve(o<<1, L, M);
  59. solve(o<<1|1, M+1, R);
  60. }
  61. sort(v[o].begin(), v[o].end());
  62. top = 0;
  63. for(int i = 0; i < v[o].size(); i++) {
  64. while(top > 1 && Cross(S[top]-S[top-1], v[o][i]-S[top]) >= 0) top--;
  65. S[++top] = v[o][i];
  66. }
  67. for(int i = L; i <= R; i++) if(type[i] == 3 && !empty[i])
  68. query(i);
  69. }
  70. int main()
  71. {
  72. int n; scanf("%d", &n);
  73. int cnt = 0;
  74. for(int i = 1; i <= n; i++) {
  75. scanf("%d", type + i);
  76. if(type[i] == 1) {
  77. p[i].read();
  78. cnt++;
  79. } else if(type[i] == 2) {
  80. int x; scanf("%d", &x);
  81. del[x] = true;
  82. cnt--;
  83. insert(1, 1, n, x, i, x);
  84. } else {
  85. scanf("%lld", &p[i].x);
  86. p[i].y = 1LL;
  87. if(!cnt) empty[i] = true;
  88. }
  89. }
  90. for(int i = 1; i <= n; i++)
  91. if(type[i] == 1 && !del[i])
  92. insert(1, 1, n, i, n, i);
  93. for(int i = 1; i <= n; i++) ans[i] = -INF;
  94. solve(1, 1, n);
  95. for(int i = 1; i <= n; i++) if(type[i] == 3) {
  96. if(empty[i]) printf("EMPTY SET\n");
  97. else printf("%lld\n", ans[i]);
  98. }
  99. return 0;
  100. }

Codeforces 678F Lena and Queries的更多相关文章

  1. [Educational Round 13][Codeforces 678F. Lena and Queries]

    题目连接:678F - Lena and Queries 题目大意:要求对一个点集实现二维点对的插入,删除,以及询问\(q\):求\(max(x\cdot q+y)\) 题解:对每个点集内的点\(P( ...

  2. [CodeForces - 678F] Lena and Queries 线段树维护凸包

    大致题意: 给出三种操作 1.往平面点集中添加一个点 2.删除第i次添加的点 3.给出一个q,询问平面点集中的q*x+y的最大值 首先对于每个询问,可将z=q*x+y转化为y=z-q*x,即过点(x, ...

  3. codeforces 797 E. Array Queries【dp,暴力】

    题目链接:codeforces 797 E. Array Queries   题意:给你一个长度为n的数组a,和q个询问,每次询问为(p,k),相应的把p转换为p+a[p]+k,直到p > n为 ...

  4. CodeForces - 369E Valera and Queries(树状数组)

    CodeForces - 369E Valera and Queries 题目大意:给出n个线段(线段的左端点和右端点坐标)和m个查询,每个查询有cnt个点,要求给出有多少条线段包含至少其中一个点. ...

  5. codeforces 1217E E. Sum Queries? (线段树

    codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...

  6. 【CF 678F】Lena and Queries

    Time Limit: 2000 ms   Memory Limit: 512 MB Description 初始有一个空集合 n个操作 有三种操作,如下: 1 a b 表示向集合中插入二元组(a,b ...

  7. Codeforces 714C. Sonya and Queries Tire树

    C. Sonya and Queries time limit per test:1 second memory limit per test: 256 megabytes input:standar ...

  8. Educational Codeforces Round 2 B. Queries about less or equal elements 水题

    B. Queries about less or equal elements Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforc ...

  9. Educational Codeforces Round 1 B. Queries on a String 暴力

    B. Queries on a String Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/59 ...

随机推荐

  1. java基础知识——Java的定义,特点和技术平台

    (作者声明:对于Java编程语言,很多人只知道怎么用,却对其了解甚少.我也是其中一员.所以菜鸟的我,去查询了教科书以及大神的总结,主要参考了<Java核心技术>这本神作.现在分享给大家!) ...

  2. Mysql5.7.6安装和主从配置手册

    Mysql5.7.6+ 安装手册 linux server版本   1.下载 http://dev.mysql.com/downloads/mysql/#downloads  2. 检查库文件是否存在 ...

  3. Google Authenticator加强ssh安全

    一.安装依赖包 软件包可以在这个地址下载:https://pan.baidu.com/s/1r0CmwbtCfNiBqU9rh_TxtA yum -y install pam-devel tar jx ...

  4. 手机端@media screen布局自适应

    @media only screen and (min-width: 310px) and (max-width: 360px) { }@media only screen and (min-widt ...

  5. Linux最常用命令实战

    1.改变机器的名称: vim /etc/hostname Master 在文件中修改机器名称为我们想要的名称(相当于域名) 可以通过shutdown -h now 关闭 2.查看当前机器IP: ifc ...

  6. hdu-1166 敌兵布阵---树状数组模板

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目大意: 维护动态的区间和,单点更新,就是模板题 #include<iostream& ...

  7. 安装CocoaPods遇到的问题 及其解决

    本人也是第一次安装这个 CocoaPods,所以刚开始也是遇到了很多懵逼的问题,今天终于搞定了,就自己总结一下,如有错误敬请指出,谢谢! 由于之前,对于终端命令行,不是很了解,总感觉很麻烦,所以也一直 ...

  8. 常量池与方法区以及又读new String对象创建问题

    又拿出这道String str1 = new String("abc");创建几个对象的面试题梳理了一下常量池与方法区的关系,希望能把这两者的关系通过这道面试题说明白 方法区是什么 ...

  9. Github的基本功能

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:Fadeoc Khaos链接:http://www.zhihu.com/question/20070065/answer/30 ...

  10. 什么是SAD,SAE,SATD,SSD,SSE,MAD,MAE,MSD,MSE?

    SAD(Sum of Absolute Difference)=SAE(Sum of Absolute Error)即绝对误差和 SATD(Sum of Absolute Transformed Di ...