CF455D. Serega and Fun
4 seconds
256 megabytes
standard input
standard output
Serega loves fun. However, everyone has fun in the unique manner. Serega has fun by solving query problems. One day Fedor came up with such a problem.
You are given an array a consisting of n positive integers and queries to it. The queries can be of two types:
- Make a unit cyclic shift to the right on the segment from l to r (both borders inclusive). That is rearrange elements of the array in the following manner:a[l], a[l + 1], ..., a[r - 1], a[r] → a[r], a[l], a[l + 1], ..., a[r - 1].
- Count how many numbers equal to k are on the segment from l to r (both borders inclusive).
Fedor hurried to see Serega enjoy the problem and Serega solved it really quickly. Let's see, can you solve it?
The first line contains integer n (1 ≤ n ≤ 105) — the number of elements of the array. The second line contains n integersa[1], a[2], ..., a[n] (1 ≤ a[i] ≤ n).
The third line contains a single integer q (1 ≤ q ≤ 105) — the number of queries. The next q lines contain the queries.
As you need to respond to the queries online, the queries will be encoded. A query of the first type will be given in format: 1 l'i r'i. A query of the second type will be given in format: 2 l'i r'i k'i. All the number in input are integer. They satisfy the constraints: 1 ≤ l'i, r'i, k'i ≤ n.
To decode the queries from the data given in input, you need to perform the following transformations:
li = ((l'i + lastans - 1) mod n) + 1; ri = ((r'i + lastans - 1) mod n) + 1; ki = ((k'i + lastans - 1) mod n) + 1.
Where lastans is the last reply to the query of the 2-nd type (initially, lastans = 0). If after transformation li is greater than ri, you must swap these values.
For each query of the 2-nd type print the answer on a single line.
分块搞搞。。比较坑,不好调试。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + ;
const int maxs = ;
const int maxb = maxn / maxs + ;
const int maxr = ;
const int maxv = maxs + maxr + ; const bool D = false; struct Block {
int size, ele[maxv], cnt[maxn], vals[maxv], tot;
Block() {
size = tot = ;
memset(cnt, , sizeof cnt);
memset(vals, , sizeof vals);
}
void append(const int &val) {
++cnt[val];
vals[++tot] = val;
ele[++size] = val;
}
void init() {
for(int i = ; i <= tot; ++i)
cnt[vals[i]] = ;
tot = size = ;
}
} b[maxb];
int a[maxn], t[maxn], n, q, nb; void print() {
if(D) {
printf("%d\n", nb);
for(int i = ; i <= nb; ++i) {
for(int j = ; j <= b[i].size; ++j)
printf("%d ", b[i].ele[j]);
puts("");
}
}
} void build() {
nb = ;
for(int i = ; i <= n; i += maxs) {
++nb;
for(int up = min(n, i + maxs - ), j = i; j <= up; ++j)
b[nb].append(a[j]);
}
}
void re_build() {
n = ;
for(int i = ; i <= nb; ++i) {
for(int j = ; j <= b[i].size; ++j)
t[++n] = b[i].ele[j];
b[i].init();
}
for(int i = ; i <= n; ++i) a[i] = t[i];
build();
} int erase(int pos) {
for(int i = , sum = ; i <= nb; ++i) {
sum += b[i].size;
if(pos <= sum) {
sum -= b[i].size;
pos -= sum;
int ret = b[i].ele[pos];
for(int j = pos + ; j <= b[i].size; ++j)
b[i].ele[j - ] = b[i].ele[j];
--b[i].cnt[ret];
--b[i].size;
return ret;
}
}
return ;
} void insert(int pos, int val) {
for(int i = , sum = ; i <= nb; ++i) {
sum += b[i].size;
if(pos <= sum) {
sum -= b[i].size;
pos -= sum;
++b[i].size;
for(int j = b[i].size; pos < j; --j)
b[i].ele[j] = b[i].ele[j - ];
b[i].vals[++b[i].tot] = val;
++b[i].cnt[val];
b[i].ele[pos + ] = val;
return ;
}
}
} void shift(int l, int r) {
if(l == r) return ;
insert(l - , erase(r));
} int count(int pos, int val) {
if(pos <= ) return ;
int ret = ;
for(int i = , sum = ; i <= nb; ++i) {
sum += b[i].size;
if(pos <= sum) {
sum -= b[i].size;
pos -= sum;
for(int j = ; j <= pos; ++j)
ret += (b[i].ele[j] == val);
return ret;
} else {
ret += b[i].cnt[val];
}
}
return ;
} int main() {
scanf("%d", &n);
for(int i = ; i <= n; scanf("%d", &a[i]), ++i);
build();
print();
scanf("%d", &q);
for(int T = , last_ans = , type, l, r, val; T <= q; ++T) {
scanf("%d%d%d", &type, &l, &r);
l = (last_ans + l - ) % n + , r = (last_ans + r - ) % n + ;
if(r < l) swap(l, r);
if(type == ) {
shift(l, r);
} else {
scanf("%d", &val);
val = (last_ans + val - ) % n + ;
last_ans = count(r, val) - count(l - , val);
printf("%d\n", last_ans);
}
if(T % maxr == ) re_build();
}
return ;
}
CF455D. Serega and Fun的更多相关文章
- Codeforces Round #260 (Div. 1) D. Serega and Fun 分块
D. Serega and Fun Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/pro ...
- CF 455D. Serega and Fun [分块 deque]
Serega and Fun 题意: [l,r]循环右移一位,查询区间内某个数出现次数 为什么好多人用链表?反正我是不会写双向链表 完全可以分块然后模拟啊...中间的块只会插入删除一个元素呀....用 ...
- 分块+deque维护 Codeforces Round #260 (Div. 1) D. Serega and Fun
D. Serega and Fun time limit per test 4 seconds memory limit per test 256 megabytes input standard i ...
- Serega and Fun CodeForces - 455D (分块 或 splay)
大意:给定n元素序列, 2种操作 将区间$[l,r]$循环右移1位 询问$[l,r]$中有多少个等于k的元素 现在给定q个操作, 输出操作2的询问结果, 强制在线 思路1: 分块 每个块内维护一个链表 ...
- Serega and Fun Codeforces - 455D || queue
https://codeforces.com/problemset/problem/455/D 其实方法很多,然而当初一个也想不到... 1.分块,块内用链表维护 修改[l,r]就当成删除第r个元素, ...
- CodeForces 151B Phone Numbers
Phone Numbers Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Sub ...
- CodeForces - 455D
Serega loves fun. However, everyone has fun in the unique manner. Serega has fun by solving query pr ...
- CF数据结构练习
1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...
随机推荐
- numpy/arrayobject.h”: No such file or directory
import numpyimport pyximportpyximport.install(setup_args={"script_args":["--compiler= ...
- Hadoop生态圈-Hbase的API常见操作
Hadoop生态圈-Hbase的API常见操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- KVM管理概述
一.使用QEMU管理虚拟机 1.KVM指南 https://activedoc.opensuse.org/book/opensuse-virtualization-with-kvm/part-iii- ...
- bzoj千题计划170:bzoj1968: [Ahoi2005]COMMON 约数研究
http://www.lydsy.com/JudgeOnline/problem.php?id=1968 换个角度 一个数可以成为几个数的约数 #include<cstdio> #incl ...
- HDU 3032 multi-sg 打表找规律
普通NIM规则加上一条可以分解为两堆,标准的Multi-SG游戏 一般Multi-SG就是根据拓扑图计算SG函数,这题打表后还能发现规律 sg(1)=1 sg(2)=2 sg(3)=mex{0,1,2 ...
- Zabbix监控PV和UV
Zabbix-server:172.21.97.153 Zabbix-agent(Nginx):172.17.27.61 # Nginx日志如下: # head -3 Syz.access.log w ...
- [整理]VS2010中如何添加“依赖","库目录","包含目录"
VS2010中如何添加“依赖","库目录","包含目录" 1. 添加编译所需要(依赖)的 lib 文件[解决方案资源管理器]“项目->属性-&g ...
- 纯javascript代码实现浏览器图片选择预览、旋转、批量上传
工作中遇到的业务场景,和同事一起研究了下,主要是为了兼容IE版本 其实就是一些琐碎的知识点在网上搜集下解决方式,然后集成了下,主要有以下点: 1. IE input type=file的图片预览要用I ...
- bzoj 2111 [ZJOI2010]Perm 排列计数(DP+lucas定理)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2111 [题意] 给定n,问1..n的排列中有多少个可以构成小根堆. [思路] 设f[i ...
- MySQL字符集 GBK、GB2312、UTF8区别 解决 MYSQL中文乱码问题 收藏 MySQL中涉及的几个字符集
MySQL中涉及的几个字符集 character-set-server/default-character-set:服务器字符集,默认情况下所采用的.character-set-database:数据 ...