列表

详情


NC212613. APartyforYouStarted

描述

Welcome to the grand party for CJLUACMer, we are glad about your arrival. Before starting, please remind that the difficulty of all the problems is absolutely not relevant to its order of permutation, so please consider the order of accomplishment by yourself. Now let us get started!

CJLUACMer comes from N factions. Some factions with the majority of people, based on broad range fields, such as Mathematics, and others may be more specific, such as Geometry, Algebra. Moreover, it contains complex relationships beyond different types of factions. For example, let faction 1(Geometry) belongs to faction 2(Mathematics) if there are two people from faction 1 and one person from faction 2 initially. We can say there are two people belongs to faction 1 or three people belongs to faction 2. People attending parties like to ask if somebody is present, so when someone from a certain faction comes, he(or she) will ask the security guard GYQ how many people have come from another particular faction, maybe wide range or just minority.

Generally, every moment there will be x people who belong to the faction u arrive at the party, and they will ask how many people had come from the faction v. The security guard GYQ had to run in to check the number of people every time, so he was so busy and sweaty.

Now given that CJLUACMer has N factions in total and M groups of people who come to the party once at a time. And given an array a, the index a_i represents the parent faction of the i-th faction, it's guaranteed that each specific faction belongs to only one superior faction(which means just a level higher), and there is exactly one 0 in the array, which indicates the whole CJLUACM association and this faction is the largest. After that, there will be M queries within the format of `u x v`, whose meaning is as described above. Could you help the security guard GYQ answer every query?

输入描述

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 a_1, a_2,……, a_n (1 ≤ a_i ≤ 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;
}

上一题