洛谷P1972 [SDOI2009]HH的项链 离线+树状数组 主席树

题目链接:https://www.luogu.com.cn/problem/P1972
题意:区间不同数的个数。

思路:第一种写法:离线询问,将其按照r大小排序,然后对于ai,将当前位置+1,上一次出现位置-1,求前缀和就是答案。

#include
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define ls rt << 1
#define rs rt << 1|1
#define po pop_back
#define pb push_back
#define mk make_pair
#define lson l, mid, ls
#define rson mid + 1, r, rs
#define pll pair
#define pii pair
#define ull unsigned long long
#define pdd pair
const int mod = 998244353;
const int maxn = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const ll linf = 0x3f3f3f3f3f3f3f3f;
struct node
{
    int l, r, id;
    bool operator < (const node &s) const
    {
        return r < s.r;
    }
}b[maxn];
int a[maxn], c[maxn], ans[maxn], vis[maxn];

int lowbit(int i)
{
    return i & (-i);
}
void add(int i, int x)
{
    while(i < maxn)
    {
        c[i] += x;
        i += lowbit(i);
    }
}
int query(int i)
{
    int res = 0;
    while(i > 0)
    {
        res += c[i];
        i -= lowbit(i);
    }
    return res;
}

int main()
{
    int n, m;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    scanf("%d", &m);
    for(int i = 1; i <= m; ++i) scanf("%d%d", &b[i].l, &b[i].r), b[i].id = i;
    sort(b + 1, b + m + 1);
    int x = 1, num = 0;
    for(int i = 1; i <= m; ++i)
    {
        while(x <= b[i].r)
        {
            if(vis[a[x]]) add(vis[a[x]], -1);
            vis[a[x]] = x;
            add(x, 1);
            ++x;
        }
        ans[b[i].id] = query(b[i].r) - query(b[i].l - 1);
    }
    for(int i = 1; i <= m; ++i) printf("%d\n", ans[i]);
	return 0;
}

第二种写法:记录一下每个数下一个数的位置,没有就是n+1,然后就转换成了区间[l, r]中有多少大于r的数,用主席树求解即可。但是本题卡主席树,所以会有几个点超时,代码看看思路就好。

#include
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define ls rt << 1
#define rs rt << 1|1
#define po pop_back
#define pb push_back
#define mk make_pair
#define lson l, mid, ls
#define rson mid + 1, r, rs
#define pll pair
#define pii pair
#define ull unsigned long long
#define pdd pair
const int mod = 1e9 + 7;
const int maxn = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const ll linf = 0x3f3f3f3f3f3f3f3f;
int sum[maxn << 5], root[maxn], Ls[maxn << 5], Rs[maxn << 5]; //sum记录前缀和数量,root记录根范围
int a[maxn], b[maxn], vis[maxn];
int n, m, tot = 0, len;

int getid(int val) //返回离散化下标
{
    return lower_bound(b + 1, b + len + 1, val) - b;
}
int build(int l, int r) //不用build root[0] = 0
{
    int x = ++tot; //新增结点
    sum[x] = 0;
    if(l == r) return x; //叶子结点返回编号
    int mid = (l + r) >> 1;
    Ls[x] = build(l, mid);
    Rs[x] = build(mid + 1, r);
    return x;
}
int update(int k, int l, int r, int rt) //插入元素k
{
    int x = ++tot;
    Ls[x] = Ls[rt], Rs[x] = Rs[rt], sum[x] = sum[rt] + 1; //复制信息
    if(l == r) return x;
    int mid = (l + r) >> 1; //接下来要更新要增加的链
    if(k <= mid) Ls[x] = update(k, l, mid, Ls[x]);
    else Rs[x] = update(k, mid + 1, r, Rs[x]);
    return x;
}
int query_kth(int u, int v, int l, int r, int k) //查询第k大
{
    int mid = (l + r) >> 1, x = sum[Ls[v]] - sum[Ls[u]]; //通过前缀和得到当前左区间的数目
    if(l == r) return l;
    if(k <= x) return query_kth(Ls[u], Ls[v], l, mid, k); //如果左边够就去左边
    else return query_kth(Rs[u], Rs[v], mid + 1, r, k - x);
}
int query_num(int u, int v, int l, int r, int ql, int qr) //查询区间个数
{
    if(ql <= l && qr >= r) return sum[v] - sum[u];
    int mid = (l + r) >> 1, res = 0;
    if(ql <= mid) res += query_num(Ls[u], Ls[v], l, mid, ql, qr);
    if(qr > mid) res += query_num(Rs[u], Rs[v], mid + 1, r, ql, qr);
    return res;
}
int query_kind(int u, int v, int l, int r, int k) //统计区间大于等于k的数目
{
    if(l == r) return sum[v] - sum[u]; //l这个点在这个区间出现次数
    int mid = (l + r) >> 1;
    //如果在左边,那么就遍历左边再加上右边全部的数量
    if(k <= mid) return query_kind(Ls[u], Ls[v], l, mid, k) + sum[Rs[v]] - sum[Rs[u]];
    else return query_kind(Rs[u], Rs[v], mid + 1, r, k);
}
void init()
{
    tot = 0;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i)
    {
        scanf("%d", &a[i]);
        if(vis[a[i]]) b[vis[a[i]]] = i;
        vis[a[i]] = i;
        //b[i] = a[i];
    }
    for(int i = 1; i <= n; ++i)
        if(!b[i]) b[i] = n + 1;
    //sort(b + 1, b + n + 1);
    //len = unique(b + 1, b + n + 1) - b - 1;
    root[0] = 0; //不用build
    for(int i = 1; i <= n; ++i)
        root[i] = update(b[i], 1, n + 1, root[i - 1]);
}

int main()
{
    init();
    scanf("%d", &m);
    while(m--)
    {
        int l, r;
        scanf("%d%d", &l, &r);
        printf("%d\n", query_kind(root[l - 1], root[r], 1, n + 1, r + 1));
    }
    return 0;
}

 

你可能感兴趣的:(主席树,树状数组,洛谷)