John Doe has a crooked fence, consisting of n rectangular planks, lined up from the left to the right: the plank that goes i-th (1 ≤ i ≤ n) (from left to right) has width 1 and height h i. We will assume that the plank that goes i-th (1 ≤ i ≤ n) (from left to right) has index i.
A piece of the fence from l to r (1 ≤ l ≤ r ≤ n) is a sequence of planks of wood with indices from l to r inclusive, that is, planks with indices l, l + 1, …, r. The width of the piece of the fence from l to r is value r - l + 1.
Two pieces of the fence from l 1 to r 1 and from l 2 to r 2 are called matching, if the following conditions hold:
- the pieces do not intersect, that is, there isn’t a single plank, such that it occurs in both pieces of the fence;
- the pieces are of the same width;
- for all i (0 ≤ i ≤ r 1 - l 1) the following condition holds: h l 1 + i + h l 2 + i = h l 1 + h l 2.
John chose a few pieces of the fence and now wants to know how many distinct matching pieces are for each of them. Two pieces of the fence are distinct if there is a plank, which belongs to one of them and does not belong to the other one.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of wood planks in the fence. The second line contains n space-separated integers h 1, h 2, …, h n (1 ≤ h i ≤ 109) — the heights of fence planks.
The third line contains integer q (1 ≤ q ≤ 105) — the number of queries. Next q lines contain two space-separated integers l i and r i (1 ≤ l i ≤ r i ≤ n) — the boundaries of the i-th piece of the fence.
Output
For each query on a single line print a single integer — the number of pieces of the fence that match the given one. Print the answers to the queries in the order, in which the queries are given in the input.
Examples
input
10
1 2 2 1 100 99 99 100 100 100
6
1 4
1 2
3 4
1 5
9 10
10 10
output
1
2
2
0
2
9
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | #include <iostream> #include <iomanip> #include <stdio.h> #include <set> #include <vector> #include <map> #include <cmath> #include <algorithm> #include <memory.h> #include <string> #include <sstream> using namespace std; const int N = 400010; vector < pair < int , int > > b; int a[N], s[N], pz[N], g[N], ng[N], lcp[N], d[20][N]; int m, n, i, j, k; int ma[N], ml[N], mr[N], ans[N]; void modify( int q, int v) { ma[q] = v; int x = q; while (x <= m) { if (v < ml[x]) ml[x] = v; x = (x | (x-1))+1; } x = q; while (x > 0) { if (v < mr[x]) mr[x] = v; x &= x-1; } } int findmin( int ll, int rr) { if (ll > rr) return ( int )1e9; int res = ( int )1e9, x = ll; while ((x | (x-1))+1 <= rr) { if (mr[x] < res) res = mr[x]; x = (x | (x-1))+1; } if (ma[x] < res) res = ma[x]; x = rr; while ((x & (x-1)) >= ll) { if (ml[x] < res) res = ml[x]; x &= x-1; } return res; } int ke; int e[N], es[N], ef[N], ew[N], sum[N], last[N], pred[N]; void addev( int u, int ss, int ff, int wh) { e[ke] = u; es[ke] = ss; ef[ke] = ff; ew[ke] = wh; ke++; } void addevent( int lower, int upper, int ss, int ff, int wh) { addev(upper, ss, ff, wh); if (lower > 0) addev(lower-1, ss, ff, ~wh); } int main() { // freopen("in","r",stdin); // freopen("out","w",stdout); scanf ( "%d" , &n); for (i=0;i<n;i++) scanf ( "%d" , s+i); for (i=0;i<n-1;i++) s[i] = s[i+1]-s[i]; n--; for (i=n;i<2*n;i++) s[i] = -s[i-n]; b.clear(); for (i=0;i<2*n;i++) b.push_back(make_pair(s[i], i)); sort(b.begin(), b.end()); int kg = 0; i = 0; while (i < 2*n) { j = i; while (j < 2*n && b[j].first == b[i].first) j++; for (k=i;k<j;k++) { a[k] = b[k].second; pz[a[k]] = k; g[k] = kg; } kg++; i = j; } int step = 1, it = 0; while (kg < 2*n) { for (i=0;i<2*n;i++) d[it][a[i]] = g[i]; it++; int nkg = 0; i = 0; while (i < 2*n) { j = i; while (j < 2*n && g[i] == g[j]) j++; b.clear(); for (k=i;k<j;k++) if (a[k]+step < 2*n) b.push_back(make_pair(g[pz[a[k]+step]], a[k])); else b.push_back(make_pair(-1, a[k])); sort(b.begin(), b.end()); for (k=0;k<j-i;k++) a[i+k] = b[k].second; for (k=i;k<j;k++) { if (k == i || b[k-i].first != b[k-i-1].first) nkg++; ng[k] = nkg-1; } i = j; } for (i=0;i<2*n;i++) pz[a[i]] = i; for (i=0;i<2*n;i++) g[i] = ng[i]; kg = nkg; step <<= 1; } for (i=0;i<2*n;i++) { int v = 0, x = a[i], y = a[i+1]; for (j=it-1;j>=0;j--) if (x+v < 2*n && y+v < 2*n && d[j][x+v] == d[j][y+v]) v += (1 << j); lcp[i] = v; /* printf("%d",a[i]); for (j=a[i];j<2*n;j++) printf(" %d",s[j]); printf("\n"); printf("%d\n",lcp[i]); */ } m = 2*n-1; for (i=1;i<=m;i++) ma[i] = ml[i] = mr[i] = ( int )1e9; for (i=1;i<=m;i++) modify(i, lcp[i-1]); int tt; scanf ( "%d" , &tt); ke = 0; for ( int qq=0;qq<tt;qq++) { int ql, qr; scanf ( "%d %d" , &ql, &qr); if (ql == qr) ans[qq] = n; else { ans[qq] = 0; int x = pz[ql-1], ll, rr, mid; ll = x-1; rr = m-1; while (ll < rr) { mid = (ll+rr+1) >> 1; if (findmin(x+1,mid+1) >= qr-ql) ll = mid; else rr = mid-1; } int upper = ll+1; ll = 0; rr = x; while (ll < rr) { mid = (ll+rr) >> 1; if (findmin(mid+1,x) >= qr-ql) rr = mid; else ll = mid+1; } int lower = ll; int ss = 1, ff = ql-(qr-ql+1); if (ss <= ff) addevent(lower, upper, ss+n-1, ff+n-1, qq); ss = qr+1; ff = n-(qr-ql+1)+2; if (ss <= ff) addevent(lower, upper, ss+n-1, ff+n-1, qq); } } for (i=0;i<2*n;i++) last[i] = -1; for (i=0;i<ke;i++) { pred[i] = last[e[i]]; last[e[i]] = i; } memset (sum,0, sizeof (sum)); for (i=0;i<2*n;i++) { int x = a[i]+1; while (x <= 2*n) { sum[x]++; x = (x | (x-1))+1; } j = last[i]; while (j >= 0) { x = ef[j]+1; while (x > 0) { if (ew[j] >= 0) ans[ew[j]] += sum[x]; else ans[~ew[j]] -= sum[x]; x &= x-1; } x = es[j]; while (x > 0) { if (ew[j] >= 0) ans[ew[j]] -= sum[x]; else ans[~ew[j]] += sum[x]; x &= x-1; } j = pred[j]; } } for ( int qq=0;qq<tt;qq++) printf ( "%d\n" , ans[qq]); return 0; } |