标签:
题意:弱校的OJ的弱题,给定一棵树,和关系,然后在节点上更新时,它的所有儿子也要更新,有个判断条件,查询是所有节点的值的和
思路:与HDU 5692一样的思路,不多说了
#pragma comment(linker, "/STACK:102400000, 102400000") #include <vector> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; const int maxn=100010; vector<int>G[maxn]; ll num[maxn*4],lazy[maxn*4],min1[maxn*4]; int L[maxn],R[maxn]; int idx; void dfs(int u,int f){ L[u]=++idx; for(unsigned int i=0;i<G[u].size();i++){ int v=G[u][i]; if(v==f) continue; dfs(v,u); } R[u]=idx; } void pushup(int node){ num[node]=num[node<<1]+num[node<<1|1]; min1[node]=min(min1[node<<1],min1[node<<1|1]); } void pushdown(int node,int m){ if(lazy[node]){ lazy[node<<1]+=lazy[node]; lazy[node<<1|1]+=lazy[node]; num[node<<1]+=lazy[node]*(ll)(m-(m>>1)); num[node<<1|1]+=lazy[node]*(ll)(m>>1); min1[node<<1]+=lazy[node]; min1[node<<1|1]+=lazy[node]; lazy[node]=0; } } void buildtree(int le,int ri,int node){ if(le==ri){ min1[node]=num[node]=1; return ; } int t=(le+ri)>>1; buildtree(le,t,node<<1); buildtree(t+1,ri,node<<1|1); pushup(node); } void update(int l,int r,ll x,int le,int ri,int node){ if(l<=le&&ri<=r){ lazy[node]+=x; num[node]+=x*(ll)(ri-le+1); min1[node]+=x; return ; } pushdown(node,ri-le+1); int t=(le+ri)>>1; if(l<=t) update(l,r,x,le,t,node<<1); if(r>t) update(l,r,x,t+1,ri,node<<1|1); pushup(node); } ll query_sum(int l,int r,int le,int ri,int node){ if(l<=le&&ri<=r) return num[node]; pushdown(node,ri-le+1); ll ans=0; int t=(le+ri)>>1; if(l<=t) ans+=query_sum(l,r,le,t,node<<1); if(r>t) ans+=query_sum(l,r,t+1,ri,node<<1|1); return ans; } ll query_min(int l,int r,int le,int ri,int node){ if(l<=le&&ri<=r) return min1[node]; pushdown(node,ri-le+1); ll ans=999999999999ll; int t=(le+ri)>>1; if(l<=t) ans=min(ans,query_min(l,r,le,t,node<<1)); if(r>t) ans=min(ans,query_min(l,r,t+1,ri,node<<1|1)); return ans; } int main(){ int T,cas=1,n,m,a,b,c; char ch[10]; scanf("%d",&T); while(T--){ scanf("%d",&n); for(int i=0;i<maxn;i++) G[i].clear(); for(int i=0;i<n-1;i++){ scanf("%d%d",&a,&b); G[a].push_back(b); } idx=0;dfs(1,0); buildtree(1,n,1); memset(lazy,0,sizeof(lazy)); scanf("%d",&m); printf("Case #%d:\n",cas++); while(m--){ scanf("%s",&ch); if(ch[0]=='C'){ scanf("%d%d",&b,&c); ll kk=(ll)c; ll minmin=query_min(L[b],R[b],1,n,1); if(c+minmin<0) kk=-minmin; update(L[b],R[b],kk,1,n,1); }else if(ch[0]=='Q'){ scanf("%d",&b); ll answe=query_sum(L[b],R[b],1,n,1); printf("%lld\n",answe); } } } return 0; }
标签:
原文地址:http://blog.csdn.net/dan__ge/article/details/51474008