题目链接

给定两个区间集合 AB,其中集合 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。

第一行:包含两个整数 NM (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周 - 水题 区间问题的更多相关文章

  1. hiho 171周 - 水题,并查集

    题目链接 题目描述: 输入4 alice 2 alice@hihocoder.com alice@gmail.com bob 1 bob@qq.com alicebest 2 alice@gmail. ...

  2. SPOJ CNTPRIME 13015 Counting Primes (水题,区间更新,求区间的素数个数)

    题目连接:http://www.spoj.com/problems/CNTPRIME/ #include <iostream> #include <stdio.h> #incl ...

  3. SPOJ 3693 Maximum Sum(水题,记录区间第一大和第二大数)

    #include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...

  4. SPOJ 7259 Light Switching (水题,区间01取反)

    #include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...

  5. hihocoder 1322 - 树结构判定 - [hiho一下161周][模板题/水题]

    题目链接:http://hihocoder.com/problemset/problem/1322 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个包含 N 个顶 ...

  6. CF451B Sort the Array 水题

    Codeforces Round #258 (Div. 2) Sort the Array B. Sort the Array time limit per test 1 second memory ...

  7. UVa 1586 Molar mass --- 水题

    UVa 1586 题目大意:给出一种物质的分子式(不带括号),求分子量.本题中分子式只包含4种原子,分别为C.H.O.N, 原子量分别为12.01,1.008,16.00,14.01 解题思路:先实现 ...

  8. Codeforces Testing Round #12 A. Divisibility 水题

    A. Divisibility Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...

  9. hdu 4548 第六周H题(美素数)

    第六周H题 - 数论,晒素数 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   De ...

随机推荐

  1. stackoverflow 加载特慢解决方案,配置 hosts 屏蔽速度慢的第三方 API

    127.0.0.1 ajax.googleapis.com www.googletagservices.com www.gravatar.com 127.0.0.1 securepubads.g.do ...

  2. java 文件下载遇到的数个坑

    文件的下载在web开发中应该是很常用的功能,近期项目中遇到的一个需求是:前端提供 查询条件以及查询结果的字段,后端拿到这些参数之后,在数据库中根据业务逻辑查询得出查询结果,导出成excel文件,同时传 ...

  3. Pyhton学习——Day58

    From表单验证 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  4. 最近邻插值法&线性插值&双线性插值&三线性插值

    最近邻插值法nearest_neighbor是最简单的灰度值插值.也称作零阶插值,就是令变换后像素的灰度值等于距它最近的输入像素的灰度值. 造成的空间偏移误差为像素单位,计算简单,但不够精确.但当图像 ...

  5. HDU 2300 Crashing Robots

    Crashing Robots 题意 模拟多个机器人在四个方向的移动,检测crash robot, crash wall, OK这些状态 这是个模拟题需要注意几点: 理解转变方向后移动多少米,和转动方 ...

  6. [BOI2011]MET-Meteors

    题目:洛谷P3527. 题目大意:n个国家在某星球上建立了m个空间站(一个空间站只属于一个国家),空间站围成一个环.现在知道要下k天陨石,每天都在一个区间内下,每个点都下同样多的(若r>l,则说 ...

  7. 新手学python-Day3-模块

    模块就是引入别人写的,官方写的工具库,就像扳手,钳子,电锯

  8. 小学生绞尽脑汁也学不会的python(异常,约束,MD5加密,日志处理)

    小学生绞尽脑汁也学不会的python(异常,约束,MD5加密,日志处理) 异常处理(处理) 1.产生异常.raise 异常类(),抛出异常2. 处理异常: try: xxxxx # 尝试执行的代码. ...

  9. JavaScript和CSS实用工具、库与资源

    JavaScript和CSS实用工具.库与资源 JavaScript 库 Particles.js  - 一个用于在网页上创建漂亮的浮动粒子的 JS 库: Three.js  - 用于在网页上创建 3 ...

  10. 【转】 C# ListView实例:文件图标显示

    [转] C# ListView实例:文件图标显示 说明:本例将目录中的文件显示在窗体的ListView控件中,并定义了多种视图浏览.通过调用Win32库函数实现图标数据的提取. 主程序: 大图标: 列 ...