Luogu-P1972 HH necklace

Topic links: https://www.luogu.org/problem/P1972

Subject description:

HH string necklace from a variety of beautiful shells composed. HH believe different shells to bring good luck, so after every walk, he will be free to shell out a while, thinking the meaning they express. HH constantly collecting new shell, so he's getting longer necklace.

One day, he suddenly raises the question: a certain period of shells, including how many different shells? This question is difficult to answer ...... because the necklace is too long. So he had to help you wise, to solve this problem.

Input formats:

Line a positive integer n, the length of the necklace.
The second line n positive integers  A i , represents necklace of i type a shell.

A third line integer  m , HH represents the number of inquiries.

Next  m lines of two integers  l, r, representing the interval inquiry.

Output formats:

Output m lines, each integer, respectively for the corresponding query answer.

For 2 0 % of the data, . 1 n- , m . 5 0 0 0;
for . 4 0 % of the data, . 1 n- , m . 1 0 . 5 ;
for . 6 0 % of the data, . 1 n- , m . 5 × . 1 0 . 5 ;
for . 1 0 0 % of the data, . 1 n- , m , a I 1061lrn。

 

This problem solution is more varied, here put off a tree-array approach:

Consider asking the same number of r all, the figures for recurring, it need only be concerned about the position of the last occurrence, other occurrence position can be ignored.

Therefore, in accordance with all inquiries r sorted, and then start from the first element, in order to ask whether the use of Fenwick tree maintenance of a digital first appearance of this position (the position before appeared before if we find that number, -1 the new position +1).

Query [l, r] in the answer section, and may be converted to query prefix.

#pragma GCC optimize (3)
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
/*#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/trie_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>*/
#define PII pair<int,int>
#define ll long long
#define ull unsigned long long
#define INFI 2147483647
#define INFL 9223372036854775807
#define INFU 18446744073709551615
#define maxn 1000005
using namespace std;
//using namespace __gnu_pbds;
const double PI=acos(-1.0);
const double eps=1e-6;
int n,m,a[maxn],tree[maxn],res[maxn],vis[maxn];
struct Query
{
    int l,r;
    int id;
}q[maxn];
struct Rule
{
    inline bool operator()(const Query& t1,const Query& t2)
    {
        if(t1.r!=t2.r) return t1.r<t2.r;
        if(t1.l!=t2.l) return t1.l<t2.l;
        return t1.id<t2.id;
    }
};
int lowbit(int p)
{
    return p&(-p);
}
void add(int p,int x)
{
    while(p<=n){
        tree[p]+=x;
        p+=lowbit(p);
    }
    return;
}
int query(int p)
{
    int ans=0;
    while(p!=0){
        ans+=tree[p];
        p-=lowbit(p);
    }
    return ans;
}
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0' || ch>'9'){
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
int main()
{
    //ios_base::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    n=read();
    for(int i=1;i<=n;i++) a[i]=read();
    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+1+m,Rule());
    int nxt=1;
    for(int i=1;i<=m;i++){
        for(int j=nxt;j<=q[i].r;j++){
            if(vis[a[j]]) add(vis[a[j]],-1);
                add(j,1);
                vis[a[j]]=j;
        }
        nxt=q[i].r+1;
        res[q[i].id]=query(q[i].r)-query(q[i].l-1);
    }
    for(int i=1;i<=m;i++) printf("%d\n",res[i]);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/HouraisanKaguya/p/11908642.html
.hh
hh