当前位置:网站首页>[Vani has a date] tail on rainy days
[Vani has a date] tail on rainy days
2022-07-27 07:18:00 【lAWTYl】
Portal :luogu4556[Vani There's a date ] The tail of a rainy day
Today, we are mainly going to learn about the merging of line segments and trees , But it feels so simple Write a dynamic opening point and it's gone qwq.
The main idea of the topic
Here's a tree for you , also m m m operations , Select one at a time from x x x To y y y The path of , Then put a type of z z z The items . After the operation is completed, the number of the type with the most items in all nodes is output , If there are the same number, output the one with small number .
analysis
First of all, when I see this problem , Immediately thought of the difference in the tree . After the difference, we have to consider how to make prefix and .
If all kinds of objects are the same , Then make prefix and d f s dfs dfs Just do it again . But now it's different , So we have to consider the merging of segment trees .
The difference part of the tree must not open a bucket at every point , Then the space must blow up , So let's consider the overall situation and open a v e c t o r vector vector To record the difference , Then when the line segment tree is merged, it is taken out for processing .
Then the merged part of the line segment tree , It is equivalent to opening a segment tree at each point , The number of the most item types in the subtree used to record this point d a t dat dat And quantity n u m num num, It is worth noting that , Segment tree is a maintenance value range ( Types of goods ) The information of .
The rest is the board that combines the difference on the tree and the segment tree .
#include<bits/stdc++.h>
using namespace std;
#define in read()
#define MAXN 100100
#define MAXM MAXN << 2
#define INFI 100100
#define ls(p) t[p].ls
#define rs(p) t[p].rs
inline int read(){
int x = 0; char c = getchar();
while(c < '0' or c > '9') c = getchar();
while('0' <= c and c <= '9'){
x = x * 10 + c - '0'; c = getchar();
}
return x;
}
int n = 0; int m = 0;
int ans[MAXN] = {
0 };
vector<int> c[MAXN];
int tot = 0;
int first[MAXN] = {
0 };
int nxt[MAXM] = {
0 };
int to[MAXM] = {
0 };
inline void add(int x, int y){
nxt[++tot] = first[x];
first[x] = tot; to[tot] = y;
}
int dep[MAXN] = {
0 };
int f[MAXN][50] = {
0 };
void prework(int x, int fa){
dep[x] = dep[fa] + 1;
for(int i = 0; i <= 30; i++) f[x][i + 1] = f[f[x][i]][i];
for(int e = first[x]; e; e = nxt[e]){
int y = to[e];
if(y == fa) continue;
f[y][0] = x;
prework(y, x);
}
}
int Lca(int x, int y){
if(dep[x] < dep[y]) swap(x, y);
for(int i = 30; i >= 0; i--){
if(dep[f[x][i]] >= dep[y]) x = f[x][i];
if(x == y) return x;
}
for(int i = 30; i >= 0; i--)
if(f[x][i] != f[y][i])
x = f[x][i], y = f[y][i];
return f[x][0];
}
struct Tnode{
int ls, rs;
int num, dat;
}t[MAXN << 5];
int cnt = 0;
int root[MAXN] = {
0 };
inline void up(int p){
if(t[ls(p)].num >= t[rs(p)].num) t[p].num = t[ls(p)].num, t[p].dat = t[ls(p)].dat;
else t[p].num = t[rs(p)].num, t[p].dat = t[rs(p)].dat;
}
void update(int &p, int l, int r, int idx, int val){
if(!p) p = ++cnt;
if(l == r) {
t[p].num += val, t[p].dat = l; return; }
int mid = (l + r) >> 1;
if(idx <= mid) update(ls(p), l, mid, idx, val);
else update(rs(p), mid + 1, r, idx, val);
up(p);
}
int merge(int p, int q, int l, int r){
if(!p or !q) return p + q;
if(l == r) {
t[p].num += t[q].num, t[p].dat = l; return p; }
int mid = (l + r) >> 1;
t[p].ls = merge(ls(p), ls(q), l, mid);
t[p].rs = merge(rs(p), rs(q), mid + 1, r);
up(p); return p;
}
void dfs(int x, int fa){
for(int e = first[x]; e; e = nxt[e]){
int y = to[e];
if(y == fa) continue;
dfs(y, x);
merge(root[x], root[y], 1, INFI);
}
for(int i = 0; i < c[x].size(); i++){
int val = c[x][i] > 0 ? 1 : -1;
int idx = c[x][i] > 0 ? c[x][i] : -c[x][i];
update(root[x], 1, INFI, idx, val);
}
if(t[x].num == 0) ans[x] = 0;
else ans[x] = t[x].dat;
}
int main(){
n = in; m = in; cnt = n;
for(int i = 1; i <= n; i++) root[i] = i;
for(int i = 1; i < n; i++){
int x = in, y = in;
add(x, y), add(y, x);
} prework(1, 0);
for(int i = 1; i <= m; i++){
int x = in, y = in, z = in;
int lca = Lca(x, y);
c[x].push_back(z), c[y].push_back(z);
c[lca].push_back(-z), c[f[lca][0]].push_back(-z);
} dfs(1, 0);
for(int i = 1; i <= n; i++) cout << ans[i] << '\n';
return 0;
}
边栏推荐
- Interpretation of deepsort source code (V)
- ShowDoc漏洞学习——CNVD-2020-26585(任意文件上传)
- 使用pip命令切换不同的镜像源
- 如何借助自动化工具落地DevOps|含低代码与DevOps应用实践
- Calledprocesserror during pre commit install
- C4D云渲染平台选哪家合作?
- Go obtains the processing results of goroutine through channel
- 指令集董事长潘爱民出席2022 ECUG Con,为中国技术力量发声
- Watermelon book chapter 3 - linear model learning notes
- flink cdc 抽取oracle的数据,会不断的占用oracle的内存吗,最后引发ora-040
猜你喜欢

网易云信亮相 GIAC 全球互联网架构大会,解密新一代音视频架构在元宇宙场景的实践...

Pytorch notes: td3

Firefox browser, when accessing Tencent cloud server, failed to establish a secure connection.

美联储SR 11-7:模型风险管理指南(Guidance on Model Risk Management)-万字收藏

从技术原理看元宇宙的可能性:Omniverse如何“造”火星

(转帖)eureka、consul、nacos的对比1

Analysis of strong tennis cup 2021 PWN competition -- babypwn

Bert and RESNET can also be trained on mobile phones?!

VIVO应用市场APP上架总结

Pan Aimin, chairman of instruction set, attended the 2022 ecug con to speak for China's technical forces
随机推荐
TS learning (VIII): classes in TS
Visual horizontal topic bug1:filenotfounderror: could not find module 'mvcameracontrol dll‘ (or one of it
内部类--看这篇就懂啦~
Quartus:往别人的工程添加.v文件报错
OpenGL development with QT (I) drawing plane graphics
ShowDoc漏洞学习——CNVD-2020-26585(任意文件上传)
Datascience: data generation adds a small amount of noise (customizable noise) to the original data to realize the construction of new data (dataframe format data storage case)
ESP8266(ESP-12F) 第三方库使用 -- SparkFun_APDS9960 (手势识别)
MySQL limit paging query optimization practice
Error in running code: libboost_ filesystem.so.1.58.0: cannot open shared object file: No such file or directory
Gbase 8C - SQL reference 6 SQL syntax (13)
Relevant principles of MySQL index optimization
线性表 -- 栈和队列
adb指令整理
Calledprocesserror during pre commit install
基于SSM音乐网站管理系统
The possibility of metauniverse from the perspective of technical principles: how Omniverse "built" Mars
Interpretation of deepsort source code (VII)
Pytorch notes: td3
运行代码报错: libboost_filesystem.so.1.58.0: cannot open shared object file: No such file or directory