UVALive 6656 Watching the Kangaroo 拆分线段,set维护



废话这么多,并没有什么用。

题目:对于x,覆盖x的线段中 取max(min(x-l,r-x))

那么对于一个线段分成两段,l,-mid, mid+1-r

在l,mid区间的最小值跟l比较,否则跟r比较即可。用set维护即可。



Memory: 0 KB		Time: 335 MS
Language: C++ 4.8.2		Result: Accepted
Public:		No Yes

#include<iostream>
#include<cstring>
#include<algorithm>
#include<set>
#include<cstdio>
using namespace std;
#define maxn 500007
multiset<int> ma;
multiset<int> mi;
struct Node{
    int x,id,ty,add;
};
Node query[maxn];
int ans[maxn];
int check[maxn];
int comp(Node a,Node b){
    if(a.x == b.x) return a.ty < b.ty;
    return a.x<b.x;
}

int main(){
    int t,n,m,tt=1;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        ma.clear();
        mi.clear();
        int cnt = 0,l,r,x;
        for(int i = 0;i < n; i++){
            scanf("%d%d",&l,&r);
            x = (l+r)/2;
            query[cnt].ty = 2;
            query[cnt].x = l;
            query[cnt].add = l;
            cnt++;
            query[cnt].ty = 3;
            query[cnt].x = x;
            query[cnt].add = l;
            cnt++;
            query[cnt].ty = 4;
            query[cnt].x = x;
            query[cnt].add = r;
            cnt++;
        }
        memset(ans,0,sizeof(ans));
        for(int i = 0;i < m; i++){
            scanf("%d",&x);
            query[cnt].ty = 1;
            query[cnt].x = x;
            query[cnt].id = i;
            cnt++;
        }
        multiset<int>::iterator it;
        sort(query,query+cnt,comp);
        for(int i = 0;i < cnt; i++){
            if(query[i].ty == 1){
                if(ma.size() > 0){
                    it = ma.end();
                    it--;
                    ans[query[i].id] = max(ans[query[i].id],*it-query[i].x);
                }
                if(mi.size() > 0){
                    ans[query[i].id] = max(ans[query[i].id],query[i].x-*mi.begin());
                }
            }
            else if(query[i].ty == 2){
                mi.insert(query[i].add);
            }
            else if(query[i].ty == 3){
                mi.erase(mi.find(query[i].add));
            }
            else if(query[i].ty == 4){
                ma.insert(query[i].add);
            }
        }
        printf("Case %d:\n",tt++);
        for(int i = 0;i < m; i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}



你可能感兴趣的:(uvalive,the,6656,Watching,Kangaro,拆分线段,set维护)