bzoj1878 [Sdoi2009]hh's Necklace

Source: Internet
Author: User

1878: [Sdoi2009]hh's Necklace time limit:4 Sec Memory limit:64 MB
submit:3199 solved:1611
[Submit] [Status] [Discuss] DESCRIPTIONHH has a chain of beautiful shells. HH believes that different shells will bring good luck, so after each walk, he will take out a shell and think about what they mean. HH keeps collecting new shells, so his necklace is getting longer. One day, he suddenly raised a question: how many different shells are included in a certain piece of shell? The question is difficult to answer ... Because the necklace is too long. So he had to ask the wise you to solve the problem. Input first line: An integer n that represents the length of the necklace. Second line: n integers representing the number of shells in the necklace (integers numbered 0 to 1000000, in turn). Line three: An integer m that indicates the number of HH queries. Next m line: two integers per line, L and R (1≤l≤r≤n), indicating the interval of the inquiry. OUTPUTM lines, one integer per line, in turn, to ask the corresponding answer. Sample Input6
1 2 3 4 3 5
3
1 2
3 5
2 6
Sample Output2
2
4
HINT


For 20% of data, n≤100,m≤1000;
For 40% of data, n≤3000,m≤200000;
For 100% of data, n≤50000,m≤200000.

Source

Day2

Analysis: See the interval to find the number, it seems can be solved with the line segment tree, but the number of colors can not be directly added, so with a line tree processing some trouble, if you do not use a line tree to solve all the queries are best ordered, so first sorted by the left endpoint.

If we ask the interval [x, y], then if there is a color repeated several times, then only need to calculate once, then the interval of all the repeating color as one, specifically which one? Because to choose a special point, then must choose a special position, then we select from the left side of the first, remember that I, if I have the same color as I on the left side, then must be in the interval [x, y], because the number of requirements, then the interval [x, y] The color of the left side of the same color position +1, because it is sorted according to the left endpoint, so does not repeat the calculation, because it involves a lot of interval calculation, so use the tree array and prefix and, say the abstract, see a set of data: 1 2 1 3, the tree array is: 0 0 0 0, first found the first 1, the tree array into 1 0 0 0, then found 2, Became 1 1 0 0, then found 1, became 0 1 1 0, and then found 3, became 0 1 1 1, for a query [2,3], then the answer is sum (3)-sum (2-1). What if it's for [a]? Now the answer is 0! What to do? In fact, according to the previous narrative, only need to consider the interval [x, y] to the left of the color can, bad description, if not understand please manually simulate a few sets of data can find the law.

Recommend a learning tree-like array blog: http://blog.csdn.net/int64ago/article/details/7429868

#include <cstdio>#include<vector>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intMAXN =50010, MAXM =200010, maxk =1000010;structnode{intx, y, id;} A[MAXM];intN,F[MAXN],NEXTT[MAXK],HEAD[MAXK],TEMPMAX,S[MAXN],M,ANS[MAXM];BOOLCMP1 (node p, node Q) {returnP.x <q.x; if(p.x = =q.x)returnP.y <q.y;}voidAddintXinty) {     for(; x <= n; x + = x&-x) s[x]+=y;}intQueryintx) {    inttemp =0;  for(; x;x-= x&-x) Temp+=S[x]; returntemp;}intMain () {scanf ("%d", &N);  for(inti =1; I <= N; i++) scanf ("%d", &F[i]);  for(inti = n; I >=1; i--) {Nextt[i]=Head[f[i]]; Head[f[i]]=i; Tempmax=Max (Tempmax, F[i]); } scanf ("%d", &m);  for(inti =1; I <= m; i++) {scanf ("%d%d", &a[i].x, &a[i].y); A[i].id=i; } sort (A+1, A +1+m, CMP1);  for(inti =1; I <= Tempmax; i++)        if(Head[i]) Add (Head[i],1); intcursor =1;  for(inti =1; I <= m; i++)    {         while(Cursor <a[i].x) {            if(Nextt[cursor]) Add (Nextt[cursor],1); Cursor++; } Ans[a[i].id]= Query (A[I].Y)-Query (a[i].x-1); }     for(inti =1; I <= m; i++) printf ("%d\n", Ans[i]); return 0;}

bzoj1878 [Sdoi2009]hh's Necklace

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.