区间染色 ZOJ1610

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define mem(a, b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define mk(a, b) make_pair(a,b)
#define p_queue priority_queue

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}
int n ,pre;
struct node
{
    int l , r ,lz ,type;
}tree[40000];
int color[40000];
void build(int i , int l , int r)
{
    tree[i].l = l , tree[i].r = r;
    tree[i].type = -1;
    if(l == r)
        return ;
    int mid = (l + r) / 2;
    build(i*2,l,mid);
    build(i*2+1,mid+1,r);
}
void push_down(int i)
{
    if(tree[i].lz >= 0)
    {
        tree[i * 2].type = tree[i].type;
        tree[i * 2 + 1].type = tree[i].type;
        tree[i * 2].lz = tree[i].lz;
        tree[i * 2 + 1].lz = tree[i].lz;
        tree[i].lz = -1;
    }
}
void add(int i , int  l , int r, int k)
{
    if(tree[i].l >= l && tree[i].r <= r) {
        tree[i].type = k;
        tree[i].lz = k;
        return ;
    }
    push_down(i);
    if(l <= tree[i*2].r)
        add(i*2,l,r,k);
    if(tree[i*2+1].l <= r)
        add(i*2+1,l,r,k);
    if(tree[i*2].type == tree[i*2+1].type)
        tree[i].type = tree[i*2].type;
    else
        tree[i].type = -2;
}
void search(int i)
{
    if(tree[i].type >= 0)
    {
        if(tree[i].type != pre)
        {
            color[tree[i].type]++;
        }
        pre = tree[i].type;
        return;
    }
    if(tree[i].type == -1)
    {
        pre = tree[i].type;
        return;
    }
    search(i*2);
    search(i*2+1);
}
int main(void)
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    while(cin>>n) {
        pre = -1;
        mem(color,0);
        build(1, 1, 8000);
        int l, r, c;
        for (int i = 1; i <= n; i++)
        {
            cin >> l >> r >> c;
            add(1, l + 1, r, c);
        }
        search(1);
        for(int i = 0 ; i <= 8000 ; i ++)
        {
            if(color[i] > 0)
            {
                cout<<i<<" "<<color[i]<<endl;
            }
        }
        cout<<endl;
    }
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值