Bzoj 3289 Mato File Management mo team algorithm + Tree array

Source: Internet
Author: User

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 &AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.