Yaroslav and Points

明明区间合并一下就好的东西, 为什么我会写得这么麻烦的方法啊啊啊。

  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. #define fi first
  4. #define se second
  5. #define mk make_pair
  6. #define PLL pair<LL, LL>
  7. #define PLI pair<LL, int>
  8. #define PII pair<int, int>
  9. #define SZ(x) ((int)x.size())
  10. #define ull unsigned long long
  11.  
  12. using namespace std;
  13.  
  14. const int N = 2e5 + ;
  15. const int inf = 0x3f3f3f3f;
  16. const LL INF = 0x3f3f3f3f3f3f3f3f;
  17. const int mod = 1e9 + ;
  18. const double eps = 1e-;
  19. const double PI = acos(-);
  20.  
  21. vector<int> oo;
  22. int getPos(int x) {
  23. return lower_bound(oo.begin(), oo.end(), x) - oo.begin() + ;
  24. }
  25. int getPosL(int x) {
  26. return lower_bound(oo.begin(), oo.end(), x) - oo.begin() + ;
  27. }
  28. int getPosR(int x) {
  29. return upper_bound(oo.begin(), oo.end(), x) - oo.begin();
  30. }
  31.  
  32. namespace SGT {
  33. #define lson l, mid, rt << 1
  34. #define rson mid + 1, r, rt << 1 | 1
  35. LL sum[N << ], cnt[N << ];
  36. LL lazya[N << ], lazyb[N << ], all[N << ];
  37. inline void push(int rt) {
  38. if(lazya[rt]) {
  39. lazya[rt << ] += lazya[rt];
  40. lazya[rt << | ] += lazya[rt];
  41. all[rt << ] += cnt[rt << ] * lazya[rt];
  42. all[rt << | ] += cnt[rt << | ] * lazya[rt];
  43. lazya[rt] = ;
  44. }
  45. if(lazyb[rt]) {
  46. lazyb[rt << ] += lazyb[rt];
  47. lazyb[rt << | ] += lazyb[rt];
  48. all[rt << ] += sum[rt << ] * lazyb[rt];
  49. all[rt << | ] += sum[rt << | ] * lazyb[rt];
  50. lazyb[rt] = ;
  51. }
  52. }
  53. inline void pull(int rt) {
  54. sum[rt] = sum[rt << ] + sum[rt << | ];
  55. cnt[rt] = cnt[rt << ] + cnt[rt << | ];
  56. all[rt] = all[rt << ] + all[rt << | ];
  57. }
  58. void modify1(int p, LL val, int l, int r, int rt) {
  59. if(l == r) {
  60. cnt[rt] += val;
  61. sum[rt] += oo[l - ] * val;
  62. if(!cnt[rt]) all[rt] = ;
  63. return;
  64. }
  65. push(rt);
  66. int mid = l + r >> ;
  67. if(p <= mid) modify1(p, val, lson);
  68. else modify1(p, val, rson);
  69. pull(rt);
  70. }
  71. void modify2(int L, int R, LL val, int l, int r, int rt) {
  72. if(L > R) return;
  73. if(l >= L && r <= R) {
  74. lazya[rt] += val;
  75. all[rt] += val * cnt[rt];
  76. return;
  77. }
  78. push(rt);
  79. int mid = l + r >> ;
  80. if(L <= mid) modify2(L, R, val, lson);
  81. if(R > mid) modify2(L, R, val, rson);
  82. pull(rt);
  83. }
  84. void modify3(int L, int R, LL val, int l, int r, int rt) {
  85. if(L > R) return;
  86. if(l >= L && r <= R) {
  87. lazyb[rt] += val;
  88. all[rt] += val * sum[rt];
  89. return;
  90. }
  91. push(rt);
  92. int mid = l + r >> ;
  93. if(L <= mid) modify3(L, R, val, lson);
  94. if(R > mid) modify3(L, R, val, rson);
  95. pull(rt);
  96. }
  97. PLL query1(int L, int R, int l, int r, int rt) {
  98. if(L > R) return mk(, );
  99. if(l >= L && r <= R) return mk(sum[rt], cnt[rt]);
  100. push(rt);
  101. int mid = l + r >> ;
  102. PLL ans = mk(, ), tmp = mk(, );
  103. if(L <= mid) {
  104. tmp = query1(L, R, lson);
  105. ans.fi += tmp.fi; ans.se += tmp.se;
  106. }
  107. if(R > mid) {
  108. tmp = query1(L, R, rson);
  109. ans.fi += tmp.fi; ans.se += tmp.se;
  110. }
  111. return ans;
  112. }
  113. LL query2(int L, int R, int l, int r, int rt) {
  114. if(L > R) return ;
  115. if(l >= L && r <= R) return all[rt];
  116. push(rt);
  117. int mid = l + r >> ;
  118. LL ans = ;
  119. if(L <= mid) ans += query2(L, R, lson);
  120. if(R > mid) ans += query2(L, R, rson);
  121. return ans;
  122. }
  123. }
  124.  
  125. int n, m, x[N], c[N];
  126. pair<PII, int> qus[N];
  127.  
  128. int main() {
  129. scanf("%d", &n);
  130. for(int i = ; i <= n; i++) {
  131. scanf("%d", &x[i]);
  132. c[i] = x[i];
  133. oo.push_back(x[i]);
  134. }
  135. scanf("%d", &m);
  136. for(int i = ; i <= m; i++) {
  137. scanf("%d%d%d", &qus[i].se, &qus[i].fi.fi, &qus[i].fi.se);
  138. if(qus[i].se == ) {
  139. c[qus[i].fi.fi] += qus[i].fi.se;
  140. oo.push_back(c[qus[i].fi.fi]);
  141. }
  142. }
  143. sort(oo.begin(), oo.end());
  144. oo.erase(unique(oo.begin(), oo.end()), oo.end());
  145. for(int i = ; i <= n; i++) SGT::modify1(getPos(x[i]), , , SZ(oo), );
  146. for(int i = ; i <= n; i++) {
  147. PLL add = SGT::query1(getPos(x[i]), SZ(oo), , SZ(oo), );
  148. SGT::modify2(getPos(x[i]), getPos(x[i]), add.fi, , SZ(oo), );
  149. SGT::modify3(getPos(x[i]), getPos(x[i]), -add.se, , SZ(oo), );
  150. }
  151. for(int i = ; i <= m; i++) {
  152. if(qus[i].se == ) {
  153. int p = qus[i].fi.fi, d = qus[i].fi.se;
  154. if(d > ) {
  155. SGT::modify1(getPos(x[p]), -, , SZ(oo), );
  156. SGT::modify2(, getPos(x[p]) - , d, , SZ(oo), );
  157. SGT::modify2(getPos(x[p]) + , getPos(x[p] + d) - , x[p] + d, , SZ(oo), );
  158. SGT::modify3(getPos(x[p]) + , getPos(x[p] + d) - , -, , SZ(oo), );
  159. x[p] += d;
  160. SGT::modify1(getPos(x[p]), , , SZ(oo), );
  161. PLL add = SGT::query1(getPos(x[p]), SZ(oo), , SZ(oo), );
  162. SGT::modify2(getPos(x[p]), getPos(x[p]), add.fi, , SZ(oo), );
  163. SGT::modify3(getPos(x[p]), getPos(x[p]), -add.se, , SZ(oo), );
  164. } else if(d < ) {
  165. d = -d;
  166. SGT::modify1(getPos(x[p]), -, , SZ(oo), );
  167. SGT::modify2(, getPos(x[p] - d) - , -d, , SZ(oo), );
  168. SGT::modify2(getPos(x[p] - d) + , getPos(x[p]) - , -x[p], , SZ(oo), );
  169. SGT::modify3(getPos(x[p] - d) + , getPos(x[p]) - , , , SZ(oo), );
  170. x[p] -= d;
  171. SGT::modify1(getPos(x[p]), , , SZ(oo), );
  172. PLL add = SGT::query1(getPos(x[p]), SZ(oo), , SZ(oo), );
  173. SGT::modify2(getPos(x[p]), getPos(x[p]), add.fi, , SZ(oo), );
  174. SGT::modify3(getPos(x[p]), getPos(x[p]), -add.se, , SZ(oo), );
  175. }
  176. } else {
  177. int L = qus[i].fi.fi, R = qus[i].fi.se;
  178. L = getPosL(L); R = getPosR(R);
  179. LL ans = SGT::query2(L, R, , SZ(oo), );
  180. PLL tmpL = SGT::query1(L, R, , SZ(oo), );
  181. PLL tmpR = SGT::query1(R + , SZ(oo), , SZ(oo), );
  182. ans -= tmpL.se * tmpR.fi;
  183. ans += tmpL.fi * tmpR.se;
  184. printf("%lld\n", ans);
  185. }
  186. }
  187. return ;
  188. }
  189.  
  190. /*
  191. */

Codeforces 295E Yaroslav and Points 线段树的更多相关文章

  1. Codeforces Beta Round #19D(Points)线段树

    D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  2. codeforces 295E Yaroslav and Points (离线操作+离散化+区间合并)

    参考链接:http://blog.csdn.net/dyx404514/article/details/8817717 写的很详细,这里就不再赘述,附上我的代码. #include <iostr ...

  3. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  4. Codeforces 1140F Extending Set of Points 线段树 + 按秩合并并查集 (看题解)

    Extending Set of Points 我们能发现, 如果把x轴y轴看成点, 那么答案就是在各个连通块里面的x轴的个数乘以y轴的个数之和. 然后就变成了一个并查集的问题, 但是这个题目里面有撤 ...

  5. CodeForces 19D Points (线段树+set)

    D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  6. Codeforces 1140F Extending Set of Points (线段树分治+并查集)

    这题有以下几个步骤 1.离线处理出每个点的作用范围 2.根据线段树得出作用范围 3.根据分治把每个范围内的点记录和处理 #include<bits/stdc++.h> using name ...

  7. codeforces 22E XOR on Segment 线段树

    题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...

  8. Codeforces 588E. A Simple Task (线段树+计数排序思想)

    题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...

  9. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

随机推荐

  1. (并发编程)RLock(与死锁现象),Semaphore,Even事件,线程Queue

    一.死锁现象与递归锁所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远在 ...

  2. ssdb主从及双主模型配置和简单管理

    ssdb主从及双主模型配置和简单管理 levelDB是一个key->value 的数据存储库,其只能在本地保存数据,支持持久化,并且支持保存非常大的数据,单机redis在保存较大数据的时候数十G ...

  3. 查看和解除Linux系统对用户使用资源的限制

    查看当前系统资源限制 ulimit -a   设置用户的最大进程数(重启后失效) ulimit -u 1024   设置用户可以打开的最大文件句柄数(重启后失效) ulimit -n 65530   ...

  4. [转]MingGW64 多个版本区别(silj, seh)

    1 引言 在https://sourceforge.net/projects/mingw-w64上会看到MinGW有各种版本如下,但区别在于哪儿呢 MinGW-W64 GCC-8.1.0 x86_64 ...

  5. vue v-time指令封装(接口返回时间戳 在到日期转换)

    // 全局时间戳转换指令注册Vue.directive('time',{ bind: function (el,binding) { let getTime = new Date(binding.va ...

  6. Codeforces 1039D You Are Given a Tree [根号分治,整体二分,贪心]

    洛谷 Codeforces 根号分治真是妙啊. 思路 考虑对于单独的一个\(k\)如何计算答案. 与"赛道修建"非常相似,但那题要求边,这题要求点,所以更加简单. 在每一个点贪心地 ...

  7. ECS分区挂载数据盘

    地址:https://help.aliyun.com/document_detail/25426.html?spm=5176.11065259.1996646101.searchclickresult ...

  8. Confluence 6 访问日志脚本

    日志访问脚本在连接:https://confluence.atlassian.com/download/attachments/133267635/Atlassian-accessLogScripts ...

  9. Confluence 6 数据库表-展现(Appearance)

    这部分存储了有关你 Confluence 的外观和布局使用的信息. decorator 使用自定义 Velocity 布局显示的自定义模板. https://www.cwiki.us/display/ ...

  10. Confluence 6 自定义站点和空间布局

    你可以通过编辑布局文件来修改 Confluence 的外观和感觉(也可以被称为装饰).编辑这些文件将会允许你对整个 Confluence 站点的外观和感觉进行修改或者仅仅是一个独立的空间. 当你对一个 ...