跳到正文

个人记录

7/6-401-数据结构-单调栈进阶

wangsy2014约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年7月7日
收录
2026年7月7日
更新
2026年11月21日
篇幅
约 9 分钟

版本

  1. v1当前

7/6-401-数据结构-单调栈进阶-总结

T1:

签到,模板题

C++
#include<bits/stdc++.h>
using namespace std;
const int mx=5e5+5;
int n,a[mx],s[mx],l[mx];
int top;
inline void push(int k)
{
	while(top && a[k]<a[s[top]])
	{
		if(top==1)
		{
			l[s[top]]=-1;
		}
		else
		{
			l[s[top]]=s[top-1];
		}
		top--;
	}
	s[++top]=k;
}
int main()//signed
{
	//ios::sync_with_stdio(false);
	//cin.tie(nullptr);
	//cout.tie(nullptr);
	//freopen("","r",stdin);
	//freopen("","w",stdout);
	cin>>n;
    a[n+1]=-1;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
    for(int i=n;i>=1;i--)
    {
        push(i);
    }
	push(n+1);
	if(n<=1e3)
	{
		for(int i=1;i<=n;i++)
		{
			cout<<l[i]<<' ';
		}
	 } 
	else
	{
		int ans=0;
		for(int i=2;i<=n;i++)
		{
			ans^=l[i];
//			cout<<ans;
		}
		cout<<ans;
	}
	cout<<'\n';
	return 0;
}
/*



*/

T2:

这一题数据范围很大,要使用unsignedunsigned longlong longlong(如果害怕爆炸可以用__int128)。
然后就是输入加速:
提供三种:
1.printf和scanf
2.ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 3:

C++
inline int read()
{
	int ans=0,f=1;
	char x=' ';
	while(x<'0' || x>'9')
	{
		if(x=='-')
		{
			f=-1;
		}
		x=getchar();
	}
	while(x>='0' && x<='9')
	{
		ans=ans*10+x-'0';
		x=getchar();
	}
	return ans*f;
}
void write(int x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>9)
	{
		write(x/10);
	}
	putchar(x%10+'0');
}

这里因为使用__int128所以我们需要使用第三种(手写)。
此时我们得到O(n2)O(n^2)的暴力代码:

C++
#include<bits/stdc++.h>
#define int __int128
using namespace std;
const int mx=1e6+5;
inline int read()
{
	int ans=0,f=1;
	char x=' ';
	while(x<'0' || x>'9')
	{
		if(x=='-')
		{
			f=-1;
		}
		x=getchar();
	}
	while(x>='0' && x<='9')
	{
		ans=ans*10+x-'0';
		x=getchar();
	}
	return ans*f;
}
inline void write(int x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>9)
	{
		write(x/10);
	}
	putchar(x%10+'0');
}
int n,a[mx];
int stk[mx];
int ans;
int top;
signed main()
{
	//ios::sync_with_stdio(false);
	//cin.tie(nullptr);
	//cout.tie(nullptr);
	//freopen("","r",stdin);
	//freopen("","w",stdout);
	n=read();
	for(int i=1;i<=n;i++)
	{
		a[i]=read();
	}
    for(int i=1;i<=n;i++)
    {
        int ans=0,maxn=0;
        for(int j=i;j>=1;j--)
        {
            if(a[j]>maxn)
            {
                ans^=j;
            }
            maxn=max(maxn,a[j]);
        }
        write(ans);
        putchar('\n');
    }
	return 0;
}
/*



*/

我们想:如果新加入的元素比前面的大,那么我们就可以直接再异或一次进行消除,途中,使用单调栈维护。

C++
#include<bits/stdc++.h>
#define int __int128
using namespace std;
const int mx=1e6+5;
inline int read()
{
	int ans=0,f=1;
	char x=' ';
	while(x<'0' || x>'9')
	{
		if(x=='-')
		{
			f=-1;
		}
		x=getchar();
	}
	while(x>='0' && x<='9')
	{
		ans=ans*10+x-'0';
		x=getchar();
	}
	return ans*f;
}
inline void write(int x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>9)
	{
		write(x/10);
	}
	putchar(x%10+'0');
}
int n,a[mx];
int stk[mx];
int ans;
int top;
signed main()
{
	//ios::sync_with_stdio(false);
	//cin.tie(nullptr);
	//cout.tie(nullptr);
	//freopen("","r",stdin);
	//freopen("","w",stdout);
	n=read();
	for(int i=1;i<=n;i++)
	{
		a[i]=read();
		ans^=i;
		while(top && a[stk[top]]<a[i])
		{
			ans^=stk[top--];
		}
		stk[++top]=i;
		write(ans);
		putchar('\n');
	}
	return 0;
}
/*



*/

T3:

这一题我们可以将

∑i=1n∑j=in(max⁡i≤k≤jak−min⁡i≤k≤jak)\sum_{i=1}^{n} \sum_{j=i}^{n} (\max_{i\le k\le j} a_k-\min_{i\le k\le j} a_k)

拆成

∑i=1n∑j=inmax⁡i≤k≤jak−∑i=1n∑j=inmin⁡i≤k≤jak\sum_{i=1}^{n} \sum_{j=i}^{n} \max_{i\le k\le j} a_k-\sum_{i=1}^{n} \sum_{j=i}^{n}\min_{i\le k\le j} a_k

我们想:这样子我们可以使用另一道题:
U514906 ١١(❛ᴗ❛)【单调栈】-子数组最小值的思路:我们使用单调栈算贡献。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mx=3e5+5;
int n,a[mx];
int s[mx],top;
int l[mx],r[mx];
long long ans;
signed main()//signed
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	//freopen("","r",stdin);
	//freopen("","w",stdout);
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	a[n+1]=2e9;
	for(int i=1;i<=n+1;i++)
	{
		while(top && a[s[top]]<a[i])
		{
			l[s[top]]=s[top-1];
			r[s[top]]=i;
			top--;
		}
		s[++top]=i;
	}
	for(int i=1;i<=n;i++)
	{
		ans+=a[i]*(i-l[i])*(r[i]-i);
	}
	top=0;
	a[n+1]=0;
	for(int i=1;i<=n+1;i++)
	{
		while(top && a[s[top]]>a[i])
		{
			l[s[top]]=s[top-1];
			r[s[top]]=i;
			top--;
		}
		s[++top]=i;
	}
	for(int i=1;i<=n;i++)
	{
		ans-=a[i]*(i-l[i])*(r[i]-i);
	}
	cout<<ans<<'\n';
	return 0;
}
/*



*/
原文