LCT

BZOJ2157

#include<cstdio>
#include<cstring>
#include<iostream>
#include<stdlib.h>
#include<ctime>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define LL long long
#define FOR(i,a,b) for (int i=a;i<=b;i++)
#define FORD(i,a,b) for (int i=a;i>=b;i--)
using namespace std;
typedef pair<int,int> pa;
void getint(int &v){
    char ch,fu=0;
    for(ch='*'; (ch<'0'||ch>'9')&&ch!='-'; ch=getchar());
    if(ch=='-') fu=1, ch=getchar();
    for(v=0; ch>='0'&&ch<='9'; ch=getchar()) v=v*10+ch-'0';
    if(fu) v=-v;
}
const int INF=1e9+10;
struct node{
	int ch[2],fa,v,mx,mn,s;
	int tgx,tgy;
	bool rev;
	node(){tgx=1;}
}t[200010];
int n,m,stk[200010],top,x,y,w;
char s[666];
bool isroot(int x){
	return (t[t[x].fa].ch[0]!=x && t[t[x].fa].ch[1]!=x);
}
void pushup(int x){
	t[x].s=0;
	t[x].mx=-INF;
	t[x].mn=INF;
	if (x>n){
		t[x].s=t[x].v;
		t[x].mx=max(t[x].mx,t[x].v);
		t[x].mn=min(t[x].mn,t[x].v);
	}
	FOR(i,0,1)
		if (t[x].ch[i]){
			t[x].mx=max(t[t[x].ch[i]].mx,t[x].mx);
			t[x].mn=min(t[t[x].ch[i]].mn,t[x].mn);
			t[x].s+=t[t[x].ch[i]].s;
		}
}
void TAG(int x,int p,int q){
	t[x].v=t[x].v*p+q;
	t[x].s=t[x].s*p+q;
	if (t[x].mx>=t[x].mn){
		t[x].mx=t[x].mx*p+q;
		t[x].mn=t[x].mn*p+q;
		if (t[x].mx<t[x].mn) swap(t[x].mx,t[x].mn);
	}
	//(*x+y)*p+q
	t[x].tgx*=p;
	t[x].tgy=t[x].tgy*p+q;
}
void REV(int x){
	swap(t[x].ch[0],t[x].ch[1]);
	t[x].rev^=1;
}
void pushdown(int x){
	if (t[x].rev){
		if (t[x].ch[0]) REV(t[x].ch[0]);
		if (t[x].ch[1]) REV(t[x].ch[1]);
		t[x].rev=0;
	}
	if (t[x].tgx!=1 || t[x].tgy!=0){
		if (t[x].ch[0]) TAG(t[x].ch[0],t[x].tgx,t[x].tgy);
		if (t[x].ch[1]) TAG(t[x].ch[1],t[x].tgx,t[x].tgy);
		t[x].tgx=1,t[x].tgy=0;
	}
}
void rot(int x){
	int fa=t[x].fa;
	int gfa=t[fa].fa;
	int t1=(x!=t[fa].ch[0]);
	int t2=(fa!=t[gfa].ch[0]);
	int ch=t[x].ch[t1^1];
	if (!isroot(fa)) t[gfa].ch[t2]=x;
	t[ch].fa=fa;
	t[x].fa=gfa;
	t[x].ch[t1^1]=fa;
	t[fa].fa=x;
	t[fa].ch[t1]=ch;
	pushup(fa);
	pushup(x);
}
void splay(int x){
	stk[top=1]=x;
	int y=x;
	while (!isroot(y)) stk[++top]=t[y].fa,y=t[y].fa;
	FORD(rp,top,1) pushdown(stk[rp]);
	while (!isroot(x)){
		int fa=t[x].fa;
		int gfa=t[fa].fa;
		if (!isroot(fa))
			(x==t[fa].ch[0]^fa==t[gfa].ch[0])?rot(x):rot(fa);
		rot(x);
	}
}
void access(int x){
	int te=0;
	while (x){
		splay(x);
		t[x].ch[1]=te;
		pushup(x);
		te=x;
		x=t[x].fa;
	}
}
void makeroot(int x){
	access(x);
	splay(x);
	REV(x);
}
void link(int x,int y){
	makeroot(x);
	t[x].fa=y;
}

int main(){
	scanf("%d",&n);
	m=n;
	FOR(i,1,n-1){
		getint(x),getint(y),getint(w);
		++x,++y;
		t[++m].v=w;
		t[m].mx=t[m].mn=t[m].s=w;
		link(m,x),link(m,y);
	}
	scanf("%d",&m);
	while (m--){
		scanf("%s",s);
		if (s[0]=='C'){
			getint(x),getint(y);
			x+=n;
			makeroot(x);
			access(x);
			splay(x);
			TAG(x,1,y-t[x].v);
		}
		else if (s[0]=='N'){
			getint(x),getint(y);
			++x,++y;
			makeroot(x);
			access(y);
			splay(y);
			TAG(y,-1,0);
		}
		else if (s[0]=='S'){
			getint(x),getint(y);
			++x,++y;
			makeroot(x);
			access(y);
			splay(y);
			printf("%d\n",t[y].s);
		}
		else if (s[1]=='A'){
			getint(x),getint(y);
			++x,++y;
			makeroot(x);
			access(y);
			splay(y);
			printf("%d\n",t[y].mx);
		}
		else{
			getint(x),getint(y);
			++x,++y;
			makeroot(x);
			access(y);
			splay(y);
			printf("%d\n",t[y].mn);
		}
	}
	return 0;
}

http://codeforces.com/contest/763/submission/34110258

带平衡树代码容易写错的一个地方:写t[xx].yy的时候可能中途旋转了,所以要开个临时变量记下。

http://codeforces.com/contest/603/submission/34171363

维护树的size的时候,要记w,sz分别表示LCT中子树的非重儿子size、总size。

然后在cut,link,access的时候要多写几句话。

注意link,为了使得x接到y上去的时候y的祖先不需要pushup,所以要让y没有父亲,即access(y),splay(y)

李超线段树

BZOJ1568

#include<cstdio>
#include<cstring>
#include<iostream>
#include<stdlib.h>
#include<ctime>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define LL long long
#define FOR(i,a,b) for (int i=a;i<=b;i++)
#define FORD(i,a,b) for (int i=a;i>=b;i--)
using namespace std;
typedef pair<int,int> pa;
void getint(int &v){
    char ch,fu=0;
    for(ch='*'; (ch<'0'||ch>'9')&&ch!='-'; ch=getchar());
    if(ch=='-') fu=1, ch=getchar();
    for(v=0; ch>='0'&&ch<='9'; ch=getchar()) v=v*10+ch-'0';
    if(fu) v=-v;
}
double ans,tagk[500010],S,P,tagb[500010];
bool flag[500010];
char s[666];
int n,x,T;
double crs(double k1,double b1,double k2,double b2){
	return (b2-b1)/(k1-k2);
}
void ins(int l,int r,int rt,double K,double B){
	if (!flag[rt]){
		flag[rt]=1;
		tagk[rt]=K;
		tagb[rt]=B;
		return;
	}
	int m=l+r>>1;
	double f1=K*l+B,f2=tagk[rt]*l+tagb[rt],f3=K*r+B,f4=tagk[rt]*r+tagb[rt];
	if (f2>=f1 && f4>=f3) return;
	if (f1>=f2 && f3>=f4){
		tagk[rt]=K,tagb[rt]=B;
		return;
	}
	double t=crs(K,B,tagk[rt],tagb[rt]);
	if (f1>=f2){
		if (t<=m){
			ins(l,m,rt<<1,K,B);
		}
		else{
			ins(m+1,r,rt<<1|1,tagk[rt],tagb[rt]);
			tagk[rt]=K,tagb[rt]=B;
		}
	}
	else{
		if (t<=m){
			ins(l,m,rt<<1,tagk[rt],tagb[rt]);
			tagk[rt]=K,tagb[rt]=B;
		}
		else{
			ins(m+1,r,rt<<1|1,K,B);
		}
	}
}
void que(int l,int r,int rt,int x){
	ans=max(ans,tagk[rt]*x+tagb[rt]);
	if (l==r) return;
	int m=l+r>>1;
	if (x<=m) que(l,m,rt<<1,x);
	else que(m+1,r,rt<<1|1,x);
}
int main(){
	T=50000;
	scanf("%d",&n);
	FOR(i,1,n){
		scanf("%s",s);
		if (s[0]=='Q'){
			getint(x);
			ans=0.0;
			que(1,T,1,x);
			printf("%lld\n",(long long)(ans/100));
		}
		else{
			scanf("%lf%lf",&S,&P);
			ins(1,T,1,P,S-P);
		}
	}
	return 0;
}

树套树

BZOJ3196,T了一点

#include<cstdio>
#include<cstring>
#include<iostream>
#include<stdlib.h>
#include<ctime>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define LL long long
#define FOR(i,a,b) for (int i=a;i<=b;i++)
#define FORD(i,a,b) for (int i=a;i>=b;i--)
using namespace std;
typedef pair<int,int> pa;
void getint(int &v){
    char ch,fu=0;
    for(ch='*'; (ch<'0'||ch>'9')&&ch!='-'; ch=getchar());
    if(ch=='-') fu=1, ch=getchar();
    for(v=0; ch>='0'&&ch<='9'; ch=getchar()) v=v*10+ch-'0';
    if(fu) v=-v;
}
const int INF=1e9+10;
struct node{
	int v,siz,cnt;
	int ch[2],fa;
}t[2000010];
int a[200010],tp,n,X,Y,m,ans,x,tot,rt[200010],l,r,k;
void debug(int x){
	if (t[x].ch[0]) debug(t[x].ch[0]);
	FOR(i,1,t[x].cnt) printf("%d ",t[x].v);
	if (t[x].ch[1]) debug(t[x].ch[1]);
}
void DEBUG(){
	FOR(i,1,n) debug(rt[i]),puts("");
	puts("**");
}
void pushup(int x){
	t[x].siz=t[t[x].ch[0]].siz+t[t[x].ch[1]].siz+t[x].cnt;
}
void rot(int x){
	int fa=t[x].fa;
	int gfa=t[fa].fa;
	int t1=(x!=t[fa].ch[0]);
	int t2=(fa!=t[gfa].ch[0]);
	int ch=t[x].ch[t1^1];
	if (gfa) t[gfa].ch[t2]=x;
	t[ch].fa=fa;
	t[x].fa=gfa;
	t[x].ch[t1^1]=fa;
	t[fa].fa=x;
	t[fa].ch[t1]=ch;
	pushup(fa);
	pushup(x);
}
void splay(int x,int too){
	while (t[x].fa!=too){
		int fa=t[x].fa;
		int gfa=t[fa].fa;
		if (gfa!=too)
			(x==t[fa].ch[0]^fa==t[gfa].ch[0])?rot(x):rot(fa);
		rot(x);
	}
}
int fnd(int x,int k){
	if (k<=t[t[x].ch[0]].siz) return fnd(t[x].ch[0],k);
	else if (k>t[t[x].ch[0]].siz+t[x].cnt) return fnd(t[x].ch[1],k-t[t[x].ch[0]].siz-t[x].cnt);
	else return x;
}
int rnk(int x,int k){
	if (!x) return 0;
	if (k<t[x].v) return rnk(t[x].ch[0],k);
	else if (k==t[x].v) return t[t[x].ch[0]].siz+t[x].cnt;
	else return t[t[x].ch[0]].siz+t[x].cnt+rnk(t[x].ch[1],k);
}
void ins(int &x,int v,int fa){
	if (!x){
		x=++tot;
		t[x].v=v;
		t[x].siz=t[x].cnt=1;
		t[x].fa=fa;
		return;
	}
	if (v==t[x].v) ++t[x].cnt;
	else if (v<t[x].v) ins(t[x].ch[0],v,x);
	else ins(t[x].ch[1],v,x);
	pushup(x);
}
void del(int x,int v){
	X=fnd(rt[x],rnk(rt[x],v-1));
	Y=fnd(rt[x],rnk(rt[x],v)+1);
	splay(X,0);
	rt[x]=X;
	splay(Y,X);
	--t[t[Y].ch[0]].cnt;
	pushup(t[Y].ch[0]);
	if (t[t[Y].ch[0]].cnt==0) t[Y].ch[0]=0;
	pushup(Y);
	pushup(X);
}
inline int lowbit(int x){
	return x&-x;
}
void ADD(int x,int T){
	for (;x<=n;x+=lowbit(x)){
		int tt=tot;
		ins(rt[x],T,0);
		if (tt!=tot){
			rt[x]=tot;
			splay(tot,0);
		}
	}
}
void DEL(int x,int t){
	for (;x<=n;x+=lowbit(x)){
		del(x,t);
	}
}
int SUM(int x,int t){
	int s=0;
	for (;x;x-=lowbit(x))
		s+=rnk(rt[x],t)-1;
	return s;
}
int main(){
//	freopen("t.in","r",stdin);
//	freopen("t.out","w",stdout);
	scanf("%d%d",&n,&m);
	FOR(i,1,n){
		rt[i]=++tot;
		t[rt[i]].v=-INF;
		t[rt[i]].ch[1]=++tot;
		t[tot].v=INF;
		t[tot].fa=tot-1;
		t[tot].cnt=t[tot].siz=1;
		t[tot-1].siz=1;
		t[tot-1].cnt=1;
		pushup(tot-1);
	}
	FOR(i,1,n) getint(a[i]),ADD(i,a[i]);
	while (m--){
		getint(tp);
		if (tp==1){
			getint(l),getint(r),getint(k);
			ans=(SUM(r,k-1)-SUM(l-1,k-1))+1;
			printf("%d\n",ans);
		}
		else if (tp==2){
			getint(l),getint(r),getint(k);
			int L=0,R=1e8;
			while (L<R){
				int O=L+R>>1;
				if (SUM(r,O)-SUM(l-1,O)>=k) R=O;
				else L=O+1;
			}
			printf("%d\n",R);
		}
		else if (tp==3){
			getint(x),getint(k);
			DEL(x,a[x]);
			a[x]=k;
			ADD(x,a[x]);
		}
		else if (tp==4){
			getint(l),getint(r),getint(k);
			k=SUM(r,k-1)-SUM(l-1,k-1);
			int L=0,R=1e8;
			while (L<R){
				int O=L+R>>1;
				if (SUM(r,O)-SUM(l-1,O)>=k) R=O;
				else L=O+1;
			}
			printf("%d\n",R);
		}
		else{
			getint(l),getint(r),getint(k);
			k=SUM(r,k)-SUM(l-1,k)+1;
			int L=0,R=1e8;
			while (L<R){
				int O=L+R>>1;
				if (SUM(r,O)-SUM(l-1,O)>=k) R=O;
				else L=O+1;
			}
			printf("%d\n",R);
		}
	}
	return 0;
}

http://codeforces.com/contest/785/submission/34064644

特技:不要从小往大insert,random_shuffle之后再insert

斯特林数

TCO2014_Wildcard_750

g[i]表示n行i列的答案然后直接容斥即可。

#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define LL long long
#define FOR(i,a,b) for (int i=a;i<=b;++i)
#define FORD(i,a,b) for (int i=a;i>=b;--i)
using namespace std;
#define pi acos(-1)
typedef pair<int,int> pa;
typedef long double ld;
const int MO=1e9+7;
int n,m,c;
LL S[4010][4010],f[4010],g[4010];
class CountTables{
	public:
	int howMany(int N, int M, int C){
		n=N,m=M,c=C;
		S[0][0]=1;
		FOR(i,1,m){
			S[i][0]=0;
			FOR(j,1,i) S[i][j]=(S[i-1][j-1]+S[i-1][j]*j)%MO;
		}
		LL x=1;
		FOR(i,1,m){
			x=x*c%MO;
			f[i]=1;
			FORD(j,x,x-n+1) f[i]=f[i]*j%MO;
			g[i]=f[i];
			FOR(j,1,i-1) (g[i]-=S[i][j]*g[j]%MO)%=MO;
		}
		(g[m]+=MO)%=MO;
		return (int)g[m];
	}
}cwy;

AC自动机

板子

http://192.168.45.99/problem.php?id=1623

#include<cstdio>
#include<cstring>
#include<iostream>
#include<stdlib.h>
#include<ctime>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define LL long long
#define FOR(i,a,b) for (int i=a;i<=b;i++)
#define FORD(i,a,b) for (int i=a;i>=b;i--)
using namespace std;
typedef pair<int,int> pa;
void getint(int &v){
    char ch,fu=0;
    for(ch='*'; (ch<'0'||ch>'9')&&ch!='-'; ch=getchar());
    if(ch=='-') fu=1, ch=getchar();
    for(v=0; ch>='0'&&ch<='9'; ch=getchar()) v=v*10+ch-'0';
    if(fu) v=-v;
}
int cnt[500010],q[500010],fail[500010],n,m,x,son[500010][26],tot;
char s[500010];
LL ans;
void ins(){
    x=0;
    FOR(i,0,m-1){
        if (!son[x][s[i]-'a']) son[x][s[i]-'a']=++tot;
        x=son[x][s[i]-'a'];
    }
    cnt[x]=1;
}
void build(){
    int he=0,ta=1;
    q[1]=0;
    while (he!=ta){
        int x=q[++he];
        cnt[x]+=cnt[fail[x]];
        FOR(i,0,25){
            int y=son[x][i];
            if (!y) continue;
            if (x){
                int tmp=fail[x];
                while (tmp && !son[tmp][i]) tmp=fail[tmp];
                fail[y]=son[tmp][i];
            }
            q[++ta]=y;
        }
    }
}
void fnd(){
    x=0;
    FOR(i,0,m-1){
        while (x && !son[x][s[i]-'a']) x=fail[x];
        if (son[x][s[i]-'a']) x=son[x][s[i]-'a'];
        ans+=cnt[x];
    }
}
int main(){
    scanf("%d",&n);
    FOR(i,1,n){
        scanf("%s",s);
        m=strlen(s);
        ins();
    }
    build();
    scanf("%s",s);
    m=strlen(s);
    fnd();
    cout<<ans<<endl;
    return 0;
}

基础练习题

http://codeforces.com/contest/86/submission/33919169

http://codeforces.com/contest/163/submission/33955397

http://codeforces.com/contest/696/submission/33960564

 

http://codeforces.com/contest/547/submission/34041438

每个串可以看成是它的所有的前缀,fail树上能走到k的是合法的,转化为静态二维数点。

CF710F强在AC自动机(留坑

splay

复习splay

http://codeforces.com/contest/38/submission/33897822

http://www.lydsy.com/JudgeOnline/problem.php?id=3786

#include<cstdio>
#include<cstring>
#include<iostream>
#include<stdlib.h>
#include<ctime>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define LL long long
#define FOR(i,a,b) for (int i=a;i<=b;i++)
#define FORD(i,a,b) for (int i=a;i>=b;i--)
using namespace std;
typedef pair<int,int> pa;
void getint(int &v){
    char ch,fu=0;
    for(ch='*'; (ch<'0'||ch>'9')&&ch!='-'; ch=getchar());
    if(ch=='-') fu=1, ch=getchar();
    for(v=0; ch>='0'&&ch<='9'; ch=getchar()) v=v*10+ch-'0';
    if(fu) v=-v;
}
struct node{
	int tp,d,fa;
	int ch[2],siz;
	LL s,v,tag;
}t[200010];
char s[55];
int rt,stk[500010],top,x,y,X,Y,nxt[500010],hed[500010],too[500010],nedge,n,m,a[100010],L[100010],R[100010],dfn,tot;
void ae(int x,int y){
	nxt[++nedge]=hed[x];
	hed[x]=nedge;
	too[nedge]=y;
}
bool isroot(int x){
	return (t[x].fa==0);
}
void dfs(int x,int l){
	L[x]=++dfn;
	t[L[x]].v=a[x];
	t[L[x]].tp=1;
	for (int i=hed[x];i;i=nxt[i]){
		int y=too[i];
		if (y==l) continue;
		dfs(y,x);
	}
	R[x]=++dfn;
	t[R[x]].v=-a[x];
	t[R[x]].tp=-1;
}
void TAG(int x,LL y){
	t[x].v+=t[x].tp*y;
	t[x].s+=t[x].d*y;
	t[x].tag+=y;
}
void pushup(int x){
	t[x].s=t[x].v+t[t[x].ch[0]].s+t[t[x].ch[1]].s;
	t[x].d=t[x].tp+t[t[x].ch[0]].d+t[t[x].ch[1]].d;
	t[x].siz=t[t[x].ch[0]].siz+t[t[x].ch[1]].siz+1;
}
void pushdown(int x){
	if (t[x].tag){
		if (t[x].ch[0]) TAG(t[x].ch[0],t[x].tag);
		if (t[x].ch[1]) TAG(t[x].ch[1],t[x].tag);
		t[x].tag=0;
	}
}
void debug(int x){
	pushdown(x);
	if (t[x].ch[0]) debug(t[x].ch[0]);
	printf("%lld ",t[x].v);
	if (t[x].ch[1]) debug(t[x].ch[1]);
}
int build(int l,int r,int fa){
	int m=l+r>>1;
	t[m].s=t[m].v;
	t[m].fa=fa;
	if (l<=m-1) t[m].ch[0]=build(l,m-1,m);
	if (m+1<=r) t[m].ch[1]=build(m+1,r,m);
	pushup(m);
	return m;
}
void rot(int x){
	int fa=t[x].fa;
	int gfa=t[fa].fa;
	int t1=(x!=t[fa].ch[0]);
	int t2=(fa!=t[gfa].ch[0]);
	int ch=t[x].ch[t1^1];
	if (!isroot(fa)) t[gfa].ch[t2]=x;
	t[ch].fa=fa;
	t[x].fa=gfa;
	t[x].ch[t1^1]=fa;
	t[fa].fa=x;
	t[fa].ch[t1]=ch;
	pushup(fa);
	pushup(x);
}
void splay(int x,int too){
	if (too==0) rt=x;
	if (too==-1) too=rt;
	stk[top=1]=x;
	int y=x;
	while (!isroot(y)) stk[++top]=t[y].fa,y=t[y].fa;
	FORD(rp,top,1) pushdown(stk[rp]);
	while (t[x].fa!=too){
		int fa=t[x].fa;
		int gfa=t[fa].fa;
		if (gfa!=too)
			x==t[fa].ch[0]^fa==t[gfa].ch[0]?rot(x):rot(fa);
		rot(x);
	}
}
int rnk(int x){
	splay(x,0);
//	debug(rt),puts("");
	return t[t[x].ch[0]].siz+1;
}
int fnd(int x,int k){
	if (x==-1) x=rt;
//	debug(rt),puts("");
	if (t[t[x].ch[0]].siz>=k) return fnd(t[x].ch[0],k);
	else if (k==t[t[x].ch[0]].siz+1) return x;
	else return fnd(t[x].ch[1],k-t[t[x].ch[0]].siz-1);
}
int main(){
//	freopen("t.in","r",stdin);
//	freopen("t.out","w",stdout);
	scanf("%d",&n);
	FOR(i,2,n){
		int t;
		getint(t);
		ae(t,i);
	}
	FOR(i,1,n) getint(a[i]);
	dfs(1,0);
	tot=dfn;
	rt=build(1,tot,0);
	t[tot+1].ch[1]=tot+2;
	t[tot+2].fa=tot+1;
	t[tot+2].ch[0]=rt;
	t[rt].fa=tot+2;
	rt=tot+1;
	scanf("%d",&m);
//	debug(rt),puts("");
	while (m--){
		scanf("%s",s);
	//	debug(rt),puts("");
		if (s[0]=='Q'){
			getint(x);
		//	debug(rt),puts("");
			int h=fnd(-1,rnk(L[x])+1);
		//	debug(rt),puts("");
			splay(h,0);
			pushdown(rt);
			printf("%lld\n",t[t[h].ch[0]].s);
		}
		else if (s[0]=='F'){
			getint(x),getint(y);
			X=fnd(-1,rnk(L[x])-1);
			Y=fnd(-1,rnk(R[x])+1);
			splay(X,0);
			splay(Y,-1);
		//	debug(rt),puts("");
			TAG(t[t[rt].ch[1]].ch[0],y);
			pushdown(rt);
			pushdown(t[rt].ch[1]);
			pushup(t[rt].ch[1]);
			pushup(rt);
		}
		else{
			getint(x),getint(y);
			X=fnd(-1,rnk(L[x])-1);
			Y=fnd(-1,rnk(R[x])+1);
			splay(X,0);
			splay(Y,-1);
		//	debug(rt),puts("");
			int w=t[t[rt].ch[1]].ch[0];
			t[t[w].fa].ch[0]=0;
			pushup(t[w].fa);
			pushup(rt);
		//	debug(rt),puts("");
			X=fnd(-1,rnk(L[y]));
			Y=fnd(-1,rnk(L[y])+1);
			splay(X,0);
			splay(Y,-1);
		//	debug(rt),puts("");
			t[Y].ch[0]=w;
			t[w].fa=Y;
			pushup(Y);
			pushup(X);
		}
	}
	return 0;
}

化不合法为不优

O(time)->O(ans)

http://www.lydsy.com/JudgeOnline/problem.php?id=4716

正解是用个堆,然而有个非常高明的”暴力“。

#pragma GCC optimize("O2")
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<iostream>
#include<algorithm>
#define rep(i,a,b) for (register int i=a; i<=b; i++)
#define per(i,a,b) for (int i=a; i>=b; i--)
typedef long long ll;
using namespace std;
typedef pair<int,int> Pii;
typedef vector<int> vec;
inline void read(int &x) {
    x=0; char c=getchar(); int f=1;
    while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();}
    while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;
}
const int N = 1050;
int n,m,xmn,ymn,k;
int a[N][N];
ll s[N][N];
inline ll getsum(int x0, int y0, int x1, int y1) {
    x0--; y0--;
    return s[x1][y1]-s[x0][y1]-s[x1][y0]+s[x0][y0];
}
inline int getans(int val) { //"<=val"
    int res=0; //if (res>=k) return res;
    rep(i,1,n-xmn+1) rep(j,1,m-ymn+1) {
        register int x=i+xmn-1,y=j+ymn-1;
        for (;x<=n;x++) {
            if (getsum(i,j,x,y)>val) break;
            rep(Y,y,m) {
                if (getsum(i,j,x,Y)>val) break;
                res++; if (res>=k) return res;
            }
        }
    }
    return res;
}
int main() {
    read(n); read(m); read(xmn); read(ymn); read(k);
    rep(i,1,n) rep(j,1,m) read(a[i][j]);
    rep(i,1,n) rep(j,1,m) s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i][j];
    int l=0,r=n*m*2000;
    while (l<r) {
        register int mid=(1LL*l+r)>>1;
        if (getans(mid)<k) l=mid+1; else r=mid;
    }
    printf("%d",l+1);
    return 0;
}

可以证明,每次getans函数,里面有用的解只有O(k)个,没用的解(多余的复杂度)也只有O(k+nm)个,所以复杂度是O(nm+k)。

生成树计数-基尔霍夫定理

http://vfleaking.blog.163.com/blog/static/1748076342013112523651955/

https://www.cnblogs.com/GerynOhenz/p/4450417.html

http://blog.csdn.net/werkeytom_ftd/article/details/60766200

 

找规律

BZOJ1228

这个SG规律真难找,找了半天也找不出最后看了题解。

这个规律不是O(1)的,是O(log)的。

SG[i/2][j/2]->SG[i][j]

int GetSG(int n,int m){
    if((n&1)&&(m&1)) return 0;
    if(!(n&1)&&!(m&1)) return GetSG(n/2,m/2)+1;
    return (n&1)?GetSG((n+1)/2,m/2)+1:GetSG(n/2,(m+1)/2)+1;
}
#include<cstdio>
#include<cstring>
#include<iostream>
#include<stdlib.h>
#include<ctime>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define LL long long
#define FOR(i,a,b) for (int i=a;i<=b;i++)
#define FORD(i,a,b) for (int i=a;i>=b;i--)
using namespace std;
typedef pair<int,int> pa;
void getint(int &v){
    char ch,fu=0;
    for(ch='*'; (ch<'0'||ch>'9')&&ch!='-'; ch=getchar());
    if(ch=='-') fu=1, ch=getchar();
    for(v=0; ch>='0'&&ch<='9'; ch=getchar()) v=v*10+ch-'0';
    if(fu) v=-v;
}
int tmp[500010],ts,mex,sg[555][555];
int main(){
	FOR(t,2,60)
		FOR(j,1,t-1){
			int i=t-j;
			ts=0;
			FOR(k,1,i-1) tmp[++ts]=sg[k][i-k];
			FOR(k,1,j-1) tmp[++ts]=sg[k][j-k];
			sort(tmp+1,tmp+ts+1);
			ts=unique(tmp+1,tmp+ts+1)-tmp-1;
			mex=-1;
			FOR(k,1,ts)
				if (tmp[k]==mex+1) ++mex;
			sg[i][j]=mex+1;
		}
	FOR(i,1,30){
		FOR(j,1,30) printf("%d ",sg[i][j]);
		puts("");
	}
	return 0;
}

http://www.lydsy.com/JudgeOnline/problem.php?id=3288

欧拉函数前缀积。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<stdlib.h>
#include<ctime>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define LL long long
#define FOR(i,a,b) for (int i=a;i<=b;i++)
#define FORD(i,a,b) for (int i=a;i>=b;i--)
using namespace std;
typedef pair<int,int> pa;
void getint(int &v){
    char ch,fu=0;
    for(ch='*'; (ch<'0'||ch>'9')&&ch!='-'; ch=getchar());
    if(ch=='-') fu=1, ch=getchar();
    for(v=0; ch>='0'&&ch<='9'; ch=getchar()) v=v*10+ch-'0';
    if(fu) v=-v;
}
const int MO=1e9+7;
int n,phi[1000010],p[1000010],pr;
LL ans;
bool u[1000010];
LL pw(LL x,LL y){
	LL t=1;
	for (;y;y>>=1){
		if (y&1) t=t*x%MO;
		x=x*x%MO;
	}
	return t;
}
void getp(){
	phi[1]=1;
	FOR(i,2,n){
		if (!u[i]){p[++pr]=i;phi[i]=i-1;}
		FOR(j,1,pr){
			if (i*p[j]>n) break;
			u[i*p[j]]=1;
			if (i%p[j]==0){phi[i*p[j]]=phi[i]*p[j];break;}
			else phi[i*p[j]]=phi[i]*phi[p[j]];
		}
	}
}
int main(){
	cin>>n;
	getp();
	ans=1;
	FOR(i,1,n) ans=ans*phi[i]%MO;
	cout<<ans<<endl;
	return 0;
}
#include<cstdio>
#include<cstring>
#include<iostream>
#include<stdlib.h>
#include<ctime>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define LL long long
#define FOR(i,a,b) for (int i=a;i<=b;i++)
#define FORD(i,a,b) for (int i=a;i>=b;i--)
using namespace std;
typedef pair<int,int> pa;
void getint(int &v){
    char ch,fu=0;
    for(ch='*'; (ch<'0'||ch>'9')&&ch!='-'; ch=getchar());
    if(ch=='-') fu=1, ch=getchar();
    for(v=0; ch>='0'&&ch<='9'; ch=getchar()) v=v*10+ch-'0';
    if(fu) v=-v;
}
const int MO=1e9+7;
int a[1010][1010],n,k;
LL pw(LL x,LL y){
	LL t=1;
	for (;y;y>>=1){
		if (y&1) t=t*x%MO;
		x=x*x%MO;
	}
	return t;
}
LL det(){
	LL ans=1;
	FOR(i,1,n){
		k=0;
		FOR(j,i,n)
			if (a[j][i]!=0){k=j;break;}
		if (!k) return 0ll;
		if (i!=k){
			FOR(j,1,n) swap(a[i][j],a[k][j]);
			ans=-ans;
		}
		LL t=pw(a[i][i],MO-2);
		FOR(j,i+1,n)
			if (a[j][i]!=0){
				LL w=-t*a[k][i]%MO;
				FOR(k,i,n) (a[j][k]+=a[i][k]*w)%=MO;
			}
	}
	FOR(i,1,n) ans=ans*a[i][i]%MO;
	return ans;
}
int main(){
	cin>>n;
	FOR(i,1,n)
		FOR(j,1,n)
			a[i][j]=__gcd(i,j);
	cout<<det()<<endl;
	return 0;
}