码迷,mamicode.com
首页 > 其他好文 > 详细

「SDOI2009」HH的项链

时间:2020-01-24 00:25:21      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:space   ace   代码   pre   com   for   char   sdoi   加强   

「SDOI2009」HH的项链

传送门
数据加强了,莫队跑不过了。
考虑用树状数组。
先把询问按右端点递增排序。
然后对于每一种贝壳,我们都用它最右一次出现的位置计算答案。
具体细节看代码吧。
参考代码:

#include <algorithm>
#include <cstdio>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
    s = 0; int f = 0; char c = getchar();
    while ('0' > c || c > '9') f |= c == '-', c = getchar();
    while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
    s = f ? -s : s;
}

const int _ = 1e6 + 5;

int n, q, a[_], res[_], tr[_], tag[_];

struct node { int l, r, id; } t[_];
inline bool cmp(const node& x, const node& y) { return x.r < y.r; }

inline int lb(int x) { return x & -x; }

inline void update(int x, int v)
{ for (rg int i = x; i <= n; i += lb(i)) tr[i] += v; }

inline int query(int x)
{ int res = 0; for (rg int i = x; i >= 1; i -= lb(i)) res += tr[i]; return res; }

int main() {
#ifndef ONLINE_JUDGE
    file("cpp");
#endif
    read(n);
    for (rg int i = 1; i <= n; ++i) read(a[i]);
    read(q);
    for (rg int i = 1; i <= q; ++i) read(t[i].l), read(t[i].r), t[i].id = i;
    sort(t + 1, t + q + 1, cmp);
    int pos = 1;
    for (rg int i = 1; i <= q; ++i) {
        for (rg int j = pos; j <= t[i].r; ++j) {
            if (tag[a[j]]) update(tag[a[j]], -1);
            update(j, 1), tag[a[j]] = j;
        }
        pos = t[i].r + 1;
        res[t[i].id] = query(t[i].r) - query(t[i].l - 1);
    }
    for (rg int i = 1; i <= q; ++i) printf("%d\n", res[i]);
    return 0;
}

「SDOI2009」HH的项链

标签:space   ace   代码   pre   com   for   char   sdoi   加强   

原文地址:https://www.cnblogs.com/zsbzsb/p/12231638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!