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 ...
随机推荐
- 获取sqlserver数据字典的完整sql
SELECTsysobjects.name AS 表名称 , --------------as 的作用:为字段起一个别名 --sysproperties.[value] AS 表说明 , ------ ...
- Mediator 基于内存的发布订阅
Github Mediator 使用方法 /// <summary> /// 返回值 BaseEntity /// </summary> public class Ping1 ...
- Smalltalk
Smalltalk is an object-oriented, dynamically typed, reflective programming language. Smalltalk was o ...
- Unity脚本生命周期 图解
简单总结的话就是: Awake():初始化时执行,类似c#中的构造函数 OnEnable() Start() FixUpdate() Update() OnDisable() OnDestory() ...
- Unity 动画系统(Mecanim) 术语及翻译 表格
原文 翻译 Animation Clip 视频片段 Avatar 阿凡达 Retargeting 重定向 Rigging 绑定 skinning 蒙皮 Animator Component 动画组件 ...
- 动态生成的dom元素绑定事件
要求:要绑定到父元素上$(".school_Inlists").on("click",".chose_Inbtn",function(){ ...
- React中的setState(obj)
1.setState(obj) 只能浅merge obj,对于复杂对象结构的不行 比如: this.state = { data:{ idx:1 } } this.setState( ...
- javascript编程风格(粗略笔记)
1.空格 紧凑型: project.MyClass = function(arg1, arg2){ 松散型: for( i = 0; i < length; i++ ){ 2.代码行长度 最多8 ...
- [Codeforces 626F]Group Projects
题目大意: 给定\(n\)个数\(a[1]\sim a[n]\),让你把它分为若干个集合,使每个集合内最大值与最小值的差的总和不超过\(K\).问总方案数. 解题思路: 一道很神的dp题. 首先将数进 ...
- Mysql怎么样避免全表扫描,sql查询优化
对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引: 尝试下面的技巧以避免优化器错选了表扫描: 使用ANALYZE TABLE tbl_name为扫 ...