Codeforces Round #371 (Div. 2) A. Meeting of Old Friends【思维,求区间交集】
来源:互联网 发布:国外免费网络视频聊天 编辑:程序博客网 时间:2024/06/05 06:18
题意:给定2个时间区间[L1,R1],[L2,R2],求交集,如果给定的k在交集中,那么交集时间-1。
思路:首先对两个区间的长度进行判断,判断之后,再以[L1,R1]这个区间不动, [L2,R2]这个区间去动,长度判断之后分类讨论就很容易了.. 主要是代码量有点大。 容易打晕。还有爆int。
数据分析:1 ≤ l1, r1, l2, r2, k ≤ 1e18, l1 ≤ r1, l2 ≤ r2 注意1e18会爆len1 int类型。 一看到这种东西,就开LL就没问题了。对数据一定要敏感
复杂度分析:勉强认为O(1)吧!
#include <bits/stdc++.h>using namespace std;typedef long long ll;ll ans=0;int main(void){ ll l1,r1,l2,r2,k; cin >> l1>> r1>> l2 >> r2 >>k; ll len1=r1-l1; //注意爆int的情况 ll len2=r2-l2; //cout << len1 <<endl << len2<< endl; if(len1 > len2)//接下就是分类讨论了 { if(r2<l1 || l2 >r1) { printf("0\n"); } else if(r2>=l1&&l2<=l1) { ans=r2-l1+1; if(l1<=k && k<=r2) ans--; cout << ans << endl; } else if(l2>=l1 && r2<=r1) { ans=r2-l2+1; if(k>=l2 && k<=r2) ans--; cout << ans << endl; } else if(l2<=r1 && r2>=r1) { ans=r1-l2+1; if(k>=l2 && k<=r1) ans--; cout << ans << endl; } } else if(len1 <= len2) { if(r2<l1 || l2 >r1) { printf("0\n"); } else if(r2>=l1 && r2<=r1 && l2<=l1) { ans=r2-l1+1; if(k>=l1&&k<=r2) ans--; cout << ans << endl; } else if(r2 >=r1 && l2<=l1) { ans=r1-l1+1; if(k>=l1 && k<=r1) ans--; cout << ans << endl; } else if(l2>=l1 && l2 <=r1 && r2 >=r1) { ans=r1-l2+1; if(k>=l2 &&k<=r1) ans --; cout << ans << endl; } } return 0;}
阅读全文
0 0
- Codeforces Round #371 (Div. 2) A. Meeting of Old Friends【思维,求区间交集】
- Codeforces Round #371 (Div. 2) A. Meeting of Old Friends
- Codeforces #371(Div.2)A.Meeting of Old Friends【模拟+思维】
- Codeforces Round #371 (Div. 2) A. Meeting of Old Friends (水题)
- Codeforces Round #371 (Div. 2) A. Meeting of Old Friends(水题)
- Codeforces Round #371 (Div. 2) A. Meeting of Old Friends 数学、A题第三次被系统hack了 (┬_┬)、集合交集
- Codeforces Problem 714A Meeting of Old Friends(区间交)
- Codeforces-714A-Meeting of Old Friends
- A. Meeting of Old Friends
- CodeForces-714A. Meeting of Old Friends(水题)
- CodeForces 714A Meeting of Old Friends 【数学计算】【基础】
- 714A. Meeting of Old Friends
- 【Codeforces Round 375 (Div 2) A】【水题】The New Year Meeting Friends
- Codeforces Round #375 (Div. 2)A. The New Year: Meeting Friends(水题)
- Codeforces Round #375 (Div. 2) A-The New Year: Meeting Friends
- 【31.42%】【CF 714A】Meeting of Old Friends
- The New Year: Meeting Friends Codeforces Round#375-A
- Codeforces Round #433 (Div. 1) B Jury Meeting(思维)
- Qt下载地址
- Azkaban入门篇
- 高并发重复插入数据的场景之一
- 推荐一款自助式BI工具
- Collections.unmodifiableMap
- Codeforces Round #371 (Div. 2) A. Meeting of Old Friends【思维,求区间交集】
- Handling truncated messages
- Sphinx 安装
- HDU 【1241】Oil Deposits(DPS)
- HDFS原理综述篇
- JDBC连接数据库oracle的代码
- Java中Socket的用法--Spring MVC
- 宏函数和函数的区别及其优缺点
- CentOS 6.4安装Oracle的jrockit-jdk