sdoi hh Necklace Heoi Picking Flowers

Source: Internet
Author: User

Main topic:

Sdoi the number of occurrences of only one occurrence within an interval. Multiple groups of queries.

Heoi

Find the number of at least two occurrences within an interval. Multiple groups of queries.

Sdoi HH ' Neckplace
If each inquiry is 1. R, then we just turn the first occurrence of each color into 1, and then maintain the interval and then we can. This is obvious.
So now consider if the interval becomes 2. R, then it is clear that the first point is removed.
Then we can record the next position of each color, then the first point is removed, it is equivalent to the point of the next position becomes the first point,

So the position of the next point becomes 1. Because we maintain the prefix and, so the removed point does not change back to 0, because the prefix and so on, so the latter will lose the 1.
Heoi Flower
By the above question, we have an analogy.
If the range of inquiry is 1. R, then all we have to do is change the position of the second occurrence of a color to 1. Then maintain the interval and then you can. This is also obvious.
So now consider if the interval becomes 2. R, then obviously the first point is removed.
Then we can record the next position of each color, then the first point is removed, it is equal to the next position of the point becomes the first point, the next position of the next position becomes the second point,

Since we only turn the second point that appears into 1, it becomes the point of the first point (that is, the next position), we want to change it back to 0, the next position in the next location, we have to change it back to 1.
The two questions are all the same. Code:

This is the Huang long code. #include <iostream> #include <cstdio> #include <algorithm> #include <cstring>using namespace    Std;inline int read () {int X=0;char ch=getchar (); while (ch< ' 0 ' | |    Ch> ' 9 ') {Ch=getchar ();}    while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x;} int n,m,mx;int a[50005],next[50005],t[50005];int p[1000005];struct data{int L,r,id,ans;} Q[200005];bool CMP1 (data a,data b) {return A.L==B.L?A.R&LT;B.R:A.L&LT;B.L;} BOOL CMP2 (data A,data b) {return a.id<b.id;} int lowbit (int x) {return x& (-X);} void update (int x,int v) {for (int i=x;i<=n;i+=lowbit (i)) t[i]+=v;} int ask (int x) {int tmp=0;for (int i=x;i>0;i-=lowbit (i)) Tmp+=t[i];return tmp;} int main () {N=read (), for (int i=1;i<=n;i++) A[i]=read (), Mx=max (Mx,a[i]), and for (int i=n;i>0;i--) next[i]=p[a[i]],p[ a[i]]=i;for (int i=1;i<=mx;i++) if (P[i]) update (p[i],1); M=read (); for (int i=1;i<=m;i++) Q[i].l=read (), q[i].r= Read (), Q[i].id=i;sort (Q+1,Q+M+1,CMP1), int l=1;for (int i=1;i<=m;i++) {while (L&LT;Q[I].L) {if (Next[l]) update (next[l],1); l++;} Q[i].ans=ask (Q[I].R)-ask (Q[I].L-1);} Sort (Q+1,Q+M+1,CMP2); for (int i=1;i<=m;i++) printf ("%d\n", Q[i].ans); return 0;}

 

#include <cstdlib> #include <iostream> #include <cstring> #include <cstdio> #include <  Algorithm> #include <cctype>using namespace std;const int N = 1000000 + 5;inline int read () {int x = 0;  char C = GetChar ();  while (!isdigit (c)) C = GetChar ();    while (IsDigit (c)) {x = x * + C-' 0 ';  c = GetChar (); } return x;}  struct Query {int L, r, id;    BOOL operator < (const Query &k) Const {if (L = = K.L) return r < K.R;  return L < K.L; }}q[n]; int n, c, M;int Cc[n], ans[n], next[n], npos[n], a[n]; int lowbit (int x) {return x & (-X);}  int query (int x) {int res = 0;  for (int i = x; i > 0; I-= Lowbit (i)) res + = Cc[i]; return res;} void update (int x, int val) {for (int i = x; i <= n; i + = Lowbit (i)) cc[i] + = val;} int main () {//freopen ("1flower.i  N "," R ", stdin);   Freopen ("1flower.out", "w", stdout); n = read (); c = Read ();  m = read ();  for (int i = 1; I <= n; + + i) a[i] = read (); for (int i = 1; I <= m; + + i){Q[I].L = read (); Q[I].R = read ();  Q[i].id = i;  } sort (q + 1, q + M + 1);    for (int i = n; I >= 1;--i) {next[i] = npos[a[i]];  Npos[a[i]] = i;  } for (int i = 1; I <= n; + + i) if (Next[npos[i]]) update (Next[npos[i]], 1);  int L = 1;      for (int i = 1; I <= m; + + i) {while (L < Q[I].L) {if (Next[l]) update (Next[l],-1);      if (Next[next[l]]) update (NEXT[NEXT[L]], 1);    L + +;  } Ans[q[i].id] = query (Q[I].R)-Query (Q[I].L-1);  } for (int i = 1; I <= m; + + i) printf ("%d\n", Ans[i]); Fclose (stdin);  Fclose (stdout); return 0;}

sdoi hh Necklace Heoi Picking Flowers

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.