LightOJ 1135 - Count the Multiples of 3 线段树
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 线段树的更多相关文章
- 1135 - Count the Multiples of 3
1135 - Count the Multiples of 3 PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limi ...
- POJ - 2777——Count Color(懒标记线段树二进制)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 53639 Accepted: 16153 Des ...
- 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 ...
- ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】
任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 ...
- kuangbin专题七 ZOJ1610 Count the Colors (灵活线段树)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
- F - Count the Colors ZOJ - 1610 线段树染色(染区间映射)
题意:给一段0-8000的线段染色 问最后 颜色x 有几段 题解:标准线段树 但是没有push_up 最后查询是单点按顺序查询每一个点 考虑过使用区间来维护不同的线段有多少种各色的线段 思路是 ...
- FZU 2105 Digits Count(按位维护线段树)
[题目链接] http://acm.fzu.edu.cn/problem.php?pid=2105 [题目大意] 给出一个序列,数字均小于16,为正数,每次区间操作可以使得 1. [l,r]区间and ...
- 【bzoj3956】Count 单调栈+可持久化线段树
题目描述 输入 输出 样例输入 3 2 0 2 1 2 1 1 1 3 样例输出 0 3 题解 单调栈+可持久化线段树 本题是 bzoj4826 的弱化版(我为什么做题总喜欢先挑难的做QAQ) $k$ ...
- HDU 6155 Subsequence Count (DP、线性代数、线段树)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6155 题解 DP+线代好题.(考场上过多时间刚前两题,没怎么想这题--) 首先列出一个DP式: 设\( ...
随机推荐
- 使用DataTables导出html表格
去年与同事一起做一个小任务,需要把HTML表格中的数据导出到Excel.用原生js想要实现,只有IE浏览器提供导出到微软的Excel的接口,这就要求你电脑上必须安装IE浏览器.Excel,而且必须修改 ...
- 软件工程 作业part3 读后感
匆匆看完构建之法,觉得这种不认真看完书就去写随笔去评价这本书是对作者的不尊重,所以觉得应该提问题和写感悟. 我的一点拙见,提的问题在现在这个信息发达的时候感觉只要有时间都可以自己解决. 感觉软件工程这 ...
- 软件功能说明书——Thunder团队
爱阅APP功能说明书 一.引言 相信大家都使用过电子书阅读器,相对于纸质版书籍电子书APP做到了环保.易存储.便携.因此我们Thunder团队开发了——爱阅APP,以下内容是Alpha版的功能说明书. ...
- C++基础和STL,Effective C++笔记
这个作者总结的c++基础,特别好. 可以看看. http://blog.csdn.net/tham_/article/details/51169792
- c# byte[] 保存图片
1.用函数即可,File.WriteAllBytes(@"E:\123.bmp", pcBMPBuffer); 2.byte[]也可和image互相转化.
- 软件工程课堂作业(三)——Right-BICEP软件单元测试
一.测试方法:Right-BICEP Right-结果是否正确?B-是否所有的边界条件都是正确的?I-能查一下反向关联吗?C-能用其他手段交叉检查一下结果吗?E-你是否可以强制错误条件发生?P-是否满 ...
- 【Linux】- 不可不知的小技巧
1.Tab键:输入文件或目录名的前几个字符,然后按TAB键,如无相重的,完整的文件名立即自动在命令行出现:如有相重的,再按一下TAB键,系统会列出当前目录下所有以这几个字符开头的名字. 在命令行下,只 ...
- shell练习题讲解
写一个脚本,计算100以内所有的奇数的和以及所有偶数的和,分别显示出来#! /bin/bashsum1=0for i in `seq 1 2 100`do sum1=$[$sum1+$i]doneec ...
- cacti添加多个tomcat监控(多端口)
1.修改tomcat的模版 Data Input Methods->Tomcat Status 把原本固定的端口,用户名和密码手动修改成变量(绿线标出的),之后save保存之后,再在Input ...
- mysql(三) 慢查询分析(二)
在一般的查询中,都要求尽量围绕创建的索引进行.针对索引,常用的有主键索引,单列索引,组合索引,索引合并等. 在评价索引时,关键看区分度.索引区分度=索引列唯一值/表记录数. 如果在区分度很低的列上建索 ...