当前位置:网站首页>牛客挑战赛57C题解
牛客挑战赛57C题解
2022-06-22 11:09:00 【caoyang1123】
最近题刷了一堆,结果碰上这种裸的数据结构题(套路题)不会做了(场上想了个对询问分块,还去搞了个O(1)求k级祖先,结果被卡爆)......remake吧。
直接大力树剖,每条修改的链被划分为log段,向上的修改存在孩子处,向下的存在父亲处,注意向上的时候跳轻边处理下即可。
#include<bits/stdc++.h>
#define pii pair<int,int>
#define fi first
#define sc second
#define pb push_back
#define ll long long
#define trav(v,x) for(auto v:x)
#define all(x) (x).begin(), (x).end()
#define VI vector<int>
#define VLL vector<ll>
#define pll pair<ll, ll>
#define double long double
//#define int long long
using namespace std;
const int N = 1e6 + 100;
const int inf = 1e9;
const ll mod = 998244353;//1e9 + 7;
#ifdef LOCAL
void debug_out(){cerr << endl;}
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T)
{
cerr << " " << to_string(H);
debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
int n, qnum;
ll a[N];
VI adj[N];
int gson[N], sz[N], dep[N], fa[N];
void dfs0(int x, int ff)
{
fa[x] = ff;
sz[x] = 1;
trav(v, adj[x])
{
if(v == ff)
continue;
dep[v] = dep[x] + 1;
dfs0(v, x);
sz[x] += sz[v];
if(sz[v] > sz[gson[x]])
gson[x] = v;
}
}
int top[N], dfn[N], tim;
void dfs1(int x, int tp, int ff)
{
top[x] = tp;
dfn[x] = ++tim;
if(gson[x])
{
dfs1(gson[x], tp, x);
trav(v, adj[x])
{
if(v == ff || v == gson[x])
continue;
dfs1(v, v, x);
}
}
}
int cal_lca(int x, int y)
{
int tx, ty;
tx = top[x], ty = top[y];
while(tx != ty)
{
if(dep[tx] < dep[ty])
swap(x, y), swap(tx, ty);
x = fa[tx];
tx = top[x];
}
return dep[x] <= dep[y] ? x : y;
}
int fen[2][N];
ll val[N];
void add(int op, int l, int r)
{
++r;
for(; l <= tim; l += l & -l)
fen[op][l]++;
for(; r <= tim; r += r & -r)
fen[op][r]--;
}
ll ask(int op, int x)
{
ll res = 0;
for(; x; x -= x & -x)
res += fen[op][x];
return res;
}
void doit(int op, int x, int y)
{
if(x == y)
return;
int tx = top[x];
while(1)
{
if(dep[tx] <= dep[y])
{
int lp = dfn[gson[y]];
int rp = dfn[x];
add(op, lp, rp);
break;
}
else
{
int lp = dfn[tx];
int rp = dfn[x];
add(op, lp, rp);
int fx = fa[tx];
if(op == 0)
val[fx] += a[tx];
x = fx;
tx = top[x];
}
}
}
void sol()
{
cin >> n >> qnum;
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int i = 1; i < n; i++)
{
int x, y;
cin >> x >> y;
adj[x].pb(y);
adj[y].pb(x);
}
dep[1] = 1, dfs0(1, 0);
dfs1(1, 1, 0);
while(qnum--)
{
int op;
cin >> op;
if(op == 1)
{
int x, y;
cin >> x >> y;
//cerr << "!!!!!!" << op << ' ' << x << ' ' << y << '\n';
int lca = cal_lca(x, y);
// /cerr << lca << '\n';
doit(0, x, lca);
doit(1, y, lca);
}
else
{
int x;
cin >> x;
//cerr << ">>" << op << ' ' << x << '\n';
ll res = 0;
res = val[x];
if(gson[x])
res += ask(0, dfn[gson[x]]) * a[gson[x]];
res += ask(1, dfn[x]) * a[fa[x]];
cout << res << '\n';
}
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
// int tt;
// cin >> tt;
// while(tt--)
sol();
}边栏推荐
- In depth analysis of business model of blind box software development in 2022
- 图片是是用啥解析的dn的binlog的tso,直接用mysqlbinlog好像没有?
- Heidisql inserts records. There are always errors. How do you change them?
- [cloud picture] episode 244 three minute understanding of container image service
- Electron ajoute une base de données SQLite
- Security risks exist in open source code: an average of 49 vulnerabilities exist in a project
- CVPR 2022 oral | a new motion oriented point cloud single target tracking paradigm
- TCP 3次握手的通俗理解
- The R language uses the matchit package for propensity matching analysis and match The data function builds the matched sample set, uses the LM function to build the linear regression model for the ma
- Redis常用命令
猜你喜欢

The software used is PHP MySQL database

Attack and defense drill | threat hunting practice case based on att & CK

Security risks exist in open source code: an average of 49 vulnerabilities exist in a project

Development technology of NFT trading platform digital collection system

electron添加SQLite數據庫

xlrd. biffh. XLRDError: Excel xlsx file; Not supported solution

From prototype chain to inheritance, illustrate the context and recommend collection

微信小程序项目实例——图片处理小工具(自制低配版美图秀秀)

2022 the latest software testing classic summarized by major manufacturers. After reading it, I'm not afraid I won't get an offer

Puzzle (019) plane forward problem
随机推荐
GEE——Global Flood Database v1 (2000-2018)
R语言epiDisplay包的idr.display函数获取泊松回归poisson模型的汇总统计信息(初始事件密度比IDR值、调整事件密度比IDR值及其置信区间、Wald检验的p值和似然比检验的p值)
6-9 inter application communication - sub application communication
rtklib postpos 梳理(以单点定位为例)
Interpretation of MAML (model agnostic meta learning)
云端极简部署Svelte3聊天室
The first "cyborg" in the world died, and he only transformed himself to "change his life against the sky"
NFT交易平台数字藏品系统开发技术
A special file upload
奋斗吧,程序员——第四十二章 会挽雕弓如满月,西北望,射天狼
The R language dplyr package mutate function divides two numeric variables in the dataframe to create a new data column (create a new variable)
Puzzle (019) plane forward problem
[shell] collection of common instructions
[user case - intelligent manufacturing] Digital generous "cloud" collaboration, leap over Qianshan "guarantee" production!
AGCO AI frontier promotion (6.22)
安装pygame
R语言dplyr包mutate函数将dataframe中的两个数值变量相除创建新的数据列(Create a new variable)
QQ email for opencv face recognition
Save: software analysis, verification and test platform
How many of the eight classic MySQL errors did you encounter?