Question:
There are N posters, each of which has a length range (a, B). They are attached to the wall in order.
Finally, I can see several posters.
Ideas:
The thought is the line segment tree, dyeing each interval, and finally finding the total number of colors.
The data size was not viewed during the first write operation. MLE .. Take a closer look. The length of the poster is 1 QW.
Then a discretization code is written, which is 300 MS +.
Let's look at the discretization of others .. God too much .. 60 MS ..
Optimized discretization
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <algorithm>
# Include <string>
# Include <cmath>
# Include <cstring>
# Include <queue>
# Include <set>
# Include <vector>
# Include <stack>
# Include <map>
# Include <iomanip>
# Define PI acos (-1.0)
# Deprecision Max 20005
# Define inf 1 <28
# Define LL (x) (x <1)
# Define RR (x) (x <1 | 1)
Using namespace std;
Struct kdq
{
Int l, r, flag;
} Tree [Max * 4];
Struct kdq1
{
Int num, id;
} Poster [Max];
Int aa [Max] [2];
Void build_tree (int l, int r, int u)
{
Tree [u]. l = l;
Tree [u]. r = r;
Tree [u]. flag = 0;
If (l = r) return;
Int mid = (l + r)> 1;
Build_tree (l, mid, LL (u ));
Build_tree (mid + 1, r, RR (u ));
}
Void update (int l, int r, int u, int I)
{
If (l> tree [u]. r | r <tree [u]. l) return;
If (l = tree [u]. l & r = tree [u]. r)
{
Tree [u]. flag = I;
Return;
}
If (tree [u]. flag> 0 & tree [u]. flag! = I)
{
Tree [LL (u)]. flag = tree [u]. flag;
Tree [RR (u)]. flag = tree [u]. flag;
Tree [u]. flag = 0;
}
Int mid = (tree [u]. l + tree [u]. r)> 1;
If (r <= mid)
Update (l, r, LL (u), I );
Else if (l> mid)
Update (l, r, RR (u), I );
Else
{
Update (l, mid, LL (u), I );
Update (mid + 1, r, RR (u), I );
}
If (tree [LL (u)]. flag = tree [RR (u)]. flag)
Tree [u]. flag = tree [LL (u)]. flag;
Else
Tree [u]. flag = 0;
}
Int ans;
Bool visit1 [20005];
Void query (int l, int r, int u)
{
If (tree [u]. flag &&! Visit1 [tree [u]. flag])
Return;
If (tree [u]. flag)
{
Ans + = visit1 [tree [u]. flag];
Visit1 [tree [u]. flag] = 0;
Return;
}
Int mid = (l + r)> 1;
Query (l, mid, LL (u ));
Query (mid + 1, r, RR (u ));
}
Bool cmp (kdq1 & a, kdq1 & B)
{
Return a. num <B. num;
}
Int main ()
{
Int I, j, k, l, n, m, T;
Scanf ("% d", & T );
Int a, B;
While (T --)
{
Memset (visit1, 1, sizeof (visit1 ));
Scanf ("% d", & n );
For (I = 0; I <n; I ++)
{
Scanf ("% d", & aa [I] [0], & aa [I] [1]);
Poster [2 * I]. num = aa [I] [0];
Poster [2 * I]. id =-(I + 1 );
Poster [2 * I + 1]. num = aa [I] [1];
Poster [2 * I + 1]. id = I + 1;
}
Sort (poster, poster + 2 * n, cmp );
Int temp = poster [0]. num;
Int tp = 1;
For (I = 0; I <2 * n; I ++)
{
If (temp! = Poster [I]. num)
{
Tp ++;
Temp = poster [I]. num;
}
If (poster [I]. id <0)
{
Aa [-poster [I]. id-1] [0] = tp;
}
Else
{
Aa [poster [I]. id-1] [1] = tp;
}
}
Build_tree (1, tp, 1 );
For (I = 0; I <n; I ++)
Update (aa [I] [0], aa [I] [1], 1, I + 1 );
Ans = 0;
Query (1, tp, 1 );
Printf ("% d \ n", ans );
}
Return 0;
}
Discretization of the first write
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <algorithm>
# Include <string>
# Include <cmath>
# Include <cstring>
# Include <queue>
# Include <set>
# Include <vector>
# Include <stack>
# Include <map>
# Include <iomanip>
# Define PI acos (-1.0)
# Deprecision Max 20005
# Define inf 1 <28
# Define LL (x) (x <1)
# Define RR (x) (x <1 | 1)
Using namespace std;
Struct kdq
{
Int l, r, flag;
} Tree [Max * 4];
Int aa [Max], bb [Max];
Void build_tree (int l, int r, int u)
{
Tree [u]. l = l;
Tree [u]. r = r;
Tree [u]. flag = 0;
If (l = r) return;
Int mid = (l + r)> 1;
Build_tree (l, mid, LL (u ));
Build_tree (mid + 1, r, RR (u ));
}
Void update (int l, int r, int u, int I)
{
If (l> tree [u]. r | r <tree [u]. l) return;
If (l = tree [u]. l & r = tree [u]. r)
{
Tree [u]. flag = I;
Return;
}
If (tree [u]. flag> 0 & tree [u]. flag! = I)
{
Tree [LL (u)]. flag = tree [u]. flag;
Tree [RR (u)]. flag = tree [u]. flag;
Tree [u]. flag = 0;
}
Int mid = (tree [u]. l + tree [u]. r)> 1;
If (r <= mid)
Update (l, r, LL (u), I );
Else if (l> mid)
Update (l, r, RR (u), I );
Else
{
Update (l, mid, LL (u), I );
Update (mid + 1, r, RR (u), I );
}
If (tree [LL (u)]. flag = tree [RR (u)]. flag)
Tree [u]. flag = tree [LL (u)]. flag;
Else
Tree [u]. flag = 0;
}
Int ans;
Int yinshe [20005];
Int visit [10000005];
Bool visit1 [20005];
Void query (int l, int r, int u)
{
If (tree [u]. flag &&! Visit1 [tree [u]. flag])
Return;
If (tree [u]. flag)
{
Ans + = visit1 [tree [u]. flag];
Visit1 [tree [u]. flag] = 0;
Return;
}
Int mid = (l + r)> 1;
Query (l, mid, LL (u ));
Query (mid + 1, r, RR (u ));
}
Int main ()
{
Int I, j, k, l, n, m, T;
Scanf ("% d", & T );
Int a, B;
While (T --)
{
Memset (visit1, 1, sizeof (visit1 ));
Memset (visit, 0, sizeof (visit ));
Memset (yinshe, 0, sizeof (yinshe ));
Scanf ("% d", & n );
Int num = 1;
For (I = 1; I <= n; I ++)
{
Scanf ("% d", & aa [I], & bb [I]);
If (! Visit [aa [I])
{
Yinshe [num] = aa [I];
Visit [aa [I] = 1;
Num ++;
}
If (! Visit [bb [I])
{
Yinshe [num] = bb [I];
Visit [bb [I] = 1;
Num ++;
}
}
Sort (yinshe + 1, yinshe + num );
For (I = 1; I <num; I ++)
Visit [yinshe [I] = I;
Build_tree (1, num-1, 1 );
For (I = 1; I <= n; I ++)
Update (visit [aa [I], visit [bb [I], 1, I );
Ans = 0;
Query (1, num-1, 1 );
Printf ("% d \ n", ans );
}
Return 0;
}