日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當前位置:首頁 > php開源 > php教程 > UVALive 4730 線段樹+并查集

UVALive 4730 線段樹+并查集

來源:程序員人生   發(fā)布時間:2016-06-07 08:42:37 閱讀次數(shù):3096次

點擊打開鏈接

題意:在座標上給n個點,r的操作是將兩個點連起來,l的操作是問你y=u的這條線連接的集合塊數(shù)和這些集合內的點的個數(shù)

思路:很麻煩的1道題,在網上看了題意和做法后,開始了1下午的調bug進程,做法很好懂,我開了兩個線段樹,1個保護點代表的直線的集合個數(shù),另外一個則是途經集合內的點的個數(shù),然后集合的判斷直接用并查集就好了,這是兩個核心,然后就是自己瞎寫的了,代碼丑的可以而且好像除本人他人看著可能要罵人了,有興趣研究的可以留言我來解答,那難的部份其實就是并查集合并時該怎樣將這兩個要保護的值弄進線段樹中,而且這題我用的離散化處理的,好像可以不用,但感覺空間上有點過意不去,所以代碼看著真是丑,然后說說合并的操作,先是集合的個數(shù),令左側上下限分別為L1,R1,右側上限是L2,R2,下面都是這樣,然后L1到R1的集合個數(shù)全部減1,也就是區(qū)間更新,然后R1到R2也是1樣,然后全部大區(qū)間+1,現(xiàn)在看若左側區(qū)間個數(shù)是1,右側也是1,那末合并后還是1,第2個線段樹的操作與這個相似,統(tǒng)計1下就能夠了,注意細節(jié),這里獻上9野聚聚的樣例,也是這個樣例終結了我1下午的找BUG旅程

  1.  
  2. 11 
  3. 1 7  
  4. 5 7  
  5. 8 6  
  6. 3 5  
  7. 5 5  
  8. 2 3  
  9. 10 3  
  10. 7 2  
  11. 4 1  
  12. 11 1  
  13. 4 6 
  14. 21 
  15. road 0 1  
  16. road 3 5  
  17. line 6.5  
  18. road 4 2  
  19. road 3 8  
  20. road 4 7  
  21. road 6 9  
  22. road 4 1  
  23. road 2 7  
  24. line 4.5  
  25. line 6.5  
  26. line 3.5 
  27. line 2.5 
  28. line 5.5 
  29. road 10 0 
  30. line 5.5 
  31. line 6.5 
  32. road 0 3 
  33. line 1.5 
  34. line 6.5 
  35. line 2.5 
  36.  
  37. ans: 
  38. 0 0  
  39. 2 8  
  40. 1 5  
  41. 2 8 
  42. 3 10 
  43. 1 5 
  44. 1 6 
  45. 1 6 
  46. 2 11 
  47. 1 9 
  48. 2 11
再獻上丑的可以的我的代碼
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; typedef unsigned long long ull; const int inf=0x3f3f3f3f; const ll INF=0x3f3f3f3f3f3f3f3fll; const int maxn=100010; int f[maxn*2],r[maxn*2],L[maxn*2],R[maxn*2],t[maxn*4],num2[maxn*8],num1[maxn*8],Q[maxn*2][2],Lazy1[maxn*8],Lazy2[maxn*8]; int len; double pos1[maxn*2]; char ch[200010][10]; int find1(int x){ if(x!=f[x]) f[x]=find1(f[x]); return f[x]; } void pushdown_1(int node){ if(Lazy1[node]){ Lazy1[node<<1]+=Lazy1[node]; Lazy1[node<<1|1]+=Lazy1[node]; num1[node<<1]+=Lazy1[node]; num1[node<<1|1]+=Lazy1[node]; Lazy1[node]=0; } } void pushdown_2(int node){ if(Lazy2[node]){ Lazy2[node<<1]+=Lazy2[node]; Lazy2[node<<1|1]+=Lazy2[node]; num2[node<<1]+=Lazy2[node]; num2[node<<1|1]+=Lazy2[node]; Lazy2[node]=0; } } void buildtree(int le,int ri,int node){ num2[node]=num1[node]=Lazy1[node]=Lazy2[node]=0; if(le==ri) return ; int t=(le+ri)>>1; buildtree(le,t,node<<1); buildtree(t+1,ri,node<<1|1); } void update_1(int l,int r,int x,int le,int ri,int node){ if(l<=le&&ri<=r){ Lazy1[node]+=x; num1[node]+=x; return ; } pushdown_1(node); int t=(le+ri)>>1; if(l<=t) update_1(l,r,x,le,t,node<<1); if(r>t) update_1(l,r,x,t+1,ri,node<<1|1); } void update_2(int l,int r,int x,int le,int ri,int node){ if(l<=le&&ri<=r){ Lazy2[node]+=x; num2[node]+=x; return ; } pushdown_2(node); int t=(le+ri)>>1; if(l<=t) update_2(l,r,x,le,t,node<<1); if(r>t) update_2(l,r,x,t+1,ri,node<<1|1); } int query_1(int pos,int le,int ri,int node){ if(le==ri) return num1[node]; pushdown_1(node); int t=(le+ri)>>1; int ans; if(pos<=t) ans=query_1(pos,le,t,node<<1); else ans=query_1(pos,t+1,ri,node<<1|1); return ans; } int query_2(int pos,int le,int ri,int node){ if(le==ri) return num2[node]; pushdown_2(node); int t=(le+ri)>>1; int ans; if(pos<=t) ans=query_2(pos,le,t,node<<1); else ans=query_2(pos,t+1,ri,node<<1|1); return ans; } void unite(int a,int b){ int aa=find1(a); int bb=find1(b); if(aa==bb) return ; if(L[aa]>=L[bb]&&R[aa]<=R[bb]){ int l1=lower_bound(t,t+len,L[aa])-t+1; int r1=lower_bound(t,t+len,R[aa])-t+1; int l2=lower_bound(t,t+len,L[bb])-t+1; int r2=lower_bound(t,t+len,R[bb])-t+1; update_1(l1,r1,⑴,1,len,1); update_1(l2,r2,⑴,1,len,1); update_1(l2,r2,1,1,len,1); update_2(l1,r1,-r[aa],1,len,1); update_2(l2,r2,-r[bb],1,len,1); update_2(l2,r2,r[aa]+r[bb],1,len,1); }else if(L[aa]>=L[bb]&&R[aa]>R[bb]){ int l1=lower_bound(t,t+len,L[aa])-t+1; int r1=lower_bound(t,t+len,R[aa])-t+1; int l2=lower_bound(t,t+len,L[bb])-t+1; int r2=lower_bound(t,t+len,R[bb])-t+1; update_1(l1,r1,⑴,1,len,1); update_1(l2,r2,⑴,1,len,1); update_1(l2,r1,1,1,len,1); update_2(l1,r1,-r[aa],1,len,1); update_2(l2,r2,-r[bb],1,len,1); update_2(l2,r1,r[aa]+r[bb],1,len,1); }else if(L[aa]<L[bb]&&R[aa]<=R[bb]){ int l1=lower_bound(t,t+len,L[aa])-t+1; int r1=lower_bound(t,t+len,R[aa])-t+1; int l2=lower_bound(t,t+len,L[bb])-t+1; int r2=lower_bound(t,t+len,R[bb])-t+1; update_1(l1,r1,⑴,1,len,1); update_1(l2,r2,⑴,1,len,1); update_1(l1,r2,1,1,len,1); update_2(l1,r1,-r[aa],1,len,1); update_2(l2,r2,-r[bb],1,len,1); update_2(l1,r2,r[aa]+r[bb],1,len,1); }else if(L[aa]<L[bb]&&R[aa]>R[bb]){ int l1=lower_bound(t,t+len,L[aa])-t+1; int r1=lower_bound(t,t+len,R[aa])-t+1; int l2=lower_bound(t,t+len,L[bb])-t+1; int r2=lower_bound(t,t+len,R[bb])-t+1; update_1(l1,r1,⑴,1,len,1); update_1(l2,r2,⑴,1,len,1); update_1(l1,r1,1,1,len,1); update_2(l1,r1,-r[aa],1,len,1); update_2(l2,r2,-r[bb],1,len,1); update_2(l1,r1,r[aa]+r[bb],1,len,1); } f[aa]=bb;r[bb]+=r[aa];L[bb]=min(L[bb],L[aa]);R[bb]=max(R[bb],R[aa]); } struct edge{ int x,y; }id[maxn]; int main(){ int T,n,m,u,v; scanf("%d",&T); while(T--){ scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d%d",&id[i].x,&id[i].y); int k=0; for(int i=0;i<n;i++){ id[i].y*=2; f[i]=i;r[i]=1;t[k++]=id[i].y;L[i]=id[i].y;R[i]=id[i].y; } scanf("%d",&m); for(int i=0;i<m;i++){ scanf("%s",ch[i]); if(ch[i][0]=='r') scanf("%d%d",&Q[i][0],&Q[i][1]); else if(ch[i][0]=='l'){ scanf("%lf",&pos1[i]); Q[i][0]=(int)2*pos1[i]; t[k++]=Q[i][0]; } } sort(t,t+k); len=unique(t,t+k)-t; buildtree(1,len,1); for(int i=0;i<m;i++){ if(ch[i][0]=='r') unite(Q[i][0],Q[i][1]); else if(ch[i][0]=='l'){ int ll=lower_bound(t,t+len,Q[i][0])-t+1; int ans1=query_1(ll,1,len,1); int ans2=query_2(ll,1,len,1); printf("%d %d\n",ans1,ans2); } } } return 0; }

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 在线国产区 | 波多野结衣乳巨码无在线观看 | 中文字幕亚洲视频 | 亚洲免费视频网站 | 国产日本在线视频 | 秋霞毛片| 日韩在线欧美 | 91午夜精品 | 丰满少妇高潮惨叫久久久一 | 玖玖久久 | 免费av一区二区三区 | 99久久免费观看 | 国产高清av在线 | 亚洲2020天天堂在线观看 | 久久久久亚洲 | 一区二区福利视频 | 久久99亚洲精品 | 成人精品国产 | 欧美日韩福利 | 美女又黄又免费的视频 | 在线播放一区二区三区 | 精品欧美乱码久久久久久1区2区 | 国产一区二区三区久久 | 久久久精品久久久久 | 香蕉午夜 | 欧美日韩1区 | 久久九九精品久久 | 美女视频黄的免费 | 久久精品视频一区 | 日韩精品免费一区二区在线观看 | 亚洲精品美女久久久久99 | 一级黄色毛片 | 久久久一二三 | 亚洲一区二区三区四区不卡 | 日本一区二区三区免费观看 | 91青青草视频 | 成人天堂资源www在线 | 日本一二三区在线 | 国产伦精品一区二区三区照片 | 日日爱视频 | 日日干夜夜操 |