The sort of the garlic-King (sort)
2000ms 262144K
The garlic is a good child to think about, this day he learned the bubble sort, so he thought, put a disorderly order by bubble sort to ascending how many times need to exchange, it is of course difficult not to pour him, so he wanted to point exciting, given a 1...n1 arrangement, each time from the arrangement to select an interval [L,r], Ask how many times the interchange operation is required to sort the interval into ascending order using bubbling sorting.
Input format
The first line is an integer n, which indicates the arrangement length.
The next row of n integers represents the arrangement.
The next line is an integer m, which indicates the number of queries.
Next m line, 2 integers per line l,r, indicates the [l,r] interval is queried.
Output format
Outputs m lines, 1 integers per line, and line I indicates the answer to the I query.
Data size
For 30% of data, meet 1≤n,m≤300;
For 60% of data, meet 1≤n,m≤1000;
For 100% of data, meet 1≤n,m≤30000,l<r,l<R, ∑∣l[i]? L [i? 1]|+∑∣R[i]? R [i? 1]∣≤7*10^6??。
Sample input
Sample input
10
9 8 7 4 5 6 10 3 2 1
5
2 4
8 10
2 8
5 9
4 9
Sample output
3
3
13
7
9
Analytic: This problem direct violence (directly with bubble sort or merge sort record reverse order) can take 60 points;
Offline use of tree array maintenance can get 70 points;
Online maintenance tree array can take 100 points ... (Why is offline timed out: )
On the maintenance of the tree array we can first define two pointers to the left and right end points, each time the query is maintained this interval (left endpoint, right end point constantly moving left to right), can query all the answers (good magic). )
It's very confusing why you can't first sort by the left endpoint, and then maintain it, so that you can do less one left endpoint left step. Unfortunately, it will expire;
about how to change from [L,r] to [l-1,r],[l+1,r],[l,r-1],[l,r+1], you can manually simulate it again, and for the first time feel good metaphysics ...
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <algorithm>5 #defineLo (x) (x& (×))6 #definell Long Long7 #defineMans 300108 using namespacestd;9 //CommonTen intN,m,c[man],pos[man],ask[man]; One ll A[man],b[man]; AInlineintRead () -{intx=0;BOOLf=0; - CharCh=GetChar (); the while(ch<'0'|| Ch>'9') {f= (ch== $); ch=GetChar ();} - while(ch>='0'&&ch<='9') {x= (x<<1) + (x<<3) + (ch^ -); ch=GetChar ();} - returnF? (~x+1): x; - } + structRange -{intX,y,id;} E[man]; + //Lowbit AInlinevoidAddintXintval) at{ while(x<=N) -{c[x]+=Val; -x+=lo (x); - } - return ; - } inInlineintCalcintx) -{intans=0; to while(x>0) +{ans+=C[x]; -x-=lo (x); the } * returnans; $ }Panax Notoginseng intMain () -{Freopen ("sort.in","R", stdin); theFreopen ("Sort.out","W", stdout); +n=read (); A for(intI=1; i<=n;i++) thescanf"%lld", &a[i]), b[i]=A[i]; +Sort (A +1, A +1+n); - for(intI=1; i<=n;i++) $Pos[i]=lower_bound (A +1, a+n+1, B[i])-A; $m=read (); - for(intI=1; i<=m;i++) -{E[i].x=read (); E[i].y=read (); e[i].id=i;} the intL=1, r=0, ans=0; - for(intI=1; i<=m;i++)Wuyi{ while(r<e[i].y) the{r++; -Ans+=calc (n)-calc (pos[r]-1); WuAdd (Pos[r],1); - } About while(l<e[i].x) ${Add (pos[l],-1); -Ans-=calc (pos[l]-1); -l++; - } A while(r>e[i].y) +{Add (pos[r],-1); theAns-=calc (n)-calc (pos[r]-1); -r--;} $ while(l>e[i].x) the{l--; theAns+=calc (pos[l]-1); theAdd (Pos[l],1); the } -ask[e[i].id]=ans; in } the for(intI=1; i<=m;i++) theprintf"%d\n", Ask[i]); About return 0; the}
The sort of garlic d2t2 (Dynamic Maintenance tree array)