跳到正文

个人记录

石门集训笔记

碳酸钙CaCO3约 32 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 32 分钟

版本

  1. v2当前
  2. v1

石门集训笔记

目录

  1. Splay树
  2. CDQ分治
  3. 二分图
  4. Hall定理
  5. 网络流

7.13 Splay树

简介

一种平衡二叉查找树,它通过 Splay/伸展操作 不断将某个节点旋转到根节点,使得整棵树仍然满足二叉查找树的性质,能够在均摊 O(log⁡N)O(\log N) 时间内完成插入,查找和删除操作,并且保持平衡而不至于退化为链。

满足性质:左子树任意节点的值 << 根节点的值 << 右子树任意节点的值。

操作

  1. 基本操作
  • maintain(x) : 再改变节点位置后,更新 xx 的子树大小,用于维护 xx。
  • get(x) : 判断节点 xx 是父亲节点的左儿子还是右儿子。
  • clear(x) : 销毁节点 xx。
  1. 旋转操作(重点内容)

为了使 Splay 保持平衡而进行旋转操作,旋转的本质是将某个节点上移一个位置。

在 Splay 中旋转分为两种:左旋和右旋。

代码实现:

C++
void rotate(int x){
		int y=fa[x],z=fa[y],chk=get(x);
		ch[y][chk]=ch[x][chk^1];
		if(ch[x][chk^1])fa[ch[x][chk^1]]=y;
		ch[x][chk^1]=y;
		fa[y]=x;fa[x]=z;
		if(z)ch[z][y==ch[z][1]]=x;
		maintain(y);
		maintain(x);
	}
  1. Splay操作

将节点 xx 通过左旋(zig)或右旋(zag)不断提升x节点,直到 xx 节点为根。

C++
void splay(int x) {
  for (int f = fa[x]; f = fa[x], f; rotate(x))
    if (fa[f]) rotate(get(x) == get(f) ? f : x);
  rt = x;
}
  1. 插入操作
  • 如果树为空,直接插入。
  • 如果树中节点的值等 kk,那么增加该节点的 cntcnt,将此节点进行 Splay 操作。
  • 否则按照二叉查找树的性质向下找,找到空节点就插入即可(不要忘记 Splay 操作)。
C++
void insert(int x){
		if(!rt){
			val[++tot]=x;
			same[tot]++;
			rt=tot;
			maintain(tot);
			return;
		}
		int cur=rt,f=0;
		while(1){
			if(val[cur]==x){
				same[cur]++;
					maintain(cur);
				maintain(f);
				splay(cur);
				break;
			}
			f=cur,cur=ch[cur][val[cur]<x];
			if(!cur){
				val[++tot]=x;
				fa[tot]=f;
				ch[f][val[f]<x]=tot;
				same[tot]++;
				maintain(tot);
				maintain(f);
				splay(tot);
				break;
			}
		}
	}
  1. 查询 xx 的排名
C++
int rk(int x){
		int res=0,cur=rt;
		while(1){

		if(x<val[cur])cur=ch[cur][0];
		else{
			res+=size[ch[cur][0]];
			if(!cur)return res+1;
			if(x==val[cur]){
				splay(cur);
				return res+1;
			}
			res+=same[cur];
			cur=ch[cur][1];
		}
	}
}
  1. 查询排名 xx 的数
C++
int kth(int x){
		int cur=rt;
		while(1){

		if(ch[cur][0]&&x<=size[ch[cur][0]])cur=ch[cur][0];
		else{
			x-=size[ch[cur][0]]+same[cur];
			if(x<=0){
				splay(cur);
				return val[cur];
			}
			else cur=ch[cur][1];
		}
	}
	}

7.查询前驱后继

C++
int pre(){
		int cur=ch[rt][0];
		if(!cur)return cur;
		while(ch[cur][1])cur=ch[cur][1];
		splay(cur);
		return cur;
	}
	int suf(){
		int cur=ch[rt][1];
		if(!cur)return cur;
		while(ch[cur][0])cur=ch[cur][0];
		splay(cur);
		return cur;
	}
  1. 删除操作
C++
void del(int k) {
  rk(k);
  if (same[rt] > 1) {
    same[rt]--;
    maintain(rt);
    return;
  }
  if (!ch[rt][0] && !ch[rt][1]) {
    clear(rt);
    rt = 0;
    return;
  }
  if (!ch[rt][0]) {
    int cur = rt;
    rt = ch[rt][1];
    fa[rt] = 0;
    clear(cur);
    return;
  }
  if (!ch[rt][1]) {
    int cur = rt;
    rt = ch[rt][0];
    fa[rt] = 0;
    clear(cur);
    return;
  }
  int cur = rt, x = pre();
  fa[ch[cur][1]] = x;
  ch[x][1] = ch[cur][1];
  clear(cur);
  maintain(rt);
}

7.14 CDQ分治

三维偏序:

C++
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,k,tot,m;
int tree[N],tmp[N];
struct items{
	int a,b,c,cnt,ans;
	bool operator !=(items other){
		if(a!=other.a)return true;
		if(b!=other.b)return true;
		if(c!=other.c)return true;
		return false;
	}
}h[N],hn[N];

bool cmpA(items A,items B){
	if(A.a!=B.a)return A.a<B.a;
	else if(A.b!=B.b)return A.b<B.b;
	else return A.c<B.c;
}
bool cmpB(items A,items B){
	if(A.b!=B.b)return A.b<B.b;
	else return A.c<B.c;
}
int lowbit(int x){return x&-x;}
void add(int x,int val){
	for(int i=x;i<=k;i+=lowbit(i)){
		tree[i]+=val;
	}
	return;
}
int ask(int x){
	int res=0;
	for(int i=x;i;i-=lowbit(i)){
		res+=tree[i];
	}
	return res;
}
void CDQ(int l, int r) {
  if (l == r) return;
  int mid = (l + r) / 2;
  CDQ(l, mid);
  CDQ(mid + 1, r);
  std::sort(hn + l, hn + mid + 1, cmpB);
  std::sort(hn + mid + 1, hn + r + 1, cmpB);
  int i = l;
  int j = mid + 1;
  while (j <= r) {
    while (i <= mid && hn[i].b <= hn[j].b) {
      add(hn[i].c, hn[i].cnt);
      i++;
    }
    hn[j].ans += ask(hn[j].c);
    j++;
  }
  for (int k = l; k < i; k++) add(hn[k].c, -hn[k].cnt);
  return;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
	cin>>n>>k;
	for(int i=1;i<=n;i++)cin>>h[i].a>>h[i].b>>h[i].c;
	sort(h+1,h+1+n,cmpA);
	for(int i=1;i<=n;i++){
		m++;
		if(h[i]!=h[i+1]){
			hn[++tot].a=h[i].a;
			hn[tot].b=h[i].b;
			hn[tot].c=h[i].c;
			hn[tot].cnt=m;

			m=0;
		}
	}
	CDQ(1,tot);
	for(int i=1;i<=tot;i++)tmp[hn[i].ans+hn[i].cnt-1]+=hn[i].cnt;
	for(int i=0;i<n;i++)cout<<tmp[i]<<endl;
	return 0;
}

7.15 二分图

二分图最大匹配模版:

C++
#include <bits/stdc++.h>
using namespace std;
const int N=2003;
int G[N][N];
int n,m,e,ans,cnt;
int ask[N],matched[N];
bool found(int u){
	for(int i=1;i<=m;i++){
		if(G[u][i]){

		if(ask[i])continue;
		ask[i]=1;
		if(!matched[i]||found(matched[i])){
		matched[i]=u;
		return true;
	}
}
	}
	return false;
}
void match(){
	memset(matched,0,sizeof matched);
	for(int i=1;i<=n;i++){
		memset(ask,0,sizeof ask);
		if(found(i))ans++;
	}
	return;
}
int main(){
	cin>>n>>m>>e;
	for(int i=1,u,v;i<=e;i++){
		cin>>u>>v;
		G[u][v]=1;
	}
	match();
	cout<<ans;
	return 0;
}

7.16 Hall定理

7.17 网络流

网络流模版

EK算法:

C++
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+5;
const int inf=1e9+5;
struct node{
    long long w;
	int to,next;
}e[N];
int n,m,s,t,tot=1;
long long head[N],dis[N],pre[N],ans;
bool vis[N];
void add(int u,int v,long long w){
	e[++tot].to=v;
	e[tot].w=w;
	e[tot].next=head[u];
	head[u]=tot;
	e[++tot].to=u;
	e[tot].w=0;
	e[tot].next=head[v];
	head[v]=tot;
	return;
}
bool bfs(){
	for(int i=1;i<=n;i++)vis[i]=0;
	queue<int> q;
	q.push(s);
	vis[s]=1;
	dis[s]=inf;
	while(!q.empty()){
		int x=q.front();q.pop();
		for(int i=head[x];i;i=e[i].next){
			if(e[i].w==0)continue;
			if(vis[e[i].to])continue;
			dis[e[i].to]=min(dis[x],e[i].w);
			pre[e[i].to]=i;
			q.push(e[i].to);
			vis[e[i].to]=1;
			if(e[i].to==t)return 1;
		}

	}
	return 0;
}
void update(){
	int x=t;
	while(x!=s){
		int v=pre[x];
		e[v].w-=dis[t];
		e[v^1].w+=dis[t];
		x=e[v^1].to;
	}
	ans+=dis[t];
}
int main(){
	cin>>n>>m>>s>>t;
	for(int i=1,u,v,w;i<=m;i++){
		cin>>u>>v>>w;
		add(u,v,w);
	}
	while(bfs()){
		update();
	}
	cout<<ans;
	return 0;
}

dinic算法:

C++
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+5;
const int inf=1e9+5;
struct node{
    long long w;
	int to,next;
}e[N];
int n,m,s,t,tot=1;
long long head[N],dis[N],pre[N],ans,cur[N];
bool vis[N];
void add(int u,int v,long long w){
	e[++tot].to=v;
	e[tot].w=w;
	e[tot].next=head[u];
	head[u]=tot;
	e[++tot].to=u;
	e[tot].w=0;
	e[tot].next=head[v];
	head[v]=tot;
	return;
}
bool bfs(){
	for(int i=1;i<=n;i++)vis[i]=0,cur[i]=head[i],dis[i]=0;
	queue<int> q;
	q.push(s);
	vis[s]=1;
	dis[s]=1;
	while(!q.empty()){
		int x=q.front();q.pop();
		for(int i=cur[x];i;i=e[i].next){
			if(!vis[e[i].to]&&e[i].w>0){
				vis[e[i].to]=1;
				dis[e[i].to]=dis[x]+1;
				q.push(e[i].to);
				if(e[i].to==t)return true;
			}

		}

	}
	return 0;
}
int dfs(int now,int t,long long flow){
	if(!flow||now==t)return flow;
	int used=0;
	for(int i=cur[now];i;i=e[i].next){
		cur[now]=i;
		if(dis[now]+1!=dis[e[i].to])continue;
		//if(!e[i].w)continue;
		int tmp=dfs(e[i].to,t,min((long long)flow-used,e[i].w));
	//	if(!tmp)dis[e[i].to]=0;
		if(tmp){
			e[i].w-=tmp;
			e[i^1].w+=tmp;

			used+=tmp;
			if(flow-used==0)return flow;
		}
	}
	return used;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n>>m>>s>>t;
	for(int i=1,u,v,w;i<=m;i++){
		cin>>u>>v>>w;
		add(u,v,w);
	}
	while(bfs()){
		ans+=dfs(s,t,inf);
	}
	cout<<ans;
	return 0;
}

7.18 费用流

最小费用最大流:

C++
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+5;
const int inf=1e9+5;
#define int long long
struct node{
    long long w;
	int to,next;	int cost;
}e[N];
int n,m,s,t,tot=1,ret;
long long head[N],dis[N],pre[N],ans,cur[N];
bool vis[N];
void add(int u,int v,long long w,int cost){
	e[++tot].to=v;
	e[tot].w=w;
	e[tot].next=head[u];
	e[tot].cost=cost;
	head[u]=tot;
	e[++tot].to=u;
	e[tot].w=0;
	e[tot].next=head[v];
	e[tot].cost=-cost;
	head[v]=tot;
	return;
}
bool bfs(){
	for(int i=1;i<=n;i++)vis[i]=0,cur[i]=head[i],dis[i]=inf;
	queue<int> q;
	q.push(s);
	dis[s]=0,vis[s]=1;
	while(!q.empty()){
		int x=q.front();q.pop();
		vis[x]=0;
		for(int i=cur[x];i;i=e[i].next){
			if(dis[e[i].to]>dis[x]+e[i].cost&&e[i].w>0){
				dis[e[i].to]=dis[x]+e[i].cost;
				if(!vis[e[i].to])q.push(e[i].to),vis[e[i].to]=1;
				//if(e[i].to==t)return true;
			}

		}

	}
	return dis[t]!=inf;
}

int dfs(int u, int t, int flow) {
  if (u==t||!flow) return flow;
  vis[u]=1;
  int used=0;
  for (int i=cur[u];i&&used<flow;i=e[i].next) {
    int v=e[i].to;
    if (!vis[v]&&dis[v]==dis[u]+e[i].cost) {
      int tmp= dfs(v,t,min(e[i].w,flow - used));
      if (tmp) ret+=tmp* e[i].cost,e[i].w-=tmp,e[i ^ 1].w+= tmp,used+=tmp;
    }
  }
  vis[u]=0;
  return used;
}


signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n>>m>>s>>t;
	for(int i=1,u,v,w,c;i<=m;i++){
		cin>>u>>v>>w>>c;
		add(u,v,w,c);
	}
	while(bfs()){
		//for(int i=1;i<=n;i++)cout<<dis[i]<<" ";
		//cout<<endl;
		ans+=dfs(s,t,inf);

}
	cout<<ans<<" "<<ret;
	return 0;
}

7.19 上下界网络流

有源汇有上下界最大流

C++
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+5;
const int inf=1e9+5;
struct node{
    long long w;
	int to,next;
}e[N];
int n,m,s,t,tot=1;
long long head[N],dis[N],pre[N],ans,cur[N],h[N];
bool vis[N];
void add(int u,int v,long long w){
	e[++tot].to=v;
	e[tot].w=w;
	e[tot].next=head[u];
	head[u]=tot;
	e[++tot].to=u;
	e[tot].w=0;
	e[tot].next=head[v];
	head[v]=tot;
	return;
}
bool bfs(){
	for(int i=1;i<=n+2;i++)vis[i]=0,cur[i]=head[i],dis[i]=0;
	queue<int> q;
	q.push(s);
	vis[s]=1;
	dis[s]=1;
	while(!q.empty()){
		int x=q.front();q.pop();
		for(int i=cur[x];i;i=e[i].next){
			if(!vis[e[i].to]&&e[i].w>0){
				vis[e[i].to]=1;
				dis[e[i].to]=dis[x]+1;
				q.push(e[i].to);
				if(e[i].to==t)return true;
			}

		}

	}
	return 0;
}
int dfs(int now,int t,long long flow){
	if(!flow||now==t)return flow;
	int used=0;
	for(int i=cur[now];i;i=e[i].next){
		cur[now]=i;
		if(dis[now]+1!=dis[e[i].to])continue;
		//if(!e[i].w)continue;
		int tmp=dfs(e[i].to,t,min((long long)flow-used,e[i].w));
	//	if(!tmp)dis[e[i].to]=0;
		if(tmp){
			e[i].w-=tmp;
			e[i^1].w+=tmp;

			used+=tmp;
			if(flow-used==0)return flow;
		}
	}
	return used;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int S,T;
	cin>>n>>m>>S>>T;
	add(T,S,inf);
	s=n+1,t=n+2;
	for(int i=1,u,v,wmin,wmax;i<=m;i++){
		cin>>u>>v>>wmin>>wmax;
		h[v]+=wmin;
		h[u]-=wmin;
		add(u,v,wmax-wmin);
	}
	int cnt=0;
	for(int i=1;i<=n;i++){
		if(h[i]>0){
			add(s,i,h[i]),cnt+=h[i];
		}else if(h[i]<0){
			add(i,t,-h[i]);
		}
	}
	while(bfs()){
		ans+=dfs(s,t,inf);
	}
	if(ans!=cnt)cout<<"please go home to sleep";
	else{
		ans=0;
		s=S,t=T;
		while(bfs()){
		ans+=dfs(s,t,inf);
	}
	cout<<ans<<endl;
	}
	return 0;
}

有源汇有上下界最小流

C++
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+5;
const int inf=1e9+5;
struct node{
    long long w;
	int to,next;
}e[N];
int n,m,s,t,tot=1;
long long head[N],dis[N],pre[N],ans,cur[N],h[N];
bool vis[N];
void add(int u,int v,long long w){
	e[++tot].to=v;
	e[tot].w=w;
	e[tot].next=head[u];
	head[u]=tot;
	e[++tot].to=u;
	e[tot].w=0;
	e[tot].next=head[v];
	head[v]=tot;
	return;
}
bool bfs(){
	for(int i=1;i<=n+2;i++)vis[i]=0,cur[i]=head[i],dis[i]=0;
	queue<int> q;
	q.push(s);
	vis[s]=1;
	dis[s]=1;
	while(!q.empty()){
		int x=q.front();q.pop();
		for(int i=cur[x];i;i=e[i].next){
			if(!vis[e[i].to]&&e[i].w>0){
				vis[e[i].to]=1;
				dis[e[i].to]=dis[x]+1;
				q.push(e[i].to);
				if(e[i].to==t)return true;
			}

		}

	}
	return 0;
}
int dfs(int now,int t,long long flow){
	if(!flow||now==t)return flow;
	int used=0;
	for(int i=cur[now];i;i=e[i].next){
		cur[now]=i;
		if(dis[now]+1!=dis[e[i].to])continue;
		//if(!e[i].w)continue;
		int tmp=dfs(e[i].to,t,min((long long)flow-used,e[i].w));
	//	if(!tmp)dis[e[i].to]=0;
		if(tmp){
			e[i].w-=tmp;
			e[i^1].w+=tmp;

			used+=tmp;
			if(flow-used==0)return flow;
		}
	}
	return used;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int S,T;
	cin>>n>>m>>S>>T;
	add(T,S,inf);
	s=n+1,t=n+2;
	for(int i=1,u,v,wmin,wmax;i<=m;i++){
		cin>>u>>v>>wmin>>wmax;
		h[v]+=wmin;
		h[u]-=wmin;
		add(u,v,wmax-wmin);
	}
	int cnt=0;
	for(int i=1;i<=n;i++){
		if(h[i]>0){
			add(s,i,h[i]),cnt+=h[i];
		}else if(h[i]<0){
			add(i,t,-h[i]);
		}
	}
	while(bfs()){
		ans+=dfs(s,t,inf);
	}
	if(ans!=cnt)cout<<"please go home to sleep";
	else{
		ans=inf-e[2].w;
		s=T,t=S;
		e[2].w=e[3].w=0;
		while(bfs()){
		ans-=dfs(s,t,inf);
	}
	cout<<ans<<endl;
	}
	return 0;
}

7.22 2-SAT

2-SAT,简单的说就是给出 nn 个集合,每个集合有两个元素,已知若干个 a,ba,b ,表示 aa 与 bb 矛盾(其中 aa 与 bb 属于不同的集合)。然后从每个集合选择一个元素,判断能否一共选 nn 个两两不矛盾的元素。显然可能有多种选择方案,一般题中只需要求出一种即可。

因为在 <a,b><a,b> 中,条件 aa 与条件 bb 不能同时满足,所以当 aa 成立时,bb 一定不成立,bb 成立时 aa 一定不成立。

假设有 [a1,a2][a_1,a_2] 和 [b1,b2][b_1,b_2] 两对,已知 a1a_1 和 b2b_2 间有矛盾,于是为了方案自洽,由于两者中必须选一个,所以我们就要拉两条有向边 [a1,b1][a_1,b_1] 和 [b2,a2][b_2,a_2] 表示选了 a1a_1 则必须选 b1b_1 ,选了 b2b_2 则必须选 a2a_2 才能够自洽。

2-SAT 模版:

C++
#include <bits/stdc++.h>
using namespace std;
const int N=2e6+5;
int n,m,tot;
int dfn[N],vis[N],low[N],color[N];
vector<int> vec[N];
stack<int> stk;
int SCC;
void dfs(int u){
	dfn[u]=low[u]=++tot;
	stk.push(u),vis[u]=1;
	for(auto to:vec[u]){
		if(!dfn[to]){
			dfs(to);
			low[u]=min(low[u],low[to]);
		}else if(vis[to])low[u]=min(low[u],low[to]);

	}
	if(low[u]==dfn[u]){
		++SCC;
		do{
			u=stk.top();
			stk.pop();
			color[u]=SCC;
			vis[u]=0;
		}while(low[u]!=dfn[u]);
	}

}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1,x,y,a,b;i<=m;i++){
		cin>>x>>a>>y>>b;
		vec[(a&1)*n+x].push_back((b^1)*n+y);
		vec[(b&1)*n+y].push_back((a^1)*n+x);
	}
	for(int i=1;i<=2*n;i++)if(!dfn[i])dfs(i);
	for(int i=1;i<=n;i++){
		if(color[i]==color[i+n]){
			cout<<"IMPOSSIBLE";
			return 0;
		}
	}
	cout<<"POSSIBLE"<<endl;
	for(int i=1;i<=n;i++){
		cout<<(color[i]<color[i+n])<<" ";
	}
	return 0;
}

7.23 高斯消元

主要步骤: 共有 nn 个要求的未知量 xx,枚举主元 xx,依次消去无用行的未知量。

  1. 当第 pp 行 xx 的系数大于当前要求行的 xx 的系数时,交换第 pp 行与该行。若系数最大为 00,返回无解或无限解。
  2. 用交换后的行,消去其他行该位置无用的未知量。
  3. 将当前行行数要求的未知量的系数变为 11。如果该系数为 00,并且等式右侧不为 00,返回无解或无限解。
C++
#include <bits/stdc++.h>


using namespace std;
const int N=1e2 +5;
const long double eps=1e-7;
int n;
long double a[N][N];
long double tmp[N][N];
long double k;
int main(){
	cin>>n;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n+1;j++){
			cin>>a[i][j];
		}


	for(int i=1;i<=n;i++){
        int now=i;
		for(int j=i+1;j<=n;j++){
			if(fabs(a[j][i])>fabs(a[i][i]))now=j;
		}
        if(fabs(a[now][i])<eps){
            cout<<"No Solution";
            return 0;
        }
		for(int j=1;j<=n+1;j++){
			swap(a[now][j],a[i][j]);
		}
	for(int j=1;j<=n;j++){
		if(j==i)continue;
		if(a[j][i]==0)continue;
		if(a[i][i])k=a[i][i]/a[j][i];
		else k=0;
		for(int l=1;l<=n+1;l++){
			a[j][l]=a[j][l]*k-a[i][l];
		}
	}
	}

	cout<<"";

	for(int i=1;i<=n;i++){
		if(a[i][i]!=1){
			if(a[i][i]==0&&a[i][n+1]!=0){
				cout<<"No Solution";
				return 0;
			}
			k=a[i][i];
			if(k!=0)for(int j=1;j<=n+1;j++){
				a[i][j]/=k;
			}
		}else continue;

	}
	for(int i=1;i<=n;i++){
        if(fabs(a[i][n+1])<eps){
            cout<<"0.00"<<endl;
        }else printf("%0.2Lf\n",a[i][n+1]);
	}
	return 0;
}

7.24

笛卡尔树

C++
#include <bits/stdc++.h>
using namespace std;
const int N=1e7+5;
int n;
int a[N];
long long l[N],r[N];
stack<pair<int,int> > s;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++){
		if(s.empty()){
			s.push(make_pair(a[i],i));
			continue;
		}
		pair<int,int> lson=make_pair(0,0);
		while(!s.empty()&&s.top().first>a[i])lson=s.top(),s.pop();
		if(!s.empty()){
			pair<int,int> fa=s.top();
			if(!lson.first&&!lson.second)r[fa.second]=i;
			else r[fa.second]=i,l[i]=lson.second;
		}else{
			l[i]=lson.second;
		}
		s.push(make_pair(a[i],i));
	}
	long long ansl=0,ansr=0;
	for(int i=1;i<=n;i++){
		ansl^=i*(l[i]+1)*1ll;
		ansr^=i*(r[i]+1)*1ll;
	}
	cout<<ansl<<" "<<ansr;
	return 0;
}
原文