题解:P12978 流星雨 Meteor
XZhuRen
·
·
题解
卡卡卡卡卡卡卡常数。
不是很好想的题,一开始想歪了就很难做出来了。
看到取整点有效,先来个无脑根号分治,阈值 B,则问题转化为 t_i,v_i\le B 部分的问题。
一个个确定交点不可取,但是对于 t_i 一定只能枚举,现考虑令更大的线段代替小的,查询大的线段的答案时直接查询了前缀答案。
考虑线段的集合,交点较多,直接对其分块,块长为 b,则现在每个集合 \mathcal{O}(b^2) 个交点。
考虑集合不能直接分出,则现在最多有 \mathcal{O}(\frac{n}{b}+B) 个集合,直接维护交点,在相交后重排有关点即可,树状数组维护答案。
分块部分分析可得,但是容易被 v_i\le B 的无用条件误导。
关于时间复杂度:
查询为 $\mathcal{O}(\log n)$。
小的部分:
共 $\mathcal{O}(B+\frac{n}{b})$ 个块,每个块需要动态处理 $\mathcal{O}(b^2)$ 个交点。
处理交点时(离线下来)基数排序则做到 $\mathcal{O}(nb)$,其他排序多一个对数。
查询为 $\mathcal{O}(q(B+\frac{n}{b})\log b)$ 的,复杂度大头在上面。
基数排序做法复杂度为 $\mathcal{O}((n+q)\sqrt{n\log n})$,其他复杂度做法为 $\mathcal{O}((n+q)\sqrt{n}\log n)$,懒人实现了后面的算法,没有用基排。
---
很好的分块题。
某些实现可能需要卡常技巧,这里给一个卡常前的代码:
```cpp
#include<bits/stdc++.h>
using namespace std;
#define pri pair<int,ll1>
#define mkp make_pair
typedef long long ll1;
const int N=1e5+5;
const int M=3e5+5;
const ll1 inf=1e16+9;
const int MaxB=1005;
ll1 Ans[M];
int n,q;
vector<pri>brs[N];
int xs[N];ll1 sms[N];
struct Qrys{int l,r,id;};
vector<Qrys>aq[N];
struct lines{int v,t,y;ll1 w;bool operator <(const lines&o)const{return t<o.t;}}lns[N];
int tl=0,B;
void init(){
scanf("%d%d",&n,&q);
B=sqrt(4*n);
for(int i=1,y,v,t,w;i<=n;i++){
scanf("%d%d%d%d",&y,&v,&t,&w);
y=n-y+1;
if(max(t,v)>B)
for(int nwy=y+v,nwt=t;nwy<=n&&nwt<=n;nwt+=t,nwy+=v)
brs[nwt].emplace_back(mkp(nwy,w));
else lns[++tl]=((lines){v,t,y,w});
}
for(int t=1;t<=n;t++){
int tot=0;
sort(brs[t].begin(),brs[t].end());
for(auto [x,w]:brs[t]){
if(xs[tot]!=x){++tot;xs[tot]=x,sms[tot]=0;}
sms[tot]+=w;
}
for(int i=tot-1;i>0;i--)sms[i]+=sms[i+1];
brs[t].clear();for(int i=1;i<=tot;i++)brs[t].emplace_back(mkp(xs[i],sms[i]));
brs[t].emplace_back(mkp(n+1,0));
}
for(int id=1,T,l,r;id<=q;id++){
scanf("%d%d%d",&T,&l,&r);
l=n-l+1,r=n-r+1;swap(l,r);
Ans[id]+=(lower_bound(brs[T].begin(),brs[T].end(),mkp(l,0ll))->second)-(upper_bound(brs[T].begin(),brs[T].end(),mkp(r,inf))->second);
aq[T].emplace_back((Qrys){l,r,id});
}
}
int tot=0;
namespace BIT{
#define lowbit(x) (x&-x)
ll1 bit[N];
void clear(){for(int i=1;i<=tot;i++)bit[i]=0;}
void add(int x,ll1 y){for(;x<=tot;x+=lowbit(x))bit[x]+=y;}
ll1 qry(int x){ll1 res=0;for(;x>0;x-=lowbit(x))res+=bit[x];return res;}
#undef lowbit
}
namespace block{
int tb;
int L[N],R[N];
int bel[N];
int Ts[N];
void build(){
sort(lns+1,lns+1+tl);
L[tb=0]=-B;lns[0].t=Ts[0]-1;
for(int i=1;i<=tl;i++){
if(L[tb]+B<=i||lns[i].t!=Ts[tb])
L[++tb]=i,Ts[tb]=lns[i].t;
R[tb]=i,bel[i]=tb;
}
}
int pos[N],tmp[N],p[N];
vector<int>pnts[N];
int stk[N],top=0;
int poss[N];
bool cmp(int x,int y){return tmp[x]<tmp[y];}
bool cmpp(int x,int y){return pos[x]<pos[y];}
ll1 getans(int x,int y){
int res=0,l=1,r=tot;
while(l<=r){
int mid=(l+r)>>1;
if(lns[p[mid]].v*x+lns[p[mid]].y<=y)res=mid,l=mid+1;
else r=mid-1;
}
return BIT::qry(res);
}
void solve(int o){
int lim=n/Ts[o]+1;
tot=R[o]-L[o]+1;
for(int i=1;i<=lim;i++)pnts[i].clear();
for(int i=L[o];i<R[o];i++){
for(int j=i+1;j<=R[o];j++){
if(lns[i].v==lns[j].v)continue;
int tx=1+(1.0*(1.0*lns[j].y-lns[i].y)/(lns[i].v-lns[j].v));
if(tx<2||tx>lim)continue;
pnts[tx].emplace_back(i),pnts[tx].emplace_back(j);
}
}
for(int i=L[o];i<=R[o];i++){tmp[i]=lns[i].v+lns[i].y;p[i-L[o]+1]=i;}
sort(p+1,p+1+tot,cmp);//按照初始的纵坐标排序
BIT::clear();
for(int i=1;i<=tot;i++){
pos[p[i]]=i;
BIT::add(i,lns[p[i]].w);
}
for(int nwt=Ts[o],nw=1;nwt<=n;nw++,nwt+=Ts[o]){
{
sort(pnts[nw].begin(),pnts[nw].end(),cmpp);
stk[top=0]=0;
for(auto x:pnts[nw])if(stk[top]!=x){//去重
stk[++top]=x;poss[top]=pos[x];
tmp[x]=lns[x].v*nw+lns[x].y;//重新计算纵坐标
BIT::add(pos[x],-lns[x].w);
}
sort(stk+1,stk+1+top,cmp);
for(int i=1;i<=top;i++){
p[poss[i]]=stk[i];
pos[stk[i]]=poss[i];
BIT::add(pos[stk[i]],lns[stk[i]].w);
}
}
for(auto [lt,rt,id]:aq[nwt])
Ans[id]+=getans(nw,rt)-getans(nw,lt-1);
}
}
}
void work(){
block::build();
for(int o=1;o<=block::tb;o++)block::solve(o);
for(int id=1;id<=q;id++)printf("%lld\n",Ans[id]);
}
int main(){
init();
work();
return 0;
}
```