The main idea: give a sequence, to find the number of reverse in a range.
Idea: Again there is no modification of the query operation, but also can make Mo team (Mo team really good engage.
Sort all the questions first, then transfer from the beginning to the next, remember a global answer, and then record the change in reverse order each time you transfer. Then output from the ANS array:
CODE:
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < Algorithm> #define MAX 50010using namespace std;struct ask{int x,y,_id;int Block;bool operator < (const ASK &A) CO NST {if (block = = A.block) return y < A.y;return block < A.block;} void Read (int p) {scanf ("%d%d", &x,&y); _id = P;}} Ask[max];int cnt,asks;int src[max];int Fenwick[max];p air<int,int *> xx[max];int ans[MAX];inline int GetSum (int x) {if (!x) return 0;int re = 0;for (; x; x-= x&-x) Re + = Fenwick[x];return Re;} inline void Fix (int x,int c) {for (; x <= cnt; x + = X&-x) fenwick[x] + = C;} int main () {cin >> cnt;for (int i = 1; I <= cnt; ++i) scanf ("%d", &xx[i].first), Xx[i].second = &src[i];sort (x x + 1,xx + cnt + 1); int t = 0;for (int i = 1; I <= cnt; ++i) {if (!t | | Xx[i].first! = xx[i-1].first) ++t;*xx[i].second = t;} CIN >> asks;for (int i = 1; I <= asks; ++i) {ask[i]. Read (i); int block = static_cast<int> (sqrt (CNT));t i = 1; I <= asks; ++i) Ask[i].block = Ask[i].x/block;sort (ask + 1,ask + asks + 1); int L = 1,r = 0;int now = 0;for (int i = 1; I <= asks; ++i) {while (R < ask[i].y) {int total = r-l + 1,larger = total-getsum (Src[++r]); now + = larger; Fix (src[r],1);} while (L > ask[i].x) {now + = Getsum (Src[--l]); Fix (src[l],1);} while (R > ask[i].y) {int total = r-l + 1,larger = total-getsum (Src[r]), now-= larger; Fix (src[r--],-1);} while (L < ask[i].x) {now-= Getsum (Src[l]-1); Fix (src[l++],-1);} ANS[ASK[I]._ID] = Now;} for (int i = 1; I <= asks; ++i) printf ("%d\n", Ans[i]); return 0;}
Bzoj 3289 Mato File Management mo team algorithm + Tree array