Codeforces 455D - Serega and Fun (Solved by Square Root Decomposition)
Problem Link:
http://codeforces.com/problemset/problem/455/D
Problem Statement:
D. Serega and Fun
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:
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.
Analysis:
There are a lot of ways to solve this problem.
(1) The most brutal way is to use square root decomposition.
(2) Some more elegant way is to use splay trees or treaps to solve it.
Here I will only present the brutal way to solve it, i.e., using square root decomposition. Maybe some time later I will post the more elegant way.
In this problem, we are asked to perform one operation and one query. For each operation, we are given two parameters l and r, and we need to perform a right shift on the interval from l to r, i.e. a[l]=a[r], a[l+1]=a[l], a[l+2]=a[l+1], …, a[r]=a[r-1]. For each query, we are given three parameters l, r and k, and we need to count the number of elements equal to k in the interval from l to r.
If we implement the data structure naively using a vector, each right shift operation would take O(n) time and each query would also take O(n) time, which would cause an overall complexity of O(nq) and consequentially lead us to “time limit exceeded”.
To implement a data structure with better performance, we can get help from a classical way called square root decomposition, i.e., dividing the elements into approximately √n blocks each of which has approximately √n elements. After that, we can keep for each block keep track of the elements inside it and the number of elements equal to 1 to 105 inside this block.
With this data structure, we can both perform the right shift operation in √n steps and count the number of elements equal to k in an interval in √n steps, since there can be at most √n blocks and each block has at most √n elements.
To perform each right shift operation, we can keep track of the elements inside each block in a deque (double ended queue) and insert the element positioned at r to position x and then push the right most element of the block which has just increased one in size to the following block until all blocks return to their normal size.
There is one last issue with this method, i.e., sometimes we do not choose the number of blocks and the size of each block to be exactly √n. If we choose some other number, it may improve the code's performance. In this problem, I filled each block with 800 elements, which makes the code accepted. However, when I choose the size to be 400, I got the verdict "limit limit exceeded". In fact, one can find the best size by mathematical calculation, which I will not present here.
Time Complexity:
O(n√n)
AC Code:
1 #include <iostream> 2 #include <sstream> 3 #include <fstream> 4 #include <string> 5 #include <vector> 6 #include <deque> 7 #include <queue> 8 #include <stack> 9 #include <set> 10 #include <map> 11 #include <algorithm> 12 #include <functional> 13 #include <utility> 14 #include <bitset> 15 #include <cmath> 16 #include <cstdlib> 17 #include <ctime> 18 #include <cstdio> 19 #include <memory.h> 20 #include <iomanip> 21 #include <unordered_set> 22 #include <unordered_map> 23 using namespace std; 24 25 #define MP make_pair 26 #define FS first 27 #define SC second 28 #define LB lower_bound 29 #define PB push_back 30 #define lc p*2+1 31 #define rc p*2+2 32 33 typedef long long ll; 34 typedef pair<int,int> pi; 35 36 const int Maxn=1e5+10; 37 const int Maxp=200; 38 const int Maxd=800; 39 40 int n,q,t; 41 int l,r,k,ans=0; 42 int a[Maxn]; 43 44 class group{ 45 public: 46 int cnt[Maxn]; 47 deque<int> v; 48 }gp[Maxp]; 49 50 void right_shift(){ //perform the right shift 51 int a=l/Maxd,b=r/Maxd,aa=l%Maxd,bb=r%Maxd; 52 int val=*(gp[b].v.begin()+bb); 53 gp[b].v.erase(gp[b].v.begin()+bb); 54 gp[a].v.insert(gp[a].v.begin()+aa,val); 55 gp[b].cnt[val]--; 56 gp[a].cnt[val]++; 57 for(int i=a;i<b;i++){ 58 val=gp[i].v.back(); 59 gp[i].v.pop_back(); 60 gp[i+1].v.push_front(val); 61 gp[i].cnt[val]--; 62 gp[i+1].cnt[val]++; 63 } 64 } 65 66 int count_val(){ //count the number of elements equal to k from l to r 67 int a=l/Maxd,b=r/Maxd,aa=l%Maxd,bb=r%Maxd; 68 int rez=0; 69 if(a==b){ 70 for(auto it=gp[a].v.begin()+aa;it<=gp[a].v.begin()+bb;it++){ 71 if(*it==k) rez++; 72 } 73 } 74 else{ 75 for(auto it=gp[a].v.begin()+aa;it!=gp[a].v.end();it++){ 76 if(*it==k) rez++; 77 } 78 for(int i=a+1;i<b;i++){ 79 rez+=gp[i].cnt[k]; 80 } 81 for(auto it=gp[b].v.begin();it<=gp[b].v.begin()+bb;it++){ 82 if(*it==k) rez++; 83 } 84 } 85 return rez; 86 } 87 88 void build(){ //initialize the blocks 89 for(int i=0;i<n;i++){ 90 gp[i/Maxd].v.push_back(a[i]); 91 gp[i/Maxd].cnt[a[i]]++; 92 } 93 } 94 95 int main(){ 96 scanf("%d",&n); 97 for(int i=0;i<n;i++){ 98 scanf("%d",a+i); 99 } 100 build(); 101 scanf("%d",&q); 102 for(int i=0;i<q;i++){ 103 scanf("%d%d%d",&t,&l,&r); 104 l=(l+ans-1)%n; 105 r=(r+ans-1)%n; 106 if(l>r) swap(l,r); 107 if(t==1){ 108 right_shift(); 109 } 110 else{ 111 scanf("%d",&k); 112 k=(k+ans-1)%n+1; 113 ans=count_val(); 114 printf("%d\n",ans); 115 } 116 } 117 return 0; 118 }