Joy of Handcraft(线段树)

题意

n n n个灯泡,每个灯泡有两个参数 t i t_i ti x i x_i xi,分别指运行周期和亮度。第 i i i个灯泡在 2 k t i + 1 2kt_i+1 2kti+1秒到 2 k t i + t i 2kt_i+t_i 2kti+ti秒是亮的,在 2 k t i + t i + 1 2kt_i+t_i+1 2kti+ti+1秒到 2 k t i + 2 t i 2kt_i+2t_i 2kti+2ti秒是灭的。问第 1 1 1秒到第 m m m秒,每一秒亮度最大值是多少。

数据范围

1 ≤ T ≤ 100 1 \leq T \leq 100 1T100
1 ≤ n , m ≤ 1 0 5 1 \leq n,m \leq 10^5 1n,m105
1 ≤ t i , x i ≤ 1 0 5 1 \leq t_i,x_i \leq 10^5 1ti,xi105

思路

其实这道题的关键是对区间数量的估计。这里有一个性质,如果两个灯泡的 t i t_i ti相等,那么我们就可以忽略掉 x i x_i xi较小的那个灯泡。因此所有的区间数量就是 ∑ i = 1 n m t i = m l o g n \sum_{i=1}^n \frac{m}{t_i} = mlogn i=1ntim=mlogn。所以,我们可以预处理出这些区间。
然后就是个区间染色问题,用线段树维护即可。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef pair<int,int> pii;

const int N = 100010;

int n, m;
pii bulbs[N];
bool is_ap[N];
struct Line
{
    int st, ed, w;
}line[20*N];

struct Node
{
    int l, r;
    int color, flag;
}tr[N * 4];

void pushup(int u)
{
    if(tr[u<<1].color==tr[u<<1|1].color) tr[u].color = tr[u<<1].color;
    else tr[u].color = 0;
}

void pushdown(int u)
{
    auto &root = tr[u], &left = tr[u<<1], &right = tr[u<<1|1];
    if(root.flag){
        left.flag = root.flag, left.color = root.flag;
        right.flag = root.flag, right.color = root.flag;
        root.flag = 0;
    }
}

void build(int u,int l,int r)
{
    tr[u] = {l,r,0,0};
    if(l==r) return;
    int mid = l + r >> 1;
    build(u<<1,l,mid), build(u<<1|1,mid+1,r);
    pushup(u);
}

void modify(int u,int l,int r,int c)
{
    if(tr[u].l>=l&&tr[u].r<=r){
        tr[u].flag = c;
        tr[u].color = c;
        return;
    }
    pushdown(u);
    int mid = tr[u].l + tr[u].r >> 1;
    if(l<=mid) modify(u<<1,l,r,c);
    if(r>mid) modify(u<<1|1,l,r,c);
    pushup(u);
}

int query(int u,int l,int r)
{
    if(tr[u].l>=l&&tr[u].r<=r) return tr[u].color;
    pushdown(u);
    int mid = tr[u].l + tr[u].r >> 1;
    if(l<=mid) return query(u<<1,l,r);
    else return query(u<<1|1,l,r);
}

int main()
{
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++){
        scanf("%d%d",&n,&m);
        memset(is_ap,0,sizeof(is_ap));
        int cnt = 1;
        for(int i=1;i<=n;i++) scanf("%d%d",&bulbs[i].second,&bulbs[i].first);
        sort(bulbs+1,bulbs+n+1);
        for(int i=n;i>=1;i--){
            int x = bulbs[i].first, t = bulbs[i].second;
            if(is_ap[t]) continue;
            //for(int k=2;k<t/k;k++) is_ap[t/k] = true;
            is_ap[t] = true;
            for(int k=0;;k++){
                int st = 2*k*t+1, ed = 2*k*t+t;
                if(st>m) break;
                if(ed>m) ed = m;
                line[cnt++] = {st,ed,x};
            }
        }
        build(1,1,m);
        printf("Case #%d:",cas);
        for(int i=cnt-1;i>0;i--){
            int st = line[i].st, ed = line[i].ed, w = line[i].w;
            modify(1,st,ed,w);
        }
        for(int i=1;i<=m;i++){
            int ans = query(1,i,i);
            printf(" %d",ans);
        }
        printf("\n");
        //for(int i=1;i<=m*4;i++) tr[i] = {0,0,0,0};
    }
    return 0;
}
/*
3
2 3
1 1
2 2
2 5
1 2
2 3
3 3
1 1
1 2
1 3
*/
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值