title: 十六届蓝桥杯-JavaA组T.H奇偶覆盖tags: ACM
abbrlink: 72bd2125
date: 2020-11-17 18:01:26

线段树初步版本,未完善

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N=1e5+5;
  4. struct node
  5. {
  6. int l,r,h;
  7. bool operator< (const node& a)const
  8. {
  9. return h<a.h;
  10. }
  11. }e[N*2];
  12. int a[N*2];
  13. int sum[N*2*4],flag[N*2*4];
  14. void cal(int l,int r,int root)
  15. {
  16. sum[root]=(a[r]-a[l])-sum[root];
  17. }
  18. void pushup(int root)
  19. {
  20. sum[root]=sum[root<<1]+sum[root<<1|1];
  21. }
  22. void pushdown(int l,int r,int root)
  23. {
  24. if(!flag[root])
  25. return ;
  26. flag[root<<1]^=1;
  27. flag[root<<1|1]^=1;
  28. int mid=l+r>>1;
  29. cal(l,mid,root<<1);
  30. cal(mid,r,root<<1|1);
  31. flag[root]=0;
  32. }
  33. void update(int l,int r,int root,int ql,int qr)
  34. {
  35. if(l>=ql&&r<=qr)
  36. {
  37. cal(l,r,root);
  38. flag[root]^=1;
  39. return ;
  40. }
  41. pushdown(l,r,root);
  42. int mid=l+r>>1;
  43. if(mid>ql)
  44. update(l,mid,root<<1,ql,qr);
  45. if(mid<qr)
  46. update(mid,r,root<<1|1,ql,qr);
  47. pushup(root);
  48. }
  49. int main()
  50. {
  51. int n;
  52. scanf("%d",&n);
  53. int x1,x2,y1,y2,all=0;
  54. for(int i=1;i<=n;i++)
  55. {
  56. scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
  57. e[2*i-1].l=min(x1,x2);
  58. e[2*i-1].r=max(x1,x2);
  59. e[2*i-1].h=min(y1,y2);
  60. e[2*i].l=min(x1,x2);
  61. e[2*i].r=max(x1,x2);
  62. e[2*i].h=max(y1,y2);
  63. a[2*i-1]=x1,a[2*i]=x2;
  64. }
  65. sort(a+1,a+1+n*2);
  66. all=unique(a+1,a+1+n*2)-a-1;
  67. for(int i=1;i<=2*n;i++)
  68. e[i].l=lower_bound(a+1,a+1+all,e[i].l)-a,e[i].r=lower_bound(a+1,a+1+all,e[i].r)-a;
  69. sort(e+1,e+1+2*n);
  70. ll ans=0;
  71. for(int i=1;i<=2*n;i++)
  72. {
  73. ans+=sum[1]*(e[i].h-e[i-1].h);
  74. update(1,all,1,e[i].l,e[i].r);
  75. }
  76. printf("%lld\n",ans);
  77. }