CodeForces - 455D
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 integers a[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.
Examples
7
6 6 2 7 4 2 5
7
1 3 6
2 2 4 2
2 2 4 7
2 2 2 5
1 2 6
1 1 4
2 1 7 3
2
1
0
0
8
8 4 2 2 7 7 8 8
8
1 8 8
2 8 1 7
1 8 1
1 7 3
2 8 8 3
1 1 4
1 2 7
1 4 5
2
0
分块+双端队列
每次更新对每块进行操作:
1.中间的整块:取出最后一个放入下一块的前端;
2.两端的块:左边的块放入给定右边界位置的数字,右端的块删除给定右边界位置的数字
每次询问:
中间的整块直接查询,两边的块遍历可访问的位置;
#include <cstdio>
#include <stack>
#include <cmath>
#include <queue>
#include <string>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm> #define lid id<<1
#define rid id<<1|1
#define closein cin.tie(0)
#define scac(a) scanf("%c",&a)
#define scad(a) scanf("%d",&a)
#define print(a) printf("%d\n",a)
#define debug printf("hello world")
#define form(i,n,m) for(int i=n;i<m;i++)
#define mfor(i,n,m) for(int i=n;i>m;i--)
#define nfor(i,n,m) for(int i=n;i>=m;i--)
#define forn(i,n,m) for(int i=n;i<=m;i++)
#define scadd(a,b) scanf("%d%d",&a,&b)
#define memset0(a) memset(a,0,sizeof(a))
#define scaddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define scadddd(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d) #define INF 0x3f3f3f3f
#define maxn 100005
typedef long long ll;
using namespace std;
//---------AC(^-^)AC---------\\ deque<int>::iterator it;
struct node
{
deque<int> q;
int num[maxn];
}block[];
int n, m, blo, ans;
int pos[maxn]; void update(int a, int b)
{
if (pos[a] == pos[b])
{
it = block[pos[b]].q.begin() + ((b-)%blo);
int tmp = *it;
block[pos[b]].q.erase(block[pos[b]].q.begin() + ((b-)%blo));
block[pos[b]].q.insert(block[pos[a]].q.begin() + ((a-)%blo), tmp);
}
else
{
it = block[pos[b]].q.begin() + ((b-)%blo);
int tmp = *it;
block[pos[b]].num[tmp]--;
block[pos[b]].q.erase(block[pos[b]].q.begin() + ((b-)%blo));
for (int i = pos[a]; i < pos[b]; i++) {
int x = block[i].q.back();
block[i].q.pop_back();
block[i].num[x]--;
block[i + ].q.push_front(x);
block[i + ].num[x]++;
}
block[pos[a]].q.insert(block[pos[a]].q.begin() + ((a-)%blo), tmp);
block[pos[a]].num[tmp]++;
}
}
void query(int a, int b, int c)
{
ans=;
if (pos[a] == pos[b])
{
for (it = block[pos[a]].q.begin() + ((a-)%blo); it <= block[pos[a]].q.begin() + ((b-)%blo); it++)
if ((*it) == c) ans++;
}
else {
for (int i = pos[a] + ; i < pos[b]; i++) ans += block[i].num[c];
for (it = block[pos[a]].q.begin() + ((a-)%blo); it < block[pos[a]].q.end(); it++)
if ((*it) == c) ans++;
for (it = block[pos[b]].q.begin(); it <= block[pos[b]].q.begin() + ((b-)%blo); it++)
if ((*it) == c) ans++;
}
printf("%d\n", ans);
} int main()
{
scad(n);
blo = sqrt(n);
if (n%blo) blo++;
forn(i, , n) pos[i] = (i - )/blo + ; forn(i, , n) {
int x;
scad(x);
int s = pos[i];
block[s].q.push_back(x);
block[s].num[x]++;
}
scad(m);
ans = ;
while (m--)
{
int op;
scad(op);
if (op == )
{
int l, r;
scadd(l, r);
l = (l + ans - ) % n+;
r = (r + ans - ) % n+;
if (l > r) swap(l, r);
update(l, r);
}
else
{
int l, r, k;
scaddd(l, r, k);
l = (l + ans - ) % n + ;
r = (r + ans - ) % n + ;
k = (k + ans - ) % n + ;
if (l > r) swap(l, r);
query(l, r, k);
}
}
return ;
}
CodeForces - 455D的更多相关文章
- CodeForces 455D 分块
题目链接:http://codeforces.com/problemset/problem/455/D 题意:给定一个长度为n的序列a[]. m次操作.共有两种操作 1 l r:将序列的a[l].a[ ...
- Serega and Fun Codeforces - 455D || queue
https://codeforces.com/problemset/problem/455/D 其实方法很多,然而当初一个也想不到... 1.分块,块内用链表维护 修改[l,r]就当成删除第r个元素, ...
- Serega and Fun CodeForces - 455D (分块 或 splay)
大意:给定n元素序列, 2种操作 将区间$[l,r]$循环右移1位 询问$[l,r]$中有多少个等于k的元素 现在给定q个操作, 输出操作2的询问结果, 强制在线 思路1: 分块 每个块内维护一个链表 ...
- Codeforces 455D 分块+链表
题意: 给定一个长度为 N 的序列两种操作1 l r 将[l,r]的数向右循环移位 2 l r 询问[l,r]内有多少个数等于 k其中 N,Q≤105,ai≤N 强制在线 思路: 1. 每块用一个链表 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
随机推荐
- PTA编程总结3—抓老鼠啊~亏了还是赚了?
题目: 某地老鼠成灾,现悬赏抓老鼠,每抓到一只奖励10元,于是开始跟老鼠斗智斗勇:每天在墙角可选择以下三个操作:放置一个带有一块奶酪的捕鼠夹(T),或者放置一块奶酪(C),或者什么也不放(X).捕鼠夹 ...
- Redis学习--Redis数据类型
Redis的5种基本类型 1.String 2.Hash 3.List 4.Set 5.Sorted Set String常见用法 1.get key 返回value 2.set key value ...
- CSP中的选择
P ∩ Q (P or Q) 由机器做出选择,环境无法控制,设计软件时只实现一个即可 P[]Q一般选择(Genral choice) 环境可以控制选择P或Q,若P不可接受这个动作,则执行Q,若Q不可接 ...
- All You Can Code 2008 (Romanian Contest) A - Tree Search
A - Tree Search 思路: 经典树形dp dp[i][0]表示i的子树中以i为端点的最大链 dp[i][1]表是整棵树中除去i的子树剩下的部分以i为端点的最大链 最后答案就是以i为端点的最 ...
- Codeforces Round #349 (Div. 1)E. Forensic Examination
题意:给一个初始串s,和m个模式串,q次查询每次问你第l到第r个模式串中包含\(s_l-s_r\)子串的最大数量是多少 题解:把初始串和模式串用分隔符间隔然后建sam,我们需要找到在sam中表示\(s ...
- ThinkPHP5.0源码学习之框架启动流程
ThinkPHP5框架的启动流程图如下: ThinkPHP5的启动流程按照文件分为三步: 1.请求入口(public/index.php) 2.框架启动(thinkphp/start.php) 3.应 ...
- GoEasy的使用
GoEasy介绍 http请求短连接,一次请求响应后关闭,而GoEasy建立了客户端与服务器之间的长连接. goeasy支持服务器到客户端的消息发布,客户端到客户端的消息发布 GoEasy用来做什么 ...
- 获取页面定位元素left top
1原生方法: 第一种方法,比较简单,就是直接通过obj.style.left和obj.style.top,但是有局限性,这种获取的方法只能获取到行内样式的left和top的属性值,不能获取到style ...
- vue-router中query和params传参(接收参数)以及$router、$route的区别
query传参: this.$router.push({ path:'/...' query:{ id:id } }) 接收参数:this.$route.query.id params传值: 传参: ...
- OpenStack 部署步骤详解(mitaka/ocata/一键部署)
正文 OpenStack作为一个由NASA(美国国家航空航天局)和Rackspace合作研发并发起的,开放源代码项目的云计算管理平台项目.具体知识我会在后面文章中做出介绍,本章主要按步骤给大家演示在C ...