LightOJ 1135 Count the Multiples of 3 (Segmengt + 懒惰标记)
来源:互联网 发布:阿里云浏览器官网 编辑:程序博客网 时间:2024/06/06 02:55
Description
You have an array with n elements which is indexed from 0 to n - 1. Initially all elements are zero. Now you have to deal with two types of operations
1. Increase the numbers between indices i and j (inclusive) by 1. This is represented by the command '0 i j'.
2. Answer how many numbers between indices i and j (inclusive) are divisible by 3. This is represented by the command '1 i j'.
Input
Input starts with an integer T (≤ 5), denoting the number of test cases.
Each case starts with a line containing two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000) denoting the number of queries. Each query will be either in the form '0 i j' or '1 i j' where i, j are integers and 0 ≤ i ≤ j < n.
Output
For each case, print the case number first. Then for each query in the form '1 i j', print the desired result.
Sample Input
1
10 9
0 0 9
0 3 7
0 1 4
1 1 7
0 2 2
1 2 4
1 8 8
0 5 8
1 6 9
Sample Output
Case 1:
2
3
0
2
这不是一道裸线段树的题。。它涉及到区间更新,这里就需要用到懒惰标记了。。而且还要自己在考虑一下结构体中的数据元素都需要什么,这里需要左右区间指示标志,懒惰标记,记录区间中对3取余分别余1、余2、余0的元素个数,还有就是需要增加一个MOVE();它的功能是当访问到该区间时,,转换m0、m1、m2之间的值的操作。分析出来这些这道题就只剩下“ma”代码的工作了。
#include <iostream>#include <cstdio>#include <string>#include <cmath>#include <algorithm>#include <cstring>#include <map>#include <queue>#include <stack>#define INF 0x3f3f3f3f#define mem(a,b) memset(a,b,sizeof(a));#define For(a,b) for(int i = a;i<b;i++)#define LL long long#define MAX_N 100010using namespace std;int x[MAX_N];struct SegmentTree{ int l; int r; int m0; int m1; int m2; int flag;}p[MAX_N<<2];void BuildTree(int root,int l,int r){ p[root].l = l; p[root].r = r; p[root].m1 = 0; p[root].m2 = 0; p[root].flag = 0; if(l == r) { p[root].m0 = 1; return ; } int mid = (l + r)/2; BuildTree(root<<1,l,mid); BuildTree(root<<1|1,mid+1,r); p[root].m0 = p[root<<1].m0 + p[root<<1|1].m0;// p[root].m1 = p[root<<1].m1 + p[root<<1|1].m1;// p[root].m2 = p[root<<1].m2 + p[root<<1|1].m2;}void mov_e(SegmentTree &Tree){ int x = Tree.m0; Tree.m0 = Tree.m2; Tree.m2 = Tree.m1; Tree.m1 = x; return ;}void pushup(int now){ p[now].m0 = p[now<<1].m0 + p[now<<1|1].m0; p[now].m1 = p[now<<1].m1 + p[now<<1|1].m1; p[now].m2 = p[now<<1].m2 + p[now<<1|1].m2; return ;}void pushdown(int now){ int tag = p[now].flag; if(tag) { tag %= 3; p[now<<1|1].flag += tag; p[now<<1].flag += tag; for(int i = 0; i<tag; i++) { mov_e(p[now<<1]); mov_e(p[now<<1|1]); } p[now].flag = 0; } return ;}void Update(int root,int l,int r){ if(l<=p[root].l && p[root].r <=r) { p[root].flag += 1; mov_e(p[root]); return ; } pushdown(root); int mid = (p[root].l+p[root].r)/2; int ans = 0; if(l>mid) Update(root<<1|1,l,r); else if(r<=mid) Update(root<<1,l,r); else { Update(root<<1|1,l,r); Update(root<<1,l,r); } pushup(root);}int Query(int root,int l,int r){ if(l <= p[root].l && p[root].r <= r) { return p[root].m0; } pushdown(root); int mid = (p[root].l + p[root].r)/2; if(l>mid) return Query(root<<1|1,l,r); else if(r<=mid) return Query(root<<1,l,r); else { int ans = 0,ans1 = 0; ans = Query(root<<1|1,l,r); ans1 = Query(root<<1,l,r); return (ans + ans1); }}int main(){ int T; while(~scanf("%d",&T)) { for(int w = 1; w<=T; w++) { int n,q,x,y,ope; scanf("%d%d",&n,&q); BuildTree(1,1,n); printf("Case %d:\n",w); for(int i = 1; i<=q; i++) { scanf("%d",&ope); if(ope == 0) { scanf("%d %d",&x,&y); Update(1,x+1,y+1); } else if(ope == 1) { int ans = 0; scanf("%d %d",&x,&y); printf("%d\n",Query(1,x+1,y+1)); } } } } return 0;}
- LightOJ 1135 Count the Multiples of 3 (Segmengt + 懒惰标记)
- LightOJ 1135 Count the Multiples of 3(线段树+懒标记)
- lightoj 1135 - Count the Multiples of 3
- lightoj 1135 - Count the Multiples of 3(线段树)
- [线段树] LightOJ 1135 - Count the Multiples of 3
- POJ 2777 Count Color(线段树+懒惰标记)
- Problem1(Multiples of 3 and 5)
- SPOJ7299. Multiples of 3
- hdu 1556 Color the ball(线段树+懒惰标记)
- zoj 2706 Thermal Death of the Universe(线段树区间更新+懒惰标记)
- Problem 1:Find the sum of all the multiples of 3 or 5 below 1000.
- Multiples of 3 and 5
- SPOJ 7299 Multiples of 3
- Multiples of 3 and 5
- Multiples of 3 and 5
- Multiples of 3 and 5
- Multiples of 3 and 5
- LightOJ - 1164 Horrible Queries 线段树+懒惰标记
- 《Linux Shell编程学习笔记之二》:特殊变量(\$*和\$@的区别)、命令替换和变量替换
- 1622-5 孔富晨 总结《2016年10月22日》 【连续第22天总结】
- java web(8)
- 除了在DDMS中可以查看进程,我们还可以通过shell命令来查看
- Android 6.0 解决recyclerview 在 scrollview 中不能全部显示,高度不正常的问题。
- LightOJ 1135 Count the Multiples of 3 (Segmengt + 懒惰标记)
- 面试中常用到机试题
- java web(9)
- Spring-Ioc
- 分别为SecondActivity和ThirdActivity指定了process属性
- IPC进程间通信/跨进程通信
- java编程思想读书笔记 第十五章 泛型 (匿名内部类和擦除)
- java web(10)
- CSS3_Node3_transform&transition&animation