http://www.lightoj.com/volume_showproblem.php?problem=1135

题意:给定两个操作,一个对区间所有元素加1,一个询问区间能被3整除的数有多少个。

思路:要求被3整除,我们可以记录3个状态,当前区间模3余1的 余2的 余0的,那么对一个数增加的时候,直接交换不同余数下的个数就可以了。

  1. /** @Date : 2016-12-06-20.00
  2. * @Author : Lweleth (SoungEarlf@gmail.com)
  3. * @Link : https://github.com/
  4. * @Version :
  5. */
  6.  
  7. #include<bits/stdc++.h>
  8. #define LL long long
  9. #define PII pair
  10. #define MP(x, y) make_pair((x),(y))
  11. #define fi first
  12. #define se second
  13. #define PB(x) push_back((x))
  14. #define MMG(x) memset((x), -1,sizeof(x))
  15. #define MMF(x) memset((x),0,sizeof(x))
  16. #define MMI(x) memset((x), INF, sizeof(x))
  17. using namespace std;
  18.  
  19. const int INF = 0x3f3f3f3f;
  20. const int N = 1e5+20;
  21. const double eps = 1e-8;
  22.  
  23. struct yuu
  24. {
  25. int l, r;
  26. int add;
  27. int m0, m1, m2;
  28. }tt[N << 2];
  29.  
  30. void pushup(int p)
  31. {
  32. tt[p].m0 = tt[p << 1].m0 + tt[p << 1 | 1].m0;
  33. tt[p].m1 = tt[p << 1].m1 + tt[p << 1 | 1].m1;
  34. tt[p].m2 = tt[p << 1].m2 + tt[p << 1 | 1].m2;
  35. }
  36.  
  37. void pushdown(int p)
  38. {
  39. if(tt[p].add != 0)
  40. {
  41. tt[p].add %= 3;
  42. ///
  43. tt[p << 1].add += tt[p].add;
  44. if(tt[p].add == 2)
  45. {
  46. swap(tt[p << 1].m0 , tt[p << 1].m1);
  47. swap(tt[p << 1].m0 , tt[p << 1].m2);
  48. }
  49. else if(tt[p].add == 1)
  50. {
  51. swap(tt[p << 1].m0 , tt[p << 1].m2);
  52. swap(tt[p << 1].m1 , tt[p << 1].m0);
  53. }
  54. ///
  55. tt[p << 1 | 1].add += tt[p].add;
  56. if(tt[p].add == 2)
  57. {
  58. swap(tt[p << 1 | 1].m0 , tt[p << 1 | 1].m1);
  59. swap(tt[p << 1 | 1].m0 , tt[p << 1 | 1].m2);
  60. }
  61. else if(tt[p].add == 1)
  62. {
  63. swap(tt[p << 1 | 1].m0 , tt[p << 1 | 1].m2);
  64. swap(tt[p << 1 | 1].m1 , tt[p << 1 | 1].m0);
  65. }
  66. tt[p].add = 0;
  67. }
  68. }
  69.  
  70. void build(int l, int r, int p)
  71. {
  72. tt[p].l = l;
  73. tt[p].r = r;
  74. tt[p].add = tt[p].m0 = tt[p].m2 = tt[p].m1 = 0;
  75. if(l == r)
  76. {
  77. tt[p].m0 = 1;
  78. return ;
  79. }
  80. int mid = (l + r) >> 1;
  81. build(l , mid, p << 1);
  82. build(mid + 1, r, p << 1 | 1);
  83. pushup(p);
  84. }
  85.  
  86. void updata(int l, int r, int v, int p)
  87. {
  88. if(l <= tt[p].l && r >= tt[p].r)
  89. {
  90. tt[p].add += v;
  91. swap(tt[p].m0 , tt[p].m2);
  92. swap(tt[p].m1 , tt[p].m0);
  93. return ;
  94. }
  95. pushdown(p);
  96. int mid = (tt[p].l + tt[p].r) >> 1;
  97. if(l <= mid)
  98. updata(l, r, v, p << 1);
  99. if(r > mid)
  100. updata(l, r, v, p << 1 | 1);
  101. pushup(p);
  102. }
  103.  
  104. int query(int l, int r, int p)
  105. {
  106. if(l <= tt[p].l && r >= tt[p].r)
  107. {
  108. return tt[p].m0;
  109. }
  110. pushdown(p);
  111. int mid = (tt[p].l + tt[p].r) >> 1;
  112. int ans = 0;
  113. if(l <= mid)
  114. ans += query(l, r, p << 1);
  115. if(r > mid)
  116. ans += query(l, r, p << 1 | 1);
  117. return ans;
  118. }
  119. int main()
  120. {
  121. int T;
  122. int cnt = 0;
  123. cin >> T;
  124. while(T--)
  125. {
  126. int n, q;
  127. scanf("%d%d", &n, &q);
  128. build(1, n, 1);
  129. printf("Case %d:\n", ++cnt);
  130. while(q--)
  131. {
  132. int t, x, y;
  133. scanf("%d%d%d", &t ,&x ,&y);
  134. if(t)
  135. printf("%d\n", query(x+1, y+1, 1));
  136. else
  137. updata(x+1, y+1, 1, 1);
  138. }
  139. }
  140. return 0;
  141. }

LightOJ 1135 - Count the Multiples of 3 线段树的更多相关文章

  1. 1135 - Count the Multiples of 3

    1135 - Count the Multiples of 3   PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limi ...

  2. POJ - 2777——Count Color(懒标记线段树二进制)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53639   Accepted: 16153 Des ...

  3. BZOJ 2588: Spoj 10628. Count on a tree-可持久化线段树+LCA(点权)(树上的操作) 无语(为什么我的LCA的板子不对)

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 9280  Solved: 2421 ...

  4. ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】

    任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 ...

  5. kuangbin专题七 ZOJ1610 Count the Colors (灵活线段树)

    Painting some colored segments on a line, some previously painted segments may be covered by some th ...

  6. F - Count the Colors ZOJ - 1610 线段树染色(染区间映射)

    题意:给一段0-8000的线段染色 问最后 颜色x 有几段 题解:标准线段树  但是没有push_up  最后查询是单点按顺序查询每一个点 考虑过使用区间来维护不同的线段有多少种各色的线段  思路是 ...

  7. FZU 2105 Digits Count(按位维护线段树)

    [题目链接] http://acm.fzu.edu.cn/problem.php?pid=2105 [题目大意] 给出一个序列,数字均小于16,为正数,每次区间操作可以使得 1. [l,r]区间and ...

  8. 【bzoj3956】Count 单调栈+可持久化线段树

    题目描述 输入 输出 样例输入 3 2 0 2 1 2 1 1 1 3 样例输出 0 3 题解 单调栈+可持久化线段树 本题是 bzoj4826 的弱化版(我为什么做题总喜欢先挑难的做QAQ) $k$ ...

  9. HDU 6155 Subsequence Count (DP、线性代数、线段树)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6155 题解 DP+线代好题.(考场上过多时间刚前两题,没怎么想这题--) 首先列出一个DP式: 设\( ...

随机推荐

  1. CDH问题集

    1.在CM中添加主机报JDK错误 手动在机器上安装oracle-jdk1.7+update64.然后在CM中选择不安装oracle-jdk即可. 2.HostMoinitor无法与server联系 查 ...

  2. Python中的list

    list的创建 1 字面量 >>>L = [1, 2, 3] [1, 2, 3] 2 通过iterable可迭代对象,比如str对象,range对象,map对象 >>&g ...

  3. 如何在html中把一个图片或者表格覆盖在一张已有图片上的任意位置

    如何在html中把一个图片或者表格覆盖在一张已有图片上的任意位置   <div style="position:relative;"> <img src=&quo ...

  4. Swift-可选值(Optional)讲解

    前提:Swift中有规定:对象中的任何属性在创建时,都必须要有明确的初始化值 1.定义可选类型 方式一:常规方式(不常用) var name : Optional<String> = ni ...

  5. python中装饰器的原理以及实现,

    python版本 3.6 1.python的装饰器说白了就是闭包函数的一种应用场景,在运用的时候我们遵循 #开放封闭原则:对修改封闭,对拓展开放 2.什么是装饰器 #装饰他人的器具,本身可以是任意可调 ...

  6. 【week2】 构建之法 读后感及问题

    上一次读后感涵盖前五章的内容包括个人技术,结对合作,小组项目等.本周作业的燃尽图以及站立会议是关于<构建之法>第六章的内容,所以关于这一章的读后感涵盖在上两篇博客中. 第七章 MSF 介绍 ...

  7. vue-cli项目里npm安装使用elementUI

    第一步:进入到项目目录里 npm i element-ui -S 第二步:在main.js中引入 import ElementUI from 'element-ui' import 'element- ...

  8. MD5加密的使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. [Redis]在Windows下的下载及安装

    1.下载 下载地址: https://github.com/MSOpenTech/redis, 下载并解压到特定的目录. 2.启动Redis服务端 CMD -> redis-server.exe ...

  10. linux后台运行之screen和nohup

    3.1 nohup命令 如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令. 该命令可以在你退出帐户/关闭终端之后继续运行相应的进程. nohup就是不挂起的意 ...