Bzoj 3289:mato File Management (MO-Team algorithm + tree-like array)

Source: Internet
Author: User

http://www.lydsy.com/JudgeOnline/problem.php?id=3289

Test instructions: ...

Idea: To find the number of exchanges is the inverse logarithm. Once this is determined, the array is discretized first. Then insert the element in the back, that is, in the interval to find the number of elements larger than it, in front of the element, is to find in the interval smaller than its number of elements. The delete operation is similar. Because the sort is small to large, so to find a larger number than it is the interval length minus less than the number of the element, so is (R-l + 1-sum (A[i])), to find a smaller number than it is (sum (a[i]-1)). Then use the MO team algorithm to deal with the interval query is good.

1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <cmath>5 using namespacestd;6 #defineN 500107 structNode {8     intL, R, id, ans;9 } P[n];Ten intBit[n], a[n], b[n], Num[n], ans, Kuai, N; One  A BOOLcmpConstNode &a,ConstNode &b) { -     if(A.l/kuai = = B.l/kuai)returnA.R <B.R; -     returnA.l/kuai < B.L/Kuai; the } - BOOLCmpid (ConstNode &a,ConstNode &b) {returna.ID <b.id;} - intLowbit (intx) {returnX & (-x); } - voidAddintXintW) { for(; x <= n; x + = Lowbit (x)) bit[x] + =W;} + intSumintx) {intAns =0; for(; x; x-= Lowbit (x)) ans + = bit[x];returnans;} -  + intMain () { A      while(~SCANF ("%d", &N)) { at          for(inti =1; I <= N; i++) scanf ("%d", &a[i]), b[i] =A[i]; -         intQ scanf"%d", &q); -          for(inti =1; I <= Q; i++) scanf ("%d%d", &AMP;P[I].L, &AMP;P[I].R), p[i].id =i; -  -memset (bit,0,sizeof(bit)); -Ans =0; Kuai =sqrt (n); inSort (b +1, B +1+n); -         intCNT = Unique (b +1, B +1+ N)-B-1; to          for(inti =1; I <= N; i++) A[i] = Lower_bound (b +1, B +1+ CNT, a[i])-b; +  -Sort (p +1, p +1+Q, CMP); the          for(intL =1, R =0, i =1; I <= Q; i++) { *             intL = p[i].l, r =P[I].R; $              for( ; L < L; l++) {ans-= SUM (A[l]-1); Add (A[l],-1); }Panax Notoginseng              for( ; L > L; l--) {ans + = SUM (a[l-1] -1); Add (a[l-1],1); } -              for( ; R < R; r++) {ans + = r-l +1-SUM (a[r+1]); Add (a[r+1],1); } the              for( ; R > R; r--) {ans-= r-l +1-SUM (a[r]); Add (A[r],-1); } +P[i].ans =ans; A         } theSort (p +1, p +1+Q, cmpid); +          for(inti =1; I <= Q; i++) printf ("%d\n", P[i].ans); -     } $     return 0; $}

Bzoj 3289:mato File Management (MO-Team algorithm + tree-like 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.