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的数取 ...
随机推荐
- JavaScript中的arguments详解
1. arguments arguments不是真正的数组,它是一个实参对象,每个实参对象都包含以数字为索引的一组元素以及length属性. (function () { console.log(ar ...
- OC中线程安全的单例
@implementation MySingleton + (instancetype)sharedInstance { static MySingleton* instance = nil; sta ...
- IOS使用mkdir创建目录
在IOS真机上可以创建目录的位置只有两个Documents和Caches,如果直接在NSHomeDirectory()上创建目录,会失败,返回的errno含义为操作被禁止. 获取Caches中的一个目 ...
- jvm如何判断对象是否可以被回收
内容基本来自周志明 深入理解Java虚拟机 第二版 第三章 .这本书还可以,不过好像也没什么其他中文的关于jvm比较好的书了 jvm要做垃圾回收时,首先要判断一个对象是否还有可能被使用.那么如何判断一 ...
- Http 学习笔记(一)
介绍 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送协议.. ...
- vbs 解析 json jsonp 方法
昨天说了下用 htmlfile 来解析 html,今天依然用他来解析,htmlfile 是一个 COM 版的 BOM 和 DOM,所以解析 html, 执行 js 完全不在话下,今天就继续解析 jso ...
- 关于JavaScript代码的执行效率总结
Javascript是一门非常灵活的语言,我们可以随心所欲的书写各种风格的代码,不同风格的代码也必然也会导致执行效率的差异,开发过程中零零散散地接触到许多提高代码性能的方法,整理一下平时比较常见并且容 ...
- 约翰·卡马克和他的id Software
John Carmack 上帝花了6天创造了这个世界,id software和它的创始人.引擎师约翰·卡马克(John Carmack),则用6款游戏创造了个人电脑的3D世界. 1992年,id做出了 ...
- thinkphp 原数据更新
调用TP的save方法更新数据时,如果新数据与数据库中得数据一致, 那么执行M('table')->save(data)方法时,该方法会返回false.现在的需求是,哪怕用户要更新的数据与原数据 ...
- HDU 2594 KMP
题目链接 题意:给定两个字符串s1,s2,求最长的s1前缀s使得s为s2的最长后缀,输出该字符串和其长度. 题解:调换s1和s2的顺序,用KMP求解即可. #include <bits/stdc ...