省赛-F

题目描述

Given l1,r1,l2,r2,l3,r3,l4,r4, please count the number of four-tuples (x1,x2,x3,x4) such that li≤ xi≤ ri and x1≠x2,x2≠x3,x3≠x4,x4≠x1. The answer should modulo 10^9+7 before output.

输入

The input consists of several test cases. The first line gives the number of test cases, T(1≤ T≤ 10^6).
For each test case, the input contains one line with 8 integers l1,r1,l2, r2, l3,r3,l4,r4(1≤ li≤ ri≤ 10^9)

输出

For each test case, output one line containing one integer, representing the answer.

样例输入

1
1 1 2 2 3 3 4 4

样例输出

1
设事件A为x1!=x2 ,事件B 为x2!=x3 ,事件C 为 x3!=x4 事件D 为 x4!=x1
问题要求:|A∩B∩C∩D|

可以转换成|U|-|A'∪B'∪C'∪D'|
即求 |A'∪B'∪C'∪D'| 就行,容斥定理
所有都不相等-两个相等+三个相等+两对两个相等-四个相等。
#include <bits/stdc++.h>
using namespace std;
const int M=1e9+7;
typedef  long long ll;
ll l[10],r[10];
ll L(int i)
{
    return r[i]-l[i]+1;
}
ll f(int a,int b)
{
    ll lt=max(l[a],l[b]);
    ll rt=min(r[a],r[b]);
    return rt-lt+1>0?rt-lt+1:0;
}
ll f2(int a,int b,int c)
{
    ll lt=max(l[a],max(l[b],l[c]));
    ll rt=min(r[a],min(r[b],r[c]));
    return rt-lt+1>0?rt-lt+1:0;
}
ll f3(int a,int b,int c,int d)
{
    ll lt=max(max(l[a],l[b]),max(l[c],l[d]));
    ll rt=min(min(r[a],r[b]),min(r[c],r[d]));
    return rt-lt+1>0?rt-lt+1:0;
}
int main()
{
    int t;
    scanf("%d",&t);
    long long sum=1;
    while(t--)
    {
        sum=1;
        for(int i=0; i<4; ++i)
        {
            scanf("%lld%lld",&l[i],&r[i]);
            l[i+4]=l[i];
            r[i+4]=r[i];
            sum*=r[i]-l[i]+1;
            sum%=M;
        }
        for(int i=0; i<4; ++i)
        {
            sum-=f(i,i+1)%M*L(i+2)%M*L(i+3)%M;
            sum=(sum+M)%M;
        }
        for(int i=0; i<4; ++i)
        {
            sum+=f2(i,i+1,i+2)%M*L(i+3)%M;
            sum=(sum+M)%M;
        }
        for(int i=0; i<2; ++i)
        {
            sum+=f(i,i+1)%M*f(i+2,i+3)%M;
            sum=(sum+M)%M;
        }
        sum-=f3(0,1,2,3)%M*3%M;
        sum=(sum+M)%M;
        printf("%lld\n",sum);
    }
    return 0;
}
View Code

 

posted @ 2018-05-27 17:13  house_cat  阅读(144)  评论(0编辑  收藏  举报