1. 程式人生 > >[spoj COT2]樹上莫隊

[spoj COT2]樹上莫隊

std ace 長度 can uniq 編號 真的是 remove log

題目鏈接:http://www.spoj.com/problems/COT2/

學會了樹上莫隊,真的是太激動了!參照博客:http://codeforces.com/blog/entry/43230 講的十分清楚。

#include<bits/stdc++.h>
using namespace std;

const int MAXN=40005;
const int maxm=100005;

int cur;
int sat[MAXN];
int ean[MAXN];
int A[MAXN*2];
int a[MAXN];
int cou[MAXN];
int vis[MAXN];

int block_size;

struct Query { int id; int l,r; int ex; bool operator < (const Query& q) const { int b1=(l-1)/block_size; int b2=(q.l-1)/block_size; return b1<b2 || b1==b2 && r<q.r; } } query[maxm]; int ans[maxm]; int rmq[2*MAXN];//rmq數組,就是歐拉序列對應的深度序列
struct ST { int mm[2*MAXN]; int dp[2*MAXN][20];//最小值對應的下標 void init(int n) { mm[0] = -1; for(int i = 1; i <= n; i++) { mm[i] = ((i&(i-1)) == 0)?mm[i-1]+1:mm[i-1]; dp[i][0] = i; } for(int j = 1; j <= mm[n]; j++)
for(int i = 1; i + (1<<j) - 1 <= n; i++) dp[i][j] = rmq[dp[i][j-1]] < rmq[dp[i+(1<<(j-1))][j-1]]?dp[i][j-1]:dp[i+(1<<(j-1))][j-1]; } int query(int a,int b)//查詢[a,b]之間最小值的下標 { if(a > b)swap(a,b); int k = mm[b-a+1]; return rmq[dp[a][k]] <= rmq[dp[b-(1<<k)+1][k]]?dp[a][k]:dp[b-(1<<k)+1][k]; } }; //邊的結構體定義 struct Edge { int to,next; }; Edge edge[MAXN*2]; int tot,head[MAXN]; int F[MAXN*2];//歐拉序列,就是dfs遍歷的順序,長度為2*n-1,下標從1開始 int P[MAXN];//P[i]表示點i在F中第一次出現的位置 int cnt; ST st; void init() { tot = 0; memset(head,-1,sizeof(head)); } void addedge(int u,int v)//加邊,無向邊需要加兩次 { edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++; } void dfs(int u,int pre,int dep) { sat[u]=++cur; F[++cnt] = u; rmq[cnt] = dep; P[u] = cnt; for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if(v == pre)continue; dfs(v,u,dep+1); F[++cnt] = u; rmq[cnt] = dep; } ean[u]=++cur; } void LCA_init(int root,int node_num)//查詢LCA前的初始化 { cnt = 0; dfs(root,root,0); st.init(2*node_num-1); } int query_lca(int u,int v)//查詢u,v的lca編號 { return F[st.query(P[u],P[v])]; } vector<int> ls; int nowL,nowR,nowAns; void inc(int i) // add or remove a[i] { if (vis[i]) { cou[a[i]]--; vis[i]^=1; if (cou[a[i]]==0) nowAns--; } else { cou[a[i]]++; vis[i]^=1; if (cou[a[i]]==1) nowAns++; } } int main() { int n,m; scanf("%d%d",&n,&m); for (int i=1; i<=n; i++) scanf("%d",&a[i]); for (int i=1; i<=n; i++) ls.push_back(a[i]); sort(ls.begin(),ls.end()); ls.erase(unique(ls.begin(),ls.end()),ls.end()); for (int i=1; i<=n; i++) a[i]=lower_bound(ls.begin(),ls.end(),a[i])-ls.begin()+1; init(); for(int i = 1; i < n; i++) { int u,v; scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } LCA_init(1,n); for (int i=1; i<=n; i++) A[sat[i]]=i; for (int i=1; i<=n; i++) A[ean[i]]=i; block_size=(int)sqrt(cur)+1; for (int i=1; i<=m; i++) { int u,v; scanf("%d%d",&u,&v); if (sat[u]>sat[v]) swap(u,v); int lc=query_lca(u,v); if (lc==u) { query[i].l=sat[u]; query[i].r=sat[v]; query[i].ex=-1; query[i].id=i; } else { query[i].l=ean[u]; query[i].r=sat[v]; query[i].ex=sat[lc]; query[i].id=i; } } sort(query+1,query+1+m); for (int i=1; i<=m; i++) { while (nowR<query[i].r) { nowR++; inc(A[nowR]); } while (nowL>query[i].l) { nowL--; inc(A[nowL]); } while (nowR>query[i].r) { inc(A[nowR]); nowR--; } while (nowL<query[i].l) { if (nowL) inc(A[nowL]); nowL++; } if (query[i].ex!=-1) inc(A[query[i].ex]); ans[query[i].id]=nowAns; if (query[i].ex!=-1) inc(A[query[i].ex]); } for (int i=1; i<=m; i++) printf("%d\n",ans[i]); return 0; }

[spoj COT2]樹上莫隊