【BZOJ 1036】[ZJOI2008]树的统计Count
【题目链接】:http://www.lydsy.com/JudgeOnline/problem.php?id=1036
【题意】
【题解】
树链剖分入门题;
每一条链维护一个线段树就好;
uppest数组维护这条链的最顶端的元素;
一条链一条链的往上走;直到两个点在同一条链里面了
balabala
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 3e4+100;
const int INF = 3e4 + 200;
int n,fa[N],siz[N],dep[N],uppest[N],bh[N],cnt;
int v[N],sum[N<<2],mx[N<<2];
vector <int> G[N];
void in()
{
rei(n);
rep1(i, 1, n - 1)
{
int x, y;
rei(x), rei(y);
G[x].pb(y), G[y].pb(x);
}
rep1(i, 1, n)
rei(v[i]);
}
void dfs1(int x)
{
int len = G[x].size();
siz[x] = 1;
rep1(i, 0, len - 1)
{
int y = G[x][i];
if (y == fa[x]) continue;
fa[y] = x;
dep[y] = dep[x] + 1;
dfs1(y);
siz[x] += siz[y];
}
}
void dfs2(int x, int chain)
{
int len = G[x].size();
int k = 0;
bh[x] = ++cnt;
uppest[x] = chain;
rep1(i, 0, len - 1)
{
int y = G[x][i];
if (dep[y]>dep[x] && siz[y] > siz[k])
k = y;
}
if (k == 0) return;
dfs2(k, chain);
rep1(i, 0, len - 1)
{
int y = G[x][i];
if (dep[y] > dep[x] && y != k)
dfs2(y, y);
}
}
void updata(int pos, int val,int l, int r, int rt)
{
if (l == r)
{
sum[rt] = mx[rt] = val;
return;
}
int m = (l + r) >> 1;
if (pos <= m)
updata(pos, val,lson);
else
updata(pos, val,rson);
sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]);
}
int query_max(int L, int R, int l, int r, int rt)
{
if (L <= l && r <= R)
return mx[rt];
int m = (l + r) >> 1;
int temp1 = -INF;
if (L <= m)
temp1 = max(temp1, query_max(L, R, lson));
if (m < R)
temp1 = max(temp1, query_max(L, R, rson));
return temp1;
}
int get_max(int u, int v)
{
int t = -3e4 - 100;
while (uppest[u] != uppest[v])
{
if (dep[uppest[u]] < dep[uppest[v]])
swap(u, v);
t = max(t, query_max(bh[uppest[u]], bh[u], 1, n, 1));
u = fa[uppest[u]];
}
if (dep[u] < dep[v])
swap(u, v);
t = max(t, query_max(bh[v], bh[u], 1, n, 1));
return t;
}
int query_sum(int L, int R, int l, int r, int rt)
{
if (L <= l && r <= R)
return sum[rt];
int m = (l + r) >> 1;
int temp = 0;
if (L <= m)
temp += query_sum(L, R, lson);
if (m < R)
temp += query_sum(L, R, rson);
return temp;
}
int get_sum(int u, int v)
{
int t = 0;
while (uppest[u] != uppest[v])
{
if (dep[uppest[u]] < dep[uppest[v]])
swap(u, v);
t += query_sum(bh[uppest[u]], bh[u], 1, n, 1);
u = fa[uppest[u]];
}
if (dep[u] < dep[v])
swap(u, v);
t+=query_sum(bh[v], bh[u], 1, n, 1);
return t;
}
void get_ans()
{
rep1(i, 1, n)
updata(bh[i], v[i],1, n, 1);
int q;
rei(q);
char s[8];
rep1(i, 1, q)
{
scanf("%s", s);
if (s[0] == 'C')
{
int u, t;
rei(u), rei(t);
updata(bh[u], t, 1, n, 1);
}
else
{
int u, v;
rei(u), rei(v);
if (s[1] == 'M')
printf("%d\n", get_max(u, v));
else
printf("%d\n", get_sum(u, v));
}
}
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
in();
dfs1(1);
dfs2(1, 1);
get_ans();
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【BZOJ 1036】[ZJOI2008]树的统计Count的更多相关文章
- BZOJ.1036 [ZJOI2008]树的统计Count ( 点权树链剖分 线段树维护和与最值)
BZOJ.1036 [ZJOI2008]树的统计Count (树链剖分 线段树维护和与最值) 题意分析 (题目图片来自于 这里) 第一道树链剖分的题目,谈一下自己的理解. 树链剖分能解决的问题是,题目 ...
- BZOJ 1036: [ZJOI2008]树的统计Count [树链剖分]【学习笔记】
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 14302 Solved: 5779[Submit ...
- BZOJ 1036: [ZJOI2008]树的统计Count
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MB Submit: 14354 Solved: 5802 [Subm ...
- bzoj 1036 [ZJOI2008]树的统计Count(树链剖分,线段树)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 10677 Solved: 4313[Submit ...
- Bzoj 1036: [ZJOI2008]树的统计Count 树链剖分,LCT
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 11102 Solved: 4490[Submit ...
- 数据结构(LCT动态树):BZOJ 1036: [ZJOI2008]树的统计Count
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 12266 Solved: 4945[Submit ...
- BZOJ 1036: [ZJOI2008]树的统计Count( 树链剖分 )
树链剖分... 不知道为什么跑这么慢 = = 调了一节课啊跪.. ------------------------------------------------------------------- ...
- bzoj 1036: [ZJOI2008]树的统计Count 树链剖分+线段树
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 16294 Solved: 6645[Submit ...
- bzoj 1036: [ZJOI2008]树的统计Count (树链剖分+线段树 点权)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 21194 Solved: 8589[Submit ...
- BZOJ 1036: [ZJOI2008]树的统计Count (树链剖分模板题)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 14982 Solved: 6081[Submit ...
随机推荐
- jfreechart,pdf生成组件iText,jasper report报表组件及POI操作excel等在企业软件开发中常遇到的第三方应用
熟悉WEB Service ,Ajax,DWR,JQuery,iBatis等技术,熟练TOMCAT,IIS,JBoss,WebLogic等服务器 图表组件JFreeChart PDF组件-iText的 ...
- 截止频率-3db
关于-3db截止频率 (2013-06-22 10:47:02) 转载▼ 分类: 信号.电路 关于-3db截止频率 为什么当信号衰减了-3db的时候就算是截止频率了.这里面有什么高深的内涵.毕竟这 ...
- .NetCore微服务Surging新手傻瓜式 入门教程 学习日志---先让程序跑起来(一)
原文:.NetCore微服务Surging新手傻瓜式 入门教程 学习日志---先让程序跑起来(一) 写下此文章只为了记录Surging微服务学习过程,并且分享给广大想学习surging的基友,方便广大 ...
- 【】queue
[链接]点击打开链接 [题意] 实话实说,给 OIER 大神们排队这种工作是最让人头疼的事情了.因为同学们都有自尊 心,都不愿意排后面. 现在共有 n 个同学要排成一列,每个同学有两个属性:影响力和承 ...
- 讨论:怎样加快android的开机时间
如题,近期项目须要,须要将android的开机时间大幅缩短,查了下网上资料,作用有限,望有处理过相关问题的兄弟姐妹參与讨论,给予不吝赐教,期待ing
- 使用BeautifulSoup爬取“0daydown”站点的信息(2)——字符编码问题解决
上篇中的程序实现了抓取0daydown最新的10页信息.输出是直接输出到控制台里面.再次改进代码时我准备把它们写入到一个TXT文档中.这是问题就出来了. 最初我的代码例如以下: #-*- coding ...
- 8.5 Android灯光系统_源码分析_通知灯
参考文章(应用程序举例)how to use the LED with Android phonehttp://androidblogger.blogspot.jp/2009/09/tutorial- ...
- iOS ASIHTTPRequest
ASIHTTPRequest对CFNetwork API进行了封装,并且使用起来非常简单,用Objective-C编写,可以很好的应用在Mac OS X系统和iOS平台的应用程序中.ASIHTTPRe ...
- [TypeStyle] Add type safety to CSS using TypeStyle
TypeStyle is the only current CSS in JS solution that is designed with TypeSafety and TypeScript dev ...
- php面试题5
php面试题5 一.总结 二.php面试题5 1. 什么事面向对象?主要特征是什么?1) 面向对象是程序的一种设计方式,它利于提高程序的重用性,是程序结构更加清晰.2) 主要特征:封装.继承.多态 2 ...