【JZOJ6273】欠钱

description


analysis

  • 读懂题就可知\(b\)的收益即为\(a\)\(b\)这一条链上边权的最小值

  • 那么就是动态维护一个森林,询问链上最小值,同时必须满足儿子走向父亲

  • 明显\(LCT\)是吧,但是需要认真思考不少额外知识

  • 由于原树是有根树,每一次查询会把一棵\(splay\)翻转,导致原树形态变化

  • 于是每次查询之后要\(makeroot\)原来的根,只有加边原树根才会变

  • 还需要保证\(b\)一定是\(a\)的祖先,\(LCT\)没有祖先的限制,要怎么做?

  • 其实可以先后\(access(x),access(y)\)\(splay(x,0)\)后,此时\(x\)是某\(splay\)的根

  • 那么\(pf[x]\)就是原树中的\(LCA\),这个方法同样可以用于求动态树的\(LCA\)

  • 然后就没了


code

  • 就没了?

  • 体验过肛\(6h+\)之后莫名其妙\(MLE\)\(RE\)根本调不出来的感觉吗?

  • 体验过卡常数卡到想吃屎吃到哭的感觉吗?

  • 妈的

#pragma GCC optimize("O3")
#pragma G++ optimize("O3")
#pragma GCC optimize(3)
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cctype>
#define MAXN 100005
#define INF 1000000007
#define reg register int
#define max(x,y) ((x>y)?(x):(y))
#define min(x,y) ((x<y)?(x):(y))
#define fo(i,a,b) for (reg i=a;i<=b;++i)
#define fd(i,a,b) for (reg i=a;i>=b;--i)

using namespace std;

int tr[MAXN*2][2];
int fa[MAXN*2],pf[MAXN*2],st[MAXN*2],fat[MAXN];
int n,m,lastans,cnt,pos,root;
bool flag,bz;

struct node
{
	int val,mn;
	bool rev;
}a[MAXN*2];
__attribute__((optimize("-O3")))
inline char getcha()
{
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
__attribute__((optimize("-O3")))
inline int read()
{
    int res=0;char ch=getcha();bool XX=false;
    for(;!isdigit(ch);ch=getcha())
	(ch=='-') && (XX=true);
    for(;isdigit(ch);ch=getcha())
    res=(res<<3)+(res<<1)+(ch^48);
    return XX?-res:res;
}
__attribute__((optimize("-O3")))
inline void write(int x)  
{  
	int num=0;static char c[15];
	while (x)c[++num]=(x%10)+48,x/=10;
	while (num)putchar(c[num--]);
	putchar('\n'); 
}
__attribute__((optimize("-O3")))
inline void swap(int &x,int &y){int z=x;x=y,y=z;}
__attribute__((optimize("-O3")))
inline int getfa(int x){return fat[x]==x?x:fat[x]=getfa(fat[x]);}
__attribute__((optimize("-O3")))
inline void update(int x)
{
	if (x)a[x].mn=min(a[x].val,min(a[tr[x][0]].mn,a[tr[x][1]].mn));
}
__attribute__((optimize("-O3")))
inline void reverse(int x)
{
	if (x)a[x].rev^=1,swap(tr[x][0],tr[x][1]);
}
__attribute__((optimize("-O3")))
inline void down(int x)
{
	if (a[x].rev)
	{
		reverse(tr[x][0]),reverse(tr[x][1]);
		a[x].rev=0;
	}
}
__attribute__((optimize("-O3")))
inline void downdata(int x)
{
	while (x)st[++st[0]]=x,x=fa[x];
	while (st[0])down(st[st[0]--]);
}
__attribute__((optimize("-O3")))
inline int lr(int x)
{
	return tr[fa[x]][1]==x;
}
__attribute__((optimize("-O3")))
inline void rotate(int x)
{
	int y=fa[x],k=lr(x);
	tr[y][k]=tr[x][!k];
	if (tr[x][!k])fa[tr[x][!k]]=y;
	fa[x]=fa[y];if (fa[y])tr[fa[y]][lr(y)]=x;
	tr[x][!k]=y,fa[y]=x,pf[x]=pf[y];
	update(y),update(x);
}
__attribute__((optimize("-O3")))
inline void splay(int x,int y)
{
	downdata(x);
	while (fa[x]!=y)
	{
		if (fa[fa[x]]!=y)rotate(lr(fa[x])==lr(x)?fa[x]:x);
		rotate(x);
	}
}
__attribute__((optimize("-O3")))
inline void access(int x)
{
	for (int y=0;x;update(x),y=x,x=pf[x])
	{
		splay(x,0);
		fa[tr[x][1]]=0,pf[tr[x][1]]=x;
		tr[x][1]=y,fa[y]=x,pf[y]=0;
	}
}
__attribute__((optimize("-O3")))
inline void makeroot(int x)
{
	access(x),splay(x,0),reverse(x);
}
__attribute__((optimize("-O3")))
inline void link(int x,int y)
{
	makeroot(x),pf[x]=y;
}
__attribute__((optimize("-O3")))
inline bool judge(int x,int y)
{
	access(x),access(y),splay(x,0);
	return pf[x]==y;
}
int main()
{
	//freopen("T3.in","r",stdin);
	freopen("money.in","r",stdin);
	freopen("money.out","w",stdout);
	n=read(),m=read();
	fo(i,0,n)a[i].mn=a[i].val=INF,fat[i]=i;
	while (m--)
	{
		int type=read(),x=(read()+lastans)%n+1,y=(read()+lastans)%n+1,z;
		if (!type)
		{
			if (!root || root==x)root=y;
			z=(read()+lastans)%n+1,++cnt;
			link(x,n+cnt),link(n+cnt,y);
			a[n+cnt].mn=a[n+cnt].val=z;
			fat[getfa(x)]=getfa(y);
		}
		else
		{
			if (getfa(x)==getfa(y) && judge(x,y))
			{
				makeroot(y),access(x),splay(x,0);
				write(a[x].mn),lastans=a[x].mn,makeroot(root);
			}
			else printf("0\n"),lastans=0;
		}
	}
	return 0;
}
posted @ 2019-08-04 21:39  路人黑的纸巾  阅读(150)  评论(0编辑  收藏  举报