3289. Mato's file Management "Mo Team + tree-like array"

Source: Internet
Author: User

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 first line a positive integer n, indicating 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. N,q <= 50000Output

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 4Sample Output0
2
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. as long as the team and the tree array of the board this problem can be easily.ask Mo Team, and then found each time the team left and right end of the transfer time and greater than/less than the current number of numbers, the tree array can betoo lazy to disperse.
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <cmath>5#include <algorithm>6 #defineN (1000000+1000)7 using namespacestd;8 9 intC[n],a[n],n,m,unit,ans;Ten  One structque{intX,y,id,num,ans;} Q[n]; A BOOLCMP1 (Que A,que b) {returnA.id<b.id | | A.id==b.id && a.y<b.y;} - BOOLCMP2 (Que A,que b) {returna.num<B.num;} -  the intLowbit (intx) {returnX &-x;} - voidUpdate (intXintV) { for(; x<=1000000; c[x]+=v,x+=lowbit (x));} - intQuery (intx) {ints=0; for(; x; s+=c[x],x-=lowbit (x));returns;} -  + voidDel (intXintdir) - { +     if(dir==-1) Ans-=query (a[x]-1), Update (a[x],-1); A     ElseAns-=query (1000000)-query (A[x]), Update (a[x],-1); at } -  - voidIns (intXintdir) - { -     if(dir==-1)  -Ans+=query (a[x]-1), Update (A[x],1); in     Else  -Ans+=query (1000000)-query (A[x]), Update (A[x],1); to } +  - intMain () the { *scanf"%d", &n); unit=sqrt (n); $      for(intI=1; i<=n; ++i)Panax Notoginsengscanf"%d",&a[i]); -scanf"%d",&m); the      for(intI=1; i<=m; ++i) +     { Ascanf"%d%d",&q[i].x,&q[i].y); theQ[i].num=i; q[i].id=q[i].x/Unit; +     } -Sort (q+1, q+m+1, CMP1); $      $     intL=1, r=0; -      for(intI=1; i<=m; ++i) -     { the          while(l<q[i].x) Del (l++,-1); -          while(l>q[i].x) Ins (--l,-1);Wuyi          while(R&LT;Q[I].Y) Ins (++r,1); the          while(R&GT;Q[I].Y) Del (R--,1); -q[i].ans=Ans; Wu     } -Sort (q+1, q+m+1, CMP2); About      for(intI=1; i<=m; ++i) $printf"%d\n", Q[i].ans); -}

3289. Mato's file Management "Mo Team + 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.