hdu1542 線段樹+掃描線+離散化,hdu1542線段

來源:互聯網
上載者:User

hdu1542 線段樹+掃描線+離散化,hdu1542線段

只想說題目給的欲實際不服     還是這類型的水題吧   建議看之前我寫的那個



#include<stdio.h>

#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define LL(x) (x<<1)
#define RR(x) ((x<<1)|1)


int n;
struct node
{
double y;
int x1,x2;
int flash;
}A[31000];
struct Node
{
int ii;
double x;
}change[35000];
int cmp1(Node a,Node b)
{
return a.x<b.x;
}
int cmp2(node a,node b)
{
return a.y<b.y;
}
int num[80000];
double map[31000];
int update(int L,int R,int left,int right,int k,int mark)
{
int mid=(L+R)/2;
if(L==left&&right==R)
{
if(num[mark]!=-1)
{
num[mark]+=k;
return 0;
}
update(L,mid,L,mid,k,LL(mark));
update(mid,R,mid,R,k,RR(mark));
if(num[LL(mark)]==num[RR(mark)]) num[mark]=num[LL(mark)];
else num[mark]=-1;
}
else
{
if(num[mark]>=0)
{
num[LL(mark)]=num[RR(mark)]=num[mark];
}
if(right<=mid)
{
update(L,mid,left,right,k,LL(mark));
}
else if(left>=mid)
{
update(mid,R,left,right,k,RR(mark));
}
else
{
update(L,mid,left,mid,k,LL(mark));
update(mid,R,mid,right,k,RR(mark));
}
if(num[LL(mark)]==num[RR(mark)]) num[mark]=num[LL(mark)];
else num[mark]=-1;
}
return 0;
}
double find(int L,int R,int mark)
{
double Long=0;
if(num[mark]>=1)
{
Long+=map[R]-map[L];
return Long;
}
if(num[mark]==0) return 0;
int mid=(L+R)/2;
Long+=find(L,mid,LL(mark))+find(mid,R,RR(mark));
return Long;
}
int main()
{
int i,j,d=1;
double x1,y1,x2,y2;
while(~scanf("%d",&n),n)
{
n*=2;
//j=0;
memset(A,0,sizeof(A));
memset(map,0,sizeof(map));
memset(change,0,sizeof(change));
for(i=1;i<=n;i+=2)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
A[i].y=y1;
A[i].flash=1;
A[i+1].y=y2;
A[i+1].flash=-1;
change[i].x=x1;
change[i+1].ii=change[i].ii=i;
change[i+1].x=x2;
} 
int leap[10000];
memset(leap,0,sizeof(leap));
sort(change+1,change+1+n,cmp1);
int j=0;
double k=-1;
for(i=1;i<=n;i++)
{
if(change[i].x!=k)
{
k=change[i].x;
++j;
map[j]=change[i].x;
}
if(leap[change[i].ii]==0)
{
A[change[i].ii].x1=A[change[i].ii+1].x1=j;
leap[change[i].ii]=1;
}
else  A[change[i].ii].x2=A[change[i].ii+1].x2=j;
}
sort(A+1,A+1+n,cmp2);
memset(num,0,sizeof(num));
update(1,j,A[1].x1,A[1].x2,1,1);
double area=0;
for(i=2;i<=n;i++)
{    
area+=find(1,j,1)*(A[i].y-A[i-1].y);
if(A[i].flash==1)
update(1,j,A[i].x1,A[i].x2,1,1);
else update(1,j,A[i].x1,A[i].x2,-1,1);
}
printf("Test case #%d\n",d++);
printf("Total explored area: %.2lf\n",area);
printf("\n");
}
return 0;
}
怎用線段樹的知識逆序數

很簡單……
設數列為a,將數列離散化,在從前往後枚舉,統計答案……
離散化:例如2 5 8 3 10 等價於 1 3 4 2 5,可以通過排序加小小處理解決。
枚舉到第i個數,我們需要求出從1到i-1中有多少個比a[i]大的數,更新答案。
具體怎麼做呢?
每次枚舉完一個數之後,將這個數插入到線段樹裡,插入到線段樹的神馬地方呢?當然是這個數多大就插入到多大的地方。
舉個例子:3 2 4 1。則線段樹的變化應該為:tree[3]+=1;tree[2]+=1;tree[4]+=1;tree[1]+=1;
設x=a[i],這樣,在插入一個數X時,首先求一下tree[x+1]~tree[n]的和,這個和就是1~i-1中有多少個比a[i]大的數。運用線段樹求和可以做到O(n log n)吧,具體實現我就不再贅述了。
 
離散化線段樹Pascal題目與程式

可以參考這個檔案

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.