description
給乙個樹,n 個點,有點權,初始根是 1。 m 個操作,每次操作:input1. 將樹根換為 x。
2. 給出兩個點 x,y,從 x 的子樹中選每乙個點,y 的子樹中選每乙個點,如果兩個點點權相等,ans++,求 ans。
第一行兩個數表示 n,m。 第二行 n 個數,表示每個點的點權 a[i]。 之後 n ? 1 行 , 每行兩個數 x , y , 表示一條邊output之後 m 行,每行為 1 x 或者 2 x y。 1 x,表示將根變成 x點。 2 x y,表示查詢 x 點的子樹與 y 點的子樹。 n
<= 100000 , m <= 500000 , 1 <= a[i] <= 1000000000
對於每個詢問,輸出乙個數表示答案。sample input
5 5sample output 題解1 2 3 4 5
1 21 3
3 43 5
2 4 5
2 1 5
2 3 5
1 52 4 5
這題其實就是3083與5016的結合版你可以把乙個詢問拆成九個搞
然後卡了半小時常數。。
#include
#include
#include
#include
#include
using
namespace
std;
inline
int read()
while(ch>='0' && ch<='9')
return x*f;
}struct lsnodew[110000];int col[110000];
bool cmp(lsnode n1,lsnode n2)a[210000];int len,last[110000];
inline
void ins(int x,int y)
int dep[110000],bin[25],fa[110000][25];
int in[110000],ot[110000],dfn;
inline
void pre_tree_node(int x)
}ot[x]=dfn;
}int root;
inline
int lca(int x,int y)
inline
int go(int x,int h)
struct aska[4500010];int cnt;
inline
void add(int l,int r,int op,int pgo)
int pos[110000],block;
bool cmpx(ask n1,ask n2)
inline
void dell(int now)
inline
void addr(int now)
inline
void delr(int now)
bool v[510000];
inline
void write(int x)
inline
int fd(int x)
int main()
fa[1][0]=0;dep[1]=1;pre_tree_node(1);
sort(w+1,w+1+n,cmp);
int tt=1;col[in[w[1].p]]=1;
for(register
int i=2;i<=n;++i)
root=1;
memset(v,false,sizeof(v));
for(register
int i=1;i<=m;++i)
if(root==y && root!=x)swap(x,y);
//register int tx=lca(x,root),ty=lca(y,root);
int tx=fd(x),ty=fd(y);
if(root==x && root!=y)
else
}if(tx==x && ty!=y)swap(x,y),swap(tx,ty);//x要換根,y不用
if(tx!=x && ty!=y)
else
if(tx!=x && ty==y)
else}}
sort(a+1,a+1+cnt,cmpx);
ans=0;
for(register
int i=1;i<=a[1].l;++i)++cx[col[i]];
for(register
int i=1;i<=a[1].r;++i)ans+=cx[col[i]],++cy[col[i]];
answer[a[1].pgo]+=ans*a[1].op;
int l=a[1].l,r=a[1].r;
for(register
int i=2;i<=cnt;++i)
for(register
int i=1;i<=m;++i)if(v[i])write(answer[i]);
return
0;}
Ynoi2016 這是我自己的發明(莫隊)
話說這道題資料是不是都是鏈啊,我不手動擴棧就全 re 不過 a 了這題還是很爽的,通過昨晚到今天早上的奮鬥,終於肝出了這題 其實樓上說的也差不多了,就是把區間拆掉然後莫隊瞎搞 弱化版 bzoj snoi2017 乙個簡單的詢問 那我先講弱化版吧 可以發現 sum get l 1,r 1,x time...
演算法複習 莫隊演算法(bzoj1878)
hh有一串由各種漂亮的貝殼組成的項鍊。hh相信不同的貝殼會帶來好運,所以每次散步 完後,他都會隨意取出一 段貝殼,思考它們所表達的含義。hh不斷地收集新的貝殼,因此他的項鍊變得越來越長。有一天,他突然提出了一 個問題 某一段貝殼中,包含了多少種不同的貝殼?這個問題很難回答。因為項鍊實在是太長了。於是...
BZOJ 3585 mex 莫隊演算法
離線處理,這題完全不用離散化直接無視 n的數就可以了 因為大於n的數對答案肯定沒有影響 與bzoj3339完全一樣 似有又水了一題 include include include include include include include include include include defi...