CF1946F Nobody is needed
赛时想出来但是没写出来。
题意简述
定义一个合法序列
- 对于
, 被 整除。
初始给定排列
分析
如果我们知道的原本的序列,考虑 dp。设
现在有了区间查询。我们先用个数据结构维护 dp 值,下文出于某些原因,
考虑离线,从后往前枚举左端点
考虑如何去除掉包含
现在考虑如何在左移左端点
那么这道题就做完了,我们发现该数据结构需要支持单点加和后缀求和,树状数组即可。
时间复杂度大体是 inline
,就能过了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P__ puts("")
#define PU puts("--------------------")
#define popc __builtin_popcount
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) (x&-x)
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mem(x,y) memset(x,y,sizeof x)
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
typedef long long i64;
using pii=pair<int,int>;
bool greating(int x,int y){return x>y;}
bool greatingll(long long x,long long y){return x>y;}
inline int rd(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}return x*f;
}
inline void write(i64 x,char ch='\0'){
if(x<0){x=-x;putchar('-');}
int y=0;char z[40];
while(x||!y){z[y++]=x%10+48;x/=10;}
while(y--)putchar(z[y]);if(ch!='\0')putchar(ch);
}
bool Mbg;
const int maxn=1e6+5,maxm=4e5+5,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,Q,a[maxn],pos[maxn];
i64 ans[maxn];
vector<pii>q[maxn];
vector<int>v[maxn];
i64 f[maxn],g[maxn];
struct BIT{
i64 c[maxn];
void init(){rep(i,1,n)c[i]=0;}
inline void add(int x,i64 y){x=n-x+1;while(x<=n)c[x]+=y,x+=lowbit(x);}
inline i64 qry(int x){i64 res=0;x=n-x+1;while(x)res+=c[x],x-=lowbit(x);return res;}
}A,B;
void init(){
A.init(),B.init();
rep(i,1,n)q[i].clear(),v[i].clear();
}
void solve_the_problem(){
cin>>n>>Q,init();
rep(i,1,n)cin>>a[i],pos[a[i]]=i;
rep(i,1,Q){
int l,r;cin>>l>>r;
q[l].pb(mp(r,i));
}
per(l,n,1){
int x=a[l];
f[l]=1;
reprange(y,x*2,n,x)if(pos[y]>l){
f[l]+=f[pos[y]],g[pos[y]]=1;
for(int u:v[pos[y]])if(a[u]%a[l]==0)g[pos[y]]+=g[u];
B.add(pos[y],g[pos[y]]),v[pos[y]].pb(l);
}
A.add(l,f[l]),B.add(l,1);
for(pii i:q[l]){
int r=i.fi,id=i.se;
ans[id]=A.qry(l)-B.qry(r+1);
}
}
rep(i,1,Q)cout<<ans[i]<<' ';cout<<'\n';
}
bool Med;
signed main(){
// freopen(".in","r",stdin);freopen(".out","w",stdout);
// fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
IOS;ITIE;OTIE;int _;cin>>_;while(_--)solve_the_problem();
}
/*
*/
分类:
数据结构 / 线段树,树状数组
, 动态规划
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现