Luo Gu P1972 [SDOI2009] HH necklace (Fenwick tree, offline)

Portal

Problem-solving ideas

Because it is the number of different types of demand interval, so we use Fenwick tree (and seemingly no direct contact)

(... to represent)

Or the same as the original, with s [i] to represent the number of kinds of a [i-lowbit (i)] ... a [i] is.

Because there is a similar operation to heavy, so there is a record about IS array [i] represents the number i where the shell first occurring, an update result each time s [is [i]] -; s [ i] ++; is [i] = i.

However, we think that one section is assumed inquiry is a ... b, next interval a ... b-5, and x in this shell b-3, b-7 has appeared (a <b-7 <b-5 <b-3 <b), then we are asking that the A ... s [b-7] when b -; then ask a ... b-5 when the answer will result in an error (less forget a). (Explanation is not clear, emotional understanding it)

So here we use an action - off-line , the so-called off-line, one-time issue is all the input, according to a certain order to sort (to facilitate problem solving), and then save the answer output according to the original order.

How do offline applications? We can use the structure data stored, each variable structure has a stored value, another serial number stored, after reading all the values ​​of the sort again by keyword, the keyword number according to the former and then outputs sort answer again, so its purpose.

For example, the questions:

. 1  struct Ques {
 2      int L, R & lt, ANS, ID;
 . 3  } Q [MAXN];
 . 4  BOOL CMP1 (Ques A, Ques B) {
 . 5      return Ar < br;
 . 6  }
 . 7  BOOL CMP2 (Ques A, Ques B) {
 . 8      return a.id < b.id;
 . 9  }
 10  int main () {
 . 11      // read data 
12 is      Sort (Q + . 1 , m + Q + . 1 , CMP1); // first pass Sort
 13 is      // obtained answer 
14      Sort (Q + . 1 , m + Q +. 1 , CMP2); // second pass Sort
 15      // output answer 
16      return  0 ;
 . 17 }

AC Code

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 using namespace std;
 5 const int maxn=500005;
 6 int s[maxn],n,a[maxn],m;
 7 int is[maxn*2];
 8 inline int lowbit(int x) {
 9     return x&(-x);
10 }
11 void insert(int id,int k) {
12     for(int i=id; i<=n; i+=lowbit(i)) {
13         s[i]+=k;
14     }
15 }
16 int query(int id) {
17     int res=0;
18     for(int i=id; i>0; i-=lowbit(i)) {
19         res+=s[i];
20     }
21     return res;
22 }
23 struct ques {
24     int l,r,ans,id;
25 } q[maxn];
26 bool cmp1(ques a,ques b) {
27     return a.r<b.r;
28 }
29 bool cmp2(ques a,ques b) {
30     return a.id<b.id;
31 }
32 int main() {
33     cin>>n;
34     int cnt=1;
35     for(int i=1; i<=n; i++) {
36         scanf("%d",&a[i]);
37     }
38     cin>>m;
39     for(int i=1; i<=m; i++) {
40         scanf("%d%d",&q[i].l,&q[i].r);
41         q[i].id=i;
42     }
43     sort(q+1,q+m+1,cmp1);
44     for(int i=1; i<=n; i++) {
45         if(is[a[i]]) {
46             insert(is[a[i]],-1);
47         }
48         insert(i,1);
49         is[a[i]]=i;
50         while(q[cnt].r==i&&cnt<=m) {
51             q[cnt].ans=query(q[cnt].r)-query(q[cnt].l-1);
52             cnt++;
53         }
54     }
55     sort(q+1,q+m+1,cmp2);
56     for(int i=1; i<=m; i++) {
57         printf("%d\n",q[i].ans);
58     }
59     return 0;
60 }

 

Guess you like

Origin www.cnblogs.com/yinyuqin/p/11110794.html