Time Limit: 2 Seconds Memory Limit: 65536 KB
Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.
Your task is counting the segments of different colors you can see at last.
Input
The first line of each data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored segments.
Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:
x1 x2 c
x1 and x2 indicate the left endpoint and right endpoint of the segment, c indicates the color of the segment.
All the numbers are in the range [0, 8000], and they are all integers.
Input may contain several data set, process to the end of file.
Output
Each line of the output should contain a color index that can be seen from the top, following the count of the segments of this color, they should be printed according to the color index.
If some color can't be seen, you shouldn't print it.
Print a blank line after every dataset.
Sample Input
5
0 4 4
0 3 1
3 4 2
0 2 2
0 2 3
4
0 1 1
3 4 1
1 3 2
1 3 1
6
0 1 0
1 2 1
2 3 1
1 2 0
2 3 0
1 2 1
Sample Output
1 1
2 1
3 1
1 1
0 2
1 1
题意有点坑,是n个染色的片段,不是染点,比如 0,3,是染的1,2,3片段。
然后就是建树建8000,因为是n个染色的片段,最后查询,直接单点查询。
维护的是片段的标号,不是点号,写的时候,要l+1,我写的l++,智障了。。。
代码:
//线段树(区间染色+统计间断区间数量)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 int now;
int col[maxn<<],num[maxn]; void pushdown(int rt)
{
if(col[rt]!=-){
col[rt<<]=col[rt<<|]=col[rt];
col[rt]=-;
}
} void build(int l,int r,int rt)
{
col[rt]=-;
if(l==r){
return ;
} int m=(l+r)>>;
build(lson);
build(rson);
} void update(int L,int R,int c,int l,int r,int rt)
{
if(r<L||l>R) return ;
if(L<=l&&r<=R){
col[rt]=c;
return ;
} pushdown(rt);
int m=(l+r)>>;
if(L<=m) update(L,R,c,lson);
if(R> m) update(L,R,c,rson);
} void query(int l,int r,int rt)
{
if(l==r){
if(col[rt]!=-&&col[rt]!=now) num[col[rt]]++;
now=col[rt];
return ;
} pushdown(rt);
int m=(l+r)>>;
query(lson);
query(rson);
} int main()
{
int n;
while(~scanf("%d",&n)){
memset(num,,sizeof(num));
build(,,);
for(int i=;i<=n;i++){
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
if(l<r){
l++;
update(l,r,k,,,);
}
}
now=-;
query(,,);
for(int i=;i<=;i++){
if(num[i]) cout<<i<<" "<<num[i]<<endl;
}
cout<<endl;
}
}