2018沈阳网络赛 - Ka Chang KD树暴力
题意:给你一棵树,n个点q次操作,操作1查询x子树深度为d的节点权值和,操作2查询子树x权值和
把每个点按(dfn,depth)的二维关系构造kd树,剩下的只需维护lazy标记即可
#include<bits/stdc++.h>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define print(a) printf("%lld",(ll)(a))
#define printbk(a) printf("%lld ",(ll)(a))
#define println(a) printf("%lld\n",(ll)(a))
using namespace std;
const int MAXN = 1e5+11;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
typedef long long ll;
ll read(){
ll x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int to[MAXN<<1],nxt[MAXN<<1],head[MAXN],tot,CLOCK;
void init(){
memset(head,-1,sizeof head);
tot=0; CLOCK=0;
}
void add(int u,int v){
to[tot]=v;
nxt[tot]=head[u];
head[u]=tot++;
}
int dep[MAXN],dfn[MAXN],dfned[MAXN],pre[MAXN];
void dfs(int u,int fa,int d){
dep[u]=d; dfn[u]=++CLOCK; pre[CLOCK]=u;
for(int i=head[u];~i;i=nxt[i]){
int v=to[i];
if(v==fa) continue;
dfs(v,u,d+1);
}
dfned[u]=CLOCK;
}
int D;
struct Point{
int x[2]; ll v;
Point(int xx=0,int yy=0,ll vv=0){x[0]=xx,x[1]=yy,v=vv;}
bool operator < (const Point &orz)const{return x[D]<orz.x[D];}
int operator [] (const int &orz)const{return x[orz];}
}p[MAXN];
struct KD{
int lx[MAXN][2],rx[MAXN][2];
int son[MAXN][2],size[MAXN];
ll sum[MAXN],lazy[MAXN];
int root;
#define lc son[o][0]
#define rc son[o][1]
void pu(int o){
size[o]=1, sum[o]=p[o].v;
if(lc) size[o]+=size[lc],sum[o]+=sum[lc];
if(rc) size[o]+=size[rc],sum[o]+=sum[rc];
rep(i,0,1){
if(lc&&lx[lc][i]<lx[o][i]) lx[o][i]=lx[lc][i];
if(rc&&lx[rc][i]<lx[o][i]) lx[o][i]=lx[rc][i];
if(lc&&rx[lc][i]>rx[o][i]) rx[o][i]=rx[lc][i];
if(rc&&rx[rc][i]>rx[o][i]) rx[o][i]=rx[rc][i];
}
}
void pd(int o){
if(lazy[o]){
if(lc) p[lc].v+=lazy[o],sum[lc]+=lazy[o]*size[lc],lazy[lc]+=lazy[o];
if(rc) p[rc].v+=lazy[o],sum[rc]+=lazy[o]*size[rc],lazy[rc]+=lazy[o];
lazy[o]=0;
}
}
int build(int d,int l,int r){
D=d;
int o=l+r>>1; nth_element(p+l,p+o,p+r+1);
rep(i,0,1) lx[o][i]=rx[o][i]=p[o][i];
size[o]=1; lc=rc=0; sum[o]=lazy[o]=0;
if(l<o) lc=build(d^1,l,o-1);
if(r>o) rc=build(d^1,o+1,r);
pu(o);
return o;
}
void update(int o,int L,int R,int d,ll v){
if(!o) return;
if(rx[o][0]<L||lx[o][0]>R||lx[o][1]>d||rx[o][1]<d) return;
if(lx[o][0]>=L&&rx[o][0]<=R&&lx[o][1]==d&&rx[o][1]==d){
lazy[o]+=v;
sum[o]+=v*size[o];
p[o].v+=v;
return;
}
if(p[o][0]>=L&&p[o][0]<=R&&p[o][1]==d){
sum[o]+=v;
p[o].v+=v;
}
pd(o);
update(lc,L,R,d,v);
update(rc,L,R,d,v);
pu(o);
}
ll ANS;
void query(int o,int L,int R){
if(!o) return;
if(rx[o][0]<L||lx[o][0]>R) return;
if(lx[o][0]>=L&&rx[o][0]<=R){
ANS+=sum[o];
return;
}
pd(o);
if(p[o][0]>=L&&p[o][0]<=R) ANS+=p[o].v;
query(lc,L,R);
query(rc,L,R);
}
}kd;
int main(){
int n,q;
while(cin>>n>>q){
init();
rep(i,1,n-1){
int u=read();
int v=read();
add(u,v);
add(v,u);
}
dfs(1,-1,0);
rep(i,1,n) p[i]=Point(dfn[i],dep[i],0);
kd.root=kd.build(0,1,n);
while(q--){
int op=read();
if(op==1){
int d=read();
ll x=read();
kd.update(kd.root,1,CLOCK,d,x);
}else{
int u=read();
kd.ANS=0;
kd.query(kd.root,dfn[u],dfned[u]);
println(kd.ANS);
}
}
}
return 0;
}
2018沈阳网络赛 - Ka Chang KD树暴力的更多相关文章
- 沈阳网络赛J-Ka Chang【分块】【树状数组】【dfs序】
Given a rooted tree ( the root is node 11 ) of NN nodes. Initially, each node has zero point. Then, ...
- 2018 CCPC网络赛 1010 hdu 6447 ( 树状数组优化dp)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 思路:很容易推得dp转移公式:dp[i][j] = max(dp[i][j-1],dp[i-1][j ...
- HDU 6203 2017沈阳网络赛 LCA,DFS+树状数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6203 题意:n+1 个点 n 条边的树(点标号 0 ~ n),有若干个点无法通行,导致 p 组 U V ...
- 2018南京网络赛 - Skr 回文树
题意:求本质不同的回文串(大整数)的数字和 由回文树的性质可知贡献只在首次进入某个新节点时产生 那么只需由pos和len算出距离把左边右边删掉再算好base重复\(O(n)\)次即可 位移那段写的略微 ...
- 2018北京网络赛 G The Mole /// 分块暴力 点线距离
题目大意: 给定n段线段 编号为1~n 接下来m个询问 给定一点 输出离该点最近的线段的最小编号(距离相等时取编号小的) 题解 大致就是 1.坐标范围为(0,2^16-1) 将坐标系划分为2^8*2^ ...
- 2018 ICPC 沈阳网络赛
2018 ICPC 沈阳网络赛 Call of Accepted 题目描述:求一个算式的最大值与最小值. solution 按普通算式计算方法做,只不过要同时记住最大值和最小值而已. Convex H ...
- 2018 CCPC网络赛
2018 CCPC网络赛 Buy and Resell 题目描述:有一种物品,在\(n\)个地点的价格为\(a_i\),现在一次经过这\(n\)个地点,在每个地点可以买一个这样的物品,也可以卖出一个物 ...
- 计蒜客 2018南京网络赛 I Skr ( 回文树 )
题目链接 题意 : 给出一个由数字组成的字符串.然后要你找出其所有本质不同的回文子串.然后将这些回文子串转化为整数后相加.问你最后的结果是多少.答案模 1e9+7 分析 : 应该可以算是回文树挺裸的题 ...
- 沈阳网络赛 F - 上下界网络流
"Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a ...
随机推荐
- qt学习(二) buttong layout spinbox slider 示例
开启qt5 creator 新建项目 qt widgets 改写main.cpp #include "mainwindow.h" #include <QApplication ...
- 前端传递对象列表到WebApi
public Int64 objectPOC(JObject jsonWrapper) { dynamic jsonValues = jsonWrapper; JArray jsonInput = j ...
- net 程序员面试宝典
第1部分 求职过程 ------------------------------------------------------------------------------------------ ...
- Redis持久化(八)
Redis特性: (1)多数据库 (2)Redis事物 (3)一个Redis最多可提供16个数据库,下标[0-15] 选择数据库: select 1 (选择1号数据库,默认连接的是0号数据库)移动数据 ...
- Java之RandomAccessFile小结
今天跟大家分享一下javase中的关于I/O的操作: 有时我们需要在文件的末尾追加一些内容,在这时用RandomAccessFile就很好. 这个类有两个构造方法: RandomAccessFile( ...
- C++中break/Continue,exit/return的理解
刚才遇到了一个问题,大概是这样的. func1执行完成,进入func1Complete,其中switch处理func1返回的数据,如果返回数据是clear则重新执行func1. 测试的时候发现func ...
- 存储系统的基本数据结构之一: 跳表 (SkipList)
在接下来的系列文章中,我们将介绍一系列应用于存储以及IO子系统的数据结构.这些数据结构相互关联又有着巨大的区别,希望我们能够不辱使命的将他们分门别类的介绍清楚.本文为第一节,介绍一个简单而又有用的数据 ...
- api接口响应类型定义
public class Response<T> { public ResponseStatus Status { get; set; } public string Message { ...
- C#Doc写入 XML文件
HTML是XML的先驱,XML延续了HTML的简单性的优点.XML不是用来替代HTML的, XML和HTML为不同的目的而设计: XML被设计用来描述数据,其焦点是数据的内容.HTML被设计用来显示数 ...
- 虚拟化安全 sandbox 技术分析
原文链接:https://cloud.tencent.com/developer/news/215218 前言: libvirt-4.3搭配qemu-2.12使用,如果使用默认的编译选项,可能会让qe ...