2018年山东省第九届acm省赛F题,容斥原理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fanbaobao829/article/details/80233299
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
int main()
{
    ll n,l1,l2,l3,l4,r1,r2,r3,r4,tp1l,tp2l,tp1r,tp2r,ans;
    scanf("%lld\n",&n);
    while(n--&&scanf("%lld%lld%lld%lld%lld%lld%lld%lld",&l1,&r1,&l2,&r2,&l3,&r3,&l4,&r4)!=EOF)
    {
        ans=(r1-l1+1)*(r2-l2+1)%mod;
        ans=ans*(r3-l3+1)%mod;
        ans=ans*(r4-l4+1)%mod;
        //1==2
        tp1l=max(l1,l2);
        tp1r=min(r1,r2);
        if(tp1l<=tp1r)
            ans=((ans-(tp1r-tp1l+1)*(r3-l3+1)%mod*(r4-l4+1)%mod)%mod+mod)%mod;
        //2==3
        tp1l=max(l3,l2);
        tp1r=min(r3,r2);
        if(tp1l<=tp1r)
            ans=((ans-(tp1r-tp1l+1)*(r1-l1+1)%mod*(r4-l4+1)%mod)%mod+mod)%mod;
        //3==4
        tp1l=max(l3,l4);
        tp1r=min(r3,r4);
        if(tp1l<=tp1r)
            ans=((ans-(tp1r-tp1l+1)*(r1-l1+1)%mod*(r2-l2+1)%mod)%mod+mod)%mod;
        //4==1
        tp1l=max(l1,l4);
        tp1r=min(r1,r4);
        if(tp1l<=tp1r)
            ans=((ans-(tp1r-tp1l+1)*(r3-l3+1)%mod*(r2-l2+1)%mod)%mod+mod)%mod;
        //1==2 2==3
        tp1l=max(l1,max(l3,l2));
        tp1r=min(r1,min(r3,r2));
        if(tp1l<=tp1r)
            ans=(ans+(tp1r-tp1l+1)*(r4-l4+1)%mod)%mod;
        //1==2 3==4
        tp1l=max(l1,l2);
        tp1r=min(r1,r2);
        tp2l=max(l3,l4);
        tp2r=min(r3,r4);
        if(tp1l<=tp1r&&tp2l<=tp2r)
            ans=(ans+(tp1r-tp1l+1)*(tp2r-tp2l+1)%mod)%mod;
        //1==2 4==1
        tp1l=max(l1,max(l4,l2));
        tp1r=min(r1,min(r4,r2));
        if(tp1l<=tp1r)
            ans=(ans+(tp1r-tp1l+1)*(r3-l3+1)%mod)%mod;
        //2==3 3==4
        tp1l=max(l4,max(l3,l2));
        tp1r=min(r4,min(r3,r2));
        if(tp1l<=tp1r)
            ans=(ans+(tp1r-tp1l+1)*(r1-l1+1)%mod)%mod;
        //2==3 1==4
        tp1l=max(l3,l2);
        tp1r=min(r3,r2);
        tp2l=max(l1,l4);
        tp2r=min(r1,r4);
        if(tp1l<=tp1r&&tp2l<=tp2r)
            ans=(ans+(tp1r-tp1l+1)*(tp2r-tp2l+1)%mod)%mod;
        //3==4 4==1
        tp1l=max(l4,max(l3,l1));
        tp1r=min(r4,min(r3,r1));
        if(tp1l<=tp1r)
            ans=(ans+(tp1r-tp1l+1)*(r2-l2+1)%mod)%mod;
        //1==2==3==4
        tp1l=max(l4,max(max(l2,l3),l1));
        tp1r=min(r4,min(min(r2,r3),r1));
        if(tp1l<=tp1r)
            ans=((ans-(tp1r-tp1l+1)*3)%mod+mod)%mod;
        printf("%lld\n",ans);
    }
    return 0;
}


/***************************************************
User name: 杨凡
Result: Accepted
Take time: 0ms
Take Memory: 184KB
Submit time: 2018-05-07 23:14:40
****************************************************/

阅读更多

没有更多推荐了,返回首页