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 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
input
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
output
2
1
0
0
input
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
output
2
0

题目大意:给你一个长度为n的a数组,然后有2个操作

①输入l, r 把a[l],a[l+1]……,a[r]变成 a[r],a[l],a[l+1]……,a[r-1]

②输入l,r,v,求[l,r]中等于v的数有多少

且强制在线

思路:

用deque维护一个序列即可(md一个字母写错了debug一个下午加晚上,TAT)

不过我是很单纯的每次把deque里面的东西每次都取出来,所以跑了2000+ms,不过这个人的代码跑了500ms左右,大家如果要看可以学习一下:http://blog.csdn.net/blankcqk/article/details/38468729

我的代码:

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 1e5 + ;
int a[maxn];
deque<int> que[maxn];
int cnt[][maxn];
int n, q;
int belong[maxn], L[maxn], R[maxn], num, block; void build(){
block = sqrt(n); num = n / block;
if (n % block) num++;
for (int i = ; i <= num; i++)
L[i] = (i - ) * block + , R[i] = i * block;
R[num] = n;
for (int i = ; i <= n; i++)
belong[i] = (i - ) / block + ;
for (int i = ; i <= num; i++)
for (int j = L[i]; j <= R[i]; j++){
cnt[i][a[j]]++;
que[i].push_back(a[j]);
}
} int tmp[], t1[], t2[];
void update(int x, int y){
if (belong[x] == belong[y]){
int px = x - L[belong[x]] + ;///在原来的里面的位置
int py = y - L[belong[y]] + ;
int t = ;
while (!que[belong[x]].empty()){
tmp[++t] = que[belong[x]].front(); que[belong[x]].pop_front();
}
for (int i = ; i <= t; i++){
if (i == px) que[belong[x]].push_back(tmp[py]);
if (i == py) continue;
que[belong[x]].push_back(tmp[i]);
}
return ;
}
int px = x - L[belong[x]] + ;///在原来的里面的位置
int py = y - L[belong[y]] + ;
int tt1 = , tt2 = ;
while (!que[belong[x]].empty()){
t1[++tt1] = que[belong[x]].front(); que[belong[x]].pop_front();
}
while (!que[belong[y]].empty()){
t2[++tt2] = que[belong[y]].front(); que[belong[y]].pop_front();
}
for (int i = ; i <= tt1; i++){
if (i == px) {
que[belong[x]].push_back(t2[py]);
cnt[belong[x]][t2[py]]++;
}
que[belong[x]].push_back(t1[i]);
}
for (int i = ; i <= tt2; i++){
if (i == py) {
cnt[belong[y]][t2[i]]--;
continue;
}
que[belong[y]].push_back(t2[i]);
}
for (int i = belong[x] + ; i <= belong[y]; i++){
int val = que[i - ].back(); que[i - ].pop_back();
cnt[i - ][val]--; cnt[i][val]++;
que[i].push_front(val);
}
} int query(int x, int y, int val){
int ans = ;
if (belong[x] == belong[y]){
int lb = x + - L[belong[x]];
int rb = y + - L[belong[y]];///修改了
int t = ;
while (!que[belong[x]].empty()){
tmp[++t] = que[belong[x]].front(); que[belong[x]].pop_front();
}
for (int i = lb; i <= rb; i++) if (tmp[i] == val) ans++;
for (int i = ; i <= t; i++) que[belong[x]].push_back(tmp[i]);
return ans;
}
///对x的操作
int lb = x + - L[belong[x]], rb = R[belong[x]] + - L[belong[x]];
int t = ;
while (!que[belong[x]].empty()){
tmp[++t] = que[belong[x]].front(); que[belong[x]].pop_front();
}
for (int i = lb; i <= rb; i++) if (tmp[i] == val) ans++;
for (int i = ; i <= t; i++) que[belong[x]].push_back(tmp[i]);
//printf("ans = %d\n", ans); ///对y的操作
lb = , rb = y + - L[belong[y]];
t = ;
while (!que[belong[y]].empty()){
tmp[++t] = que[belong[y]].front(); que[belong[y]].pop_front();
}
for (int i = lb; i <= rb; i++) if (tmp[i] == val) ans++;
for (int i = ; i <= t; i++) que[belong[y]].push_back(tmp[i]);
//printf("ans = %d\n", ans); ///对整体的操作
for (int i = belong[x] + ; i < belong[y]; i++)
ans += cnt[i][val];
return ans;
} int main(){
cin >> n;
for (int i = ; i <= n; i++)
scanf("%d", a + i);
build();
cin >> q;
int lastans = ;
for (int i = ; i <= q; i++){
int ty, l, r, k;
scanf("%d%d%d", &ty, &l, &r);
l = (l + lastans - ) % n + , r = (r + lastans - ) % n + ;
if (l > r) swap(l, r);
if(ty == ){
if (l == r) continue;
update(l, r);
}
else {
scanf("%d", &k);
k = (k + lastans - ) % n + ;
printf("%d\n", lastans = query(l, r, k));
}
}
return ;
}

分块+deque维护 Codeforces Round #260 (Div. 1) D. 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. DP Codeforces Round #260 (Div. 1) A. Boredom

    题目传送门 /* 题意:选择a[k]然后a[k]-1和a[k]+1的全部删除,得到点数a[k],问最大点数 DP:状态转移方程:dp[i] = max (dp[i-1], dp[i-2] + (ll) ...

  3. 递推DP Codeforces Round #260 (Div. 1) A. Boredom

    题目传送门 /* DP:从1到最大值,dp[i][1/0] 选或不选,递推更新最大值 */ #include <cstdio> #include <algorithm> #in ...

  4. Codeforces Round #260 (Div. 2)AB

    http://codeforces.com/contest/456/problem/A A. Laptops time limit per test 1 second memory limit per ...

  5. Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径

    C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...

  6. Codeforces Round #260 (Div. 1) A - Boredom DP

    A. Boredom Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/problem/A ...

  7. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

  8. 暴力+树状数组维护 Codeforces Round #378 (Div. 2) C

    题目大意:给你一个长度为n的数组a,然后数值大的可以合并数值小的,且合并了以后该数组的长度-1.给你一个长度为k目标数组b,问,是否可以从a数组变到b数组,是就yes并且输出步骤.否就输出no 思路: ...

  9. Codeforces Round #260 (Div. 1) 455 A. Boredom (DP)

    题目链接:http://codeforces.com/problemset/problem/455/A A. Boredom time limit per test 1 second memory l ...

随机推荐

  1. “Hello World!”团队第五周第一次会议

    今天是我们团队“Hello World!”团队第五周召开的第一次会议,欢迎我们的新小伙伴刘耀泽同学.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.Todo List 六.会议 ...

  2. 软工实践第八次作业(课堂实战)- 项目UML设计(第五组)

    本次作业博客 团队信息 队名:起床一起肝活队 原组长: 白晨曦(101) 原组员: 李麒 (123) 陈德斌(104) 何裕捷(214) 黄培鑫(217) 王焕仁(233) 林志华(128) 乐忠豪( ...

  3. SGU 126 Boxes(模拟题|二进制)

    Translate:Sgu/126 126. 盒子 time limit per test: 0.5 sec. memory limit per test: 4096 KB 有两个盒子. 第一个盒子里 ...

  4. Alpha事后诸葛亮(团队)

    设想和目标 1.我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件要解决用手机使没有指纹验证硬件的电脑可以利用指纹进行文件的加密.定义的很清楚.我们针对的是 ...

  5. jar读取外部和内部配置文件的问题

    最近修改XX应用的时候,涉及到需要在jar包中读取工程配置文件的问题.在jar包中,读取配置文件,需要单独处理. 项目中的一些配置文件,如dbconfig.properties log4j.xml 不 ...

  6. 【EF】Entity Framework Core 软删除与查询过滤器

    本文翻译自<Entity Framework Core: Soft Delete using Query Filters>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 注意 ...

  7. 【bzoj2669】[cqoi2012]局部极小值 容斥原理+状压dp

    题目描述 有一个n行m列的整数矩阵,其中1到nm之间的每个整数恰好出现一次.如果一个格子比所有相邻格子(相邻是指有公共边或公共顶点)都小,我们说这个格子是局部极小值. 给出所有局部极小值的位置,你的任 ...

  8. 【uoj#244】[UER #7]短路 CDQ分治+斜率优化dp

    题目描述 给出 $(2n+1)\times (2n+1)$ 个点,点 $(i,j)$ 的权值为 $a[max(|i-n-1|,|j-n-1|)]$ ,找一条从 $(1,1)$ 走到 $(2n+1,2n ...

  9. Django安装及简介

    一. Django简介 Python下有许多款不同的 Web 框架.Django是重量级选手中最有代表性的一位.许多成功的网站和APP都基于Django. Django是一个开放源代码的Web应用框架 ...

  10. WIN7 右下角音量图标不见了

    1.呼叫出  任务管理器,结束掉 explorer.exe 进程 2.新建任务,浏览,找到 C:/windows/system32/systray.exe,确定加载 3.新建任务,输入explorer ...