HDH 1264 Counting Squares (線段樹+掃描線|暴力)
Counting Squares
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2113 Accepted Submission(s): 1056
Problem Description
Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) that specify the opposite corners of a rectangle. All coordinates will be integers in the range 0 to 100. For example, the line
5 8 7 10
specifies the rectangle who's corners are(5,8),(7,8),(7,10),(5,10).
If drawn on graph paper, that rectangle would cover four squares. Your job is to count the number of unit(i.e.,1*1) squares that are covered by any one of the rectangles given as input. Any square covered by more than one rectangle should only be counted once.
5 8 7 10
specifies the rectangle who's corners are(5,8),(7,8),(7,10),(5,10).
If drawn on graph paper, that rectangle would cover four squares. Your job is to count the number of unit(i.e.,1*1) squares that are covered by any one of the rectangles given as input. Any square covered by more than one rectangle should only be counted once.
Input
The input format is a series of lines, each containing 4 integers. Four -1's are used to separate problems, and four -2's are used to end the last problem. Otherwise, the numbers are the x-ycoordinates of two points that are opposite corners of a rectangle.
Output
Your output should be the number of squares covered by each set of rectangles. Each number should be printed on a separate line.
Sample Input
5 8 7 10
6 9 7 8
6 8 8 11
-1 -1 -1 -1
0 0 100 100
50 75 12 90
39 42 57 73
-2 -2 -2 -2
Sample Output
8
10000
Source
題意:
給你幾個矩形,算面積並。
POINT:
注意輸入和輸出方式,沒有規定先輸入左下和右上,都有可能,被這個坑的WA了好久。別的就和HDU 1542一樣。
還不需要離散化。
法2:
因為資料很小而且是整數,可以暴力。就是遍歷一個座標系,才100*100;
法1:
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <map>
#include <string.h>
#include <algorithm>
#include <vector>
#include <fstream>
using namespace std;
#define lt 2*x
#define rt 2*x+1
#define LL long long
const int N = 101*100;
int num=0;
int sum[N];
int cnt[N];
struct node
{
int l,r;
int h;
int f;
}len[N];
bool cmd(node a,node b)
{
return a.h<b.h;
}
void init()
{
num=0;
memset(sum,0,sizeof sum);
memset(cnt,0,sizeof cnt);
memset(len,0,sizeof len);
}
void pushup(int x,int l,int r)
{
/* if(l==r&&cnt[x]) sum[x]=1;
else if(cnt[x]) sum[x]=r-l;
else
sum[x]=sum[rt]+sum[lt];*/
if(cnt[x]) sum[x]=r+1-l;
else sum[x]=sum[lt]+sum[rt];
}
void add(int x,int l,int r,int ll,int rr,int f)
{
if(ll<=l&&rr>=r)
{
cnt[x]+=f;
}
else
{
int mid=(l+r)>>1;
if(ll<=mid) add(lt,l,mid,ll,rr,f);
if(mid<rr) add(rt,mid+1,r,ll,rr,f);
}
pushup(x,l,r);
}
void gao()
{
sort(len+1,len+1+num,cmd);
int ans=0;
for(int i=1;i<=num;i++)
{
add(1,0,1000,len[i].l,len[i].r-1,len[i].f);
ans+=sum[1]*(len[i+1].h-len[i].h);
}
printf("%d\n",ans);
}
int main()
{
int x1,x2,y1,y2;
while(~scanf("%d %d %d %d",&x1,&y1,&x2,&y2))
{
if(x1==-1&&x2==-1&&y1==-1&&y2==-1)
{
gao();
init();
}
else if(x1==-2&&x2==-2&&y1==-2&&y2==-2)
{
gao();
break;
}
else
{
if(x1>x2) swap(x1, x2);
if(y1>y2) swap(y1, y2);
num++;
len[num].f=1,len[num].l=x1,len[num].r=x2,len[num].h=y1;
num++;
len[num].f=-1,len[num].l=x1,len[num].r=x2,len[num].h=y2;
}
}
return 0;
}
法2:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
int ans=0;
int flag[120][120];
int main()
{
int x1,x2,y1,y2;
while(~scanf("%d %d %d %d",&x1,&y1,&x2,&y2))
{
if(x1==-1&&x2==-1&&y1==-1&&y2==-1)
{
printf("%d\n",ans);
memset(flag,0,sizeof flag);
ans=0;
}
else if(x1==-2&&x2==-2&&y1==-2&&y2==-2)
{
printf("%d\n",ans);
memset(flag,0,sizeof flag);
ans=0;
break;
}
else
{
for(int i=min(x1,x2);i<max(x2,x1);i++)
{
for(int j=min(y1,y2);j<max(y1,y2);j++)
{
if(!flag[i][j])
{
ans++;
flag[i][j]=1;
}
}
}
}
}
return 0;
}
相關文章
- HDU 1542 Atlantis (線段樹+離散化+掃描線)
- HDU 5862 Counting Intersections(樹狀陣列+掃描線+離散化)陣列
- HDU 1255 覆蓋的面積(線段樹+掃描線+離散化)
- HDU 1394 Minimum Inversion Number (暴力+線段樹)
- BZOJ4756: [Usaco2017 Jan]Promotion Counting(線段樹合併)
- 淺談掃描線
- 線段樹
- 線~段~樹
- 掃描線及其應用
- HDU 1542 Atlantis(掃描線)
- 01 線段樹
- 線段樹--RMQMQ
- 【模版】線段樹
- 線段樹模板
- 線段樹 hate it
- ut.cpp 最大線段並減線段交 [線段樹]
- 掃描所有無線網路
- 洛谷P4632 [APIO2018] New Home 新家(動態開節點線段樹 二分答案 掃描線 set)API
- 李超線段樹
- 線段樹進階
- 權值線段樹
- 線段樹筆記筆記
- Segment Tree(線段樹)
- 線段樹入門
- 【恐怖の演算法】 掃描線演算法
- 洛谷題單指南-線段樹-P3373 【模板】線段樹 2
- MySQL中的全表掃描和索引樹掃描MySql索引
- 懶標記線段樹
- 可持久化線段樹持久化
- 線段樹(超詳解)
- 線段樹 transformation——hdu 4578ORM
- 深入理解線段樹
- 線段樹簡單思路
- 線段樹擴充套件套件
- 第二課——線段樹
- 線段樹模板總結
- 線段樹(毒瘤)總結
- POJ 3667 Hotel 線段樹