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

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

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

/** @Date    : 2016-12-06-20.00
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version :
*/ #include<bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; struct yuu
{
int l, r;
int add;
int m0, m1, m2;
}tt[N << 2]; void pushup(int p)
{
tt[p].m0 = tt[p << 1].m0 + tt[p << 1 | 1].m0;
tt[p].m1 = tt[p << 1].m1 + tt[p << 1 | 1].m1;
tt[p].m2 = tt[p << 1].m2 + tt[p << 1 | 1].m2;
} void pushdown(int p)
{
if(tt[p].add != 0)
{
tt[p].add %= 3;
///
tt[p << 1].add += tt[p].add;
if(tt[p].add == 2)
{
swap(tt[p << 1].m0 , tt[p << 1].m1);
swap(tt[p << 1].m0 , tt[p << 1].m2);
}
else if(tt[p].add == 1)
{
swap(tt[p << 1].m0 , tt[p << 1].m2);
swap(tt[p << 1].m1 , tt[p << 1].m0);
}
///
tt[p << 1 | 1].add += tt[p].add;
if(tt[p].add == 2)
{
swap(tt[p << 1 | 1].m0 , tt[p << 1 | 1].m1);
swap(tt[p << 1 | 1].m0 , tt[p << 1 | 1].m2);
}
else if(tt[p].add == 1)
{
swap(tt[p << 1 | 1].m0 , tt[p << 1 | 1].m2);
swap(tt[p << 1 | 1].m1 , tt[p << 1 | 1].m0);
}
tt[p].add = 0;
}
} void build(int l, int r, int p)
{
tt[p].l = l;
tt[p].r = r;
tt[p].add = tt[p].m0 = tt[p].m2 = tt[p].m1 = 0;
if(l == r)
{
tt[p].m0 = 1;
return ;
}
int mid = (l + r) >> 1;
build(l , mid, p << 1);
build(mid + 1, r, p << 1 | 1);
pushup(p);
} void updata(int l, int r, int v, int p)
{
if(l <= tt[p].l && r >= tt[p].r)
{
tt[p].add += v;
swap(tt[p].m0 , tt[p].m2);
swap(tt[p].m1 , tt[p].m0);
return ;
}
pushdown(p);
int mid = (tt[p].l + tt[p].r) >> 1;
if(l <= mid)
updata(l, r, v, p << 1);
if(r > mid)
updata(l, r, v, p << 1 | 1);
pushup(p);
} int query(int l, int r, int p)
{
if(l <= tt[p].l && r >= tt[p].r)
{
return tt[p].m0;
}
pushdown(p);
int mid = (tt[p].l + tt[p].r) >> 1;
int ans = 0;
if(l <= mid)
ans += query(l, r, p << 1);
if(r > mid)
ans += query(l, r, p << 1 | 1);
return ans;
}
int main()
{
int T;
int cnt = 0;
cin >> T;
while(T--)
{
int n, q;
scanf("%d%d", &n, &q);
build(1, n, 1);
printf("Case %d:\n", ++cnt);
while(q--)
{
int t, x, y;
scanf("%d%d%d", &t ,&x ,&y);
if(t)
printf("%d\n", query(x+1, y+1, 1));
else
updata(x+1, y+1, 1, 1);
}
}
return 0;
}

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. canvas学习(三):文字渲染

    一.绘制基本的文字: var canvas = document.getElementById("myCanvas") var ctx = canvas.getContext('2 ...

  2. 实用的ES6特性

    1. 函数参数默认值 不使用ES6 为函数的参数设置默认值: function foo(height, color) { var height = height || 50; var color = ...

  3. Python字符串格式化表达式和格式化方法

    Python格式化字符串由两种方式可以选择:一种是格式化表达式(Formatting Expression),一种是格式化方法(Formatting Method).其中格式化表达式在全Python版 ...

  4. 什么是Frozen Binary

    对于Python来说,你可以将Python的字节码,PVM(也就是解析器),以及需要的相关类库,打包成一个package,这个package实际上是一个二进制可执行文件,这样,用户获取到这个packa ...

  5. NYOJ 35 表达式求值(逆波兰式求值)

    http://acm.nyist.net/JudgeOnline/problemset.php?typeid=4 NYOJ 35 表达式求值(逆波兰式求值) 逆波兰式式也称后缀表达式. 一般的表达式求 ...

  6. EasyJSWebView原理分析

    概述 在iOS6之前,native只能调用webiew里的js代码,官方没有提供js调用native方法的接口.到了iOS7,官方提供了JSContext用来与js交互,native和js可以双向调用 ...

  7. Acm hust 1.25

    闲着无聊做了点hust上 acm的训练题 A题 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=104738#problem/A 看了20分 ...

  8. ACM 第六天

    图论 网络流 最大流 INF(初始值) 路径上权值最小的边,决定流量大小. 流量网络的三个特性: ①流量控制 ②反对称性 ③流量守恒 残余网络:保留了c(e)容量<f(e)流量[可以继续流,因为 ...

  9. TCP系列06—连接管理—5、TCP fastopen(TFO)

    一.TFO背景 当前web和web-like应用中一般都是在三次握手后开始数据传输,相比于UDP,多了一个RTT的时延,即使当前很多应用使用长连接来处理这种情况,但是仍然由一定比例的短连接,这额外多出 ...

  10. 学习 SQL 语句 - Select(9): 其他

    //只要前五条记录 procedure TForm1.Button1Click(Sender: TObject); begin   with ADODataSet1 do begin     Clos ...