F - Count the Colors
#include<bits/stdc++.h>
using namespace std;
#define maxn 8888
struct node
{
int l,r;
int color;
} tree[maxn*4];
int color[maxn];
int temp,n,a,b,c,ans;
void build(int root,int l,int r)
{
tree[root].l=l;
tree[root].r=r;
tree[root].color=-1;
if(l+1==r)
return;
//存储的段不能出现点的请况
int mid =(l+r)/2;
build(root*2,l,mid);
build(root*2+1,mid,r);
//这里不同的是;两个都是mid,从而达到了题目中所需的把区间作为点的实现
}
void updata(int root,int l,int r,int ad)
{
if(l==r)return;
if(tree[root].color==ad)return ;
if(l<=tree[root].l&&tree[root].r<=r)
{
tree[root].color=ad;
return;
}
if(tree[root].color>=0)
{
tree[root*2].color=tree[root*2+1].color=tree[root].color;
tree[root].color=-2;
}
int mid=(tree[root].l+tree[root].r)/2;
if(mid>=r)
updata(root*2,l,r,ad);
else if(l>=mid)
updata(root*2+1,l,r,ad);
else
{
updata(root*2,l,mid,ad);
updata(root*2+1,mid,r,ad);
}
tree[root].color=-2;
}
void query(int root)
{
if(tree[root].color==-1)
{
temp=-1;
return ;
}
if(tree[root].color!=-2)
{
if(tree[root].color!=temp)
{
color[tree[root].color]++;
temp=tree[root].color;
}
return ;
}
if(tree[root].l+1!=tree[root].r)
{
query(root*2);
query(root*2+1);
}
}
int main()
{
while(~scanf("%d",&n))
{
build(1,0,8008);
ans=0;
while(n--)
{
scanf("%d%d%d",&a,&b,&c);
updata(1,a,b,c);
ans=max(ans,c);//记录一下颜色最大的范围
}
temp=-1;
memset(color,0,sizeof(color));
query(1);
for(int i=0; i<=ans; i++)
if(color[i])
printf("%d %d\n",i,color[i]);
printf("\n");
}
return 0;
}