/****************************************************************************************************
离散化+线段树,离散化的时候注意,另外统计要用hash表~
****************************************************************************************************/
#include <iostream>
#include <functional>
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
using namespace std;
#define LOWBIT(x) ( (x) & ( (x) ^ ( (x) - 1 ) ) )
#define CLR(x, k) memset((x), (k), sizeof(x))
#define CPY(t, s) memcpy((t), (s), sizeof(s))
#define SC(t, s) static_cast<t>(s)
#define LEN(s) static_cast<int>( strlen((s)) )
#define SZ(s) static_cast<int>( (s).size() )
typedef double LF;
//typedef long long LL; //GNU C++
//typedef unsigned long long ULL;
typedef __int64 LL; //Visual C++ 2010
typedef unsigned __int64 ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;
typedef map<int, int>::iterator MI;
typedef vector<int>::iterator VI;
typedef list<int>::iterator LI;
typedef set<int>::iterator SI;
template <typename T>
T sqa(const T &x)
{
return x * x;
}
template <typename T>
T gcd(T a, T b)
{
if (!a || !b)
{
return max(a, b);
}
T t;
while (t = a % b)
{
a = b;
b = t;
}
return b;
}
template <typename T>
T ext_gcd(T a, T b, T &x, T &y)
{
if (!b)
{
x = 1;
y = 0;
return a;
}
T d = ext_gcd(b, a % b, x, y);
T t = x;
x = y;
y = t - a / b * y;
return d;
}
template <typename T>
T invmod(T a, T p)
{
LL inv, y;
ext_gcd(a, p, inv, y);
inv < 0 ? inv += p : 0;
return inv;
}
const int INF_INT = 0x3f3f3f3f;
const LL INF_LL = 0x7fffffffffffffffLL; //15f
const double oo = 10e9;
const double eps = 10e-7;
const double PI = acos(-1.0);
const int MAXN = 10004 << 2;
int test, n;
struct Line
{
int x, y;
int id;
}ln[MAXN];
int pcnt, pp[MAXN];
struct Node
{
int left, right;
int col;
Node()
{
CLR(this, 0);
}
}st[MAXN << 2];
bool hs[MAXN];
int inline ll(const int &x)
{
return x << 1;
}
int inline rr(const int &x)
{
return x << 1 | 1;
}
void initST()
{
CLR(st, 0);
return ;
}
void buildST(int l, int r, int t)
{
st[t].left = l;
st[t].right = r;
st[t].col = 0;
if (l == r)
{
return ;
}
int mid = (l + r) >> 1;
buildST(l, mid, ll(t));
buildST(mid + 1, r, rr(t));
return ;
}
void updateST(int l, int r, int c, int t)
{
if (l <= st[t].left && st[t].right <= r)
{
st[t].col = c;
return ;
}
if (st[t].col)
{
st[ ll(t) ].col = st[ rr(t) ].col = st[t].col;
st[t].col = 0;
}
int mid = st[ ll(t) ].right;
if (r <= mid)
{
updateST(l, r, c, ll(t));
}
else if (l > mid)
{
updateST(l, r, c, rr(t));
}
else
{
updateST(l, mid, c, ll(t));
updateST(mid + 1, r, c, rr(t));
}
return ;
}
int query(int l, int r, int t)
{
if (st[t].col || st[t].left == st[t].right)
{
if (!hs[ st[t].col ] && st[t].col > 0)
{
hs[ st[t].col ] = true;
return 1;
}
return 0;
}
int mid = st[ ll(t) ].right;
if (r <= mid)
{
return query(l, r, ll(t));
}
else if (l > mid)
{
return query(l, r, rr(t));
}
return query(l, mid, ll(t)) + query(mid + 1, r, rr(t));
}
int bisearch(int x, int l, int r)
{
int mid = (l + r) >> 1, res = mid;
while (l <= r)
{
mid = (l + r) >> 1;
if (x == pp[mid])
{
res = mid;
break ;
}
if (x < pp[mid])
{
r = mid - 1;
}
else
{
l = mid + 1;
}
}
return res;
}
void ace()
{
for (scanf("%d", &test); test--; )
{
scanf("%d", &n);
for (int i = 0; i < n; ++i)
{
scanf("%d %d", &ln[i].x, &ln[i].y);
if (ln[i].x > ln[i].y)
{
swap(ln[i].x, ln[i].y);
}
ln[i].id = i + 1;
pp[i * 4 + 0] = ln[i].x - 1;
pp[i * 4 + 1] = ln[i].x;
pp[i * 4 + 2] = ln[i].y;
pp[i * 4 + 3] = ln[i].y + 1;
}
sort(pp, pp + 4 * n);
pcnt = unique_copy(pp, pp + 4 * n, pp) - pp;
initST();
buildST(0, pcnt - 1, 1);
for (int i = 0; i < n; ++i)
{
int l = bisearch(ln[i].x, 0, pcnt - 1);
int r = bisearch(ln[i].y, 0, pcnt - 1);
updateST(l, r, ln[i].id, 1);
}
CLR(hs, false);
printf("%d\n", query(0, pcnt - 1, 1));
}
return ;
}
int main()
{
ace();
return 0;
}
PKU 2528 线段树
最新推荐文章于 2021-02-27 12:08:25 发布