bzoj 1878

来源:互联网 发布:域名绑定服务器 编辑:程序博客网 时间:2024/06/16 20:33

这题妙啊
离线处理, 先将询问右端点排序
因为是问不同的
so 这样, 不可用语言描述啊

#include <bits/stdc++.h>using namespace std;typedef long long LL;const int INF = 0x3f3f3f3f;inline int read(void){    int x = 0, c = 0, f = 1;    for(;c<'0'||c>'9';c=getchar())f=c!='-';    for(;c>='0'&&c<='9';c=getchar())x=x*10+c-'0';    return f ? x : -x;}const int N = 51000, M = 201000;struct node{    int l, r, id;    bool operator < (const node &t) const {        return r != t.r ? r < t.r : l < t.l;    }} que[M];int cnt[N], ans[M], n, m, col[N], last[1010000], pre[N];#define lowbit(x) (x&-x)inline void Updata(int pos,int val) {    for(;pos<=n;pos+=lowbit(pos))        cnt[pos] += val;}inline int Query(int pos){    int ret = 0;    for(;pos;pos^=lowbit(pos))        ret += cnt[pos];    return ret;}int main() {    n = read();     for (int i = 1; i <= n; i++) {        col[i] = read();        pre[i] = last[col[i]];        last[col[i]] = i;    }    m = read();    for (int i = 1; i <= m; i++) {        que[i].l = read(), que[i].r = read();        que[i].id = i;    }    sort(que+1,que+m+1);    int now = 0;    for (int i = 1; i <= m; i++) {        while (now < que[i].r) {            now++;            Updata(pre[now]+1, 1);            Updata(now+1, -1);        }        ans[que[i].id] = Query(que[i].l);    }    for (int i = 1; i <= m; i++)        printf("%d\n",ans[i]);}
原创粉丝点击