题目链接

给定两个区间集合 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. 【原创】spring中的事务传播特性

    关于spring的传播特性,我对其进行了详细的叙述了下: PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务.这是最常见的选择. 比如方法A调用方法B,如果方法 ...

  2. fiddler一些高级用法

    https://my.oschina.net/leejun2005/blog/399108

  3. map————两个数组的交集(2)

    class Solution { public: vector<int> intersect(vector<int>& nums1, vector<int> ...

  4. node——文件夹创建

    //创建文件夹 var fs=require('fs'); //1.异步 fs.mkdir("./第一个目录",function(err){ if (err) { return c ...

  5. Java web课程学习之会话(Session)

    Session会话   l web应用中的会话是指一个客户端浏览器与web服务器之间连续发生一系列请求和响应过程 l web应用的会话状态是指web服务器与浏览器在会话过程中产生的状态信息,借助会话状 ...

  6. 决策树(Decision Trees)

    简介 决策树是一个预测模型,通过坐标数据进行多次分割,找出分界线,绘制决策树. 在机器学习中,决策树学习算法就是根据数据,使用计算机算法自动找出决策边界. 每一次分割代表一次决策,多次决策而形成决策树 ...

  7. pytorch 6 batch_train 批训练

    import torch import torch.utils.data as Data torch.manual_seed(1) # reproducible # BATCH_SIZE = 5 BA ...

  8. SQL的运算符优先级

    注: 1.乘除的优先级高于加减: 2.同一优先级运算符从左向右执行: 3.括号内的运算先执行.

  9. Null值操作

      1)NULL值写入的操作    create table j010(     id number(7),     name varchar2(20),     salary number(7,2) ...

  10. [ReactVR] Start a Virtual Reality Project Using the React VR CLI

    We will learn how to set up a React VR project, run the development mode with hot reloading, and tak ...