UESTC 574 High-level ancients
分析:
无论父节点增加了多少,子节点的增量总比父节点多1。
这种差分的关系是保存不变的,我们可以一遍dfs根据结点深度得到在根结点的每个点的系数。
估且把一开始的结点深度称做c0吧,对于子树的修改就只是结点的系数就只是c0+d,d是修正值。
dfs得到树的dfs序列,子树的结点连续,就变成区间更新了。
区间更新的时候,在线段树上保存好初始的系数,修改的时候把系数的lazy标记和普通的lazy标记分开。
这道题学到的新东西:在线段树上不仅可以总体+d,还可以总体增加某一系列特定的系数。
这个系数甚至是可变的,感觉这里可以挖掘一下。
/*********************************************************
* ------------------ *
* author AbyssFish *
**********************************************************/
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<numeric>
#include<climits>
using namespace std; typedef long long ll;
const int MAX_N = 5e4+;
//MAX_P = 1e5 , K <= 1e3
int fa[MAX_N];
int son[MAX_N], bro[MAX_N]; int N;
int dep[MAX_N]; int L[MAX_N], R[MAX_N];
int c[MAX_N]; //dfs_order
int dfs_clk; void dfs(int u = ,int d = )
{
c[dfs_clk] = dep[u] = d;
L[u] = dfs_clk++;
for(int v = son[u]; v; v = bro[v]){
dfs(v,d+);
}
R[u] = dfs_clk;
} #define para int o = 1,int l = 0,int r = N
#define lo (o<<1)
#define ro (o<<1|1)
#define Tvar int md = (l+r)>>1;
#define lsn lo,l,md
#define rsn ro,md,r
#define insd ql <= l && r <= qr const int ST_SIZE = <<;
ll t_c[ST_SIZE];//O 5e4*5e4/2
ll sum[ST_SIZE];//O 1e5*(1e3*5e4+5e4*5e4/2)
int dwn_c[ST_SIZE];//O 1e5
ll dwn[ST_SIZE];//O (5e4+1e3)*1e5 void build(para)
{
sum[o] = ;
if(r-l == ){
t_c[o] = c[l];
}
else {
dwn_c[o] = dwn[o] = ;
Tvar
build(lsn);
build(rsn);
t_c[o] = t_c[lo]+t_c[ro];
}
} inline void sink_d(int o,ll d,int len)
{
sum[o] += len*d;
dwn[o] += d;
} inline void sink_c(int o,int k)
{
sum[o] += t_c[o]*k;
dwn_c[o] += k;
} inline void push_down(int o,int l,int r)
{
if(dwn_c[o]){
sink_c(lo,dwn_c[o]);
sink_c(ro,dwn_c[o]);
dwn_c[o] = ;
}
if(dwn[o]){
Tvar
sink_d(lo,dwn[o],md-l);
sink_d(ro,dwn[o],r-md);
dwn[o] = ;
}
} #define upara d,ql,qr
void update(int d,int ql,int qr,para)
{
if(insd){
sink_d(o,d,r-l);//O 5e4+1e3
sink_c(o,);
}
else {
Tvar
push_down(o,l,r);
if(ql < md) update(upara,lsn);
if(qr > md) update(upara,rsn);
sum[o] = sum[lo] + sum[ro];
}
} ll query(int ql,int qr,para)
{
if(insd) return sum[o];
else {
Tvar
push_down(o,l,r);
ll re = ;
if(ql < md) re += query(ql,qr,lsn);
if(qr > md) re += query(ql,qr,rsn);
return re;
}
} void solve()
{
int i, P;
scanf("%d%d",&N,&P);
memset(son+,,sizeof(int)*N);
for(i = ; i <= N; i++){
scanf("%d",fa+i);
bro[i] = son[fa[i]];
son[fa[i]] = i;
} dfs_clk = ;
dfs();
build(); char op[];
int u;
while(P--){
scanf("%s%d",op,&u);
if(*op == 'A'){
scanf("%d",&i);
update(i-dep[u],L[u],R[u]);
}
else {
printf("%lld\n",query(L[u],R[u]));
}
}
} //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
int T, kas = ;
scanf("%d",&T);
while(++kas <= T){
printf("Case #%d:\n",kas);
solve();
}
return ;
}
UESTC 574 High-level ancients的更多相关文章
- cdoj 574 High-level ancients dfs序+线段树
High-level ancients Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/s ...
- cdoj 574 High-level ancients dfs序+线段树 每个点所加权值不同
High-level ancients Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/s ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Demacia of the Ancients
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5504 The 12th Zhejiang Provincial ...
- 第十二届浙江省大学生程序设计大赛-Demacia of the Ancients 分类: 比赛 2015-06-26 14:39 30人阅读 评论(0) 收藏
Demacia of the Ancients Time Limit: 2 Seconds Memory Limit: 65536 KB There is a popular multiplayer ...
- 简洁常用权限系统的设计与实现(六):不维护节点的深度level,手动计算level,构造树 (把一颗无序的树,变成有序的)
本篇介绍的方法,参考了网上的代码.在递归过程中,计算level,是受到了这种方法的启发. CSDN上有篇关于树的算法,目标是把一个无序的树,变成有序的. 我看了下代码,并运行了下,感觉是可行的. 我 ...
- Java compiler level does not match解决方法
从别的地方导入一个项目的时候,经常会遇到eclipse/Myeclipse报Description Resource Path Location Type Java compiler level d ...
- Android requires compiler compliance level 5.0 or 6.0. Found '1.4' instead的解决办法
今天在导入工程进Eclipse的时候竟然出错了,控制台输出的是: [2013-02-04 22:17:13 - takepicture] Android requires compiler compl ...
- Android版本和API Level对应关系
http://developer.android.com/guide/topics/manifest/uses-sdk-element.html Platform Version API ...
- [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
随机推荐
- python3.6 子类的__init__调用父类的__init__
python3.6 子类的__init__调用父类的__init__ 父类 class worker: def __init__(self): self.a=1 self.b=2 if __name_ ...
- PIE SDK存储格式转换
1.算法功能简介 影像存储格式转换可以实现栅格数据存储格式的自由转换,其中存储格式可以是 BSQ. BIP. BIL 三种格式. 遥感数字图像数据的存储与分发,通常采用以下三种数据格式: BSQ( ...
- Node.js frameworks
1. Express 2. Koa 3. LoopBack egghead.io What is egghead? egghead is a group of working web developm ...
- Linux Jenkins
部署与运行: Jenkins 依赖于 Tomcat 才能跑起来,把 Jenkins 的 jenkins.war 文件放到 Tomcat 的安装目录的 webapps 目录下,配置好端口,正常访问 lo ...
- linux 输出 之 nl 命令
1.命令格式:nl [选项]... [文件]... 2.命令参数: -b :指定行号指定的方式,主要有两种: -b a :表示不论是否为空行,也同样列出行号(类似 cat -n): -b t :如果 ...
- centOS7关闭防火墙的命令
centOS7下关闭防火墙的命令已经改了,如下: systemctl stop firewalld
- Python高级用法------字典无需提前定义key
from collections import defaultdict import json def tree(): return defaultdict(tree) categories = tr ...
- 深入理解JavaScript系列(15):函数(Functions)
介绍 本章节我们要着重介绍的是一个非常常见的ECMAScript对象——函数(function),我们将详细讲解一下各种类型的函数是如何影响上下文的变量对象以及每个函数的作用域链都包含什么,以及回答诸 ...
- js.css嵌入dll
处理请求,返回 public ActionResult Get() { //传递一个部分名称 var n = Request["n"]; n = n.Replace('/', '. ...
- Python3.4 获取百度网页源码并保存在本地文件中
最近学习python 版本 3.4 抓取网页源码并且保存在本地文件中 import urllib.request url='http://www.baidu.com' #上面的url一定要写明确,如果 ...