题意:
就是给你n个灯泡,一共m个时间。每个灯泡有两个参数t和x,灯泡会在[2kt+1,2kt+t]亮,也就是先亮t秒再关t秒再亮t秒…。然后亮的时候亮度为x。现在问你从1到m每个时间点亮的灯泡中亮度最大的亮度是多少,也就是最大的x是多少。
思考:
- 当时看到这题,思考了下感觉应该不难,一看这种要对每个点都求出来答案的,肯定是预处理好。然后我就想了对于每个灯泡暴力去枚举他所有的区间然后线段树更新,这样复杂度也是n*m的啊。不过完全可以把相同的t中取那个x最大的,因为相同的t对区间的影响范围都一样,所以取x最大就可以了,这样就可以和调和级数应用在一起,每个i只会被枚举到一次。
- 不过当时我没想起来,队友一提就醒悟了,还是对调和级数不太敏感,对于调和级数就是所有的n/i(1<=i<=n)之和。也就是对于i,我可以枚举1倍的i,2倍的,3倍的等等等等直到i*k>n。也就是只要涉及到枚举每个i的多少多少倍去操作就是可以的。就和之前的江苏省赛一样:2022江苏省赛-Jump and Treasure。
- 这里其实每次只要枚举i的偶数倍,比真正的调和级数还小一些。然后这里枚举i的倍数,然后把这个区间范围更新x。
- 注意行末空格,不管怎么样,以后正式赛,看题目给的output是什么输出格式。就按什么格式输出。
代码:
#include<bits/stdc++.h>
#define int long long
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int N = 1e5+10;
int T,n,m;
int va[N];
struct seg_max{
#define node_l node<<1
#define node_r node<<1|1
struct node{
int L,R;
int laz,maxn;
}t[4*N];
void pushup(int node)
{
t[node].maxn = max(t[node_l].maxn,t[node_r].maxn);
}
void pushdown(int node)
{
int laz = t[node].laz;
if(laz>0)
{
t[node_l].laz = max(t[node_l].laz,laz);
t[node_l].maxn = max(t[node_l].maxn,laz);
t[node_r].laz = max(t[node_r].laz,laz);
t[node_r].maxn = max(t[node_r].maxn,laz);
t[node].laz = 0;
}
}
void build(int node,int l,int r)
{
t[node].L = l,t[node].R = r;
t[node].laz = t[node].maxn = 0;
if(l==r)
{
return ;
}
int mid = (l+r)>>1;
build(node_l,l,mid);build(node_r,mid+1,r);
pushup(node);
}
void update(int node,int l,int r,int value)
{
if(t[node].L>=l&&t[node].R<=r)
{
t[node].maxn = max(t[node].maxn,value);
t[node].laz = max(t[node].laz,value);
return ;
}
pushdown(node);
int mid = (t[node].L+t[node].R)>>1;
if(r<=mid) update(node_l,l,r,value);
else if(l>mid) update(node_r,l,r,value);
else update(node_l,l,mid,value),update(node_r,mid+1,r,value);
pushup(node);
}
int query(int node,int x)
{
if(t[node].L==x&&t[node].R==x) return t[node].maxn;
pushdown(node);
int mid = (t[node].L+t[node].R)>>1;
if(x<=mid) return query(node_l,x);
else return query(node_r,x);
pushup(node);
}
}t_max;
void init()
{
t_max.build(1,1,m);
for(int i=1;i<=1e5;i++) va[i] = 0;
}
signed main()
{
IOS;
cin>>T;
for(int cs=1;cs<=T;cs++)
{
cin>>n>>m;
init();
int maxn = 0;
for(int i=1;i<=n;i++)
{
int a,b;
cin>>a>>b;
va[a] = max(va[a],b);
maxn = max(maxn,a);
}
for(int i=1;i<=maxn;i++)
{
if(va[i]==0) continue;
for(int j=0;j*i+1<=m;j+=2)
{
int l = j*i+1,r = min((j+1)*i,m);
t_max.update(1,l,r,va[i]);
}
}
cout<<"Case #"<<cs<<": ";
for(int i=1;i<=m;i++)
{
cout<<t_max.query(1,i);
if(i!=m) cout<<" ";
}
if(cs!=T) cout<<"\n";
}
return 0;
}
总结:
多多思考,联想联想相关的知识。