博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3277 线段树+扫描线
阅读量:7062 次
发布时间:2019-06-28

本文共 1406 字,大约阅读时间需要 4 分钟。

题意:

这里写图片描述
思路:
线段树求矩形面积的并。。。同 POJ 1151

//By SiriusRen#include 
#include
using namespace std;#define N 88888int n,u,tot,xx,yy,h[N];long long ans;struct Node{
int x,h,cover;}node[4*N];struct Tree{
int l,r,cover,len;}tree[4*N];bool cmp(Node a,Node b){
return a.x
>1,lson=pos<<1,rson=pos<<1|1; build(l,mid,lson),build(mid,r,rson);}void push_up(int l,int r,int pos){ if(tree[pos].cover)tree[pos].len=tree[pos].r-tree[pos].l; else if(l+1==r)tree[pos].len=0; else tree[pos].len=tree[pos<<1].len+tree[pos<<1|1].len;}void insert(int l,int r,int pos,Node jy){ if(tree[pos].r<=jy.h){tree[pos].cover+=jy.cover,push_up(l,r,pos);return;} int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1; if(tree[lson].r>=jy.h)insert(l,mid,lson,jy); else insert(l,mid,lson,jy),insert(mid,r,rson,jy); push_up(l,r,pos);}int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d%d%d",&xx,&yy,&h[i]); node[++tot].x=xx,node[tot].h=h[i],node[tot].cover=1; node[++tot].x=yy,node[tot].h=h[i],node[tot].cover=-1; } sort(node+1,node+1+tot,cmp),sort(h+1,h+1+n); u=unique(h+1,h+1+n)-h-1; build(0,u,1),insert(0,u,1,node[1]); for(int i=2;i<=tot;i++){ ans+=(long long)(node[i].x-node[i-1].x)*tree[1].len; insert(0,u,1,node[i]); } printf("%lld\n",ans);}

这里写图片描述

转载于:https://www.cnblogs.com/SiriusRen/p/6532274.html

你可能感兴趣的文章
Bootstrap小体验
查看>>
数据库设计中的14个技巧
查看>>
Transform 1
查看>>
MYSQL 引擎的情况
查看>>
Python enumerate索引迭代
查看>>
无法定位序数XX于动态链接库XX.dll的解决的方法
查看>>
wxPython简单入门
查看>>
WPF老矣,尚能饭否——且说说WPF今生未来(下):安心
查看>>
IE下必须点击一下页面空白的地方才可以激活onchange事件
查看>>
Asp.net webform scaffolding结合Generic Unit of Work & (Extensible) Repositories Framework代码生成向导...
查看>>
linux 配置静态IP
查看>>
linux Posix 信号量 二
查看>>
【leetcode】 Letter Combinations of a Phone Number(middle)
查看>>
poj 1061青蛙的约会
查看>>
Linux系统管理员的命令行工具箱目录
查看>>
去掉浏览器的代理服务器
查看>>
【转】 JSONObject使用方法
查看>>
(多图) FIR数字滤波器的FPGA实现研究
查看>>
Simple Factory vs. Factory Method vs. Abstract Factory【简单工厂,工厂方法以及抽象工厂的比较】...
查看>>
Sublime Text 3 史上最性感的编辑器
查看>>