//#pragma GCC target("avx")
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize("Ofast")
// created by myq
#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;
#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=0;
// int cost=res;
for(int i=2;i<=n;i++)
{
res+=(h[i]-h[i-1])*(h[i]-h[i-1]);
}
int cost=res;
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;
}
/**
5 4
1 2 3 4 5
1 2 3 4 5
5 1 2 3 4
6
6
8
10
**/
银川G题链表,逆向思维
最新推荐文章于 2021-11-18 13:32:33 发布