【jzoj5329】【NOIP2017提高A组模拟8.22】【时间机器】【数据结构】【扫描线】

来源:互联网 发布:威海市网络推广专员 编辑:程序博客网 时间:2024/05/17 22:44

description

这里写图片描述

solution

把机器和电阻按l排序,l相等时电阻排前面,扫描线从左往右扫,遇到电阻把右端点放入set,遇到机器lowerbound找到最小的r比机器的r大匹配即可。

code

#include<set>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#define LL long long#define fo(i,j,k) for(int i=j;i<=k;i++)#define fd(i,j,k) for(int i=j;i>=k;i--)#define fr(i,j) for(int i=begin[j];i;i=next[i])using namespace std;int const mn=1e5+9,inf=1e9+7;int t,n,m;struct rec{    int l,r,s,op;};rec a[mn];struct re2{    int r,s;    friend bool operator<(re2 x,re2 y){        return (x.r<y.r)||((x.r==y.r)&&(x.s<y.s));    }    re2(int R,int S){        r=R;s=S;    }};multiset<re2>s;bool cmp(rec x,rec y){    return (x.l<y.l)||((x.l==y.l)&&(x.op>y.op));}int read(){    char ch=getchar();    while((ch<'0')||(ch>'9'))ch=getchar();    int v=0;    while((ch>='0')&&(ch<='9'))v=v*10+ch-'0',ch=getchar();    return v;}int main(){    freopen("machine.in","r",stdin);    freopen("machine.out","w",stdout);    scanf("%d",&t);    fo(cas,1,t){        scanf("%d%d",&n,&m);        int tmp=0;        fo(i,1,n){            tmp++;            a[tmp].l=read();            a[tmp].r=read();            a[tmp].s=read();            a[tmp].op=-1;        }        fo(i,1,m){            tmp++;            a[tmp].l=read();            a[tmp].r=read();            a[tmp].s=read();            a[tmp].op=1;        }        sort(a+1,a+tmp+1,cmp);        int ok=1;        s.clear();        fo(i,1,tmp)if(a[i].op==1)            s.insert(re2(a[i].r,a[i].s));        else{            while((a[i].s)&&(s.lower_bound(re2(a[i].r,0))!=s.end())){                re2 tm2=*s.lower_bound(re2(a[i].r,0));                s.erase(s.lower_bound(re2(a[i].r,0)));                int tm3=min(a[i].s,tm2.s);                a[i].s-=tm3;                if(tm3!=tm2.s)s.insert(re2(tm2.r,tm2.s-tm3));            }            if(a[i].s){                ok=0;                break;            }        }        if(ok)printf("Yes\n");        else printf("No\n");    }    return 0;}
阅读全文
0 0
原创粉丝点击