HYSBZ 1036 树的统计Count (水题树链剖分)
题意:中文题。
析:就是直接维护一个最大值和一个和,用线段树维护即可,这个题很简单,但是我卡了一晚上,就是在定位的时候,位置直接反过来了,但是样例全过了。。。真是。。。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 30000 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} struct Edge{
int to, next;
};
Edge edge[maxn<<1];
int head[maxn], cnt, fa[maxn];
int dep[maxn], top[maxn], num[maxn];
int son[maxn], p[maxn], pos, a[maxn], fp[maxn];
int sum[maxn<<2], maxv[maxn<<2]; void init(){
memset(son, -1, sizeof son);
cnt = 0; pos = 1;
memset(head, -1, sizeof head);
} void add_edge(int u, int v){
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
} void dfs1(int u, int f, int d){
dep[u] = d; fa[u] = f;
num[u] = 1;
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to;
if(v == f) continue;
dfs1(v, u, d+1);
num[u] += num[v];
if(son[u] == -1 || num[v] > num[son[u]]) son[u] = v;
}
} void dfs2(int u, int sp){
top[u] = sp; p[u] = pos++;
fp[p[u]] = u;
if(son[u] == -1) return ;
dfs2(son[u], sp);
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to;
if(v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
} void push_up(int rt){
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
maxv[rt] = max(maxv[rt<<1], maxv[rt<<1|1]);
} void build(int l, int r, int rt){
if(l == r){
sum[rt] = maxv[rt] = a[fp[l]];
return ;
}
int m = l + r >> 1;
build(lson);
build(rson);
push_up(rt);
} void update(int M, int val, int l, int r, int rt){
if(l == r){
sum[rt] = maxv[rt] = val;
return ;
}
int m = l + r >> 1;
if(M <= m) update(M, val, lson);
else update(M, val, rson);
push_up(rt);
} int queryMax(int L, int R, int l, int r, int rt){
if(L <= l && r <= R) return maxv[rt];
int m = l + r >> 1;
int ans = -INF;
if(L <= m) ans = queryMax(L, R, lson);
if(R > m) ans = max(ans, queryMax(L, R, rson));
return ans;
} int querySum(int L, int R, int l, int r, int rt){
if(L <= l && r <= R) return sum[rt];
int m = l + r >> 1;
int ans = 0;
if(L <= m) ans = querySum(L, R, lson);
if(R > m) ans += querySum(L, R, rson);;
return ans;
} int solve(int u, int v, bool ok){
int f1 = top[u], f2 = top[v];
int ans = -INF;
if(ok) ans = 0;
while(f1 != f2){
if(dep[f1] < dep[f2]){
swap(f1, f2);
swap(u, v);
}
if(ok) ans += querySum(p[f1], p[u], 1, n, 1);
else ans = max(ans, queryMax(p[f1], p[u], 1, n, 1));
u = fa[f1];
f1 = top[u];
}
if(dep[u] > dep[v]) swap(u, v);
if(ok) return ans + querySum(p[u], p[v], 1, n, 1);
return max(ans, queryMax(p[u], p[v], 1, n, 1));
} int main(){
while(scanf("%d", &n) == 1){
init();
for(int i = 1; i < n; ++i){
int u, v;
scanf("%d %d", &u, &v);
add_edge(u, v);
add_edge(v, u);
}
for(int i = 1; i <= n; ++i) scanf("%d", a+i);
dfs1(1, 0, 0);
dfs2(1, 1);
build(1, n, 1);
scanf("%d", &m);
while(m--){
char op[10];
int u, v;
scanf("%s %d %d", op, &u, &v);
if(op[0] == 'C') update(p[u], v, 1, n, 1);
else printf("%d\n", solve(u, v, op[1] == 'S'));
}
}
return 0;
}
HYSBZ 1036 树的统计Count (水题树链剖分)的更多相关文章
- BZOJ.1036 [ZJOI2008]树的统计Count ( 点权树链剖分 线段树维护和与最值)
BZOJ.1036 [ZJOI2008]树的统计Count (树链剖分 线段树维护和与最值) 题意分析 (题目图片来自于 这里) 第一道树链剖分的题目,谈一下自己的理解. 树链剖分能解决的问题是,题目 ...
- [BZOJ1036][ZJOI2008]树的统计Count 解题报告|树链剖分
树链剖分 简单来说就是数据结构在树上的应用.常用的为线段树splay等.(可现在splay还不会敲囧) 重链剖分: 将树上的边分成轻链和重链. 重边为每个节点到它子树最大的儿子的边,其余为轻边. 设( ...
- 【BZOJ】1036: [ZJOI2008]树的统计Count(lct/树链剖分)
http://www.lydsy.com/JudgeOnline/problem.php?id=1036 lct: (ps:为嘛我的那么慢T_T,不知道排到哪了..难道别人都是树剖吗...看来有必要学 ...
- BZOJ 1036 [ZJOI2008]树的统计Count(动态树)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 题意:一棵树,每个节点有一个权值.三种操作:(1)修改某个节点的权值:(2)输出某两 ...
- Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树)
Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树) Description 一棵树上有n个节点,编号分别 ...
- BZOJ 1036: [ZJOI2008]树的统计Count (树链剖分模板题)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 14982 Solved: 6081[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 ...
- 1036: [ZJOI2008]树的统计Count
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 7496 Solved: 3078[Submit] ...
随机推荐
- oracle系统表的查询
oracle查询用户下的所有表 select * from all_tab_comments -- 查询所有用户的表,视图等select * from user_tab_comments -- 查 ...
- maven依赖顺序原则
使用maven的程序员都会遇到一个问题,那就是maven依赖冲突的问题,这会导致ClassNotFound或者MethodNotFound这样的异常.其实只要明白maven依赖的根本性的原则就不怕这样 ...
- 插入排序的JavaScript实现
思想 每次在现有已经排好的数组的基础上排入一个新的数组项. 先把第一项看做是已经排好的,第二项应该排在第一项之前还是之后呢?当前两项排好后,第三项应该排在这已排好的两项的之前还是之后还是中间呢?当前三 ...
- 在 windows7 中使用 vs2003 时,“在文件中查找”导致无响应的问题
解决 Win7 32bit/64bit环境下,在使用VS2003的查找功能时,会导致VS2003无响应. 解决方法:找到VS2003的安装目录,修改"...\Microsoft Visual ...
- SCSI, (P)ATA, SAS, NL-SAS and SATA, what’s the difference?
Everybody needs storage space nowadays. Whether it is used for high performance computing or simply ...
- java代码实现鼠标双击出现画图-----------paint()方法由系统自动调用,且一定是小写的字母p
总结:在运行过程中,自己不是很认真,没有检查自己写的代码,结果是无论你怎么运行,双击 frame都没用,因为系统根本就没有调用paint()方法绘图.所以很重要的是实现这个方法 package com ...
- 事务之四:Spring事务--原理
一.Spring事务的基本原理 Spring事务的本质其实就是数据库对事务的支持,没有数据库的事务支持,spring是无法提供事务功能的.对于纯JDBC操作数据库,想要用到事务,可以按照以下步骤进行: ...
- PHP5.3.8连接Sql Server SQLSRV30
PHP5.3连接SQL Server就不能用php_mssql.dll了. 网上下载了好多都不行,因为它的版本是5.2的,不能再PHP5.3中使用. 后来听说微软专门为PHP出了自己的dll. 叫做M ...
- Docker - 避免启动container后运行shell脚本执行完成后docker退出container
问题 最近在使用 Dockerfile 启动容器,发现使用Dockerfile调用容器里面的shell,当shell执行完成以后,docker会退出容器. 分析 Docker 在执行shell的时候, ...
- oracle数据库中函数的递归调用
如有下面的表结构AAAA,用一个字段prev_id表示记录的先后顺序,要对其排序,需要用的递归函数 ID PREV_ID CONT 99 a 23 54 d 21 23 e 54 33 c 33 ...