传送门
题意
Chancy妹妹总是做一些充满快乐的手工艺品。这一次,他建立了一个电路,可以定期打开和关闭灯泡。
电路中有 n 个灯泡, 第 i 个灯泡拥有周期ti 和亮度xi。严格意义上, 第i个灯泡会在 ( 2 ∗ k ∗ t i + 1 ) s (2*k*ti+1)s (2∗k∗ti+1)s 至 ( 2 ∗ k ∗ t i + t i ) s (2*k*ti+ti)s (2∗k∗ti+ti)s的时间区间开启,并且它会在 ( 2 ∗ k ∗ t i + t i + 1 ) s (2*k*ti+ti+1)s (2∗k∗ti+ti+1)s 至 ( 2 ∗ k ∗ t i + 2 ∗ t i ) s (2*k*ti+2*ti)s (2∗k∗ti+2∗ti)s 的时间区间关闭, 其中 k = 0 , 1 , 2 , 3 , . . . . . . , k=0,1,2,3,......, k=0,1,2,3,......,当第i个灯泡开启的时候它的亮度为xi,其余时刻它的亮度为0。
Chancy妹妹想知道,从第1秒到第m秒,每一秒所有灯泡的最大亮度是多少。
分析
涉及到区间修改
m
a
x
max
max,考虑线段树的写法
我们把所有灯泡的亮度从大到小排序,然后如果遇到相同的周期可以跳过
复杂度可以证明为调和级数
1
/
m
+
2
/
m
+
.
.
.
+
m
/
m
1/m + 2 / m + ... + m / m
1/m+2/m+...+m/m
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
int n,m;
bool st[N];
struct Node{
int l,r;
int mx,add;
}tr[N * 4];
struct node{
int t,x;
}p[N];
int ans[N];
bool cmp(node A,node B){
return A.x > B.x;
}
void push(int u){
tr[u].mx = max(tr[u << 1].mx,tr[u << 1 ].mx);
}
void down(int u){
if(tr[u].add){
int &k = tr[u].add;
tr[u << 1].mx = max(tr[u << 1].mx,k);
tr[u << 1].add = max(tr[u << 1].add,k);
tr[u << 1 | 1].mx = max(tr[u << 1 | 1].mx,k);
tr[u << 1 | 1].add = max(tr[u << 1 | 1].add,k);
k = 0;
}
}
void build(int u,int l,int r){
tr[u] = {l,r};
if(l == r){
tr[u].mx = 0,tr[u].add = 0;
return;
}
int mid = (l + r) >> 1;
build(u << 1,l,mid),build(u << 1 | 1,mid + 1,r);
push(u);
}
void modify(int u,int l,int r,int x){
if(tr[u].l >= l && tr[u].r <= r){
tr[u].mx = max(tr[u].mx,x);
tr[u].add = max(tr[u].add,x);
return;
}
down(u);
int mid = (tr[u].l + tr[u].r) >> 1;
if(l <= mid) modify(u << 1,l,r,x);
if(r > mid) modify(u << 1 | 1,l,r,x);
push(u);
}
void query(int u){
if(tr[u].l == tr[u].r) {
ans[tr[u].l] = tr[u].mx;
return;
}
down(u);
query(u << 1),query(u << 1 | 1);
}
int main() {
int T;
read(T);
for(int ppppp = 1;ppppp <= T;ppppp++){
memset(st,0,sizeof st);
printf("Case #%d: ",ppppp);
read(n),read(m);
build(1,1,m);
for(int i = 1;i <= n;i++) read(p[i].t),read(p[i].x);
sort(p + 1,p + 1 + n,cmp);
for(int i = 1;i <= n;i++){
int t,x;
t = p[i].t,x = p[i].x;
if(st[t]) continue;
st[t] = true;
int k = 0;
while(k * t + 1 <= m){
int st = k * t + 1,ed = k * t + t;
k += 2;
modify(1,st,min(ed,m),x);
}
}
query(1);
for(int i = 1;i <= m;i++) {
if(i == m) printf("%d\n",ans[i]);
else printf("%d ",ans[i]);
}
}
return 0;
}