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的数取 ...
随机推荐
- Jmeter之性能测试
Jmeter除了可以做接口测试外,还可以做性能测试.在 Jmeter中做性能测试,需要做如下相关设置 图片中有10个线程,Ramp-Up Period(in seconds)=1,那么线程的启动时间间 ...
- 哈密顿图 哈密顿回路 哈密顿通路(Hamilton)
本文链接:http://www.cnblogs.com/Ash-ly/p/5452580.html 概念: 哈密顿图:图G的一个回路,若它通过图的每一个节点一次,且仅一次,就是哈密顿回路.存在哈密顿回 ...
- bzoj千题计划114:bzoj1791: [Ioi2008]Island 岛屿
http://www.lydsy.com/JudgeOnline/problem.php?id=1791 就是求所有基环树的直径之和 加手工栈 #include<cstdio> #incl ...
- Java并发编程原理与实战三十四:并发容器CopyOnWriteArrayList原理与使用
1.ArrayList的实现原理是怎样的呢? ------>例如:ArrayList本质是实现了一个可变长度的数组. 假如这个数组的长度为10,调用add方法的时候,下标会移动到下一位,当移动到 ...
- soj1011. Lenny's Lucky Lotto
1011. Lenny's Lucky Lotto Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Lenny like ...
- Android 动态添加线性布局(.java文件内) 实现控件按比例分割空间
这里实现 两个 编辑框同一水平上 按1:1分割空间 这里的1:1 比例可以通过 lp1.weight : 1p2.weight =m:n 实现 { LinearLayout l=new Linea ...
- LintCode 58: Compare Strings
LintCode 58: Compare Strings 题目描述 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是大写字母. 样例 给出A = "ABCD&q ...
- 【AtCoder Grand Contest 012C】Tautonym Puzzle [构造]
Tautonym Puzzle Time Limit: 50 Sec Memory Limit: 256 MB Description 定义一个序列贡献为1,当且仅当这个序列 由两个相同的串拼接而成 ...
- 20155306 2016-2017-2 《Java程序设计》第七周学习总结
20155306 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 第十三章 时间与日期 三种时间: 格林威治标准时间(GMT)的正午是太阳抵达天空最高点之时, ...
- 未来人类T5 安装win10,ubuntu双系统
1.首先确保win10已经安装,u盘中已刻录好系统,下载好英伟达最新驱动保存在u盘中,压缩100g的磁盘空间给ubuntu. 2.设置双显卡模式,重启时按F7选择进入u盘启动. 3.进入安装界面,选择 ...