J. Joy of Handcraft
题意:
给定 n 个灯泡的时间周期以及对应的亮度值,求 1 ~ m 的时刻,每一时刻的灯泡最大亮度
分析:
按时间轴建树,维护时间区间的亮度最大值
按亮度值递减排序,遍历灯泡时只 modify 为相同周期中亮度值最大的一个灯泡作为区间亮度最大值
区间修改,单点查询
实现:
#include <bits/stdc++.h>
using namespace std;
#define mst(x, y) memset(x, y, sizeof x)
#define endl '\n'
#define INF LONG_LONG_MAX
#define int long long
#define Lson u << 1, l, mid
#define Rson u << 1 | 1, mid + 1, r
#define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
const int N = 200010, MOD = 1e9 + 7;
const double EPS = 1e-6;
typedef pair<int, int> PII;
int T;
int n, m;
int Case = 1;
bool st[N];
struct Q
{
int t, x;
} q[N];
struct Node
{
int l, r;
int val;
int lz;
} tr[N << 2];
void pushup(Node u, Node l, Node r)
{
u.val = max(l.val, r.val);
}
void pushup(int u)
{
pushup(tr[u], tr[u << 1], tr[u << 1 | 1]);
}
void pushdown(int u)
{
tr[u << 1].lz = max(tr[u].lz, tr[u << 1].lz);
tr[u << 1 | 1].lz = max(tr[u].lz, tr[u << 1 | 1].lz);
tr[u << 1].val = max(tr[u << 1].val, tr[u].lz);
tr[u << 1 | 1].val = max(tr[u << 1 | 1].val, tr[u].lz);
tr[u].lz = 0;
}
void build(int u, int l, int r)
{
if (l == r)
tr[u] = {l, l, 0, 0};
else
{
tr[u] = {l, r};
int mid = l + r >> 1;
build(Lson), build(Rson);
pushup(u);
}
}
void modify(int u, int l, int r, int k)
{
if (tr[u].l >= l && tr[u].r <= r)
{
tr[u].lz = max(tr[u].lz, k);
tr[u].val = max(tr[u].val, tr[u].lz);
}
else
{
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid)
modify(u << 1, l, r, k);
if (r > mid)
modify(u << 1 | 1, l, r, k);
pushup(u);
}
}
int query(int u, int l, int r)
{
if (tr[u].l >= l && tr[u].r <= r)
return tr[u].val;
else
{
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);
}
}
void solve()
{
mst(st, false);
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> q[i].t >> q[i].x;
sort(q + 1, q + 1 + n, [&](Q a, Q b)
{ return a.x > b.x; });
build(1, 1, m);
for (int i = 1; i <= n; i++)
{
if (st[q[i].t])
continue;
st[q[i].t] = true;
int l = 1, r = q[i].t;
while (l <= m)
{
// int tmp = query(1, l, min(r, m));
// if (tmp < q[i].x)
modify(1, l, min(r, m), q[i].x);
l += 2 * q[i].t, r += 2 * q[i].t;
}
}
for (int i = 1; i <= m; i++)
{
cout << query(1, i, i) << " \n"[i == m];
}
}
signed main()
{
FAST;
T = 1;
cin >> T;
while (T--)
{
cout << "Case #" << Case++ << ": ";
solve();
}
return 0;
}
标签:int,Joy,tr,亮度,Handcraft,灯泡,define
From: https://www.cnblogs.com/Aidan347/p/17405377.html