NC212613. APartyforYouStarted
描述
输入描述
Each input file only contains one test case.
The first line contains two integers N and M (1 ≤ N ≤ 2 × , 1 ≤ M ≤ ), indicating the number of the factions and the queries.
The second line contains N integers , ,……, (1 ≤ ≤ N), indicating the parent faction of the i-th faction, and there is exactly one 0 in the array.
Then followed M lines, each line contains three integers u, x, v (1 ≤ u, v ≤ N, 1 ≤ x ≤ ), indicating there are x people who belong to the faction u arrive at the party, and they asked how many people had come from the faction v.
输出描述
Print M lines, each line contains an integer indicating the answer of the security guard GYQ in order.
示例1
输入:
4 5 0 1 1 2 1 2 2 2 1 2 4 1 2 3 1 3 3 1 4
输出:
0 2 3 2 4
C++14(g++5.4) 解法, 执行用时: 143ms, 内存消耗: 10448K, 提交时间: 2020-09-28 18:00:14
#include <bits/stdc++.h> //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); //freopen("tout.txt", "w", stdout); using namespace std; typedef double dou; typedef long long ll; typedef pair<int, int> pii; typedef map<int, int> mii; #define pai acos(-1.0) #define maxn 300050 #define inf 0x3f3f3f3f const ll mod=1e9+7; #define ms(a,b) memset(a,b,sizeof(a)) #define false_stdio ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) int n,m,rt; vector<int>edge[maxn]; int cnt; ll tree[maxn]; int L[maxn],R[maxn]; void dfs(int u){ L[u]=++cnt; for(auto iter:edge[u]){ dfs(iter); } R[u]=cnt; } int lowbit(int x){ return x&(-x); } ll query(int i){ ll re=0; while(i){ re+=tree[i]; i-=lowbit(i); } return re; } void add(int i,int add){ while(i<=n){ tree[i]+=add; i+=lowbit(i); } } int main() { false_stdio; cin>>n>>m; for(int i=1,a;i<=n;i++){ cin>>a; if(a==0){ rt=i; continue; } edge[a].push_back(i); } dfs(rt); while(m--){ ll u,x,v; cin>>u>>x>>v; cout<<query(L[v])<<endl; add(L[u],x); add(R[u]+1,-x); } return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 56ms, 内存消耗: 3368K, 提交时间: 2020-10-01 10:30:27
#include<cstdio> using namespace std; typedef long long ll; const ll N = 2e4 + 5; ll c[N], h[N], t, in[N], out[N]; struct edge{ll b, l;}g[N]; void addedge(ll a, ll b){ g[++t] = (edge){b, h[a]}; h[a] = t; } void dfs(ll u){ in[u] = ++t; for(ll i = h[u];i;i = g[i].l)dfs(g[i].b); out[u] = t; } void add(ll x, ll n, ll d){ ll i; for(i = x;i <= n;i += -i & i)c[i] += d; } ll query(ll x){ ll i, ans = 0; for(i = x;i;i -= -i & i)ans += c[i]; return ans; } int main(){ ll n, m, i, j, k; scanf("%lld%lld", &n, &m); for(i = 1;i <= n;i++){ scanf("%lld", &j); if(!j)k = i; else addedge(j, i); }t = 0;dfs(k); while(m--){ scanf("%lld%lld%lld", &i, &j, &k); printf("%lld\n", query(in[k])); add(in[i], n, j);add(out[i] + 1, n, -j); } return 0; }