欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

HDU - 5820 Lights(主席树)

发布时间:2024/4/11 49 豆豆
生活随笔 收集整理的这篇文章主要介绍了 HDU - 5820 Lights(主席树) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

题目链接:点击查看

题目大意:给出一个 n * m 的矩阵,再给出 N 个点,问其中的任意两个点 ( x1 , y1 ) 与 ( x2 , y2 ) 之间,最短路径为 abs( x1 - x2 ) + abs( y1 - y2 ) ,是否存在一条最短路径,使得拐弯的地方都存在着点

题目分析:正难则反,我们可以选定一个矩形区域作为非法区域,若非法区域内存在点的话,那么答案显然就是 NO 了

这样的话必须要正着扫一遍,再倒着扫一遍,不然没法同时解决以下两个样例:
 

input:

2

1 3

3 1

2

1 1

3 3

output:

NO
NO

代码:
 

#include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=5e5+100;const int M=5e4+100;int prex[M],prey[M],n;pair<int,int>p[N]; /*主席树*/ struct Node {int l,r,sum; }tree[N*30];int root[N],cnt;void init() {memset(prex,0,sizeof(prex));memset(prey,0,sizeof(prey));root[0]=0;tree[0].l=tree[0].r=tree[0].sum=0;cnt=1; }void change(int pos,int &k,int l,int r) {tree[cnt++]=tree[k];k=cnt-1;tree[k].sum++;if(l==r)return;int mid=l+r>>1;if(pos<=mid)change(pos,tree[k].l,l,mid);elsechange(pos,tree[k].r,mid+1,r); }int query(int i,int j,int l,int r,int L,int R)//左根,右根,当前区间,目标区间 {if(L>R)return 0;if(l>=L&&r<=R)return tree[j].sum-tree[i].sum;if(r<L||l>R)return 0;int mid=l+r>>1;return query(tree[i].l,tree[j].l,l,mid,L,R)+query(tree[i].r,tree[j].r,mid+1,r,L,R); } /*主席树*/ bool solve() {init();int last=0;for(int i=1;i<=n;i++){if(p[i].first==p[i-1].first&&p[i].second==p[i-1].second)continue;int x=p[i].first,y=p[i].second;int xx=p[prey[y]].first,yy=p[prex[x]].second;root[x]=root[last];change(y,root[x],1,M);if(query(root[xx],root[x],1,M,yy+1,y-1))return false;last=x,prex[x]=i,prey[y]=i;}return true; }int main() { #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif // ios::sync_with_stdio(false);while(scanf("%d",&n)!=EOF&&n){for(int i=1;i<=n;i++)scanf("%d%d",&p[i].first,&p[i].second);sort(p+1,p+1+n);bool flag=solve();for(int i=1;i<=n;i++)p[i].first=M-p[i].first;sort(p+1,p+1+n);flag&=solve();if(flag)puts("YES");elseputs("NO");}return 0; }

 

总结

以上是生活随笔为你收集整理的HDU - 5820 Lights(主席树)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。