HihoCoder ——1305

来源:互联网 发布:淘宝禁售商品管理规范 编辑:程序博客网 时间:2024/06/10 21:35

hihocoder 1305

题解

|A+B| = |A| + |B| - |AB|

|A-B| = |A| - |AB|

求出A,B,A+B 的实际长度,再用逻辑运算即可!

AC code

#include <bits/stdc++.h>typedef long long LL;using namespace std;const int maxn = 2e5 + 10;const int INF = 0x3f3f3f3f;pair <int,int> cntM[maxn];pair <int,int> cntN[maxn];pair <int,int> cntN_M[2*maxn];int main(){    int N,M;    scanf("%d%d",&N,&M);    for(int i = 0; i < N; i++)        scanf("%d%d",&cntN[i].first,&cntN[i].second);    for(int i = 0; i < M; i++)        scanf("%d%d",&cntM[i].first,&cntM[i].second);    for(int i = 0; i < N; i++)        cntN_M[i] = cntN[i];    for(int i = 0; i < M; i++)        cntN_M[i+N] = cntM[i];    sort(cntN,cntN+N);    sort(cntM,cntM+M);    sort(cntN_M,cntN_M+N+M);    LL lena = 0,lenb = 0,lenc = 0;    LL st = cntN[0].first;    LL ed = cntN[0].second;    for(int i = 1; i < N; i++)         {              if(cntN[i].first >= ed)              {lena += (ed -st); st = cntN[i].first;ed = cntN[i].second;}              else               ed = max(ed,(LL)cntN[i].second);         }    lena += (ed -st);    st = cntM[0].first;    ed = cntM[0].second;    for(int i = 0; i < M; i++)         {              if(cntM[i].first >= ed)              {lenb += (ed -st); st = cntM[i].first;ed = cntM[i].second;}              else               ed = max(ed,(LL)cntM[i].second);         }    lenb += (ed -st);    st = cntN_M[0].first;    ed = cntN_M[0].second;    for(int i = 0; i < N+M; i++)        {              if(cntN_M[i].first >= ed)              {lenc += (ed -st); st = cntN_M[i].first;ed = cntN_M[i].second;}              else               ed = max(ed,(LL)cntN_M[i].second);        }    lenc += (ed -st);    cout <<  lena - (lena + lenb - lenc) << endl ;  return 0;}
原创粉丝点击