不一定正确,先贴一下
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<sstream>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long ll;
#define x first
#define y second
typedef pair<int,int> pii;
const int N = 400010;
const int mod=998244353;
inline int read()
{
int res=0;
int f=1;
char c=getchar();
while(c>'9' ||c<'0')
{
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
res=(res<<3)+(res<<1)+c-'0';
}
return res;
}
#define int long long
int h[N];
int ne[N];
int p[N];
int pre[N];
int n,q;
int lastans;
void solve(int x)
{
int from=1+x;
if(from==n+1) from=1;
for(int i=1;i<=n-1;i++)
{
ne[i]=i+1;
pre[i+1]=i;
}
int res=n-1;
int cost=n-1;
for(int i=from-1;i>=1;i--)
{
int x=p[i];
int x1=pre[x];
int x2=ne[x];
if(x1)
{
cost-=(h[x]-h[x1])*(h[x]-h[x1]);
}
if(x2)
{
cost-=(h[x]-h[x2])*(h[x]-h[x2]);
}
if(x1&&x2)
cost+=(h[x2]-h[x1])*(h[x2]-h[x1]);
ne[x1]=x2;
pre[x2]=x1;
res+=cost;
}
for(int i=n;i>=from;i--)
{
int x=p[i];
int x1=pre[x];
int x2=ne[x];
if(x1)
{
cost-=(h[x]-h[x1])*(h[x]-h[x1]);
}
if(x2)
{
cost-=(h[x]-h[x2])*(h[x]-h[x2]);
}
if(x1&&x2)
cost+=(h[x2]-h[x1])*(h[x2]-h[x1]);
ne[x1]=x2;
pre[x2]=x1;
res+=cost;
}
cout<<res<<endl;
lastans=x+res;
}
signed main()
{
cin>>n>>q;
for(int i=1;i<=n;i++) cin>>h[i];
for(int i=1;i<=n;i++) cin>>p[i];
lastans=0;
solve(lastans);
while(q--)
{
int x;
cin>>x;
x+=lastans;
x%=n;
solve(x);
}
return 0;
}
|