hiho152周 - 水题 区间问题
给定两个区间集合 A 和 B,其中集合 A 包含 N 个区间[ A1, A2 ], [ A3, A4 ], ..., [ A2N-1, A2N ],集合 B 包含 M 个区间[ B1, B2 ], [ B3, B4 ], ..., [ B2M-1, B2M ]。求 A - B 的长度。
例如对于 A = {[2, 5], [4, 10], [14, 18]}, B = {[1, 3], [8, 15]}, A - B = {(3, 8), (15, 18]},长度为8。
第一行:包含两个整数 N 和 M (1 ≤ N, M ≤ 100000)。
第二行:包含 2N 个整数 A1, A2, ..., A2N (1 ≤ Ai ≤ 100000000)。
第三行:包含 2M 个整数 B1, B2, ..., B2M (1 ≤= Bi ≤ 100000000)。
---------------------------------------------------------------------------------------------------------
一看是区间的问题,就写了个线段树800msAC了,可是看了分析才知道大材小用了,线段树适合多次修改多次询问的,而这个只是一次询问也没动态修改,
所以直接:排序然后用两个计数器记录每个区间起点处A和B的覆盖情况就行了。
线段树版 823ms:
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #define MAX(a,b) ((a)>=(b)?(a):(b))
#define MIN(a,b) ((a)<=(b)?(a):(b))
#define OO 0x0fffffff
using namespace std;
typedef long long LL;
const int N = 100100;
struct Node{
int l,r;
int lazy;
Node(){lazy=0;}
int mid(){return (l+r)>>1;}
};
Node segtree[N*12];
int uni0[N*4],uni1[N*4],flag[N*4];
int n,m;
void build(int id,int l,int r){
segtree[id].l = l;
segtree[id].r = r;
if(l==r) return ;
int mid = (l+r)>>1;
build(id*2+0,l,mid);
build(id*2+1,mid+1,r);
} void modify(int id,int spos,int epos,int tag){
if(segtree[id].l==spos&&segtree[id].r==epos){
segtree[id].lazy|=tag;
return ;
}
if(segtree[id].lazy&tag) return ;
if(segtree[id].lazy){
segtree[id*2+0].lazy|=segtree[id].lazy;
segtree[id*2+1].lazy|=segtree[id].lazy;
segtree[id].lazy = 0;
}
int mid = segtree[id].mid();
if(epos<=mid){
modify(id*2+0,spos,epos,tag);
}
else if(spos>mid){
modify(id*2+1,spos,epos,tag);
}
else{
modify(id*2+0,spos,mid,tag);
modify(id*2+1,mid+1,epos,tag);
}
}
void traverse(int id){
if(segtree[id].l==segtree[id].r) {
flag[segtree[id].l]=segtree[id].lazy;
return ;
}
segtree[id*2+0].lazy|=segtree[id].lazy;
segtree[id*2+1].lazy|=segtree[id].lazy;
traverse(id*2+0);
traverse(id*2+1);
} int main(){
cin>>n>>m;
int cnt=(m+n)*2;
for(int i=0;i<cnt;i++) cin>>uni0[i]; map<int,int> refl;
memcpy(uni1,uni0,sizeof(int)*cnt);
sort(uni1,uni1+cnt);
int maxn = unique(uni1,uni1+cnt)-uni1;
for(int i=0;i<maxn;i++) { refl[uni1[i]] = i; } build(1,0,maxn-2);
for(int i=0;i<n;i++) {
if(uni0[i*2+0]==uni0[i*2+1]){continue;}
int l = refl[uni0[i*2+0]];
int r = refl[uni0[i*2+1]];
if(l>r) swap(l,r);
modify(1,l,r-1,1);
}
for(int i=n;i<m+n;i++){
if(uni0[i*2+0]==uni0[i*2+1]){continue;}
int l = refl[uni0[i*2+0]];
int r = refl[uni0[i*2+1]];
if(l>r) swap(l,r);
modify(1,l,r-1,2);
}
traverse(1);
long long ans = 0;
for(int i=0;i<maxn-1;i++){
if(flag[i]==1){
ans+=(uni1[i+1]-uni1[i]);
}
}
printf("%lld\n",ans);
return 0;
}
简单版 104ms
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #define MAX(a,b) ((a)>=(b)?(a):(b))
#define MIN(a,b) ((a)<=(b)?(a):(b))
#define OO 0x0fffffff
using namespace std;
typedef long long LL;
const int N = 100100*4;
struct NODE{
int value;
int type;
friend bool operator<(const NODE &a,const NODE &b){
return a.value<b.value;
}
};
NODE ps[N];
int cnta,cntb;
void check(int type){
switch(type){
case 0 : cnta++; break;
case 1 : cnta--; break;
case 2 : cntb++; break;
case 3 : cntb--; break;
}
} int main(){
int n,m,cnts;
cin>>n>>m;
cnts = m+n;
for(int i=0;i<n;i++){
scanf("%d%d",&ps[i*2+0].value,&ps[i*2+1].value);
ps[i*2+0].type = 0;
ps[i*2+1].type = 1;
}
for(int i=n;i<cnts;i++){
scanf("%d%d",&ps[i*2+0].value,&ps[i*2+1].value);
ps[i*2+0].type = 2;
ps[i*2+1].type = 3;
}
cnts<<=1;
std::sort(ps,ps+cnts); int ans = 0;
cnta = cntb = 0;
check(ps[0].type);
int lpos = ps[0].value;
for(int i=1;i<cnts;i++){
if(ps[i].value!=lpos){
if((cnta)&&(!cntb)){
ans += ps[i].value-lpos;
}
lpos = ps[i].value;
}
check(ps[i].type);
} printf("%d\n",ans);
return 0;
}
hiho152周 - 水题 区间问题的更多相关文章
- hiho 171周 - 水题,并查集
题目链接 题目描述: 输入4 alice 2 alice@hihocoder.com alice@gmail.com bob 1 bob@qq.com alicebest 2 alice@gmail. ...
- SPOJ CNTPRIME 13015 Counting Primes (水题,区间更新,求区间的素数个数)
题目连接:http://www.spoj.com/problems/CNTPRIME/ #include <iostream> #include <stdio.h> #incl ...
- SPOJ 3693 Maximum Sum(水题,记录区间第一大和第二大数)
#include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...
- SPOJ 7259 Light Switching (水题,区间01取反)
#include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...
- hihocoder 1322 - 树结构判定 - [hiho一下161周][模板题/水题]
题目链接:http://hihocoder.com/problemset/problem/1322 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个包含 N 个顶 ...
- CF451B Sort the Array 水题
Codeforces Round #258 (Div. 2) Sort the Array B. Sort the Array time limit per test 1 second memory ...
- UVa 1586 Molar mass --- 水题
UVa 1586 题目大意:给出一种物质的分子式(不带括号),求分子量.本题中分子式只包含4种原子,分别为C.H.O.N, 原子量分别为12.01,1.008,16.00,14.01 解题思路:先实现 ...
- Codeforces Testing Round #12 A. Divisibility 水题
A. Divisibility Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...
- hdu 4548 第六周H题(美素数)
第六周H题 - 数论,晒素数 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...
随机推荐
- JavaScript实现乘法表
JavaScript实现乘法表 <script type="text/javascript"> function c(n,m) { ...
- Cookie是存储在客户端上的一小段数据
背景 在HTTP协议的定义中,采用了一种机制来记录客户端和服务器端交互的信息,这种机制被称为cookie,cookie规范定义了服务器和客户端交互信息的格式.生存期.使用范围.安全性. 在JavaSc ...
- BroadcastReceiver广播接受者简单使用
1.注册BrocadcastReceiver <receiver android:name=".FirstReceiver" > <!-- 指定能够接收的广播类型 ...
- BZOJ 3527: [Zjoi2014]力 FFT_卷积
Code: #include <cmath> #include <cctype> #include <cstdio> #include <cstring> ...
- K3内部表数据名称
在后台数据库ICClassType表中,字段FID<0的是老单,FID>0的是新单.----------------系统设置------------------------FStatus: ...
- underscore的简单了解
1.underscore:一个封装好的js工具库,它提供了一整套函数式编程的使用功能,但是没有扩展任何js内置对象.它解决了这个问题:如果我面对一个空白的HTML,并希望立即开始工作,我需要什么? 2 ...
- ajax中遇到无法发送的问题,以及收不到返回信息的问题
1.在做ajax时,数据发送成功,后台确认了也返回了信息,但是怎么都在success里面接收不了,我遇见的造成的原因时因为dataType返回值类型错误造成的原因. var url = "请 ...
- Nginx Zabbix
https://www.cnblogs.com/wangxiaoqiangs/archive/2016/04/20/5412111.html
- javascript-知识点集合
第三课.JavaScript的语法与关键字 1.JavaScript的语法 字符串.数字.布尔.数组.对象.Null.Undefined 1.js的变量区分大小写 username userName ...
- u-boot学习(六):自己写bootloader
依照前面分析的u-boot的启动流程,自己写一个简单的Bootloader.这是參考韦东山老师的视频写的. 1.初始化硬件:关看门狗.设置时钟.设置SDRAM.初始化NAND Flash 2.假设Bo ...