链接
吐槽
丧心病狂,居然卡常数
题解
某次查询的答案就等于:
∑ i = 1 + K [ p − 1 ( i ) < p − 1 ( i − K ) ] \sum_{i=1+K} [p^{-1}(i)<p^{-1}(i-K)] i=1+K∑[p−1(i)<p−1(i−K)]
对于 K > n K > \sqrt n K>n直接暴力
对 K ≤ n K \le \sqrt n K≤n的情况,单独维护
这题烦人之处就在于卡常
对 K ≤ n K \le \sqrt n K≤n的情况我做了一些优化才过
代码
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1int<<60)
#define eps 1e-8
#define maxn 100010
#define maxs 320
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef pair<int,int> pii;
int read(int x=0)
{
int c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
int ans[maxs+10], p[maxn], S=maxs, n, rev[maxn];
vector<int> v[maxn];
int con(int K, int pos)
{
if(pos<1 or pos>n)return 0;
if(pos-K>0)return rev[pos]<rev[pos-K];
return 0;
}
int force(int K)
{
int ret = 0;
for(int i=1+K;i<=n;i+=K)
ret += ( rev[i] < rev[i-K] );
return ret;
}
void Swap(int x, int y)
{
int i;
if(p[x]>p[y])swap(x,y);
for(auto K:v[p[x]])
{
ans[K] -= con(K,p[x]);
ans[K] -= con(K,p[x]+K);
}
for(auto K:v[p[y]])
{
if(p[x]+K!=p[y])ans[K] -= con(K,p[y]);
ans[K] -= con(K,p[y]+K);
}
swap(p[x],p[y]);
swap(rev[p[x]],rev[p[y]]);
for(auto K:v[p[y]])
{
ans[K] += con(K,p[y]);
ans[K] += con(K,p[y]+K);
}
for(auto K:v[p[x]])
{
if(p[y]+K!=p[x])ans[K] += con(K,p[x]);
ans[K] += con(K,p[x]+K);
}
}
int main()
{
int T=read(), i, K, type, x, y, m;
while(T--)
{
n = read();
rep(i,1,n)p[i]=read(), rev[p[i]]=i;
rep(K,1,S)ans[K]=force(K);
rep(i,1,n)v[i].clear();
rep(K,1,S)rep(i,1,n)if((i-1)%K==0)v[i].emb(K);
m = read();
while(m--)
{
type = read();
if(type==1)
{
x=read(), y=read();
Swap(x,y);
}
else
{
K = read();
if(K<=S)
{
printf("%d\n",ans[K]+1);
}
else
{
printf("%d\n",force(K)+1);
}
}
}
}
return 0;
}