Dlutoj #1394 Magic Questions

Source: Internet
Author: User

Transmission Door

Time Limit:3 Sec Memory limit:128 MB


Alice likes playing games. So she'll take part in the movements of M within N days, and each game was represented in an integer between 1 and M. Roo Mmates has Q magic questions:how many different kinds of games does Alice participate between Lth day and Rth Day (includ ing Lth Day and Rth day)?


You'll be given a number of cases; Each case contains blocks of several lines. The first line contains 2 numbers of N and M. The second line contains n numbers implying the game numbers this Alice take part in within n days. The third line contains a number of Q. Then Q lines is entered. Each line contain the numbers of L and R.



There should is Q output lines per test case containing Q answers required.

Sample INPUT5 3 1 2 3 2 2 3 1 4 2 4 1 5Sample Output3 2 3HINT

This is the school game of this year's K, a classic topic, but the scene did not a.

Online can be used by the Chairman tree, not yet. There is a clever use of the array of the offline solution, relatively good writing.

The main points are:

1. Sort the query by the right endpoint from small to large.

2. Record the position of each number in the last occurrence. When the number reappears, it removes the count from its last occurrence position.


The subject is a double pointer.

#include <bits/stdc++.h>using namespacestd;Const intN (1e5+5);intN, M, Q, A[n], pos[n], bit[n], ans[n];voidAddintXintv) {     for(; x<=n; Bit[x]+=v, x+=x&-x);}intSumintx) {    intres=0;  for(; x; res+=bit[x], x-=x&-x); returnRes;}structp{intL, R, id; P (intLintRintID): L (L), R (r), ID (ID) {} P () {}; BOOL operator< (ConstP&AMP;B)Const{returnr<B.R;}} P[n];intMain () {//Ios::sync_with_stdio (false);     for(; ~SCANF ("%d%d", &n, &m); ){         for(intI=1; i<=n; i++) scanf ("%d", A +i); scanf ("%d", &P);  for(intL, R, i=0; i<q; i++) scanf ("%d%d", &l, &r), p[i]={L, R, i}; Sort (p, p+Q);//Error-pronememset (bit,0,sizeof(bit)); memset (POS,0,sizeof(POS));  for(intI=1, j=0K j<q&&i<=n; ){//Error-prone             for(; i<=p[j].r; i++){                if(Pos[a[i]]) Add (Pos[a[i]],-1); Pos[a[i]]=i; Add (i,1); }             for(k=j; k<q&&p[k].r==p[j].r; k++)//Error-proneAns[p[k].id]=sum (P[K].R)-sum (p[k].l-1); J=K; }         for(intI=0; i<q; i++) printf ("%d\n", Ans[i]);//Error-prone    }    return 0;}

Dlutoj #1394 Magic Questions

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.