线段树--扫描线模板自下而上

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>  https://www.luogu.org/problem/P5490
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int,vector<int>,greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first,nth,last,compare)
 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 23 //******************
 24 int abss(int a);
 25 int lowbit(int n);
 26 int Del_bit_1(int n);
 27 int maxx(int a,int b);
 28 int minn(int a,int b);
 29 double fabss(double a);
 30 void swapp(int &a,int &b);
 31 clock_t __STRAT,__END;
 32 double __TOTALTIME;
 33 void _MS(){__STRAT=clock();}
 34 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
 35 //***********************
 36 #define rint register int
 37 #define fo(a,b,c) for(rint a=b;a<=c;++a)
 38 #define fr(a,c) for(rint a=b;a>=c;--a)
 39 #define mem(a,b) memset(a,sizeof(a))
 40 #define pr printf
 41 #define sc scanf
 42 #define ls rt<<1
 43 #define rs rt<<1|1
 44 typedef long long ll;
 45 const double E=2.718281828;
 46 const double PI=acos(-1.0);
 47 //const ll INF=(1LL<<60);
 48 const int inf=(1<<30);
 49 const double ESP=1e-9;
 50 const int mod=(int)1e9+7;
 51 const int N=(int)1e6+10;
 52 
 53 struct node
 54 {
 55     ll l,r,h;
 56     int f;
 57     friend bool operator<(node a,node b)
 58     {
 59         return a.h<b.h;
 60     }
 61 }edge[N<<1];
 62 ll temp[N<<1],res[N<<1];
 63 int LS(int n)
 64 {
 65     int m=0;
 66     for(int i=1;i<=n;++i)
 67         temp[++m]=res[i];
 68     sort(temp+1,temp+1+m);
 69     m=unique(temp+1,temp+1+m)-temp-1;
 70     for(int i=1;i<=n;++i)
 71         res[i]=lower_bound(temp+1,temp+1+m,res[i])-temp;
 72     return m;
 73 }
 74 struct
 75 {
 76     ll l,cnt,sum;
 77 }tr[N<<2];
 78 void up(int rt)
 79 {
 80     if(tr[rt].cnt) tr[rt].sum=temp[tr[rt].r+1]-temp[tr[rt].l];//因为是点之间的距离,而我代入的是
 81     else tr[rt].sum=tr[ls].sum+tr[rs].sum;                //线段前面的点,所以要加+1,向后移动一格;
 82 }
 83 void Build(int l,int r,int rt)
 84 {
 85     tr[rt].l=l,tr[rt].r=r;
 86     if(l==r)return;
 87     int mid=(l+r)>>1;
 88 
 89     Build(l,mid,rt<<1);
 90     Build(mid+1,rt<<1|1);
 91     up(rt);
 92 }
 93 void update_qu(ll L,ll R,int V,int l,int rt)
 94 {
 95     if(L<=l&&r<=R)
 96     {
 97         tr[rt].cnt+=V;
 98         up(rt);
 99         return;
100     }
101 
102     int mid=(l+r)>>1;
103 //    dn(rt,mid-l+1,r-mid);
104     if(L<=mid)
105         update_qu(L,R,V,l,rt<<1);
106     if(R>mid)
107         update_qu(L,mid+1,rt<<1|1);
108     up(rt);
109 }
110 
111 int main()
112 {
113 //    freopen("D:\\Chrome Download\\testdata (3).in",stdin);
114     int n;
115     sc("%d",&n);
116     for(int i=1;i<=n;++i)
117     {
118         int x1,y1,x2,y2;
119         sc("%d%d%d%d",&x1,&y1,&x2,&y2);
120         edge[i*2-1].h=y1;
121         edge[i*2].h=y2;
122         res[i*2-1]=x1;
123         res[i*2]=x2;
124     }
125     int n_=LS(n*2);
126     for(int i=1;i<=n;++i)
127     {
128         edge[2*i-1].l=edge[2*i].l=res[2*i-1];
129         edge[2*i-1].r=edge[2*i].r=res[2*i];
130         edge[i*2-1].f=1,edge[i*2].f=-1;
131     }
132     sort(edge+1,edge+1+2*n);
133     Build(1,n_,1);
134     update_qu(edge[1].l,edge[1].r-1,edge[1].f,1,1);
135     ll ans=0;
136     ll th=edge[1].h;
137     for(int i=2;i<=2*n;++i)
138     {
139         ans+=tr[1].sum*(edge[i].h-th);
140         th=edge[i].h;
141         update_qu(edge[i].l,edge[i].r-1,edge[i].f,1);//因为是点之间的距离所以要减去;
142     }                                                    //与80行对应;
143     pr("%lld\n",ans);
144     return 0;
145 }
146 
147 /**************************************************************************************/
148 
149 int maxx(int a,int b)
150 {
151     return a>b?a:b;
152 }
153 
154 void swapp(int &a,int &b)
155 {
156     a^=b^=a^=b;
157 }
158 
159 int lowbit(int n)
160 {
161     return n&(-n);
162 }
163 
164 int Del_bit_1(int n)
165 {
166     return n&(n-1);
167 }
168 
169 int abss(int a)
170 {
171     return a>0?a:-a;
172 }
173 
174 double fabss(double a)
175 {
176     return a>0?a:-a;
177 }
178 
179 int minn(int a,int b)
180 {
181     return a<b?a:b;
182 }

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...