P3709 string question (50 points), p3709 string

Source: Internet
Author: User

P3709 string question (50 points), p3709 string
Background

There is a school in the far southwest.

/* Harmony part */

Then go to the provincial election and abuse venues in the province.

Then a certain operator won't do it, so there is also a string question:

Description

Give you a string a. Each time you ask for the contribution of a range

Contribution definition:

Each time a random character x is taken out from this range, and then x is deleted from this range. You need to maintain a set of S

If S is empty, you rp minus 1

If one element in S is not less than x, then you rp minus 1 to clear S

Then insert x into S.

As you are a big master, you will be admitted to the exams you have done at ordinary times. So every time you ask you how many rp files are there after you have completed the characters in this interval? Rp is initially 0

The query does not affect each other ~

Input/Output Format

Input Format:

 

The number n and m in the first line, indicating the length of the string and the number of inquiries.

N numbers in the next row, indicating the string

Because you are an uncle, so the character set 1e9

The next m rows have two numbers in each row, indicating the left and right intervals of the query.

 

Output Format:

 

M rows. Each row has one number to indicate the answer.

 

Input and Output sample input sample #1:
3 33 3 33 33 33 3
Output sample #1:
-1-1-1
Description

The first four vertices are 1 s, and the following vertices are 4 s.

For 10% of the data, this is an example.

For another 10% of data, n, m <= 100

For another 10% of data, n, m <= 1000

For another 10% of data, n, m <= 10000

For another 10% of data, n, m <= 100000

For 100% of data, n, m <= 200000

Make sure that the data is the same as sb FOR day1T2 in a province. You can use brute force questions!

It's okay. If you are in a good school, even if you only get 10 points for this question, you can join the team.

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <cstdlib> 6 # include <algorithm> 7 # include <vector> 8 # define hh 10 9 using namespace std; 10 const int MAXN = 2000001; 11 void read (int & n) 12 {13 char c = '+'; int x = 0; bool flag = 0; 14 while (c <'0' | c> '9') 15 {c = getchar (); if (c = '-') flag = 1 ;} 16 while (c> = '0' & c <= '9') 17 {x = x * 10 + (c-48); c = getchar ();} 18 flag = 1? N =-x: n = x; 19} 20 int n, m; 21 int pos [MAXN]; 22 int base; 23 int rp = 0; 24 struct node 25 {26 int l, r, id; 27} q [MAXN]; 28 struct dr 29 {30 int a, p; 31} a [MAXN]; 32 int comp (const node & a, const node & B) 33 {34 if (pos [. l] = pos [B. l]) 35 return. r <B. r; 36 else 37 return pos [. l] <pos [B. l]; 38} 39 int cop (const dr & a, const dr & B) 40 {41 return (. a <B. a) | (. a = B. a &. p <B. p); 42} 43 int happen [MAX N]; // record the number of times I appears 44 int cnt [MAXN]; // record the number of times j appears 45 int out [MAXN]; 46 void dele (int p) 47 {48 if (rp = happen [p] & cnt [happen [p] + hh] = 1) 49 rp --; 50 cnt [happen [p] + hh] --; 51 cnt [happen [p] + hh-1] +; 52 happen [p] --; 53} 54 void add (int p) 55 {56 if (rp = happen [p]) 57 rp ++; 58 cnt [happen [p] + hh] --; 59 cnt [happen [p] + hh + 1] ++; 60 happen [p] ++; 61} 62 int ls [MAXN]; 63 void modui () 64 {65 int ll = 1, rr = 0; 66 f Or (int I = 1; I <= m; I ++) 67 {68 for (; ll <q [I]. l; ll ++) 69 dele (ls [ll]); 70 for (; ll> q [I]. l; ll --) 71 add (ls [ll-1]); 72 for (; rr> q [I]. r; rr --) 73 dele (ls [rr]); 74 for (; rr <q [I]. r; rr ++) 75 add (ls [rr + 1]); 76 out [q [I]. id] =-rp; 77} 78 for (int I = 1; I <= m; I ++) 79 printf ("% d \ n ", out [I]); 80} 81 int main () 82 {83 read (n); read (m); 84 base = sqrt (n ); 85 for (int I = 1; I <= n; I ++) 86 read (a [I]. a), a [I]. p = I; 87 sort (a + 1, a + n + 1, c Op); 88 int j; 89 for (int I = 1, j = 0; I <= n; I ++) 90 {91 if (I = 1 | a [I]. a! = A [I-1]. a) j ++; 92 ls [a [I]. p] = j; 93} 94 // for (int I = 1; I <= n; I ++) 95 // pos [I] = (I-1) /base + 1; 96 int sqt = 0; 97 for (sqt = 1; sqt * sqt <= n; sqt ++); 98 99 for (int I = 1; I <= n; I ++) pos [I] = I/sqt; 100 101 cnt [0] = j; 102 for (int I = 1; I <= m; I ++) 103 {104 read (q [I]. l); 105 read (q [I]. r); 106 q [I]. id = I; 107} 108 sort (q + 1, q + m + 1, comp); 109 modui (); 110 return 0; 111}

 

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.