D. Serega and Fun
time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

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:

  1. 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].
  2. 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?

Input

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.

Output

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的更多相关文章

  1. 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 ...

  2. CF 455D. Serega and Fun [分块 deque]

    Serega and Fun 题意: [l,r]循环右移一位,查询区间内某个数出现次数 为什么好多人用链表?反正我是不会写双向链表 完全可以分块然后模拟啊...中间的块只会插入删除一个元素呀....用 ...

  3. 分块+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 ...

  4. Serega and Fun CodeForces - 455D (分块 或 splay)

    大意:给定n元素序列, 2种操作 将区间$[l,r]$循环右移1位 询问$[l,r]$中有多少个等于k的元素 现在给定q个操作, 输出操作2的询问结果, 强制在线 思路1: 分块 每个块内维护一个链表 ...

  5. Serega and Fun Codeforces - 455D || queue

    https://codeforces.com/problemset/problem/455/D 其实方法很多,然而当初一个也想不到... 1.分块,块内用链表维护 修改[l,r]就当成删除第r个元素, ...

  6. CodeForces 151B Phone Numbers

     Phone Numbers Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Sub ...

  7. CodeForces - 455D

    Serega loves fun. However, everyone has fun in the unique manner. Serega has fun by solving query pr ...

  8. CF数据结构练习

    1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...

随机推荐

  1. Java基础-初识面向对象编程(Object-Oriented-Programming)

    Java基础-初识面向对象编程(Object-Oriented-Programming) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Java是一门面向对象的程序设计语言.那么什 ...

  2. tomcat启动时,内存溢出,Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "main"

     问题原因 通过tomcat启动项目,也许是因为项目太大,配置的内存不够用了.老是报内存溢出的问题. 解决办法 1.选中项目 右键 run as ->Run Configurations... ...

  3. Vue入坑教程(一)——搭建vue-cli脚手架

    1. Vue简介 详细内容可以参考官网Vue.js 1)兼容性 Vue 不支持 IE8 及以下版本,因为 Vue 使用了 IE8 无法模拟的 ECMAScript 5 特性.但它支持所有兼容 ECMA ...

  4. Sparse Filtering简介

    当前很多的特征学习(feature learning)算法需要很多的超参数(hyper-parameter)调节, Sparse Filtering则只需要一个超参数--需要学习的特征的个数, 所以非 ...

  5. hdu 1254 推箱子(双重bfs)

    题目链接 Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能 ...

  6. 【划水闲谈】Terraria 1.3.5更新

    我知道这本应是一个算法博客,但又有谁规定了不能发点其他内容呢? Terraria,一个有趣的沙盒游戏.在这里,你可以建造,挖掘,开始一次又一次新的冒险. 4月19日,Re-Logic承诺的官方中文版终 ...

  7. 已知可生成0~4的rand5(),实现生成0~6的rand7()

    若已知生成0~6的rand7(),求生成0~4的rand5(),则一个方法就是不断生成0~7的数,直到这个数满足0~4就返回. int rand5(){ int res; do{ res = rand ...

  8. 揭秘Patchwork APT攻击-恶意软件样本BADNEWS

    1.前言 在2016年左右研究人员发现一个与东南亚和中国南海问题的APT攻击,该APT攻击利用MS Offcie系列漏洞通过钓鱼邮件的形式欺骗受害者点击木马.以美国在内的各国政府和公司为目标发送了大量 ...

  9. 对于Linux平台下C语言开发中__sync_函数的认识(转)

    reference:http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html#Atomic-Builtins A built-i ...

  10. mysql只读模式的设置方法与实验【转】

    在MySQL数据库中,在进行数据迁移和从库只读状态设置时,都会涉及到只读状态和Master-slave的设置和关系. 经过实际测试,对于MySQL单实例数据库和master库,如果需要设置为只读状态, ...