HDU5919 Sequence II

链接

Sequence II

题解

好题。第一反应肯定是确定区间有多少个不同的数,以为很好维护,其实不然。想法是看上一次出现位置在不在区间内的数的个数。注意到对于一个区间来说,每个数只有第一次出现的位置才对答案有贡献,这样我们对每一个区间左端点,可以只保留每个数第一次出现的位置,然后求区间第 K K K大,自然想到主席树,由于要保留第一次出现位置,所以从后往前扫,建主席树即可。

代码

#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

const int maxn = 222222;
// const int maxn = 50;
const int maxm = 400000 + 100;
// const int maxm = 100;
const int maxt = 3000000 + 100;
const int maxk = 1000 + 5;

typedef long long LL;
typedef long double LD;
typedef unsigned long long uLL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;

const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const LL Inf = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1e-4;
const double inf = 1e15;
const double pi = acos(-1.0);
const LL mod = 1000000007;

inline int read()
{
    register int x = 0, t = 1;
    register char ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-')
        ch = getchar();
    if (ch == '-')
        t = -1, ch = getchar();
    while (ch <= '9' && ch >= '0')
        x = x * 10 + ch - 48, ch = getchar();
    return x * t;
}

struct SegTree
{
    int lc, rc, sum;
} tree[maxn << 5];

int n, m, tot, ans;
int a[maxn], root[maxn], pos[maxn];

void Modify(int &now, int rt, int l, int r, int p, int v)
{
    now = ++tot;
    tree[now] = tree[rt];
    tree[now].sum += v;
    if (l == r)
        return;
    int m = (l + r) >> 1;
    if (p <= m)
        Modify(tree[now].lc, tree[rt].lc, l, m, p, v);
    else
        Modify(tree[now].rc, tree[rt].rc, m + 1, r, p, v);
}

int AskSum(int rt, int l, int r, int L, int R)
{
    if (L <= l && r <= R)
        return tree[rt].sum;
    int m = (l + r) >> 1, ans = 0;
    if (L <= m)
        ans += AskSum(tree[rt].lc, l, m, L, R);
    if (R > m)
        ans += AskSum(tree[rt].rc, m + 1, r, L, R);
    return ans;
}

int AskPos(int rt, int l, int r, int k)
{
    if (l == r)
        return l;
    int m = (l + r) >> 1, s = tree[tree[rt].lc].sum;
    if (k <= s)
        return AskPos(tree[rt].lc, l, m, k);
    else
        return AskPos(tree[rt].rc, m + 1, r, k - s);
}

int iCase;

int main()
{
    int T;
    T = read();
    while (T--)
    {
        printf("Case #%d:", ++iCase);
        tot = ans = 0;
        memset(root, 0, sizeof(root));
        memset(pos, 0, sizeof(pos));
        memset(tree, 0, sizeof(tree));
        n = read(), m = read();
        for (int i = 1; i <= n; ++i)
            a[i] = read();
        for (int i = n; i; --i)
        {
            if (!pos[a[i]])
                Modify(root[i], root[i + 1], 1, n, i, 1), pos[a[i]] = i;
            else
            {
                Modify(root[i], root[i + 1], 1, n, i, 1);
                Modify(root[i], root[i], 1, n, pos[a[i]], -1);
                pos[a[i]] = i;
            }
        }
        int l, r;

        while (m--)
        {
            l = (read() + ans) % n + 1, r = (read() + ans) % n + 1;
            // printf("\nl: %d r: %d\n", l, r);
            if (l > r)
                swap(l, r);
            int ss = AskSum(root[l], 1, n, l, r);
            ans = AskPos(root[l], 1, n, (ss + 1) / 2);
            printf(" %d", ans);
        }
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值