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] ...
随机推荐
- 安装jenkins 的时候 记录默认密码文件为空的情况
1.把文件的权限改成 chmod 777 .jenkins/secrets/initialAdminPassword 然后再使用编辑器打开,密码就出来的 密码文件的地址 /var/root/.hud ...
- MySQL存储过程中的3种循环,存储过程的基本语法,ORACLE与MYSQL的存储过程/函数的使用区别,退出存储过程方法
在MySQL存储过程的语句中有三个标准的循环方式:WHILE循环,LOOP循环以及REPEAT循环.还有一种非标准的循环方式:GOTO,不过这种循环方式最好别用,很容易引起程序的混乱,在这里就不错具体 ...
- 内核中的内存申请:kmalloc、vmalloc、kzalloc、get_free_pages 之间的区别
kmalloc vmalloc kzalloc get_free_page()是内核空间申请内存空间函数 malloc是用户空间申请内存函数 一 ,kmalloc() 与 kfree() ...
- ubuntu 14.04 no valid active connections found
ubuntu 14.04 强制重启后出现不能上网,点击connection information 后出现error: no valid active connections found 解决办法是在 ...
- springboot成神之——拦截器
本文介绍spring boot拦截器 创建拦截器类LogInterceptor.java 创建拦截器类OldLoginInterceptor.java 拦截器配置类WebMvcConfig.java ...
- DRF - 序列化组件(GET/PUT/DELETE接口设计)、视图优化组件
一.序列化组件 基于上篇随笔的表结构 , 通过序列化组件的ModelSerializer设计如下三个接口 : GET 127.0.0.1:8000/books/{id} # 获取一条数据,返回值:{} ...
- leetcode695
public class Solution { public int MaxAreaOfIsland(int[,] grid) { ); ); bool[,] Visited = new bool[r ...
- MySQL(数据库)
数据库概念: 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库在实际应用中会遇到各式各样的数据库如nosql非关系数据库(memcached,redis,mangodb),RDBM ...
- 一个页面中内嵌页面 iframe元素
iframe.html: <!DOCTYPE html><html lang="en"><head> <meta charset=&quo ...
- Apache Hive 建表操作的简单描述
客户端连接hive [root@bigdata-02 bin]# ./beeline Beeline version by Apache Hive beeline: Connecting : Ente ...