题解看这里 liouzhou_101的博客园

更简洁的代码看这里:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. #define X first
  5. #define Y second
  6. inline void read(int &x) {
  7. int flag = 1; char ch;
  8. while(!isdigit(ch=getchar()))if(ch=='-')flag=-flag;
  9. for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
  10. x*=flag;
  11. }
  12. struct node {
  13. node *ls, *rs;
  14. bool cov; //是否被全覆盖
  15. int val; //被覆盖的值
  16. LL sum, lsum;
  17. }*root;
  18. inline node* Newnode() {
  19. node *re = new node;
  20. re->ls = re->rs = 0;
  21. re->sum = re->lsum = re->val = re->cov = 0;
  22. return re;
  23. }
  24. inline void upd(node *&p) { //update
  25. if(!p->ls) p->ls = Newnode();
  26. if(!p->rs) p->rs = Newnode();
  27. p->sum = p->ls->sum + p->rs->sum;
  28. p->lsum = min(p->ls->lsum, p->ls->sum + p->rs->lsum);
  29. }
  30. inline void cover(node *&p, int l, int r, int val) { //覆盖
  31. if(!p) p = Newnode();
  32. p->cov = 1, p->val = val;
  33. p->sum = 1ll * (r-l+1) * val;
  34. p->lsum = val >= 0 ? 0 : p->sum;
  35. }
  36. inline void pd(node *&p, int l, int r, int mid) { //pushdown
  37. if(p->cov) {
  38. cover(p->ls, l, mid, p->val);
  39. cover(p->rs, mid+1, r, p->val);
  40. p->val = p->cov = 0;
  41. }
  42. }
  43. void Modify(node *&p, int l, int r, int x, int y, int val) {
  44. //printf("(%d, %d)\n", l, r);
  45. if(!p) p = Newnode();
  46. if(x == l && y == r) {
  47. cover(p, l, r, val);
  48. return;
  49. }
  50. int mid = (l + r) >> 1;
  51. pd(p, l, r, mid);
  52. if(y <= mid) Modify(p->ls, l, mid, x, y, val);
  53. else if(x > mid) Modify(p->rs, mid+1, r, x, y, val);
  54. else Modify(p->ls, l, mid, x, mid, val), Modify(p->rs, mid+1, r, mid+1, y, val);
  55. upd(p);
  56. }
  57. #define pdl pair<double, long long>
  58. pdl query(node *&p, int l, int r, int x, int y, LL V) {
  59. if(!p) p = Newnode();
  60. if(l == x && r == y) {
  61. if(V + p->lsum > 0) return pdl(-1, p->sum);
  62. if(l == r || p->cov) return pdl(l-1.0*V/(p->sum/(r-l+1)), p->sum); //p->sum < 0
  63. }
  64. int mid = (l + r) >> 1;
  65. pd(p, l, r, mid);
  66. if(y <= mid) return query(p->ls, l, mid, x, y, V);
  67. else if(x > mid) return query(p->rs, mid+1, r, x, y, V);
  68. else {
  69. auto l_ans = query(p->ls, l, mid, x, mid, V);
  70. if(l_ans.X > 0) return l_ans;
  71. else {
  72. auto r_ans = query(p->rs, mid+1, r, mid+1, y, V + l_ans.Y);
  73. return pdl(r_ans.X, l_ans.Y + r_ans.Y);
  74. }
  75. }
  76. }
  77. map<int, int>H;
  78. int main () {
  79. int L = 1, R = 1000000000, Q;
  80. read(Q); H[L-1] = H[R+1] = 0;
  81. root = Newnode();
  82. int op, l, r, v;
  83. while(Q--) {
  84. read(op);
  85. if(op == 1) {
  86. read(l), read(v); H[l] = v;
  87. auto it = H.find(l), nxt = it;
  88. ++nxt;
  89. Modify(root, L, R, l, min(nxt->X-1, R), v);
  90. }
  91. else if(op == 2) {
  92. read(l);
  93. auto it = H.find(l), pre = it, nxt = it;
  94. --pre, ++nxt;
  95. Modify(root, L, R, l, min(nxt->X-1, R), pre->Y);
  96. H.erase(l);
  97. }
  98. else {
  99. read(l), read(r), read(v);
  100. if(v == 0) printf("%d\n", l);
  101. else {
  102. auto it = H.lower_bound(l);
  103. l = it->X;
  104. if(l >= r) puts("-1");
  105. else {
  106. auto ans = query(root, L, R, l, r-1, v);
  107. if(ans.X < 0) puts("-1");
  108. else printf("%.8f\n", ans.X);
  109. }
  110. }
  111. }
  112. }
  113. }

Codeforces Round #539 (Div. 1) C. Sasha and a Patient Friend 动态开点线段树的更多相关文章

  1. Codeforces Round #539 (Div. 1) 1109F. Sasha and Algorithm of Silence's Sounds LCT+线段树 (two pointers)

    题解请看 Felix-Lee的CSDN博客 写的很好,不过最后不用判断最小值是不是1,因为[i,i]只有一个点,一定满足条件,最小值一定是1. CODE 写完就A,刺激. #include <b ...

  2. Codeforces Round #539 (Div. 2) - D. Sasha and One More Name(思维)

    Problem   Codeforces Round #539 (Div. 2) - D. Sasha and One More Name Time Limit: 1000 mSec Problem ...

  3. Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax(思维题)

    Problem   Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax Time Limit: 2000 mSec Problem ...

  4. Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】

    传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...

  5. Codeforces Round #539 (Div. 2) C. Sasha and a Bit of Relax(前缀异或和)

    转载自:https://blog.csdn.net/Charles_Zaqdt/article/details/87522917 题目链接:https://codeforces.com/contest ...

  6. Codeforces Round #539 (Div. 2) C Sasha and a Bit of Relax

    题中意思显而易见,即求满足al⊕al+1⊕…⊕amid=amid+1⊕amid+2⊕…⊕ar且l到r的区间长为偶数的这样的数对(l,r)的个数. 若al⊕al+1⊕…⊕amid=amid+1⊕amid ...

  7. Codeforces Round #539 (Div. 1) E - Sasha and a Very Easy Test 线段树

    如果mod是质数就好做了,但是做除法的时候对于合数mod可能没有逆元.所以就只有存一下mod的每个质因数(最多9个)的幂,和剩下一坨与mod互质的一部分.然后就能做了.有点恶心. CODE #incl ...

  8. Codeforces Round #169 (Div. 2) E. Little Girl and Problem on Trees dfs序+线段树

    E. Little Girl and Problem on Trees time limit per test 2 seconds memory limit per test 256 megabyte ...

  9. Codeforces Round #539 (Div. 2)

    Codeforces Round #539 (Div. 2) A - Sasha and His Trip #include<bits/stdc++.h> #include<iost ...

随机推荐

  1. vscode配置phpxdebug

    打debug还是很有必要的,以前嫌麻烦,现在觉得,通过debug可以看自己写的代码的执行的逻辑,更容易理清别人代码的逻辑. 步骤: 下载phpdebug插件 查看自己的php版本信息,下载对应的deb ...

  2. Python3 使用pygal 生成世界人口地图

    最近在看<python从入门到实践>,其中有一个例子是使用pygal制作世界人口地图,觉得挺有意思的,这里就记录下来了, 其实代码不是很复杂,使用环境环境python3.废话不多说,直接上 ...

  3. CAS 5.x搭建常见问题系列(2).PKIX path building failed

    错误原因 服务端的证书是不安全的,Cas的客户端在调用时因为安全提醒造成调用失败. CAS的客户端需要导入服务端的证书后,就正常了. 具体操作步骤如下: 1. 首先启动tomcat,看下之前搭建的ca ...

  4. (二)ActiveMQ之点对点消息实现

    一.点对点消息实现概念 在点对点或队列模型下,一个生产者向一个特定的队列发布消息,一个消费者从该队列中读取消息.这里,生产者知道消费者的队列,并直接将消息发送到消费者的队列.这种模式被概括为:只有一个 ...

  5. Python中的一些常用模块1

    OS模块,sys模块,time模块,random模块,序列化模块 os模块是与操作系统交互的一个接口 OS模块简单的来说是一个Python的系统编程操作模块,可以处理文件和目录这些我们日常手动需要做的 ...

  6. js之向div contenteditable光标位置添加字符

    js之向div contenteditable光标位置添加字符  原理: 在HTML里面,光标是一个对象,光标对象是只有当你选中某个元素的时候才会出现的. 当我们去点击一个输入框的时候,实际上它会产生 ...

  7. S2-045、S2-046

    前言 S2-045依然是一个Ognl表达式注入导致的RCE漏洞,且漏洞很严重.另外,还是建议读者阅读本篇文章前先看下系列文章的第一篇. 正文 依然是第一篇文章中讲过,StrutsPrepareFilt ...

  8. Struts2系列漏洞起始篇

    前言 到目前位置struts2的漏洞编号已经到了S2-057,一直想系统的学习下Struts2的漏洞,但由于工作量较大,一直搁浅.最近由于工作需要,借此机会来填下坑.个人认为一个框架漏洞出来了仅仅看下 ...

  9. leetcode-55. Jump Game · Array

    题面 这个题面挺简单的,不难理解.给定非负数组,每一个元素都可以看作是一个格子.其中每一个元素值都代表当前可跳跃的格子数,判断是否可以到达最后的格子. 样例 Input: [2,3,1,1,4] Ou ...

  10. goroutine的设计与实现

    goroutine背后的系统知识 http://www.sizeofvoid.net/goroutine-under-the-hood/ 下周写完