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的数取 ...
随机推荐
- Git2:Git基本操作
目录 一.git全局配置 二.创建一个版本库 三.git的常用操作 1.版本提交与回退 1.1.版本提交 1.2.版本回退 2.工作区.版本库与暂存区 2.1.工作区 2.2.版本库 3.管理文件的修 ...
- Mongo副本集搭建
解压mongodb-linux-x86_64-rhel70-3.2.0.tgz 将解压后的bin路径添加到系统环境变量,保证mongo.mongod等命令可用 创建副本集目录mongo/27017.2 ...
- vue短信验证性能优化写入localstorage中
平时我们在项目中进行注册等的时候,会经常用到短信验证的功能,但是现在现在很多短信验证都是存在下面几个问题,例如短信验证时间为60s的时候, 1. 当点击完按钮时,倒计时还没到60s过完时,刷新浏览器, ...
- Codeforces 877 D. Olya and Energy Drinks
http://codeforces.com/contest/877/problem/D D. Olya and Energy Drinks time limit per test 2 second ...
- 算法习题-FFT
Q1(hdu1402): 给出两个很大的数字A,B,计算二者乘积. 分析:这个题目java应该能过,用FFT做能够加速计算.这里将字符串A按权(10进制)展开,前面的系数就是多项式的系数,这样就构造出 ...
- [整理]前端模块化开发AMD CMD
前端模块化开发的价值 AMD (中文版) CMD 模块定义规范 标准构建 http://seajs.org http://chaoskeh.com/blog/why-seajs.html http:/ ...
- 地位尴尬的WebForm、ASP.NET核心知识(10)
WebForm之烦恼 1.winform式的开发方式 WebForm的开发方式中,只需要从工具箱中拖拽一个控件,再从.aspx.cs中写控件的事件逻辑,就好了. 微软为我们做了很多工作,很多东西不需要 ...
- GDB基本用法
基本命令 进入GDB:#gdb test test是要调试的程序,由gcc test.c -g -o test生成.进入后提示符变为(gdb) . 查看源码:(gdb) l 源码会进行行号提示. 如果 ...
- post请求远程url 报错“基础连接已经关闭...Authentication.AuthenticationException...远程证书无效”解决方案
当我们有时用代码编写post请求url远程地址会报“基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系. ---> System.Security.Authentication.A ...
- python面向对象——类
from:http://www.runoob.com/python3/python3-class.html Python3 面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在P ...