Bzoj_3289_mato File Management _ Mo Team + tree-like array

Source: Internet
Author: User

Bzoj_3289_mato File Management _ Mo Team + tree-like array

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. Analysis:mo team naked problem. when the pointer l,r moves, the answer is updated with a tree array, considering the contribution of each number. Code:
#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h>using namespace STD; #define N 50050#define LL long longint c[n], N, Q, V[n], pos[n];struct A {int num,d,id;} B[n];bool Cmp3 (const a &x,const a &y) {return x.num<y.num;} BOOL Cmp4 (const a &x,const a &y) {return x.id < y.id;} struct B {int s,t,id; LL ans;} A[n];bool CMP1 (const b &x,const b &y) {if (pos[y.s] = = Pos[x.s]) return x.t < Y.t;return POS[X.S] < POS[Y.S]; }bool CMP2 (const b &x,const b &y) {return x.id < y.id;} LL Now;int Main () {scanf ("%d", &n); int I, j, block = sqrt (n), L, R = 0;for (i = 1;i <= block; + + i) {L = r + 1;r = I * Block;for (j = l;j <= R; + + j) Pos[j] = i;} if (r! = N) {+ + Block;l = r + 1;r = n;for (i = L;i <= R; + + i) pos[i] = block;} for (i = 1;i <= n; + + i) scanf ("%d", &b[i].num), b[i].id = I;sort (b + 1, B + n + 1, Cmp3); B[0].num = -84234820;for (i = 1, j = 0;i <= N; + + i) {if (b[i].num! = B[i-1].num) j + +; b[i].D = j; }sort (b + 1, B + n + 1, cmp4); for (i = 1;i <= n; + + i) v[i] = b[i].d;scanf ("%d", &q); for (i = 1;i <= q; + + i) scan F ("%d%d", &a[i].s, &a[i].t), a[i].id = I;sort (A + 1, a + q + 1, CMP1); for (L = 1, r = 0, i = 1;i <= q; + + i) {WHI Le (L < A[I].S) {int t = v[l]-1, re = 0;for (; t; t = t & (-T)) re + = C[t];now = Re;t = V[l];for (; t <= n;t + = T & (-T)) C[t]--;l + +; }while (L > A[i].s) {int t = v[l-1]-1, re = 0;for (; t; t = t & (-T)) re + = C[t];now + = Re;t = V[l-1];for (; t & lt;= n;t + = t & (-T)) c[t]++;l--; }while (R > a[i].t) {int t = V[r], re = 0;for (; t; t-= t & (-T)) re + = C[t];re = r-l + 1-re;now-= Re;t = V[r];f or (; t <= n;t + = t & (-T)) C[t]--;r--;} while (R < a[i].t) {int t = v[r + 1], re = 0;for (; t; t = t & (-T)) re + = C[t];re = r-l + 1-re;now + = Re;t = v[ R + 1];for (; t <= n;t + = t & (-T)) C[t]++;r + +;} A[i].ans = Now;} Sort (A + 1, a + q + 1, CMP2); for (i = 1;i <= q; + + i) printf ("%lld\n", A[i].ans);} 

Bzoj_3289_mato 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.