描述:(原题地址:http://hihocoder.com/problemset/problem/1079?sid=601532)
给定一个区间0~L,以及N条线段(li, ri),这N条线段按照输入顺序覆盖到0~L区间。0<L<=10^9, 0<=N<=10^5
问最后有几条线段没有被完全覆盖?
算是线段树的另一种使用,虽然标题是离散化,但其实是连续区间下使用线段树。中间还有一些小问题要处理,就是线段树的边界问题。离散情况下,线段树每一个节点都代表一个个体;连续情况下,每一个节点代表一个区间。也就是,left和right有微妙的变化,离散情况下left = [left, mid], right = [mid + 1, right]; 而连续情况下则是left = [left, mid], right = [mid, right],算是一个小trick。
小ho提出的方法通俗易懂,由于输入的区间太大,所以需要做一个映射,把其中使用到的N对数据映射到0~2N之间,这样就可以放心地用线段树了。
不过我一开始想的是,不做映射,而是按需分区间。一开始就是一个0~L的Node,然后当有线段插入这个节点的时候,再去把这个节点分开。我这里用的是二分,更快的方法可能是根据输入的线段来分左右。
最后两种方法都能AC,时间上也差不多。代码如下:
映射方法:
// Problems.cpp : Defines the entry point for the console application.
//
#include <sstream>
#include <stdio.h>
#include <functional>
#include <string.h>
#include <iomanip>
#include <time.h>
#include <math.h>
#include <fstream>
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <tuple>
#include <map>
#include <string>
#include <set>
#include <algorithm>
#include <list>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef long long ll;
const int MAXPRIME = 1000000007;
class Node {
public:
static unordered_set<int> mark;
bool same;
int id;
int rangeleft, rangeright, mid;
Node *left, *right;
Node(int l, int r) : same(false), id(-1), left(NULL), right(NULL) {
mid = rangeleft = l, rangeright = r;
if (l + 1 == r) return;
mid = (l + r) >> 1;
left = new Node(l, mid);
right = new Node(mid, r);
}
void add(int i, int l, int r) {
if (l <= rangeleft && r >= rangeright) {
same = true;
id = i;
return;
}
if (same) {
right->same = left->same = true;
left->id = right->id = id;
same = false;
}
if (r <= mid) {
left->add(i, l, r);
}
else if (l >= mid) {
right->add(i, l, r);
}
else {
left->add(i, l, mid);
right->add(i, mid, r);
}
}
void count() {
if (same) {
mark.insert(id);
return;
}
if (left) left->count();
if (right) right->count();
}
};
unordered_set<int> Node::mark;
void pro() {
Node::mark.clear();
int n, m;
cin >> n >> m;
vector<int> data;
while (n--) {
int a, b;
cin >> a >> b;
data.push_back(a);
data.push_back(b);
}
vector<int> ind = data;
sort(ind.begin(), ind.end());
auto it = unique(ind.begin(), ind.end());
ind.erase(it, ind.end());
map<int, int> mp;
n = ind.size();
for (int i = 0; i < n; ++i) {
mp[ind[i]] = i;
ind[i] = i;
}
Node *root = new Node(0, n);
for (int i = 0; i < data.size(); i += 2) {
root->add(i, mp[data[i]], mp[data[i + 1]]);
}
root->count();
cout << Node::mark.size() << endl;
}
void handle() {
pro();
}
int main() {
handle();
system("PAUSE");
return 0;
}
按需划分:
// Problems.cpp : Defines the entry point for the console application.
//
#include <sstream>
#include <stdio.h>
#include <functional>
#include <string.h>
#include <iomanip>
#include <time.h>
#include <math.h>
#include <fstream>
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <tuple>
#include <map>
#include <string>
#include <set>
#include <algorithm>
#include <list>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef long long ll;
const int MAXPRIME = 1000000007;
class Node {
public:
static unordered_set<int> mark;
int rangeleft, rangeright, mid;
int id;
Node *left, *right;
Node(int l, int r) : id(-1), mid(-1), left(NULL), right(NULL) {
rangeleft = l, rangeright = r;
}
void add(int i, int l, int r) {
if (l <= rangeleft && r >= rangeright) {
id = i;
return;
}
if (mid == -1) {
mid = (rangeleft + rangeright) >> 1;
left = new Node(rangeleft, mid);
right = new Node(mid, rangeright);
}
if (id >= 0) {
left->id = right->id = id;
}
if (r <= mid) {
left->add(i, l, r);
}
else if (l >= mid) {
right->add(i, l, r);
}
else {
left->add(i, l, mid);
right->add(i, mid, r);
}
id = -1;
}
void count() {
if (id >= 0) {
mark.insert(id);
return;
}
if (left) left->count();
if (right) right->count();
}
};
unordered_set<int> Node::mark;
void pro() {
Node::mark.clear();
int n, m;
cin >> n >> m;
Node *root = new Node(0, m);
while (n--) {
int a, b;
cin >> a >> b;
root->add(n, a, b);
}
root->count();
cout << Node::mark.size() << endl;
}
void handle() {
pro();
}
int main() {
handle();
system("PAUSE");
return 0;
}