3289:mato file Management time Limit: $ Sec Memory Limit: MB
Submit: 2171 Solved: 891
[Submit] [Status] [Discuss] DescriptionMato students from all the way God Ben in a variety of ways (you know) collected a lot of information, there are a total of n copies, each has 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.
Title Link: Bzoj 3289
Oh, my God. In the collation of the = = into the "!!" from the MO Squad violence to the N*n explosion T (already more than once hit wrong), also thought is the amount of data of the pot add a plug or t,tn after the discovery of this error ...
Just a little bit of discretization. It's so convenient to use the dispersion of the vector method that Daniel learned recently. Hey, whoa.
There is a small optimization, Getsum (n) when in fact, most of the value is 0, using interval instead of the same, such as 5 4 3 2 to the right to increase X, when the number of reverse order will increase getsumsum[x+1~n] that is, add (x,1) and then calculate Getsum (N)-getsum (x), The getsum here (N) is the 1~n of all the number, because the update is only the current number of intervals, it is not as direct r-l+1 effect, the +1 with the Add implementation also line, the order can be changed
Code:
#include <stdio.h> #include <iostream> #include <algorithm> #include <cstdlib> #include < sstream> #include <cstring> #include <bitset> #include <string> #include <deque> #include <stack> #include <cmath> #include <queue> #include <set> #include <map>using namespace std ; #define INF 0x3f3f3f3f#define CLR (x, y) memset (x,y,sizeof) #define LC (x) (x<<1) #define RC (x) ((x<<1) +1) # Define MID (x, y) ((x+y) >>1) typedef pair<int,int> PII;TYPEDEF Long Long ll;const double Pi=acos ( -1.0); const int n=50010;struct info{int l,r; int b,id; BOOL operator< (const info &t) Const {if (B==T.B) return r<t.r; Return b<t.b; }}q[n];int arr[n],c[n],ans[n];vector<int>pos;void Add (int k,int val) {while (k<n) {c[k]+=val; k+= (K&-K); }}int getsum (int k) {int ret=0; while (k) {ret+=c[k]; k-= (K&-K); } return RET;} int main (void) {int n,i,q; while (~SCANF ("%d", &n)) {CLR (c,0); Pos.clear (); for (I=1; i<=n; ++i) {scanf ("%d", &arr[i]); Pos.push_back (Arr[i]); } sort (Pos.begin (), Pos.end ()); Pos.erase (Unique (Pos.begin (), Pos.end ()), Pos.end ()); for (I=1; i<=n; ++i) Arr[i]=lower_bound (Pos.begin (), Pos.end (), Arr[i])-pos.begin () +1; scanf ("%d", &q); int unit= (int) sqrt (n); for (i=0; i<q; ++i) {scanf ("%d%d", &Q[I].L,&Q[I].R); Q[i].id=i; Q[i].b=q[i].l/unit; } sort (q,q+q); int l=1,r=0,ans=0; For (i=0, i<q; ++i) {while (L>Q[I].L) {--l; Ans+=getsum (arr[l]-1); Add (arr[l],1); } while (L<Q[I].L) {ans-=getsum (arr[l]-1); Add (arr[l],-1); ++l; } while (R>Q[I].R) {ans-= (R-l+1-getsum (Arr[r])); Add (arr[r],-1); --r; } while (R<Q[I].R) {++r; ans+= (R-l-getsum (arr[r)); Add (arr[r],1); } Ans[q[i].id]=ans; } for (i=0; i<q; ++i) printf ("%d\n", Ans[i]); } return 0;}
Bzoj 3289 Mato File Management (Mo Team + discretization for reverse order number)