HYSBZ 1878 HH的项链


#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <functional>
using namespace std;

//----------------------------------------------------------
#define CL(a,b)       memset(a,b,sizeof(a))
#define CLQ(q)        while(!q.empty())q.pop();
#define FOR(i,a,b)    for(int i=a;i<=b;++i)
#define FD(i,a,b)     for(int i=a;i>=b;--i)
#define FOS(i,a,b,c)  for(int i=a;i<=b;i+=c)
#define FS(i,a)       for(int i=0;a[i];++i)
#define REP(i,n)      for(int i=0;i<n;++i)
#define PR2(a,n,m)    for(int i=0;i<n;++i){for(int j=0;j<m;++j)cout<<a[i][j]<<" ";puts("");}
#define max(a,b)      ((a)>(b)?(a):(b))
#define min(a,b)      ((a)<(b)?(a):(b))
#define checkMax(a,b) {if(a<b)a=b;}
#define checkMin(a,b) {if(a>b)a=b;}
#define READ(a)       freopen(a,"r",stdin)
#define WRITE(a)      freopen(a,"w",stdout)
#define sqr(x)        ((x)*(x))
#define inf           0x3f3f3f3f
#define INF           0x3f3f3f3f3f3f3f3fLL
#define eps           1e-10
typedef long long LL;
const double pi  = acos(-1.0);
const double hpi = asin(1.0);
//-----------------------------------------------------------
/*HYSBZ 1878 	HH的项链 */
/*利用树状数组统计区间相异元素的个数*/
/*
const int MAXN = 50010;
const int MAXM = 200010;

struct Node
{
    int pos, val;
    Node() {}
    Node(int t_pos, int t_val) : pos(t_pos), val(t_val) {}
    friend bool operator < (const Node &p1, const Node &p2)
    {
        return p1.val < p2.val;
    }
}p[MAXN];

struct Query
{
    int l, r, pos;
    Query() {}
    Query(int t_l, int t_r, int t_pos) : l(t_l), r(t_r), pos(t_pos) {}
    friend bool operator < (const Query &q1, const Query &q2)
    {
        if(q1.l == q2.l)
            return q1.r < q2.r;
        return q1.l < q2.l;
    }
}q[MAXM];

int Tree[MAXN], head[MAXN], next[MAXN], reflect[MAXN];
int ans[MAXM];
int n, m;

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

void modify(int x, int value)
{
    for(int i = x; i <= n; i += lowbit(i))
    {
        Tree[i] += value;
    }
    return ;
}

int getsum(int x)
{
    int ret = 0;
    for(int i = x; i > 0; i -= lowbit(i))
    {
        ret += Tree[i];
    }
    return ret;
}


int main()
{
    //READ("aa.in"); WRITE("bb.out");

    int cur_l, pos;
    CL(Tree, 0); CL(head, 0); CL(next, 0); CL(reflect, 0);

    scanf("%d", &n);
    FOR(i, 1, n)
    {
        scanf("%d", &p[i].val);
        p[i].pos = i;
    }
    scanf("%d", &m);
    FOR(i, 1, m)
    {
        scanf("%d %d", &q[i].l, &q[i].r);
        q[i].pos = i;
    }
    sort(p + 1 , p + n + 1);
    reflect[p[1].pos] = 1;
    FOR(i, 2, n)
    {
        if(p[i].val == p[i-1].val)
            reflect[p[i].pos] = reflect[p[i-1].pos];
        else
            reflect[p[i].pos] = i;
    }
    FD(i, n, 1)   //逆序
    {
        next[i] = head[reflect[i]];
        head[reflect[i]] = i;
    }
    FOR(i, 1, n)
    {
        if(head[i])
            modify(head[i], 1);
    }
    sort(q + 1, q + 1 + m);
    cur_l = pos = 1;
    while(pos <= m)
    {
        while(cur_l < q[pos].l)
        {
            if(next[cur_l])
                modify(next[cur_l], 1);
            //modify(cur_l, -1);   //可有可无,我们只需要保证在求解的区间内每一种颜色只有一个即可
            cur_l++;
        }
        while(cur_l == q[pos].l)
        {
            ans[q[pos].pos] = getsum(q[pos].r);
            if(q[pos].l > 1)
                ans[q[pos].pos] -= getsum(q[pos].l - 1);
            pos++;
        }
    }
    FOR(i, 1, m)
    {
        printf("%d\n", ans[i]);
    }
    return 0;
}
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值