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
  1. 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
  1. 2
    1
    0
    0
Input
  1. 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
  1. 2
    0
    分块+双端队列
    每次更新对每块进行操作:
    1.中间的整块:取出最后一个放入下一块的前端;
    2.两端的块:左边的块放入给定右边界位置的数字,右端的块删除给定右边界位置的数字
    每次询问:
    中间的整块直接查询,两边的块遍历可访问的位置;
  1. #include <cstdio>
  2. #include <stack>
  3. #include <cmath>
  4. #include <queue>
  5. #include <string>
  6. #include <queue>
  7. #include <cstring>
  8. #include <iostream>
  9. #include <algorithm>
  10.  
  11. #define lid id<<1
  12. #define rid id<<1|1
  13. #define closein cin.tie(0)
  14. #define scac(a) scanf("%c",&a)
  15. #define scad(a) scanf("%d",&a)
  16. #define print(a) printf("%d\n",a)
  17. #define debug printf("hello world")
  18. #define form(i,n,m) for(int i=n;i<m;i++)
  19. #define mfor(i,n,m) for(int i=n;i>m;i--)
  20. #define nfor(i,n,m) for(int i=n;i>=m;i--)
  21. #define forn(i,n,m) for(int i=n;i<=m;i++)
  22. #define scadd(a,b) scanf("%d%d",&a,&b)
  23. #define memset0(a) memset(a,0,sizeof(a))
  24. #define scaddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
  25. #define scadddd(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
  26.  
  27. #define INF 0x3f3f3f3f
  28. #define maxn 100005
  29. typedef long long ll;
  30. using namespace std;
  31. //---------AC(^-^)AC---------\\
  32.  
  33. deque<int>::iterator it;
  34. struct node
  35. {
  36. deque<int> q;
  37. int num[maxn];
  38. }block[];
  39. int n, m, blo, ans;
  40. int pos[maxn];
  41.  
  42. void update(int a, int b)
  43. {
  44. if (pos[a] == pos[b])
  45. {
  46. it = block[pos[b]].q.begin() + ((b-)%blo);
  47. int tmp = *it;
  48. block[pos[b]].q.erase(block[pos[b]].q.begin() + ((b-)%blo));
  49. block[pos[b]].q.insert(block[pos[a]].q.begin() + ((a-)%blo), tmp);
  50. }
  51. else
  52. {
  53. it = block[pos[b]].q.begin() + ((b-)%blo);
  54. int tmp = *it;
  55. block[pos[b]].num[tmp]--;
  56. block[pos[b]].q.erase(block[pos[b]].q.begin() + ((b-)%blo));
  57. for (int i = pos[a]; i < pos[b]; i++) {
  58. int x = block[i].q.back();
  59. block[i].q.pop_back();
  60. block[i].num[x]--;
  61. block[i + ].q.push_front(x);
  62. block[i + ].num[x]++;
  63. }
  64. block[pos[a]].q.insert(block[pos[a]].q.begin() + ((a-)%blo), tmp);
  65. block[pos[a]].num[tmp]++;
  66. }
  67. }
  68. void query(int a, int b, int c)
  69. {
  70. ans=;
  71. if (pos[a] == pos[b])
  72. {
  73. for (it = block[pos[a]].q.begin() + ((a-)%blo); it <= block[pos[a]].q.begin() + ((b-)%blo); it++)
  74. if ((*it) == c) ans++;
  75. }
  76. else {
  77. for (int i = pos[a] + ; i < pos[b]; i++) ans += block[i].num[c];
  78. for (it = block[pos[a]].q.begin() + ((a-)%blo); it < block[pos[a]].q.end(); it++)
  79. if ((*it) == c) ans++;
  80. for (it = block[pos[b]].q.begin(); it <= block[pos[b]].q.begin() + ((b-)%blo); it++)
  81. if ((*it) == c) ans++;
  82. }
  83. printf("%d\n", ans);
  84. }
  85.  
  86. int main()
  87. {
  88. scad(n);
  89. blo = sqrt(n);
  90. if (n%blo) blo++;
  91. forn(i, , n) pos[i] = (i - )/blo + ;
  92.  
  93. forn(i, , n) {
  94. int x;
  95. scad(x);
  96. int s = pos[i];
  97. block[s].q.push_back(x);
  98. block[s].num[x]++;
  99. }
  100. scad(m);
  101. ans = ;
  102. while (m--)
  103. {
  104. int op;
  105. scad(op);
  106. if (op == )
  107. {
  108. int l, r;
  109. scadd(l, r);
  110. l = (l + ans - ) % n+;
  111. r = (r + ans - ) % n+;
  112. if (l > r) swap(l, r);
  113. update(l, r);
  114. }
  115. else
  116. {
  117. int l, r, k;
  118. scaddd(l, r, k);
  119. l = (l + ans - ) % n + ;
  120. r = (r + ans - ) % n + ;
  121. k = (k + ans - ) % n + ;
  122. if (l > r) swap(l, r);
  123. query(l, r, k);
  124. }
  125. }
  126. return ;
  127. }

CodeForces - 455D的更多相关文章

  1. CodeForces 455D 分块

    题目链接:http://codeforces.com/problemset/problem/455/D 题意:给定一个长度为n的序列a[]. m次操作.共有两种操作 1 l r:将序列的a[l].a[ ...

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

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

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

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

  4. Codeforces 455D 分块+链表

    题意: 给定一个长度为 N 的序列两种操作1 l r 将[l,r]的数向右循环移位 2 l r 询问[l,r]内有多少个数等于 k其中 N,Q≤105,ai≤N 强制在线 思路: 1. 每块用一个链表 ...

  5. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  6. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  7. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  8. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  9. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

随机推荐

  1. 5、zabbix使用进阶(01)

    详细描述user parameters.定义主机发现规则实现自动发现.如何定义和实现自动注册方式 zabbix常用术语 1.主机(host):要监控的网络设备,可有IP或DNS名称指定: 2.主机组( ...

  2. css 两端对其

    大段的文字直接写会有不整齐的现象  所以ui小姐姐就会和我们讲你能不能把两端对其呀  ps里面就可以呀 okok  那就两端对其好了 text-align:justify 效果为⬇️⬇️⬇️  上面是 ...

  3. 对啊英语音标---二、ghywr这些辅音怎么发音

    对啊英语音标---二.ghywr这些辅音怎么发音 一.总结 一句话总结:对比法,和汉语拼音做对比 对比法,和汉语拼音做对比 1.清辅音和浊辅音的区别是什么? 清辅音-[不需要声带震动]就能发出的音节: ...

  4. Ajax+Struts2用户注册功能实现

    详细请参考源码(Github):https://github.com/QQ3330447288/ajaxRegister 1.目录结构 2.截图 3.核心代码: register.jsp <sc ...

  5. 4、NFS

    一.NFS简介 4.1.1:什么是NFS NFS(Network File System,网络文件系统)是由SUN公司开发,并于1984年推出的技术,通过使用NF,用户和程序可以向访问本地文件一样访问 ...

  6. C++将十进制数转化为二进制

    #include<iostream> using namespace std; void main() { ; ]; cin>>n; i=n; while(i) { a[j]= ...

  7. input 标签的 disabled 和 readonly 属性

    首先这两种属性都会使显示出来的文本框不能输入. disabled 属性:规定禁用 input 元素.被禁用的 input 元素既不可用,也不可点击和编辑,使用 tab 键时将会被跳过,用户的所有操作对 ...

  8. createDocumentFragment() 方法

    //createdocumentfragment()方法创建了一虚拟的节点对象,节点对象包含所有属性和方法. //当你想提取文档的一部分,改变,增加,或删除某些内容及插入到文档末尾可以使用create ...

  9. JDBC数据库连接工具

    什么是JDBC? JDBC是一种可以执行sql语句的Java API,提供对数据库的访问方法. 什么是JDBC驱动? JDBC连接数据库需要驱动,驱动是两个设备要进行通信,满足一定的数据驱动格式.一般 ...

  10. 跟随我在oracle学习php(5)

    框架(把一个页面引入当前页面 易维护 扩展 复用)<iframe src=”” frameborder=“”> 格式:iframe <frameset> <frame&g ...