时间限制: 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,x4x1,x2,x3,x4),要求
x1≠x2,x2≠x3,x3≠x4,x4≠x1x1≠x2,x2≠x3,x3≠x4,x4≠x1

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

#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;
}

【容斥】Four-tuples @山东省第九届省赛 F的更多相关文章

  1. 【二分图最大匹配】Bullet @山东省第九届省赛 B

    时间限制: 6 Sec 内存限制: 128 MB 题目描述 In GGO, a world dominated by gun and steel, players are fighting for t ...

  2. 【二分图带权匹配】Anagram @山东省第九届省赛 A

    题目描述 Orz has two strings of the same length: A and B. Now she wants to transform A into an anagram o ...

  3. nyoj1273 河南省第九届省赛_"宣传墙"、状压DP+矩阵幂加速

    宣传墙 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 ALPHA 小镇风景美丽,道路整齐,干净,到此旅游的游客特别多.CBA 镇长准备在一条道路南 面 4*N 的墙上做 ...

  4. NYOJ 1272 表达式求值 第九届省赛 (字符串处理)

    title: 表达式求值 第九届省赛 nyoj 1272 tags: [栈,数据结构] 题目链接 描述 假设表达式定义为: 1. 一个十进制的正整数 X 是一个表达式. 2. 如果 X 和 Y 是 表 ...

  5. 河南省acm第九届省赛--《表达式求值》--栈和后缀表达式的变形--手速题

    表达式求值 时间限制:1000 ms | 内存限制:65535 KB 难度:3   描述 假设表达式定义为:1. 一个十进制的正整数 X 是一个表达式.2. 如果 X 和 Y 是 表达式,则 X+Y, ...

  6. 第七届河南省赛F.Turing equation(模拟)

    10399: F.Turing equation Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 151  Solved: 84 [Submit][St ...

  7. SD第九届省赛B题 Bullet

    Bullet Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Description In G ...

  8. ZOJ 3606 Lazy Salesgirl 浙江省第九届省赛

    Lazy Salesgirl Time Limit: 5 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who ma ...

  9. ZOJ 3601 Unrequited Love 浙江省第九届省赛

    Unrequited Love Time Limit: 16 Seconds      Memory Limit: 131072 KB There are n single boys and m si ...

随机推荐

  1. BZOJ 4282(慎二的随机数列

    题解: 网上题解还没看 我的方法是用平衡树维护一个单调栈 由于N用了一定是赚的 所以它的作用是让f[i+1]=f[i]+1 这个是可以记录的 就跟noip蚯蚓那题一样 然后插入一个值的时候查询前面的最 ...

  2. centos7 查看ip地址

    命令: ip  address 简写ip  a 过滤出来某个网卡的ip: ip a show ens33 |awk -F ' ' 'NR==3{print$2}'|cut -d / -f1

  3. C# 之 HttpResponse 类

    Response 对象,派生自HttpResponse 类,该类封装来自 ASP.NET 操作的 HTTP 响应信息.存在于System.Web命名空间下. 注:MIME(Multipurpose I ...

  4. NOI2018Day2T1 屠龙勇士 set 扩展欧几里德 中国剩余定理

    原文链接https://www.cnblogs.com/zhouzhendong/p/NOI2018Day2T1.html 题目传送门 - 洛谷P4774 题意 题解 首先我们仔细看一看样例可以发现如 ...

  5. Tomcat中文乱码解决办法

    有时候发现自己将中文编码后还是会存在乱码的情况,解决办法就是在Server.xml中的Connector结点,配置 URIEncoding="UTF-8"即可

  6. pip软件包安装 + Anaconda软件库安装 教程

    PIP软件包安装(适用于Ubuntu和Windows10): 下载pip的安装包,官网链接如下:https://pypi.python.org/pypi/pip 我选择了Source源的安装方式,单击 ...

  7. ZOJ 1610 Count the Colors 【线段树】

    <题目链接> 题目大意: 在[0,8000]这个区间内,不断进行一些操作,将其中的一些区间染成特定颜色,如果区间重复的话,后面染的色块会覆盖前面染的色块,问最终[0,8000]这个区间内每 ...

  8. hadoop离线计算项目上线配置问题记录

    最近上线一个hadoop离线处理项目,因为在低配置(8G,4核)的时候装的CDH,后来集群配置(64G,16核)上来了,但许多参数不会自动修改,需要自己调整,处理过程中遇到的配置问题记录下. 1.hi ...

  9. 博客第一篇 osi七层网络传输模型

  10. MacOs brew 命令行安装常见工具

    brew类似ubuntu系统下的apt-get的功能 安装方法:  在Mac中打开Termal:  输入命令: ruby -e "$(curl -fsSL https://raw.githu ...