The Street

思路:

  动态开节点线段树;

  等差序列求和于取大,是两个独立的子问题;

  所以,建两颗线段树分开维护;

  求和:等差数列的首项和公差直接相加即可;

  取大:

    对于线段树每个节点储存一条斜率为等差数列公差的线段;

    当添加线段到已有线段的节点,下传一条线段,当前节点留下一条线段;

    当要添加的线段完全覆盖或者被覆盖当前节点储存的线段时,选择更新或者不更新;

  单点查询时,从根节点到叶节点的路径上去最大值;

来,上代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. #define ll long long
  9. #define INF (1LL<<62)
  10.  
  11. struct TreeNodeType {
  12. ll a,b;
  13.  
  14. bool if_;
  15.  
  16. struct TreeNodeType *lc,*rc;
  17.  
  18. TreeNodeType()
  19. {
  20. lc=NULL,rc=NULL,if_=false,a=,b=;
  21. }
  22. };
  23.  
  24. ll n,m;
  25.  
  26. inline void in(ll &now)
  27. {
  28. ll if_z=;now=;
  29. char Cget=getchar();
  30. while(Cget>''||Cget<'')
  31. {
  32. if(Cget=='-') if_z=-;
  33. Cget=getchar();
  34. }
  35. while(Cget>=''&&Cget<='')
  36. {
  37. now=now*+Cget-'';
  38. Cget=getchar();
  39. }
  40. now*=if_z;
  41. }
  42.  
  43. class SumTreeType {
  44. public:
  45. struct TreeNodeType *root;
  46.  
  47. SumTreeType(){}
  48.  
  49. inline void tree_down(TreeNodeType *&now,ll l,ll r)
  50. {
  51. if(now->lc==NULL) now->lc=new TreeNodeType;
  52. if(now->rc==NULL) now->rc=new TreeNodeType;
  53. now->lc->a+=now->a,now->lc->b+=now->b;
  54. now->rc->a+=now->a+((l+r>>)-l+)*now->b,now->rc->b+=now->b;
  55. now->a=,now->b=;
  56. }
  57.  
  58. void tree_add(TreeNodeType *&now,ll l,ll r,ll li,ll ri,ll a,ll b)
  59. {
  60. if(now==NULL) now=new TreeNodeType;
  61. if(l==li&&r==ri)
  62. {
  63. now->a+=a,now->b+=b;
  64. return ;
  65. }
  66. ll mid=l+r>>;
  67. if(now->a!=&&now->b!=) tree_down(now,l,r);
  68. if(li>mid) tree_add(now->rc,mid+,r,li,ri,a,b);
  69. else if(ri<=mid) tree_add(now->lc,l,mid,li,ri,a,b);
  70. else
  71. {
  72. tree_add(now->lc,l,mid,li,mid,a,b);
  73. tree_add(now->rc,mid+,r,mid+,ri,a+(mid-li+)*b,b);
  74. }
  75. }
  76.  
  77. ll tree_query(TreeNodeType *&now,ll l,ll r,ll to)
  78. {
  79. if(now==NULL) return ;
  80. if(l==r) return now->a;
  81. if(now->a!=||now->b!=) tree_down(now,l,r);
  82. ll mid=l+r>>;
  83. if(to<=mid) return tree_query(now->lc,l,mid,to);
  84. else return tree_query(now->rc,mid+,r,to);
  85. }
  86. };
  87. class SumTreeType ai;
  88.  
  89. class MaxTreeType {
  90. public:
  91. ll X;
  92.  
  93. bool op;
  94.  
  95. struct TreeNodeType *root;
  96.  
  97. MaxTreeType(){}
  98.  
  99. inline double com(ll a1,ll b1,ll a2,ll b2)
  100. {
  101. if(a1==a2) return ;
  102. return (double)(a1-a2)/(double)(b2-b1);
  103. }
  104.  
  105. void tree_down(TreeNodeType *&now,ll l,ll r,ll a,ll b)
  106. {
  107. if(now==NULL)
  108. {
  109. now=new TreeNodeType;
  110. now->if_=true;
  111. now->a=a,now->b=b;
  112. return ;
  113. }
  114. if(!now->if_)
  115. {
  116. now->a=a,now->b=b,now->if_=true;
  117. return ;
  118. }
  119. double xx=com(now->a-l*now->b,now->b,a-l*b,b),mid=l+r>>;
  120. if(xx<=l||xx>=r)
  121. {
  122. if((mid-l)*b+a>(mid-l)*now->b+now->a) now->a=a,now->b=b;
  123. return ;
  124. }
  125. if(xx<=mid)
  126. {
  127. if(now->b<b)
  128. {
  129. tree_down(now->lc,l,mid,now->a,now->b);
  130. now->a=a,now->b=b,now->if_=true;
  131. }
  132. else tree_down(now->lc,l,mid,a,b);
  133. }
  134. else
  135. {
  136. if(now->b<b) tree_down(now->rc,mid+,r,a+(mid-l+)*b,b);
  137. else
  138. {
  139. tree_down(now->rc,mid+,r,now->a+(mid-l+)*now->b,now->b);
  140. now->a=a,now->b=b,now->if_=true;
  141. }
  142. }
  143. }
  144.  
  145. void tree_add(TreeNodeType *&now,ll l,ll r,ll li,ll ri,ll a,ll b)
  146. {
  147. if(now==NULL) now=new TreeNodeType;
  148. if(l==li&&r==ri)
  149. {
  150. if(!now->if_)
  151. {
  152. now->if_=true;
  153. now->a=a,now->b=b;
  154. }
  155. else tree_down(now,l,r,a,b);
  156. return ;
  157. }
  158. ll mid=l+r>>;
  159. if(ri<=mid) tree_add(now->lc,l,mid,li,ri,a,b);
  160. else if(li>mid) tree_add(now->rc,mid+,r,li,ri,a,b);
  161. else
  162. {
  163. tree_add(now->lc,l,mid,li,mid,a,b);
  164. tree_add(now->rc,mid+,r,mid+,ri,a+(mid-li+)*b,b);
  165. }
  166. }
  167.  
  168. void tree_query(TreeNodeType *&now,ll l,ll r,ll to)
  169. {
  170. if(now==NULL) return ;
  171. if(now->if_) X=max(X,now->a+(to-l)*now->b);
  172. if(l==r) return ;
  173. ll mid=l+r>>;
  174. if(to<=mid) tree_query(now->lc,l,mid,to);
  175. else tree_query(now->rc,mid+,r,to);
  176. }
  177. };
  178. class MaxTreeType bi;
  179.  
  180. int main()
  181. {
  182. in(n),in(m);ll op,u,v,a,b;
  183. for(ll i=;i<=m;i++)
  184. {
  185. in(op);
  186. if(op==)
  187. {
  188. in(u),in(v),in(b),in(a);
  189. bi.tree_add(bi.root,,n,u,v,a,b);
  190. }
  191. else if(op==)
  192. {
  193. in(u),in(v),in(b),in(a);
  194. ai.tree_add(ai.root,,n,u,v,a,b);
  195. }
  196. else if(op==)
  197. {
  198. in(u);
  199. bi.X=-INF;
  200. bi.tree_query(bi.root,,n,u);
  201. if(bi.X==-INF) printf("NA\n");
  202. else printf("%lld\n",bi.X+ai.tree_query(ai.root,,n,u));
  203. }
  204. }
  205. return ;
  206. }

AC日记——The Street codechef March challenge 2014的更多相关文章

  1. Codechef March Challenge 2014——The Street

    The Street Problem Code: STREETTA https://www.codechef.com/problems/STREETTA Submit Tweet All submis ...

  2. codechef January Challenge 2014 Sereja and Graph

    题目链接:http://www.codechef.com/JAN14/problems/SEAGRP [题意] 给n个点,m条边的无向图,判断是否有一种删边方案使得每个点的度恰好为1. [分析] 从结 ...

  3. 【分块+树状数组】codechef November Challenge 2014 .Chef and Churu

    https://www.codechef.com/problems/FNCS [题意] [思路] 把n个函数分成√n块,预处理出每块中各个点(n个)被块中函数(√n个)覆盖的次数 查询时求前缀和,对于 ...

  4. CodeChef November Challenge 2014

    重点回忆下我觉得比较有意义的题目吧.水题就只贴代码了. Distinct Characters Subsequence 水. 代码: #include <cstdio> #include ...

  5. 刷漆(Codechef October Challenge 2014:Remy paints the fence)

    [问题描述] Czy做完了所有的回答出了所有的询问,结果是,他因为脑力消耗过大而变得更虚了:).帮助Czy恢复身材的艰巨任务落到了你的肩上. 正巧,你的花园里有一个由N块排成一条直线的木板组成的栅栏, ...

  6. [Codechef October Challenge 2014]刷漆

    问题描述 Czy做完了所有的回答出了所有的询问,结果是,他因为脑力消耗过大而变得更虚了:).帮助Czy恢复身材的艰巨任务落到了你的肩上. 正巧,你的花园里有一个由N块排成一条直线的木板组成的栅栏,木板 ...

  7. Codechef December Challenge 2014 Chef and Apple Trees 水题

    Chef and Apple Trees Chef loves to prepare delicious dishes. This time, Chef has decided to prepare ...

  8. CodeChef March Challenge 2019题解

    传送门 \(CHNUM\) 显然正数一组,负数一组 for(int T=read();T;--T){ n=read(),c=d=0; fp(i,1,n)x=read(),x>0?++c:++d; ...

  9. CODECHEF Oct. Challenge 2014 Children Trips

    @(XSY)[分塊, 倍增] Description There's a new trend among Bytelandian schools. The "Byteland Tourist ...

随机推荐

  1. 将list中的元素按照属性分类成树状的map

    技术交流群: 233513714 public LinkedHashMap<String, List<TPhoneModel>> queryPhoneList(List< ...

  2. 哪些工具能有效管理Azure Active Directory?

    [TechTarget中国原创] 管理Azure Active Directory有四种常见的工具:Azure Web门户.Azure PowerShell.Azure命令行接口和Azure Mana ...

  3. 利用插件对某些网页执行javascript代码

    说明 javascript在浏览器地址栏中可以运行,也可以按F12在控制台中运行,还可以写一个插件让javascript针对某些网页执行,可以使用chrome浏览器的Content scripts,C ...

  4. 剑指Offer - 九度1361 - 翻转单词顺序

    剑指Offer - 九度1361 - 翻转单词顺序2013-11-23 02:45 题目描述: JOBDU最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上.同事Cat对Fi ...

  5. 《数据结构》C++代码 邻接表与邻接矩阵

    上一篇“BFS与DFS”写完,突然意识到这个可能偏离了“数据结构”的主题,所以回来介绍一下图的存储:邻接表和邻接矩阵. 存图有两种方式,邻接矩阵严格说就是一个bool型的二维数组,map[i][j]表 ...

  6. shell脚本获取网页快照并生成缩略图

    获取网页快照并生成缩略图可分两步进行: 1.获取网页快照 2.生成缩略图 获取网页快照 这里我们用 phantomjs 来实现.关于 phantomjs 的详细用法可参考官方网站. 1.安装 我的环境 ...

  7. js对象使用

    以下是js对象使用的两种方式 <script type="text/javascript"> var people = function () { } //方法1 pe ...

  8. PAT——乙级1028

    这道题花了我半个多小时,对呀乙级算是挺多时间的了. 1028 人口普查 (20 point(s)) 某城镇进行人口普查,得到了全体居民的生日.现请你写个程序,找出镇上最年长和最年轻的人. 这里确保每个 ...

  9. beta版本前准备

    目录 过去存在的问题 任务分工 开发规范 后端总结 卉卉 家灿 前端总结 绪佩 青元 恺琳 宇恒 丹丹 算法&API接口 家伟 鸿杰 一好 文档&博客撰写 政演 产品功能 我们已经做了 ...

  10. mysql5.6版本修改密码

     UPDATE user SET Password=PASSWORD('新密码') WHERE User='root';