【USACO 2007 Open Silver】City Horizon 线段树
来源:互联网 发布:数组去重复js 编辑:程序博客网 时间:2024/06/05 04:19
题目描述
题目大意
矩形面积并(虽然这道题很水)
数据范围
n<=40000 A,B<=10^9
样例输入
4
2 5 1
9 10 4
6 8 2
4 6 3
样例输出
16
解题思路
离散化+线段树
线段树写了很久没写出来,被迫换成权值线段树
代码
#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>#include <map>using namespace std;namespace IStream{const int LLL=1<<15;char buffer[LLL],*SSS,*TTT;char Get_Char(){if(SSS==TTT){TTT=(SSS=buffer)+fread(buffer,1,LLL,stdin);if(SSS==TTT) return EOF;}return *SSS++;}int Getint(){char c;int re=0,f=1;for(c=Get_Char();c<'0'||c>'9';c=Get_Char())if(c=='-')f=-1;while(c>='0'&&c<='9')re=(re<<1)+(re<<3)+(c-'0'),c=Get_Char();return re*f;}}class OStream{private:static const int LLL=1<<15;char staack[21];int topp;char buffer[LLL],*SSS;public:OStream(){SSS=buffer;}void Putint(int x,int flag){bool fl=false;if(flag==1) staack[++topp]=' ';if(flag==2) staack[++topp]='\n';if(x<0) x=-x,fl=true;if(!x) staack[++topp]='0';while(x)staack[++topp]=x%10+'0',x/=10;if(fl)staack[++topp]='-';while(topp){if(SSS==buffer+LLL-1){fwrite(buffer,1,SSS-buffer,stdout);SSS=buffer;}*SSS++=staack[topp--];}}~OStream(){fwrite(buffer,1,SSS-buffer,stdout);*SSS=0;}}os;#ifndef ONLINE_JUDGEinline int Getint(){int x=0,f=1;char ch=getchar();while('0'>ch||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while('0'<=ch&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}#elseusing namespace IStream;//这上面是从PoPoQQQ那儿学来的输入输出,比一般的Getint还是要快些#endifstruct Seg{ int x,h,flag;}w[80005];bool cmp(Seg a,Seg b){ return a.x<b.x;}map<int,int>Map;int cnt=0,mk[40005];struct node{ int L,r,Sum;}Tree[160005];void Build(int v,int L,int r){ Tree[v]=(node){L,r,0}; if(L==r)return; Build(2*v,L,(L+r)/2); Build(2*v+1,(L+r)/2+1,r);}struct NODe{ int vl,pos,hh;}h[40005];bool cmp2(NODe a,NODe b){ return a.vl<b.vl;}bool cmpcmp(NODe a,NODe b){ return a.pos<b.pos;}void Insert(int v,int pos,int vl){ Tree[v].Sum+=vl; if(Tree[v].L==Tree[v].r)return; else Insert(2*v+(pos>Tree[2*v].r),pos,vl);}long long Ask(int v){ if(Tree[v].L==Tree[v].r)return mk[Tree[v].L]; if(Tree[2*v+1].Sum)return Ask(2*v+1); return Ask(2*v);}int LL[40005],rr[40005];int main(){ int n=Getint(); for(int i=1;i<=n;i++){//很少写离散化,所以离散化的部分代码很丑(其实整个代码都很丑) LL[i]=Getint(),rr[i]=Getint(); h[i]=(NODe){Getint(),i}; } sort(h+1,h+n+1,cmp2); for(int i=1;i<=n;i++) h[i].hh=h[i-1].hh+(h[i-1].vl!=h[i].vl),mk[h[i].hh]=h[i].vl; cnt=h[n].hh; sort(h+1,h+n+1,cmpcmp); for(int i=1;i<=n;i++){ w[2*i-1]=(Seg){LL[i],h[i].hh,1}; w[2*i]=(Seg){rr[i],h[i].hh,-1}; } sort(w+1,w+2*n+1,cmp); long long Area=0; Build(1,0,cnt); for(int i=1;i<=2*n;i++){ Area+=Ask(1)*(long long)(w[i].x-w[i-1].x); Insert(1,w[i].h,w[i].flag); } cout<<Area<<"\n"; return 0;}
0 0
- [USACO 2007 Open Silver] City Horizon - 离散化+线段树
- 【USACO 2007 Open Silver】City Horizon 线段树
- 【线段树】City Horizon
- 【POJ3277】City Horizon,线段树
- 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线【线段树】【矩形面积并】
- [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线(扫描线+线段树)
- bzoj 1645: [Usaco2007 Open]City Horizon 城市地平线(线段树扫描线)
- poj -- 3277City Horizon (线段树)
- poj3277 City Horizon(离散化+线段树)
- poj 3277 city horizon 线段树
- POJ 3277 City Horizon 线段树
- City Horizon poj3277 线段树+离散化
- poj 3277 City Horizon 线段树
- Poj 3277 City Horizon - 线段树
- poj 3279 Filptile (USACO 2007 Open Silver)
- TOJ 1706: City Horizon -- 线段树,平衡树
- pku 3277 City Horizon(离散化+线段树)
- PKU 3327City Horizon离散化+线段树
- Android中的MVC
- 正则表达式
- Sea Battle<海战>(思路题)
- android开发中防止刚进入activity时edittext获取焦点,防止自动弹出软键盘
- 把数组排成最小的数。充分利用string的自带compare
- 【USACO 2007 Open Silver】City Horizon 线段树
- 自定义两端圆角且图片为背景的进度条
- 取出ios模拟器中程序写下的文件
- linux线程互斥与同步
- 第二十二、Java之单例设计模式
- 2017.03.10【NOIP 普及组】模拟赛C组 三条直线Three lines 题解
- 像是突然打通任督二脉
- 浅谈Html的内容加载及JS执行顺序
- matlab傅里叶变换矩阵