P3899 [湖南集训]谈笑风生 主席树解决二维数点
傳送門
文章目錄
- 題意:
- 思路:
題意:
思路:
由于a,ba,ba,b都比ccc厲害,那么a,ba,ba,b一定是某個(gè)是某個(gè)的祖先。那么就分為兩種情況了:
(1)(1)(1) bbb在aaa上面,約定depth[1]=1depth[1]=1depth[1]=1,此時(shí)答案顯然為min(depth[a]?1,k)?(se[a]?1)min(depth[a]-1,k)*(se[a]-1)min(depth[a]?1,k)?(se[a]?1)。
(2)(2)(2)bbb在aaa的下下面,這個(gè)時(shí)候就不是那么容易搞了,問(wèn)題轉(zhuǎn)化成我們要求aaa這個(gè)子樹(shù)中depthdepthdepth范圍在[depth[a]+1,depth[a]+k][depth[a]+1,depth[a]+k][depth[a]+1,depth[a]+k]內(nèi)的所有點(diǎn)的se[i]?1se[i]-1se[i]?1,先考慮暴力怎么寫呢?顯然我們可以暴力對(duì)這顆子樹(shù)的深度建線段樹(shù),這樣就變成了區(qū)間查詢的問(wèn)題了。而這樣復(fù)雜度是肯定不行的,所以我們考慮建可持久化線段樹(shù),根據(jù)樹(shù)的dfsdfsdfs序建主席樹(shù),以節(jié)點(diǎn)深度為下標(biāo),那么查詢就變成了query(root[dfn[p]],root[dfn[p]+se[p]?1],1,n,depth[p]+1,min(depth[p]+k,n))query(root[dfn[p]],root[dfn[p]+se[p]-1],1,n,depth[p]+1,min(depth[p]+k,n))query(root[dfn[p]],root[dfn[p]+se[p]?1],1,n,depth[p]+1,min(depth[p]+k,n)),再加上(1)(1)(1)的答案即可。
我們還可以將其轉(zhuǎn)換成二維數(shù)點(diǎn)的問(wèn)題。
通過(guò)以上分析不難發(fā)現(xiàn)我們要找的點(diǎn)就是
深度范圍是[depth[p]+1,depth[p]+k][depth[p]+1,depth[p]+k][depth[p]+1,depth[p]+k],dfsdfsdfs序范圍是[dfn[p],dfn[p]+se[p]?1][dfn[p],dfn[p]+se[p]-1][dfn[p],dfn[p]+se[p]?1],那么我們以深度建立xxx軸,以dfsdfsdfs序建立yyy軸,讓后統(tǒng)計(jì)就好啦。
主席樹(shù)O(nlogn)O(nlogn)O(nlogn)
//#pragma GCC optimize(2) #include<cstdio> #include<iostream> #include<string> #include<cstring> #include<map> #include<cmath> #include<cctype> #include<vector> #include<set> #include<queue> #include<algorithm> #include<sstream> #include<ctime> #include<cstdlib> #define X first #define Y second #define L (u<<1) #define R (u<<1|1) #define pb push_back #define mk make_pair #define Mid (tr[u].l+tr[u].r>>1) #define Len(u) (tr[u].r-tr[u].l+1) #define random(a,b) ((a)+rand()%((b)-(a)+1)) #define db puts("---") using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); } //void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); } //void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f; const double eps=1e-6;int n,m; int root[N],tot,idx; int dfn[N],se[N],depth[N]; vector<int>v[N]; struct Node {int l,r;LL sum; }tr[N*40];void insert(int p,int &q,int l,int r,int pos,int x) {q=++tot; tr[q]=tr[p];tr[q].sum+=x;if(l==r) return;int mid=l+r>>1;if(pos<=mid) insert(tr[p].l,tr[q].l,l,mid,pos,x);else insert(tr[p].r,tr[q].r,mid+1,r,pos,x); }LL query(int p,int q,int l,int r,int ql,int qr) {if(l>=ql&&r<=qr) return tr[q].sum-tr[p].sum;LL ans=0;int mid=l+r>>1;if(ql<=mid) ans+=query(tr[p].l,tr[q].l,l,mid,ql,qr);if(qr>mid) ans+=query(tr[p].r,tr[q].r,mid+1,r,ql,qr);return ans; }void dfs1(int u,int fa) {se[u]=1; dfn[u]=++idx;depth[u]=depth[fa]+1;for(auto x:v[u]) if(x!=fa) dfs1(x,u),se[u]+=se[x]; }void dfs2(int u,int fa) {insert(root[dfn[u]-1],root[dfn[u]],1,n,depth[u],se[u]-1);for(auto x:v[u]) if(x!=fa) dfs2(x,u); }int main() { // ios::sync_with_stdio(false); // cin.tie(0);scanf("%d%d",&n,&m);for(int i=1;i<=n-1;i++){int a,b; scanf("%d%d",&a,&b);v[a].pb(b); v[b].pb(a);}dfs1(1,0); dfs2(1,0);while(m--){int p,k; scanf("%d%d",&p,&k);LL ans=1ll*min(depth[p]-1,k)*(se[p]-1);ans+=query(root[dfn[p]],root[dfn[p]+se[p]-1],1,n,depth[p]+1,min(depth[p]+k,n));printf("%lld\n",ans);}return 0; } /**/總結(jié)
以上是生活随笔為你收集整理的P3899 [湖南集训]谈笑风生 主席树解决二维数点的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 颅脑损伤后遗症
- 下一篇: P3293 [SCOI2016]美味