"BZOJ3289" Mato's file management Descriptionmato students from all the way God Ben in various ways (you know) the collection of a lot of information, there are a total of n copies, each with a size and a number. To prevent others from stealing, the data is encrypted and can only be accessed using a program written by Mato. Mato randomly selected an interval every day [L,r], and today he looks at the data numbered in this interval. Mato has a habit, he always from the file size from small to large look at the data. He first copies the files to be read in numbered order, and then uses the sort program he wrote to sort the file size. The sequencer can exchange 2 contiguous files within 1 unit time (because encryption is required and cannot be accessed randomly). Mato want to minimize the number of file exchanges, can you tell him how many times you need to swap each day? Input
The first line is a positive integer n, which represents the number of copies of the Mato.
The second line consists of n positive integers separated by a space, and the first I represents the size of the data that is numbered I.
The third line is a positive integer q, indicating that Mato will look at the data for a few days.
After the Q line of two positive integers per line L, R, indicating Mato this day to see the [l,r] interval of the file.
Output
Q lines, one positive integer per line, indicating the number of times the Mato need to be exchanged this day.
Sample Input4
1 4 2 3
2
1 2
2 4
Sample Output0
2
HINT
Hint
N,q <= 50000
Sample interpretation: The first day, Mato do not need to exchange
The next day, Mato can move the number 2nd 2 times to the end.
Solution: Directly using the MO team algorithm, here we still only consider the impact of r+1 on [L,r] Answers
When the r++, the original [l,r] are already lined up, and our newly added r+1 should also be queued, the number of calls required is [l,r] in all the number of larger than r+1, directly using a tree-like array, r-l+1 minus less than equals r+1 number of numbers on the line
The rest of the same
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < Cmath>using namespace Std;struct node{int num,org;} P[50010];struct query{int qa,qb,org;} Q[50010];int n,m,nm,siz,sum;int V[50010],ans[50010],s[50010];bool cmp1 (node A,node b) {return a.num<b.num;} BOOL Cmp2 (QUERY A,query b) {if ((a.qa-1)/siz== (b.qa-1)/siz) return A.qb<b.qb;return (a.qa-1)/siz< (B.qa-1)/siz;} void Updata (int x,int val) {for (int i=x;i<=nm;i+=i&-i) S[i]+=val;} int query (int x) {int i,ret=0;for (i=x;i;i-=i&-i) ret+=s[i];return ret;} int main () {scanf ("%d", &n), int i;siz= (int) sqrt ((double) n), for (i=1;i<=n;i++) scanf ("%d", &p[i].num), P[i]. Org=i;sort (P+1,P+N+1,CMP1);p [0].num=-1;for (i=1;i<=n;i++) {if (p[i].num>p[i-1].num) nm++;v[p[i].org]=nm;} scanf ("%d", &m), for (i=1;i<=m;i++) scanf ("%d%d", &Q[I].QA,&Q[I].QB), Q[i].org=i;sort (Q+1,Q+M+1,CMP2) ; int L=1,r=0;for (i=1;i<=m;i++) {while (R<Q[I].QB) sum+=r-l+1-query (V[++r]), Updata (v[r],1), and while (R>Q[I].QB) Sum-=r-l+1-query (V[r]), Updata (V[r--],-1), while (L>Q[I].QA) sum+=query (v[--l]-1), Updata (v[l],1); while (L<Q[I].QA) sum-=query (v[l]-1), Updata (v[l++],-1); ans[q[i].org]=sum;} for (i=1;i<=m;i++) printf ("%d\n", Ans[i]); return 0;}
"BZOJ3289" Mato File Management team algorithm + tree-like array