HDU 5289 Assignment (线段树)

Problem Description

Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this company, and every staff has a ability. Now, Tom is going to assign a special task to some staffs who were in the same group. In a group, the difference of the ability of any two staff is less than k, and their numbers are continuous. Tom want to know the number of groups like this.

Input

In the first line a number T indicates the number of test cases. Then for each case the first line contain 2 numbers n, k (1<=n<=100000, 0

Output

For each test,output the number of groups.

题意

按序给出 n 个职工的能力值 ai 。问存在多少个 group ?其中 group 被定义为在同一个 group 中的任意两个职工的能力差不能大于等于 k ,且同一 group 中的职工编号连续。每个职工可以属于多个 group

解题思路

为避免重复统计,以编号 i 的职工对结果的贡献仅统计以其为 group 左区间,右区间为 R 的贡献。

对于编号 i 的职工,查找满足 ajai+k ajaik 的最小 j ,并将 j 作为编号 i 对结果贡献的右边界。其中 j 必须大于 i。

则对于职工 i 的真正右边界,即为区间 [i, j] 的所有职工的右边界的最小值,通过线段树求最小值。

代码

#include<bits/stdc++.h>
using namespace std;
const int N = 100000 + 10;
int T, n, k, a[N], r[N];
pair<int, int> p, q;
set<pair<int, int> > st;
set<pair<int, int> >::iterator it, ip;
vector<set<pair<int, int> >::iterator > v;
bool operator<(pair<int, int> a, pair<int, int> b) {
    if(a.first == b.first)
        return a.second < b.second;
    return a.first < b.first;
}
void ERASE() {
    for(int i=0;i<v.size();i++)
        st.erase(v[i]);
    v.clear();
}
const int TREE_SIZE = (N << 2) + 10;
class IntervalTree
{
    private:
        int Cover[TREE_SIZE],Top[TREE_SIZE];
        int size;
        int _Query(int a,int b,int l,int r,int idx)
        {
            if(a <= l && b >= r)    return Top[idx];
            int mid = (l+r)>>1, ret = Cover[idx];
            if(a<=mid)  ret = min(ret,_Query(a,b,l,mid,idx<<1));
            if(b>mid)   ret = min(ret,_Query(a,b,mid+1,r,(idx<<1)+1));
            return ret;
        }
        void _Modify(int a,int l,int r,int idx,int d)
        {
            if(l==r && l==a)
            {
                Cover[idx]=Top[idx]=d;
                return;
            }
            int mid = (l+r)>>1;
            if(a<=mid)  _Modify(a,l,mid,idx<<1,d);
            else    _Modify(a,mid+1,r,(idx<<1)+1,d);
            Top[idx]=min(Top[idx<<1],Top[(idx<<1)+1]);
        }
    public:
        IntervalTree(){
            memset(Cover,0x3f,sizeof(Cover));
            memset(Top,0x3f,sizeof(Top));
            size = (TREE_SIZE>>2) - 1;
        }
        IntervalTree(int size):size(size){
            memset(Cover,0x3f,sizeof(Cover));
            memset(Top,0x3f,sizeof(Top));
        }
        void init() {
            memset(Cover, 0x3f, sizeof(Cover));
            memset(Top, 0x3f, sizeof(Top));
            size = (TREE_SIZE>>2) - 1;
        }
        int Query(int a,int b){
            return _Query(a,b,1,size,1);
        }
        void Modify(int a,int d){
            return _Modify(a,1,size,1,d);
        }
} rmq;
int main()
{
    scanf("%d",&T);
    while(T-- && scanf("%d %d",&n,&k)!=EOF)
    {
        rmq.init();
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]),  r[i] = n;
        st.clear();
        for(int i=1;i<=n;i++)
        {
            p = make_pair(a[i]-k, n+1);
            it = st.upper_bound(p);
            while(1) {
                if(it == st.begin())    break;
                it--;
                r[it->second] = i-1;
                v.push_back(it);
            }
            ERASE();
            p = make_pair(a[i]+k, 0);
            it = st.lower_bound(p);
            for(;it!=st.end();it++) {
                r[it->second] = i-1;
                v.push_back(it);
            }
            ERASE();
            st.insert(make_pair(a[i], i));
        }
        for(int i=1;i<=n;i++)
            rmq.Modify(i, r[i]);
        long long ans = 0;
        for(int i=1;i<=n;i++)
            ans += rmq.Query(i, r[i])-i+1;
        printf("%I64d\n", ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值