POJ - 1177 线段树

2023-11-11 23:10
文章标签 poj 线段 1177

本文主要是介绍POJ - 1177 线段树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

POJ - 1177 扫描线

这道题也算是一道扫描线的经典题目了。

只不过这道题是算周长,非常有意思的一道题。我们已经知道了,一般求面积并,是如何求的,现在我们要把扫描线进行改造一下,使得能算周长。

我们大致考虑一下图像上是如何实现的:

这样一个图我们要如何求他的面积?

 

 

我们把轮廓画出来

 

 

我们把扫描线画出来

 

 

我们发现

从上到下我们竖直方向的长度,是每条线高度差*2*线段树的连续的段数目。

从上到下我们水平方向的长度,是横线的长度现在这次总区间被覆盖的长度和上一次总区间被覆盖的长度之差的绝对值。

这样我们就找到解决的办法,维护就非常容易了,本题范围比较小,因此不用离散化,直接区间建树,节点维护4个值,

Len:区间内部被覆盖一次以上的长度

S:区间内被完全覆盖的次数

这个是常规操作。

然后维护区间内部,连续区间(每个之间是隔离的)的个数

然后两个lc,rc,代表区间左端点和右端点是否在连续区间内(合并区间的时候有用)

这样就行了。

然后考虑子节点往上pushup的情况,

首先区间被完全填满,那么len等于区间长度,lc,rc,num都是1

如果到叶节点,都是0

否则,len,rc,lc,的长度由两个儿子节点提供,需要注意的是,num的情况是由两个儿子提供,如果左儿子的右边界和右儿子的左边界都是在连续的区间中,那么这个区间会被合成为1个区间,从而个数需要减1.

最后常规的操作即可。

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N = 5007;
const int X = 20007;
const int inf = 1<<29;
inline int L(int r){return r<<1;};
inline int R(int r){return r<<1|1;};
inline int MID(int l,int r){return (l+r)>>1;};
struct Edge{int l,r;int h;int f;
}line[N*2];
struct node{int l,r,len,s,num;//num这个区间有多少不连续的线段bool lc,rc;//区间左右端点是否被覆盖
}tree[X<<2];
bool cmp(Edge a,Edge b)
{return a.h<b.h;
}
void pushup(int root)
{if (tree[root].s){tree[root].len=tree[root].r-tree[root].l+1;//没有离散化tree[root].rc=tree[root].lc=1;tree[root].num=1;}else if (tree[root].l == tree[root].r){tree[root].len=0;tree[root].lc=tree[root].rc=0;tree[root].num=0;}else{tree[root].len=tree[L(root)].len+tree[R(root)].len;tree[root].lc=tree[L(root)].lc;tree[root].rc=tree[R(root)].rc;tree[root].num=tree[L(root)].num+tree[R(root)].num-(tree[L(root)].rc && tree[R(root)].lc);}
}
void buildtree(int root,int l,int r){tree[root].l=l;tree[root].r=r;tree[root].s=tree[root].len=0;tree[root].lc = tree[root].rc = tree[root].num = 0;if (l==r){return ;}int mid = MID(l,r);buildtree(L(root),l,mid);buildtree(R(root),mid+1,r);
}
void update(int root,int ul,int ur,int v)
{int l=tree[root].l;int r=tree[root].r;//cout<<root<<l<<" "<<r<<" "<<ul<<" "<<ur<<endl;if (ul==l && ur==r){tree[root].s+=v;pushup(root);return;}int mid = MID(l,r);if (ur<=mid)update(L(root),ul,ur,v);else if(ul>mid)update(R(root),ul,ur,v);else{update(L(root),ul,mid,v);update(R(root),mid+1,ur,v);}pushup(root);
}
int main(){int n;while(scanf("%d",&n)!=EOF){int x1,x2,y1,y2,mx=-inf,mn=inf;int tot=0;for (int i=0;i<n;i++){scanf("%d%d%d%d",&x1,&y1,&x2,&y2);mx=max(mx,max(x1,x2));mn=min(mn,min(x1,x2));line[tot].l=x1;line[tot].r=x2;line[tot].h=y1;line[tot++].f=1;line[tot].l=x1;line[tot].r=x2;line[tot].h=y2;line[tot++].f=-1;}sort(line,line+tot,cmp);int ans=0;int last=0;buildtree(1,mn,mx-1);// cout<<"ss"<<endl;for (int i=0;i<tot;i++){//  cout<<line[0].l<<" "<<line[0].r<<endl;update(1,line[i].l,line[i].r-1,line[i].f);ans+=abs(tree[1].len-last);ans+=(line[i+1].h-line[i].h)*2*tree[1].num;last=tree[1].len;}printf("%d\n",ans);}return 0;
}

 

#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>usingnamespacestd; constint N = 5007; constint X = 20007; constint inf = 1<<29; inline int L(int r){return r<<1;}; inline int R(int r){return r<<1|1;}; inline int MID(int l,int r){return (l+r)>>1;}; struct Edge{ int l,r; int h; int f; }line[N*2]; struct node{ int l,r,len,s,num; //num这个区间有多少不连续的线段bool lc,rc;//区间左右端点是否被覆盖 }tree[X<<2]; bool cmp(Edge a,Edge b) { return a.h<b.h; } void pushup(int root) { if (tree[root].s){ tree[root].len=tree[root].r-tree[root].l+1;//没有离散化 tree[root].rc=tree[root].lc=1; tree[root].num=1; }elseif (tree[root].l == tree[root].r){ tree[root].len=0; tree[root].lc=tree[root].rc=0; tree[root].num=0; }else{ tree[root].len=tree[L(root)].len+tree[R(root)].len; tree[root].lc=tree[L(root)].lc; tree[root].rc=tree[R(root)].rc; tree[root].num=tree[L(root)].num+tree[R(root)].num-(tree[L(root)].rc && tree[R(root)].lc); } } void buildtree(int root,int l,int r){ tree[root].l=l; tree[root].r=r; tree[root].s=tree[root].len=0; tree[root].lc = tree[root].rc = tree[root].num = 0; if (l==r){ return ; } int mid = MID(l,r); buildtree(L(root),l,mid); buildtree(R(root),mid+1,r); } void update(int root,int ul,int ur,int v) { int l=tree[root].l; int r=tree[root].r; //cout<<root<<l<<" "<<r<<" "<<ul<<" "<<ur<<endl;if (ul==l && ur==r) { tree[root].s+=v; pushup(root); return; } int mid = MID(l,r); if (ur<=mid)update(L(root),ul,ur,v); elseif(ul>mid)update(R(root),ul,ur,v); else{ update(L(root),ul,mid,v); update(R(root),mid+1,ur,v); } pushup(root); } int main(){ int n; while(scanf("%d",&n)!=EOF){ int x1,x2,y1,y2,mx=-inf,mn=inf; int tot=0; for (int i=0;i<n;i++){ scanf("%d%d%d%d",&x1,&y1,&x2,&y2); mx=max(mx,max(x1,x2)); mn=min(mn,min(x1,x2)); line[tot].l=x1; line[tot].r=x2; line[tot].h=y1; line[tot++].f=1; line[tot].l=x1; line[tot].r=x2; line[tot].h=y2; line[tot++].f=-1; } sort(line,line+tot,cmp); int ans=0; int last=0; buildtree(1,mn,mx-1); // cout<<"ss"<<endl;for (int i=0;i<tot;i++) { // cout<<line[0].l<<" "<<line[0].r<<endl; update(1,line[i].l,line[i].r-1,line[i].f); ans+=abs(tree[1].len-last); ans+=(line[i+1].h-line[i].h)*2*tree[1].num; last=tree[1].len; } printf("%d\n",ans); } return0; }

转载于:https://www.cnblogs.com/bluefly-hrbust/p/10360175.html

这篇关于POJ - 1177 线段树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/393439

相关文章

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

poj 1287 Networking(prim or kruscal最小生成树)

题意给你点与点间距离,求最小生成树。 注意点是,两点之间可能有不同的路,输入的时候选择最小的,和之前有道最短路WA的题目类似。 prim代码: #include<stdio.h>const int MaxN = 51;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int P;int prim(){bool vis[MaxN];

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D