bzoj 3221: Obserbing the tree树上询问 树链剖分+线段树
题目大意:
题解
啊呀。。。这是昨天的考试题啊。。。直接就粘了。。
与4515: [Sdoi2016]游戏类似,还更简单了一些。
(考场上因为没开long long爆了25分)
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(ll &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const ll maxn = 100010;
struct Edge{
ll to,next;
}G[maxn<<1];
ll head[maxn],cnt;
void add(ll u,ll v){
G[++cnt].to = v;
G[cnt].next = head[u];
head[u] = cnt;
}
#define v G[i].to
ll fa[maxn],dfn[maxn],top[maxn],dep[maxn],siz[maxn];
ll son[maxn],dfs_clock,seq[maxn],sum[maxn];
void dfs(ll u){
siz[u] = 1;
for(ll i = head[u];i;i=G[i].next){
if(v == fa[u]) continue;
fa[v] = u;
dep[v] = dep[u] + 1;
dfs(v);
siz[u] += siz[v];
if(siz[son[u]] < siz[v]) son[u] = v;
}
}
void dfs(ll u,ll tp){
top[u] = tp;dfn[u] = ++dfs_clock;
seq[dfs_clock] = u;
if(son[u]) dfs(son[u],tp);
for(ll i = head[u];i;i=G[i].next){
if(v == son[u] || v == fa[u]) continue;
dfs(v,v);
}
}
#undef v
inline ll lca(ll u,ll v){
while(top[u] != top[v]){
if(dep[top[u]] < dep[top[v]]) swap(u,v);
u = fa[top[u]];
}return dep[u] < dep[v] ? u : v;
}
struct Node{
Node *ch[2];
ll sum,k,b;
ll dep_sum;
void update(){
dep_sum = ch[0]->dep_sum + ch[1]->dep_sum;
sum = ch[0]->sum + ch[1]->sum;
}
}*null,*root[maxn],*rt;
Node mem[25001001],*it;
inline void init(){
it = mem;
null = it++;null->ch[0] = null->ch[1] = null;
null->sum = null->k = null->b = 0;null->dep_sum = 0;
root[0] = null;
}
inline Node *newNode(){
Node *p = it++;p->ch[0] = p->ch[1] = null;
p->sum = p->k = p->b = p->dep_sum = 0;
return p;
}
ll L,R,K,B,n,T;
Node *change(Node *rt,ll l,ll r){
Node *p = newNode();*p = *rt;
if(L <= l && r <= R){
p->k += K;p->b += B;
p->sum += K*p->dep_sum + B*(r-l+1);
return p;
}ll mid = (l+r) >> 1;
if(L <= mid) p->ch[0] = change(p->ch[0],l,mid);
if(R > mid) p->ch[1] = change(p->ch[1],mid+1,r);
p->update();p->sum += p->dep_sum*p->k + (r-l+1)*p->b;
return p;
}
void change(ll u,ll v,ll a,ll b){
ll lc = lca(u,v);
K = -b;B = (a+dep[u]*b);
while(top[u] != top[lc]){
L = dfn[top[u]];R = dfn[u];
rt = change(rt,1,n);
u = fa[top[u]];
}
L = dfn[lc];R = dfn[u];
rt = change(rt,1,n);
K = b;B -= 2*dep[lc]*b;
while(top[v] != top[lc]){
L = dfn[top[v]];R = dfn[v];
rt = change(rt,1,n);
v = fa[top[v]];
}
L = dfn[lc];R = dfn[v];
if(L == R) return;++L;
rt = change(rt,1,n);
}
ll query(Node *p,ll l,ll r,ll L,ll R){
if(L <= l && r <= R){
return p->sum;
}
ll ret = (sum[min(r,R)] - sum[max(l,L)-1])*p->k + p->b*(min(r,R) - max(l,L) + 1);
ll mid = (l+r) >> 1;
if(L <= mid) ret += query(p->ch[0],l,mid,L,R);
if(R > mid) ret += query(p->ch[1],mid+1,r,L,R);
return ret;
}
inline ll query(ll u,ll v){
ll ret = 0;
while(top[u] != top[v]){
if(dep[top[u]] < dep[top[v]]) swap(u,v);
ret += query(rt,1,n,dfn[top[u]],dfn[u]);
u = fa[top[u]];
}if(dep[u] > dep[v]) swap(u,v);
ret += query(rt,1,n,dfn[u],dfn[v]);
return ret;
}
Node *build(ll l,ll r){
Node *p = newNode();
if(l == r){
p->dep_sum = dep[seq[l]];
p->sum = 0;
return p;
}
ll mid = (l+r) >> 1;
p->ch[0] = build(l,mid);
p->ch[1] = build(mid+1,r);
p->update();return p;
}
char s[10];
int main(){
init();
ll last = 0,total = 0;
ll m;read(n);read(m);
for(ll i=1,u,v;i<n;++i){
read(u);read(v);
add(u,v);add(v,u);
}dfs(1);dfs(1,1);root[0] = build(1,n);
sum[0] = 0;
for(ll i=1;i<=n;++i) sum[i] = sum[i-1] + dep[seq[i]];
rt = root[0];
ll u,v,a,b,w;
while(m--){
scanf("%s",s);
if(*s == 'c'){
read(u);read(v);read(a);read(b);
u ^= last;v ^= last;
change(u,v,a,b);
root[++total] = rt;
}else if(*s == 'q'){
read(u);read(v);
u ^= last;v ^= last;
printf("%lld\n",last = query(u,v));
}else{
read(w);w ^= last;
rt = root[w];
}
}
getchar();getchar();
return 0;
}
bzoj 3221: Obserbing the tree树上询问 树链剖分+线段树的更多相关文章
- BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 )
BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 ) 题意分析 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 ...
- 【POJ3237】Tree(树链剖分+线段树)
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
- POJ3237 Tree 树链剖分 线段树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ3237 题意概括 Description 给你由N个结点组成的树.树的节点被编号为1到N,边被编号为1 ...
- BZOJ.1036 [ZJOI2008]树的统计Count ( 点权树链剖分 线段树维护和与最值)
BZOJ.1036 [ZJOI2008]树的统计Count (树链剖分 线段树维护和与最值) 题意分析 (题目图片来自于 这里) 第一道树链剖分的题目,谈一下自己的理解. 树链剖分能解决的问题是,题目 ...
- BZOJ 3672[NOI2014]购票(树链剖分+线段树维护凸包+斜率优化) + BZOJ 2402 陶陶的难题II (树链剖分+线段树维护凸包+分数规划+斜率优化)
前言 刚开始看着两道题感觉头皮发麻,后来看看题解,发现挺好理解,只是代码有点长. BZOJ 3672[NOI2014]购票 中文题面,题意略: BZOJ 3672[NOI2014]购票 设f(i)f( ...
- Spoj Query on a tree SPOJ - QTREE(树链剖分+线段树)
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- Aizu 2450 Do use segment tree 树链剖分+线段树
Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...
- bzoj 4196 [Noi2015]软件包管理器 (树链剖分+线段树)
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 2852 Solved: 1668[Submit][Sta ...
随机推荐
- willMoveToParentViewController和didMoveToParentViewController
本文转载至 http://blog.csdn.net/yongyinmg/article/details/40619727 iOS 5.0 后UIViewController新增:willMoveTo ...
- pthon 基础 9.8 增加数据
#/usr/bin/python #-*- coding:utf-8 -*- #@Time :2017/11/24 2:59 #@Auther :liuzhenchuan #@File :增加 ...
- poj3708(公式化简+大数进制装换+线性同余方程组)
刚看到这个题目,有点被吓到,毕竟自己这么弱. 分析了很久,然后发现m,k都可以唯一的用d进制表示.也就是用一个ai,和很多个bi唯一构成. 这点就是解题的关键了. 之后可以发现每次调用函数f(x),相 ...
- eclipse 创建maven web 项目
虽然网上教程一大把,但也重新整理下. 一.创建项目 1.Eclipse中用Maven创建项目 上图中Next 2.继续Next 3.选maven-archetype-webapp后,next 4.填写 ...
- Biorhythms
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 135099 Accepted: 43146 Description So ...
- js跨浏览器复制: ZeroClipboard
实例结构: demo.html <script type="text/javascript" src='http://code.jquery.com/jquery.js'&g ...
- PAT 1059. C语言竞赛(20)
C语言竞赛是浙江大学计算机学院主持的一个欢乐的竞赛.既然竞赛主旨是为了好玩,颁奖规则也就制定得很滑稽: 0. 冠军将赢得一份“神秘大奖”(比如很巨大的一本学生研究论文集……). 1. 排名为素数的学生 ...
- You are using pip version 8.1.2, however version 9.0.1 is available.
[root@localhost ~]# pip install virtualenvmapperCollecting virtualenvmapper Could not find a versio ...
- js实现粘贴板复制
<a href = '#' onclick ='javascript:window.clipboardData.setData('text','${form.param}');alert('クリ ...
- 更换好的yum源
最近重装了虚拟机,因为之前总是碰到一些 yum的软件太 旧了,索性重装了 虚拟机,从零开始,然后配置yum源,以便以后安装 插件包的时候是最新的.如下: 1,进入yum源配置目录cd /etc/yum ...