这道题写哭了快,为什么要放在线段树简单题里,我觉得一点也不简单啊,起码让我做个求逆序对的线段树再来这个 啊,一上来就这道题,给我做懵逼了,别人的题解也看不懂,上午做了道求逆序对的终于感觉开了窍。
思路:离散化应该不用我说了,不会的看这一篇(虽然我还是因为离散化写错莫名re),然后就是区间覆盖值,单点取值,常规操作了。
#include<bits/stdc++.h>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define ll long long
#define ld long double
#define mem(ar,num) memset(ar,num,sizeof(ar))
#define me(ar) memset(ar,0,sizeof(ar))
#define lowbit(x) (x&(-x))
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define lcm(a,b) ((a)*(b)/(__gcd((a),(b))))
#define Max 100010
#define mod 1000000007
using namespace std;
ll n, tree[Max << 2], lz[Max << 2], a[Max << 2], b[Max << 2], lisan[Max << 2], li = 1;
void pushup(ll rt) {
tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
}
void pushdown(ll rt, ll ln, ll rn) {
if(lz[rt]) {
lz[rt << 1] = lz[rt << 1 | 1] = lz[rt];
tree[rt << 1] = lz[rt] * ln;
tree[rt << 1 | 1] = lz[rt] * rn;
lz[rt] = 0;
}
}
void update(ll L, ll R, ll F, ll l, ll r, ll rt) {
if(L <= l && r <= R) {
tree[rt] = F * (r - l + 1);
lz[rt] = F;
return;
}
ll m = (l + r) >> 1;
pushdown(rt, m - l + 1, r - m);
if(L <= m)
update(L, R, F, l, m, rt << 1);
if(R > m)
update(L, R, F, m + 1, r, rt << 1 | 1);
pushup(rt);
}
ll query(ll L, ll R, ll l, ll r, ll rt) {
if(L <= l && r <= R) {
return tree[rt];
}
ll m = (r + l) >> 1;
ll ans = 0;
pushdown(rt, m - l + 1, r - m);
if(L <= m)
ans += query(L, R, l, m, rt << 1);
if(R > m)
ans += query(L, R, m + 1, r, rt << 1 | 1);
return ans;
}
int main() {
IOS
ll t;
cin >> t;
while(t--) {
cin >> n;
set<ll> s;
s.clear();
me(tree), me(lz), me(a), me(b), me(lisan), li = 1;
for(ll i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
lisan[li++] = a[i], lisan[li++] = b[i];
}
li--;
sort(lisan + 1, lisan + li + 1);
ll num = unique(lisan + 1, lisan + li + 1) - lisan - 1;
for(ll i = 1; i <= n; i++) {
a[i] = lower_bound(lisan + 1, lisan + num + 1, a[i]) - lisan;//把num写错成li,re,难受
b[i] = lower_bound(lisan + 1, lisan + num + 1, b[i]) - lisan;
}
for(ll i = 1; i <= n; i++) {
update(a[i], b[i], i, 1, num, 1);
}
for(ll i = 1; i <= num; i++) {
ll q = query(i, i, 1, num, 1);
if(q != 0)
s.insert(q);
}
cout << s.size() << endl;
}
return 0;
}