# BZOJ1878 [SDOI2009]HH的项链

（同时我们发现由于前缀和相减，于是当前左端点左边的a数组就不用管它了，于是每次只要修改一个值即可）

 1 /**************************************************************
2     Problem: 1878
3     User: rausen
4     Language: C++
5     Result: Accepted
6     Time:960 ms
7     Memory:8428 kb
8 ****************************************************************/
9
10 #include <cstdio>
11 #include <algorithm>
12
13 #define lowbit(x) x & -x
14 using namespace std;
15 const int N = 50005;
16 const int Color = 1000005;
17 const int Q = 200005;
18
19 struct data{
20     int l, r, w;
21     inline bool operator < (const data &x) const {
22         return l == x.l ? r < x.r : l < x.l;
23     }
24 } q[Q];
25
26 int n, m, mx;
27 int a[N], next[N];
28 int BIT[N];
29 int ans[Q];
30 int now[Color];
31
33     int x = 0;
34     char ch = getchar();
35     while (ch < '0' || '9' < ch)
36         ch = getchar();
37     while ('0' <= ch && ch <= '9') {
38         x = x * 10 + ch - '0';
39         ch = getchar();
40     }
41     return x;
42 }
43
44 void update(int x, int v) {
45     while (x <= n)
46         BIT[x] += v, x += lowbit(x);
47 }
48
49 int query(int x) {
50     int res = 0;
51     while (x)
52         res += BIT[x], x -= lowbit(x);
53     return res;
54 }
55
56 int L, pr[10];
57 void print(int x) {
58     if (!x) {
59         puts("0");
60         return;
61     }
62     while (x)
63         pr[++L] = x % 10, x /= 10;
64     while (L)
65         putchar(pr[L--] + '0');
66     putchar('\n');
67 }
68
69 int main() {
70     int i, l;
72     for (i = 1; i <= n; ++i)
73         a[i] = read(), mx = max(mx, a[i]);
74     for (i = n; i; --i)
75         next[i] = now[a[i]], now[a[i]] = i;
76     for (i = 1; i <= mx; ++i)
77         if (now[i]) update(now[i], 1);
78
80     for (i = 1; i <= m; ++i)
82     sort(q + 1, q + m + 1);
83     for (i = l = 1; i <= m; ++i) {
84         for (; l < q[i].l; ++l)
85             if (next[l]) update(next[l], 1);
86         ans[q[i].w] = query(q[i].r) - query(q[i].l - 1);
87     }
88     for (i = 1; i <= m; ++i)
89         print(ans[i]);
90     return 0;
91 }
View Code

posted on 2014-12-14 10:54  Xs酱~  阅读(935)  评论(3编辑  收藏  举报