費了一番功夫,神犇 CJK 終于完成了前三道題目。“不錯,不愧是新一代神犇 啊!” JesseLiu 滿意地說道,“不過,你在算法方面的功底固然不錯。對于數據結 構的運用,你又掌握地如何呢?” 聽到“數據結構”這四個字,就連身為神犇的 CJK 也不禁嚇出一身冷汗?!澳贻p 人,現在,對于我給定一棵樹,你需要完成以下操作: 1.修改某個點的權值; 2.查詢某兩點間路徑上所有點的權值和; 3.查詢某點子樹的權值和?!? CJK 臉上閃過一絲不屑:不就是道鏈剖裸題嗎? “這只是第一問?!盝esseLiu 似乎也覺得這道題太水了,于是補充道:“完成以 上所有操作后,我還會有以下幾種詢問: 1. 詢問某個點子樹上有多少個點的權值小于等于 k; 2. 詢問某兩點之間的路徑上有多少點的權值小于等于 k;” 盡管 CJK 是神犇,但遇到這種題,也不禁感到一絲恐懼。還好,通過自己的 玄學力量,他聯系到了他的同學——你。現在,就請你 A 掉這最后一道水題。
第一行一個數 n,表示點的總數。接下來 n-1 行,一行兩個整數 x 和 y,表示 x 和 y 之間有邊相連。接下來一個整數 m1,表示第一問中操作的數量。接下來 m1 行表示操作,格式如下: “1 x y”將點 x 的權值修改為 y; “2 x y”詢問 x 到 y 路徑上的點權之和(包括 x 和 y); “3 x”詢問 x 的子樹上的點權和(包括 x); 接下來一行 m2,表示第二問中操作的數量。接下來 m2 行描述操作,格式 如下: “1 x y z”詢問 x 到 y 的路徑上有多少個點的權值小于等于 z; “2 x y”詢問 x 的子樹上有多少個點的權值小于等于 y; 每個點的初始權值為 0,默認樹根為 1 號節點。
對于每一次詢問,輸出一個整數表示詢問的答案。
樣例①:
3 1 2 1 3 3 1 1 3 1 3 1 3 1 2 2 1 2 1 1 3 1
樣例②:
5 1 2 1 3 3 4 3 5 5 3 1 1 3 1 2 4 5 1 4 -1 3 3 2 1 1 5 0 2 3 -1
樣例①:
4 2 1
樣例②:
0 1 0 2 1
對于 30%的數據,n<=100,m1<=100,m2<=100。剩下的數據保證 n<=50000; 對于另外 30%的數據,m2=0。其中有 1 組數據,樹退化為鏈; 對于 100%的數據, 保證 n<=50000,m1<=50000,m2<=50000, 保證題目中所有輸入、 輸出、中間運算不超過 int 范圍。
葉聞捷出題 NOIP 模擬測試T4
這道題的第一問是一個樹鏈剖分,在此就不作贅述了。 對于第二問,我們可以采取一種離線算法: 先將線段樹中的值清空,然后將詢問與添加操作放入一個數組,按詢問(添加)的值從小到大排序,然后按順序一個一個詢問(添加),這樣可以保證在詢問時,線段樹中的值都小于等于k。
代碼#include <cstring>#include <cmath>#include <iostream>#include <cstdio>#include <algorithm>#define lson num<<1#define rson num<<1|1using namespace std;typedef long long ll;const int maxn = 50010;int cnt, tt, n;int a[maxn], dfn[maxn], l[maxn*4], r[maxn*4], ch[maxn], top[maxn], to[maxn*2], sz[maxn];int nxt[maxn*2], head[maxn],ed[maxn], id[maxn], father[maxn], dep[maxn],tr[maxn*4], ans[maxn];struct tree_node { int x, y, z, id, t;}g[maxn*3];inline void dfs1(int x,int d) { dep[x] = d,sz[x] = 1; for(int i = head[x]; i; i = nxt[i]) if(!dep[to[i]]) { dfs1(to[i] ,d + 1),sz[x] += sz[to[i]],father[to[i]]=x; if(sz[to[i]]>sz[ch[x]]) ch[x]=to[i]; }}inline void dfs2(int x,int f) { top[x]=f,dfn[x]=++cnt; if(ch[x]) dfs2(ch[x],f); for(int i=head[x]; i; i=nxt[i]) if(to[i]!=father[x]&&to[i]!=ch[x]) dfs2(to[i],to[i]); ed[x]=cnt;}inline void update(int x,int y,int l,int r,int num) { if(l==r) {tr[num]=y;return;} int mid=(l+r)>>1; if(x<=mid) update(x,y,l,mid,lson); else update(x,y,mid+1,r,rson); tr[num]=tr[lson]+tr[rson];}inline int query(int L,int R,int l,int r,int num) { if(L<=l&&r<=R) return tr[num]; int mid=(l+r)>>1; ll ret=0; if(L<=mid) ret+=query(L,R,l,mid,lson); if(R>mid) ret+=query(L,R,mid+1,r,rson); return ret;}inline int lca(int x,int y) { int ans=0; while(top[x]!=top[y]) { if(dep[top[x]]<dep[top[y]]) swap(x,y); ans+=query(dfn[top[x]],dfn[x],1,n,1); x=father[top[x]]; } if(dep[x]>dep[y]) swap(x,y); ans+=query(dfn[x],dfn[y],1,n,1); return ans;}inline bool cmp(const tree_node x,const tree_node y) { if(x.z!=y.z) return x.z<y.z; return x.id<y.id; }int main() { freopen("input.in", "r", stdin); freopen("output.out", "w", stdout); int m, flag, x, y; scanf("%d", &n); for(int i = 1; i < n; i++) { scanf("%d%d", &x, &y); to[++tt] = y,nxt[tt] = head[x], head[x] = tt; to[++tt] = x,nxt[tt] = head[y], head[y] = tt; } dfs1(1, 1),dfs2(1, 1); scanf("%d", &m); for(int i = 1; i <= m; i++) { scanf("%d", &flag); if(flag == 1) scanf("%d%d", &x, &y), update(dfn[x], y, 1, n, 1); else if(flag == 2) scanf("%d%d", &x, &y),新聞熱點
疑難解答