CF1946F Nobody is needed
Believe_in_dreams · · 题解
来一发神经人类智慧做法。
首先对询问离线。
注意到满足条件的集合总数不会太多,考虑枚举。
扫描
查询返回 ask(q[i].r)-ask(q[i].l-1) 即可。
讲一下如何加入点
事实上是因为有冗余,比如集合枚举到
所以我们搞一个优化,就是用 st 数组标记是否在队列中,实现合并操作,从而优化。
还有,可以预处理
事实上我直接用的
讲一下清空树状数组。因为直接清空时间复杂度
再来说下 long long 的问题。首先不开 long long 肯定是不行的,可以构造数据卡掉。又因为这个代码本身速度就很极限,所以只能部分开 long long。
考时真能过我都意想不到。
赛事代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int read(){
int ans=0,j=1;
char c=getchar();
while(c>'9' or c<'0'){
if(c=='-')
j=-1;
c=getchar();
}
while(c>='0' and c<='9')
ans=(ans<<1)+(ans<<3)+(c^48),
c=getchar();
return ans*j;
}
string READ(){
string ret="";
char c=getchar();
while(c==' ' or c=='\n')
c=getchar();
while(c!=' ' and c!='\n'){
ret+=c,
c=getchar();
if(c==EOF)
return ret;
}
return ret;
}
void write(ll x){
if(x<0)
x=-x,
putchar('-');
if(x>9)
write(x/10);
putchar(48+x%10);
}
const int N=1e6+5;
int T,n,m,a[N],b[N];
bool st[N];
int cnt[N];
struct node{
int x;
bool operator<(const node &p)const{
return a[x]<a[p.x];
}
};
priority_queue<node> q;
ll t[N];int lst[N],tim;
inline void add(int p,int x){
while(p<=n){
if(lst[p]!=tim)lst[p]=tim,t[p]=0;
t[p]+=x,p+=-p&p;
}
}
inline ll ask(int p){
ll x=0;
while(p){
if(lst[p]!=tim)lst[p]=tim,t[p]=0;
x+=t[p],p-=-p&p;
}
return x;
}
struct quo{//query
int id,l,r;ll ans;
bool operator<(const quo &p)const{
return r<p.r;
}
}g[N];
bool cmp(quo a,quo b){
return a.id<b.id;
}
vector<int> v[N];
signed main(){
for(int i=1;i<=1e6;++i)
for(int j=1;j*j<=i;++j)
if(i%j==0){
v[i].push_back(j);
if(j!=1 and j*j!=i)
v[i].push_back(i/j);
}
T=read();
while(T--){
++tim;
n=read(),m=read();
for(int i=1;i<=n;++i)
a[i]=read(),b[a[i]]=i;
for(int i=1;i<=m;++i)
g[i]={i,read(),read(),0};
sort(g+1,g+m+1);
int tmp=1;
for(int i=1;i<=n;++i){//以i为结尾
cnt[i]=1;
q.push({i});
st[i]=1;
while(q.size()){
int x=q.top().x;q.pop();
add(x,cnt[x]),st[x]=0;
for(int j:v[a[x]])
if(b[j]<x){
cnt[b[j]]+=cnt[x];
if(!st[b[j]])
st[b[j]]=1,q.push({b[j]});
}
}
cnt[i]=0;
for(int j:v[a[i]])cnt[b[j]]=0;
while(tmp<=m and g[tmp].r<=i)
g[tmp].ans=ask(g[tmp].r)-ask(g[tmp].l-1),++tmp;
}
sort(g+1,g+m+1,cmp);
for(int i=1;i<=m;++i)
write(g[i].ans),putchar(' ');
putchar(10);
}
return 0;
}