1135 - Count the Multiples of 3
| Time Limit: 3 second(s) | Memory Limit: 64 MB |
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
- Increase the numbers between indices i and j (inclusive) by 1. This is represented by the command '0 i j'.
- 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 |
Output for 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 |
Case 1: 2 3 0 2 |
Note
Dataset is huge, use faster i/o methods.
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<queue>
7 #include<math.h>
8 #include<vector>
9 using namespace std;
10 typedef struct node
11 {
12 int mod1;
13 int mod2;
14 int mod3;
15 int val;
16 node()
17 {
18 val = 0;
19 }
20 } tr;
21 tr tree[4*200005];
22 void build(int l,int r,int k);
23 void mov(int k);
24 void in(int l,int r,int k,int nn,int mm);
25 void up(int k);
26 int query(int l,int r,int k,int nn,int mm);
27 int main(void)
28 {
29 int i,j;
30 int T;
31 int __ca = 0;
32 scanf("%d",&T);
33 while(T--)
34 {
35 int n,m;
36 scanf("%d %d",&n,&m);
37 memset(tree,0,sizeof(tree));
38 build(0,n-1,0);
39 printf("Case %d:\n",++__ca);
40 while(m--)
41 {
42 int val ,x,y;
43 scanf("%d%d%d",&val,&x,&y);
44 if(val)
45 {
46 printf("%d\n",query(x,y,0,0,n-1));
47 }
48 else
49 {
50 in(x,y,0,0,n-1);
51 }
52 }
53 }
54 return 0;
55 }
56 void build(int l,int r,int k)
57 {
58 if(l==r)
59 {
60 tree[k].mod3 = 1;
61 tree[k].mod1 = 0;
62 tree[k].mod2 = 0;
63 tree[k].val = 0;
64 return ;
65 }
66 tree[k].val = 0;
67 build(l,(l+r)/2,2*k+1);
68 build((l+r)/2+1,r,2*k+2);
69 tree[k].mod1 = tree[2*k+1].mod1 + tree[2*k+2].mod1;
70 tree[k].mod2 = tree[2*k+1].mod2 + tree[2*k+2].mod2;
71 tree[k].mod3 = tree[2*k+1].mod3 + tree[2*k+2].mod3;
72 }
73 void mov(int k)
74 {
75 int x = tree[k].mod1;
76 tree[k].mod1 = tree[k].mod3;
77 tree[k].mod3 = tree[k].mod2;
78 tree[k].mod2 = x;
79 return ;
80 }
81 void in(int l,int r,int k,int nn,int mm)
82 {
83 if(l > mm||r < nn)
84 {
85 return ;
86 }
87 else if(l <= nn&& r>=mm)
88 {
89 tree[k].val++;
90 tree[k].val%=3;
91 int x = tree[k].val;
92 tree[k].val = 0;
93 if(x)
94 {
95 tree[2*k+1].val += x;
96 tree[2*k+2].val +=x;
97 tree[2*k+1].val%=3;
98 tree[2*k+2].val%=3;
99 while(x)
100 {
101 mov(k);
102 x--;
103 }
104 }
105 up(k);
106 }
107 else
108 {
109 int x= tree[k].val;
110 tree[2*k+1].val = (tree[2*k+1].val + x)%3;
111 tree[2*k+2].val = (tree[2*k+2].val + x)%3;
112 tree[k].val = 0;
113 in(l,r,2*k+1,nn,(nn+mm)/2);
114 in(l,r,2*k+2,(nn+mm)/2+1,mm);
115 }
116 }
117 void up(int k)
118 {
119 if(k == 0)
120 return ;
121 while(k)
122 {
123 k = (k-1)/2;
124 int xll = 2*k+1;
125 int xrr = 2*k+2;
126 if(tree[xll].val)
127 {
128 int x = tree[xll].val;
129 {
130 tree[xll].val = 0;
131 tree[2*xll+1].val = (tree[2*xll+1].val + x)%3;
132 tree[2*xll+2].val = (tree[2*xll+2].val + x)%3;
133 while(x)
134 {
135 mov(xll);
136 x--;
137 }
138 }
139 }
140 if(tree[xrr].val)
141 {
142 int x= tree[xrr].val;
143 tree[2*xrr+1].val = (tree[2*xrr+1].val+x)%3;
144 tree[2*xrr+2].val = (tree[2*xrr+2].val+x)%3;
145 tree[xrr].val = 0;
146 while(x)
147 {
148 mov(xrr);
149 x--;
150 }
151 }
152 tree[k].mod1 = tree[2*k+1].mod1+tree[2*k+2].mod1;
153 tree[k].mod2 = tree[2*k+1].mod2+tree[2*k+2].mod2;
154 tree[k].mod3 = tree[2*k+1].mod3+tree[2*k+2].mod3;
155 }
156 }
157 int query(int l,int r,int k,int nn,int mm)
158 {
159 if(l > mm||r < nn)
160 {
161 return 0;
162 }
163 else if(l <=nn&&r>=mm)
164 {
165 if(tree[k].val)
166 {
167 int x= tree[k].val;
168 tree[k].val = 0;
169 tree[2*k+1].val = (tree[2*k+1].val+x)%3;
170 tree[2*k+2].val = (tree[2*k+2].val+x)%3;
171 while(x)
172 {
173 mov(k);
174 x--;
175 }
176 }
177 up(k);
178 return tree[k].mod3;
179 }
180 else
181 {
182 if(tree[k].val)
183 {
184 int x = tree[k].val;
185 tree[k].val = 0;
186 tree[2*k+1].val = (tree[2*k+1].val+x)%3;
187 tree[2*k+2].val = (tree[2*k+2].val+x)%3;
188 }
189 int nx = query(l,r,2*k+1,nn,(nn+mm)/2);
190 int ny = query(l,r,2*k+2,(nn+mm)/2+1,mm);
191 return nx+ny;
192 }
193 }
1135 - Count the Multiples of 3的更多相关文章
- LightOJ 1135 - Count the Multiples of 3 线段树
http://www.lightoj.com/volume_showproblem.php?problem=1135 题意:给定两个操作,一个对区间所有元素加1,一个询问区间能被3整除的数有多少个. ...
- nodejs api 中文文档
文档首页 英文版文档 本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可. Node.js v0.10.18 手册 & 文档 索引 | 在单一页面中浏览 | JSON格 ...
- 【LeetCode】204 - Count Primes
Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...
- Java [Leetcode 204]Count Primes
题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...
- Codeforces Round #506 (Div. 3) D. Concatenated Multiples
D. Concatenated Multiples You are given an array aa, consisting of nn positive integers. Let's call ...
- POJ 1135 -- Domino Effect(单源最短路径)
POJ 1135 -- Domino Effect(单源最短路径) 题目描述: 你知道多米诺骨牌除了用来玩多米诺骨牌游戏外,还有其他用途吗?多米诺骨牌游戏:取一 些多米诺骨牌,竖着排成连续的一行,两 ...
- C#中Length和Count的区别(个人观点)
这篇文章将会很短...短到比你的JJ还短,当然开玩笑了.网上有说过Length和count的区别,都是很含糊的,我没有发现有 文章说得比较透彻的,所以,虽然这篇文章很短,我还是希望能留在首页,听听大家 ...
- [PHP源码阅读]count函数
在PHP编程中,在遍历数组的时候经常需要先计算数组的长度作为循环结束的判断条件,而在PHP里面对数组的操作是很频繁的,因此count也算是一个常用函数,下面研究一下count函数的具体实现. 我在gi ...
- EntityFramework.Extended 实现 update count+=1
在使用 EF 的时候,EntityFramework.Extended 的作用:使IQueryable<T>转换为update table set ...,这样使我们在修改实体对象的时候, ...
随机推荐
- GIFS服务的使用
1.安装Samba服务 登录192.168.200.20虚拟机,首先修改主机名,命令如下: [root@nfs-client ~]# hostnamectl set-hostname samba [r ...
- Typora数学公式输入指导手册
Markdown 公式指导手册 公式大全的链接 https://www.zybuluo.com/codeep/note/163962#mjx-eqn-eqsample 目录 Markdown 公式指导 ...
- 【PS算法理论探讨一】 Photoshop中两个32位图像混合的计算公式(含不透明度和图层混合模式)。
大家可以在网上搜索相关的主题啊,你可以搜索到一堆,不过似乎没有那一个讲的很全面,我这里抽空整理和测试一下数据,分享给大家. 我们假定有2个32位的图层,图层BG和图层FG,其中图层BG是背景层(位于下 ...
- Kafka(一)【概述、入门、架构原理】
目录 一.Kafka概述 1.1 定义 二.Kafka快速入门 2.1 安装部署 2.2 配置文件解析 2.3Kafka群起脚本 2.4 topic(增删改查) 2.5 生产和消费者命令行操作 三.K ...
- spring注解事务管理
使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间<beans xmlns="http://www.springframework.org/schema/b ...
- Maven错误收集
Eclipse 创建项目时报错 Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart:1 ...
- MySQL 用户权限相关命令
##1.创建用户: create user test identified by '123456';##identified后面跟密码 ##2.查询所有用户: select user from mys ...
- SQL注入 (1) SQL注入类型介绍
SQL注入 SQL注入介绍与分类 1. 什么是sql注入 通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. 2. sql注入类型 按照注入 ...
- Lock 锁的实现
锁的种类 自旋锁(spinlock):无法获得锁,就一直循环获取,适合短时间的加锁 睡眠锁(sleeplock):为了防止长时间的循环等待,在获取不到锁时,进程陷入睡眠,当锁释放时对睡眠进程进行唤醒 ...
- 如何为Dash/Zeal生成c++ 文档: 以abseil文档为例
目录 1. 软件安装 2 Sample源文件下载: 3. 生成步骤 3.1 使用doxygen生成html文件 3.2 使用docsetutil 生成 dash/Zeal 格式 1. 软件安装: 1. ...