考前模版整理

快速幂

ll pow(ll n, ll t)
    {
    ll ret = 1;
    for(; t; t >>= 1, n = (n * n) % mo)
        if(t & 1) ret = (ret * n) % mo;
    return ret;
    }

快速幂求逆元

ll inv(ll n, ll p)
    {
    ll ret = 1;
    for(ll i = p; i; i >>= 1, n = (n * n) % mo)
        if(i & 1) ret = (ret * n) % mo;
    return ret;
    }
int main()
    {
    inv(b,mo - 2)
//a/b,求b对mo的逆元
    }

快速乘(求x和y的乘积)

ll mul(ll x, ll y)
    {
    ll ret = 0;
    for(; y; y >>= 1, x = (x + x) % P)
        if(y & 1) ret = (ret + x) % P;
    return ret;
    }

int heap[10010], heap_size;
void put(int d)
    {
    int now, next;
    heap[++heap_size] = d;
    now = heap_size;
    while(now > 1)
        {
        next = now >> 1;
        if(heap[now] >= heap[next]) return;
        swap(heap[now], heap[next]);
        now = next;
        }
    }
int get()
    {
    int res = heap[1], now, next;
    heap[1] = heap[heap_size--];
    now = 1;
    while(now * 2 <= heap_size)
        {
        next = now << 1;
        if(next < heap_size && heap[next] > heap[next|1]) next|=1;
        if(heap[next] >= heap[now]) break;
        swap(heap[next], heap[now]);
        now = next;
        }
    return res;
    }

单调队列(原题)

struct Que
	{
	int head, tail, ck[10000 + 3];
	void fresh() {head = 1; tail = 0;}
	void pop_top(RG int p)
		{ while(head <= tail && ck[head] + B < p) ++head;}
	void push(RG int i, RG ll k, RG int p)
		{
			while(head <= tail && f[i - 1][ck[tail]] - G[i] * X[ck[tail]] > k) --tail;
			ck[++tail] = p;
		}
	int front() { return ck[head];}
	}Q;

 最长上升/下降子序列(nlogn写法)+判断是否一定在解内

	for(RG int i = 1; i <= n; ++i)
		{
		if(a[i] > d[len]) d[++len] = a[i], f[i] = len;
		else
			{
			RG int L = 1, R = len, mid;
			while(L <= R)
				{
				mid = L + R >> 1;
				if(d[mid] >= a[i]) R = mid - 1;
				else L = mid + 1;
				}
			d[L] = a[i];
			f[i] = L;
			}
		}
	len = 0, d[0] = oo;
	for(RG int i = n; i; --i)
		{
		if(a[i] < d[len]) d[++len] = a[i], g[i] = len;
		else
			{
			RG int L = 1, R = len, mid;
			while(L <= R)
				{
				mid = L + R >> 1;
				if(d[mid] <= a[i]) R = mid - 1;
				else L = mid + 1;
				}
			d[L] = a[i];
			g[i] = L;
			}
		}

最短路(dijkstra+判断是否在路径内+标记重复)

#include <queue>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define oo 0x7fffffff - 1
#define max(a, b) ((a) > (b) ? (a) : (b))
using namespace std;
inline int read()
	{
	int c = getchar(), x = 0;
	while(c < '0' || c > '9') c = getchar();
	while(c >= '0' &&  c <= '9') x = (x << 1) + (x << 3) + c - '0', c = getchar();
	return x;
	}
int ans, n, m, A, B, C, D, cnt, ncnt, fcnt, h[50003], nh[50003], fh[50003], d1[50003], d2[50003], hash[200003], du[50003], Q[50003], f[50003];
struct pt {int v, w, ne;} a[200003], na[200003], fa[200003];
struct ed {int u, v, w;} e[200003];
struct abcd {
	int fir, sec;
	bool operator < (const abcd oth) const {return sec > oth.sec;} };
priority_queue<abcd> q;
bool mark[50003];
inline void link(int u, int v, int w) {a[++cnt].v = v, a[cnt].w = w, a[cnt].ne = h[u], h[u] = cnt;}
inline void nlink(int u, int v, int w) {na[++ncnt].v = v, na[ncnt].w = w, na[ncnt].ne = nh[u], nh[u] = ncnt;}
inline void flink(int u, int v, int w) {fa[++fcnt].v = v, fa[fcnt].w = w, fa[fcnt].ne = fh[u], fh[u] = fcnt;}
void dijkstra(int x, int d[], pt a[], int h[])
	{
	for(int i = 1; i <= n; ++i) d[i] = oo, mark[i] = 0;
	d[x] = 0; q.push((abcd){x, 0});
	while(!q.empty())
		{
		int now = q.top().fir; q.pop();
		if(mark[now]) continue;
		mark[now] = 1;
		for(int j = h[now]; j; j = a[j].ne)
			if(!mark[a[j].v] && d[a[j].v] > d[now] + a[j].w)
				{
				d[a[j].v] = d[now] + a[j].w;
				q.push((abcd){a[j].v, d[a[j].v]});
				}
		}
	}
bool preced()
	{
	dijkstra(A, d1, a, h);
	dijkstra(B, d2, na, nh);
	if(d1[B] == oo || d2[A] == oo) return 0;
	for(int j = 1; j <= m; ++j) if(d1[e[j].u] + e[j].w + d2[e[j].v] == d1[B]) ++hash[j];
	dijkstra(C, d1, a, h);
	dijkstra(D, d2, na, nh);
	if(d1[D] == oo || d2[C] == oo) return 0;
	for(int j = 1; j <= m; ++j)
		{
		if(d1[e[j].u] + e[j].w + d2[e[j].v] == d1[D]) ++hash[j];
		if(hash[j] == 2) {flink(e[j].u, e[j].v, e[j].w); ++du[e[j].v];}
		}
	return 1;
	}
void sortdp()
	{
	for(int i = 1; i <= n; ++i) {f[i] = 1; if(!du[i]) Q[++Q[0]] = i;}
	for(int i = 1; i <= Q[0]; ++i)
		{
		int now = Q[i];
		for(int j = fh[now]; j; j = fa[j].ne)
			{
			f[fa[j].v] = max(f[fa[j].v], f[now] + 1);
			ans = max(ans, f[fa[j].v]);
			--du[fa[j].v];
			if(!du[fa[j].v]) Q[++Q[0]] = fa[j].v;
			}
		}
	printf("%d\n", ans);
	}
int main()
	{
	freopen("game.in", "r", stdin), freopen("game.out", "w", stdout);
	n = read(), m = read();
	for(int i = 1; i <= m; ++i)
		{
		e[i].u = read(), e[i].v = read(), e[i].w = read();
		link(e[i].u, e[i].v, e[i].w);
		nlink(e[i].v, e[i].u, e[i].w);
		}
	A = read(), B = read(), C = read(), D = read();
	if(preced()) sortdp();
	else puts("-1");
	fclose(stdin), fclose(stdout);
	return 0;
	} 

树链剖分 LCA

#include<stdio.h>
#pragma GCC optimize("O2")
using namespace std;
#define inline __inline__ __attribute__((always_inline))
namespace io{
	const int MAXBUF=1<<9;
	char B[MAXBUF],*S=B,*T=B;
	#define gt() (S == T && (T = (S = B) + fread(B, 1, MAXBUF, stdin), S == T) ? 0 : *S++)
	template<class Type> inline Type read(){
		Type aa=0;bool bb=0;char c,*S=io::S,*T=io::T;
		for(c=gt();(c<'0'||c>'9')&&c!='-';c=gt())
			;
		for(c=='-'?bb=1:aa=c-'0',c=gt();c>='0'&&c<='9';c=gt())
			aa=(aa<<1)+(aa<<3)+c-'0';
		io::S=S,io::T=T;return bb?-aa:aa;
	}
	char BUF[MAXBUF],*iter=BUF;
	template<class T> void P(T x,char c='\n'){
		static int stk[110];int O=0;char *iter=io::iter;
		if(!x)*iter++='0';
		else{
			if(x<0)x=-x,*iter++='-';
			for(;x;x/=10)stk[++O]=x%10;
			for(;O;*iter++='0'+stk[O--])
				;
		}
		*iter++=c,io::iter=iter;
	}
	inline void output(){
		fwrite(BUF,1,iter-BUF,stdout),iter=BUF;
	}
}
namespace pb_ds{
	const int N=500001;
	int (*F)()=io::read<int>;
	struct Pointer{int to;Pointer *nxt;}*fst[N];
	inline void link(int u,int v){
		static Pointer mem[N<<1],*tot=mem;
		*++tot=(Pointer){v,fst[u]},fst[u]=tot;
		*++tot=(Pointer){u,fst[v]},fst[v]=tot;
	}
	bool vis[N];
	int fa[N],dep[N],sz[N],top[N],hs[N];
	void dfs_init(int x){
		vis[x]=sz[x]=1;
		for(Pointer *iter=fst[x];iter;iter=iter->nxt)
			if(!vis[iter->to]){
				fa[iter->to]=x;
				dep[iter->to]=dep[x]+1;
				dfs_init(iter->to);
				sz[x]+=sz[iter->to];
				if(sz[hs[x]]<sz[iter->to])
					hs[x]=iter->to;
			}
	}
	void dfs_make(int x){
		vis[x]=0;
		top[x]=x^hs[fa[x]]?x:top[fa[x]];
		if(hs[x]){
			dfs_make(hs[x]);
			for(Pointer *iter=fst[x];iter;iter=iter->nxt)
				if(vis[iter->to])
					dfs_make(iter->to);
		}
	}
	inline int lca(int x,int y){
		while(top[x]^top[y])
			dep[top[x]]>dep[top[y]]
			?x=fa[top[x]]
			:y=fa[top[y]];
		return dep[x]<dep[y]?x:y;
	}
	void main(){
		int n=F(),que=F(),root=F();
		for(int i=1,x,y;i<n;i++)
			x=F(),y=F(),link(x,y);
		dfs_init(root);
		dfs_make(root);
		for(int x,y;que;que--)
			x=F(),y=F(),io::P(lca(x,y));
	}
}
int main(){
	pb_ds::main();
	io::output();
}

 

posted @ 2017-10-19 17:56  keshuqi  阅读(219)  评论(0编辑  收藏  举报