银川G题链表,逆向思维

Posted 爷灬傲奈我何123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了银川G题链表,逆向思维相关的知识,希望对你有一定的参考价值。

不一定正确,先贴一下

//#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;
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;
//	cout<<x<<endl;
	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<<endl;
	
//	cout<<endl;
	cout<<res<<endl;
	lastans=x+res;
//	lastans%=n;
}
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
6
6
8
10
**/



以上是关于银川G题链表,逆向思维的主要内容,如果未能解决你的问题,请参考以下文章

Buy Tickets(线段树单点更新,逆向思维)

ICPC2021银川C

leetcode刷题链表-第1刷

●CodeForces 480E Parking Lot

2019icpc银川站 复现赛

Codeforces Round #632 (Div. 2)F. Kate and imperfection(逆向贪心因子)