bzoj 5045: 打砖块

来源:互联网 发布:软件版权说明 编辑:程序博客网 时间:2024/06/09 19:47

题解:

一道不太难的题,比赛时就A了。
就将每一层的砖插入堆里面,合并下,再往上加,就可以了。
最多就2n。
code:

#include<queue>#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>#define LL long longusing namespace std;struct node{    int y,x1,x2;    bool operator < (node a) const{if(a.x1==x1) return a.x2>x2;return a.x1<x1;}}a[100010],b[200010];int tot;LL ans=0;priority_queue<node> q;int n,pos;bool cmp(node a,node b){    if(a.y==b.y) return a.x1<b.x1;    return a.y<b.y;}void solve(int y,int c){    if(c==0&&q.empty()) return;    tot=0;    while(c<=n&&a[c].y==y)    {        node tmp;tmp.x1=a[c].x1;tmp.x2=a[c].x2;        q.push(tmp);        pos++;c++;    }    node now;bool first=true;    while(!q.empty())    {        node t=q.top();q.pop();        if(first) now=t,first=false;        else        {            if(now.x2+1>=t.x1) now.x2=max(now.x2,t.x2);            else b[++tot]=now,now=t;        }    }    b[++tot]=now;    for(int i=1;i<=tot;i++)    {        int l=b[i].x1,r=b[i].x2;        ans+=(LL)(r-l+1)/2;        if(l+1<r)        {            node tmp;            tmp.x1=l+1;tmp.x2=r-1;            q.push(tmp);        }    }    if(pos<=n&&a[pos].y==y+1) solve(y+1,pos);    else solve(y+1,0);}int main(){    scanf("%d",&n);    for(int i=1;i<=n;i++) scanf("%d %d",&a[i].y,&a[i].x1),a[i].x2=a[i].x1+1;    sort(a+1,a+n+1,cmp);    while(!q.empty()) q.pop();    pos=1;    while(pos<=n) solve(a[pos].y,pos);    printf("%lld",ans);}
原创粉丝点击