【容斥】Four-tuples @山东省第九届省赛 F

时间限制: 10 Sec 内存限制: 128 MB
题目描述
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

题意:
给你四个区间,要求每个区间选一个数组成一个四元组( x1,x2,x3,x4 x 1 , x 2 , x 3 , x 4 ),要求
x1x2,x2x3,x3x4,x4x1 x 1 ≠ x 2 , x 2 ≠ x 3 , x 3 ≠ x 4 , x 4 ≠ x 1

solution
1.先将四个区间长度的乘积作为答案
2.分别减去 x1=x2,x2=x3,x3=x4,x4=x1 x 1 = x 2 , x 2 = x 3 , x 3 = x 4 , x 4 = x 1 四种情况的组合数量(每种情况中未提及的变量在其区间中任选,即统计答案时直接乘区间长度)
3.因为减去 x1=x2 x 1 = x 2 x2=x3 x 2 = x 3 时会重复减去 x1=x2=x3 x 1 = x 2 = x 3 的情况,所以要加回来
类似的还有 x1=x2=x4, x 1 = x 2 = x 4 , x2=x3=x4, x 2 = x 3 = x 4 , x1=x3=x4, x 1 = x 3 = x 4 , x1=x2x3=x4, x 1 = x 2 且 x 3 = x 4 , x2=x3x1=x4 x 2 = x 3 且 x 1 = x 4
4.第一步的答案中应该减去1个 x1=x2=x3=x4 x 1 = x 2 = x 3 = x 4 ,但是在第二步中减去了4个,第三步中又加了6个,所以总共加了2个,最终应该减去3个 x1=x2=x3=x4 x 1 = x 2 = x 3 = x 4 的情况

#define IN_LB() freopen("C:\\Users\\acm2018\\Desktop\\in.txt","r",stdin)
#define OUT_LB() freopen("C:\\Users\\acm2018\\Desktop\\out.txt","w",stdout)
#define IN_PC() freopen("C:\\Users\\hz\\Desktop\\in.txt","r",stdin)
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int T;
const ll MOD = 1e9+7;
int main() {
//    IN_LB();
    scanf("%d",&T);
    while(T--) {
        ll l1,r1,l2,r2,l3,r3,l4,r4;
        ll maxl_1,minr_1,maxl_2,minr_2;
        scanf("%lld%lld%lld%lld%lld%lld%lld%lld",&l1,&r1,&l2,&r2,&l3,&r3,&l4,&r4);
        ll ans = (r1-l1+1)*(r2-l2+1)%MOD;
        ans = ans*(r3-l3+1)%MOD;
        ans = ans*(r4-l4+1)%MOD;
        //1==2
        maxl_1 = max(l1,l2);
        minr_1 = min(r1,r2);
        if(maxl_1<=minr_1) {
            ans = ((ans-(minr_1-maxl_1+1)*(r3-l3+1)%MOD*(r4-l4+1)%MOD)%MOD+MOD)%MOD;
        }
        //2==3
        maxl_1 = max(l2,l3);
        minr_1 = min(r2,r3);
        if(maxl_1<=minr_1) {
            ans = ((ans-(minr_1-maxl_1+1)*(r4-l4+1)%MOD*(r1-l1+1)%MOD)%MOD+MOD)%MOD;
        }
        //3==4
        maxl_1 = max(l3,l4);
        minr_1 = min(r3,r4);
        if(maxl_1<=minr_1) {
            ans = ((ans-(minr_1-maxl_1+1)*(r1-l1+1)%MOD*(r2-l2+1)%MOD)%MOD+MOD)%MOD;
        }
        //1==4
        maxl_1 = max(l1,l4);
        minr_1 = min(r1,r4);
        if(maxl_1<=minr_1) {
            ans = ((ans-(minr_1-maxl_1+1)*(r2-l2+1)%MOD*(r3-l3+1)%MOD)%MOD+MOD)%MOD;
        }
        //1==2&&2==3
        maxl_1 = max(l1,max(l2,l3));
        minr_1 = min(r1,min(r2,r3));
        if(maxl_1<=minr_1) {
            ans = (ans+(minr_1-maxl_1+1)*(r4-l4+1)%MOD)%MOD;
        }
        //1==2&&1==4
        maxl_1 = max(l1,max(l2,l4));
        minr_1 = min(r1,min(r2,r4));
        if(maxl_1<=minr_1) {
            ans = (ans+(minr_1-maxl_1+1)*(r3-l3+1)%MOD)%MOD;
        }
        //1==2&&3==4
        maxl_1 = max(l1,l2);
        minr_1 = min(r1,r2);
        maxl_2 = max(l3,l4);
        minr_2 = min(r3,r4);
        if(minr_1>=maxl_1&&minr_2>=maxl_2){
            ans = (ans+(minr_1-maxl_1+1)*(minr_2-maxl_2+1)%MOD)%MOD;
        }
        //2==3&&3==4
        maxl_1 = max(l2,max(l3,l4));
        minr_1 = min(r2,min(r3,r4));
        if(maxl_1<=minr_1) {
            ans = (ans+(minr_1-maxl_1+1)*(r1-l1+1)%MOD)%MOD;
        }
        //2==3&&1==4
        maxl_1 = max(l3,l2);
        minr_1 = min(r3,r2);
        maxl_2 = max(l1,l4);
        minr_2 = min(r1,r4);
        if(minr_1>=maxl_1&&minr_2>=maxl_2){
            ans = (ans+(minr_1-maxl_1+1)*(minr_2-maxl_2+1)%MOD)%MOD;
        }
        //3==4&&1==4
        maxl_1 = max(l1,max(l3,l4));
        minr_1 = min(r1,min(r3,r4));
        if(maxl_1<=minr_1) {
            ans = (ans+(minr_1-maxl_1+1)*(r2-l2+1)%MOD)%MOD;
        }
        //1==2&&2==3&&3==4
        maxl_1 = max(max(l1,l2),max(l3,l4));
        minr_1 = min(min(r1,r2),min(r3,r4));
        if(maxl_1<=minr_1){
            ans = ((ans-(minr_1-maxl_1+1)*3)%MOD+MOD)%MOD;
        }
        printf("%lld\n",ans);
    }
    return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值