当前位置:网站首页>[ACM-ICPC 2018 Shenyang Network preliminaries] J. Ka Chang (block + DFS sequence)
[ACM-ICPC 2018 Shenyang Network preliminaries] J. Ka Chang (block + DFS sequence)
2022-04-23 09:43:00 【Zimba_】
The question :
Given a tree n n n A dot tree , There are two operations :
1. 1. 1. Give all depths to L L L Add the point weight of X X X.
2. 2. 2. Query root is X X X The point weight sum of the subtree of .
give q q q operations , For each operation 2 2 2, Output results .
( 1 ≤ n , q ≤ 1 0 5 ) (1\leq n,q \leq 10^{5}) (1≤n,q≤105)
Ideas :
If only the operation 1 1 1, We can maintain b f s bfs bfs order , In this way, the points with the same depth must be in a continuous interval .
If only the operation 2 2 2, We can maintain d f s dfs dfs order , In this way, the subtree of a point must be in a continuous interval .
But you can't have both , We can only keep one of them .
For this operation 2 2 2, We still maintain a d f s dfs dfs order , It becomes interval summation .
Now the main problem is to solve the operation 1 1 1.
We can find out , The point weights of points at the same depth must be the same . So we need to get the weight sum of a subtree , We only need to know the number of points in each depth of the subtree . But when we calculate , Cannot traverse all depths , We can't preprocess the number of all depth points of its subtree for each point . Then there is the following practice .
We classify all points by depth , Put together at the same depth .
consider Block according to the size of the classified set .
When the set size of a certain depth ≤ n \leq \sqrt{n} ≤n when , So when we want to update the depth , Direct violence + + + Single point modification , The complexity is o ( n n l o g n ) o(n\sqrt{n}logn) o(nnlogn).
When the set size of a certain depth ≥ n \geq \sqrt{n} ≥n when , The number of such collections must be less than n \sqrt{n} n, We can for each point ,
Preprocessing shows that this is less than n \sqrt{n} n The size of a set in a subtree , And then when you check , Violence traverses these depths , Get the number directly to calculate .
such , The total complexity is o ( n n l o g n ) o(n\sqrt{n}logn) o(nnlogn).
Code
ps: The depth of the question is from 0 0 0 At the beginning ...
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll mod=1000000007;
const int N = 100001;
ll c[100005];
void add(int x,ll k) {
while(x<=N) {
c[x]+=k;
x+=x&-x;
}
}
ll getSum(int x) {
ll res=0;
while(x>0) {
res+=c[x];
x-=x&-x;
} return res;
}
int n,B;
vector<int>v[100005];
vector<int>st[100005];
ll dval[100005];
int dep[100005],id[100005],idx,ed[100005],edd;
void dfs(int x,int pre)
{
id[x]=++idx;
edd=x;
dep[x]=dep[pre]+1;
for(int i=0;i<v[x].size();i++)
{
int to=v[x][i];
if(to!=pre)
{
dfs(to,x);
}
}
ed[x]=edd;
}
vector<int>dv[100005],g;
void dfs2(int x,int pre)
{
for(int i=0;i<v[x].size();i++)
{
int to=v[x][i];
if(to!=pre)
{
dfs2(to,x);
for(int j=0;j<g.size();j++)dv[x][j]+=dv[to][j];
}
}
for(int j=0;j<g.size();j++)if(g[j]==dep[x])dv[x][j]++;
}
int main()
{
int q;
scanf("%d%d",&n,&q);
B=sqrt(n);
for(int i=1;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
}
dfs(1,0);
for(int i=1;i<=n;i++)st[dep[i]].push_back(i);
for(int i=1;i<=n;i++)if(st[i].size()>B)g.push_back(i);
for(int i=1;i<=n;i++)
{
for(int j=0;j<g.size();j++)dv[i].push_back(0);
}
dfs2(1,0);
while(q--)
{
int opt;
scanf("%d",&opt);
if(opt==1)
{
int d,x;
scanf("%d%d",&d,&x);
d++;
if(st[d].size()<=B)
{
for(auto t:st[d])add(id[t],x);
}
else
{
dval[d]+=x;
}
}
else
{
int x;
scanf("%d",&x);
ll ans=getSum(id[ed[x]])-getSum(id[x]-1);
for(int j=0;j<g.size();j++)ans+=dv[x][j]*dval[g[j]];
printf("%lld\n",ans);
}
}
return 0;
}
版权声明
本文为[Zimba_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230621424953.html
边栏推荐
- 云身份过于宽松,为攻击者打开了大门
- Leetcode question bank 78 Subset (recursive C implementation)
- 数据清洗 ETL 工具Kettle的安装
- The sap export excel file opens and shows that the file format and extension of "XXX" do not match. The file may be damaged or unsafe. Do not open it unless you trust its source. Do you still want to
- ABAP 7.4 SQL Window Expression
- Little girl walking
- Single sign on SSO
- 论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》
- MySQL - Chapter 1 (data types in MySQL)
- MySQL of database -- basic common query commands
猜你喜欢

Redis 内存占满导致的 Setnx 命令执行失败

Cross domain configuration error: when allowcredentials is true, allowedorigins cannot contain the special value "*“

Flink 流批一体在小米的实践

Two methods of building Yum source warehouse locally

云身份过于宽松,为攻击者打开了大门

Installation of data cleaning ETL tool kettle

Node installation

Number of islands

501. Mode in binary search tree

Kettle experiment (III)
随机推荐
How to obtain geographical location based on photos and how to prevent photos from leaking geographical location
Where is int a = 1 stored
Two methods of building Yum source warehouse locally
How to use SQL statement union to get another column of another table when the content of a column in a table is empty
kernel-pwn学习(4)--Double Fetch&&0CTF2018-baby
GUI, CLI and UNIX Philosophy
DVWA range practice record
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》
Alibaba cloud architects interpret the four mainstream game architectures
STM32 and FreeRTOS stack parsing
php 二维数组指定元素相等后相加否则新增
Flutter 的加载动画这么玩更有趣
Chapter VIII project stakeholder management of information system project manager summary
Kettle实验 (三)
NPM installation yarn
SAP RFC_ CVI_ EI_ INBOUND_ Main BP master data creation example (Demo customer only)
Kettle实验 转换案例
Learn FPGA (from Verilog to HLS)
1 + X cloud computing intermediate -- script construction, read-write separation
理解作用域