题目大意:

http://www.lydsy.com/JudgeOnline/problem.php?id=3221

题解

啊呀。。。这是昨天的考试题啊。。。直接就粘了。。

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树上询问 树链剖分+线段树的更多相关文章

  1. BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 )

    BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 ) 题意分析 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 ...

  2. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  3. POJ3237 Tree 树链剖分 线段树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ3237 题意概括 Description 给你由N个结点组成的树.树的节点被编号为1到N,边被编号为1 ...

  4. BZOJ.1036 [ZJOI2008]树的统计Count ( 点权树链剖分 线段树维护和与最值)

    BZOJ.1036 [ZJOI2008]树的统计Count (树链剖分 线段树维护和与最值) 题意分析 (题目图片来自于 这里) 第一道树链剖分的题目,谈一下自己的理解. 树链剖分能解决的问题是,题目 ...

  5. BZOJ 3672[NOI2014]购票(树链剖分+线段树维护凸包+斜率优化) + BZOJ 2402 陶陶的难题II (树链剖分+线段树维护凸包+分数规划+斜率优化)

    前言 刚开始看着两道题感觉头皮发麻,后来看看题解,发现挺好理解,只是代码有点长. BZOJ 3672[NOI2014]购票 中文题面,题意略: BZOJ 3672[NOI2014]购票 设f(i)f( ...

  6. 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, ...

  7. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  8. 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 ...

  9. bzoj 4196 [Noi2015]软件包管理器 (树链剖分+线段树)

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 2852  Solved: 1668[Submit][Sta ...

随机推荐

  1. vue项目在APP禁止页面缩放

    veu-cli自动生成的项目中,index.html中meta 标签内容如下,放在手机上浏览 是可以放大缩小的<meta name="viewport" content=&q ...

  2. Python中的TCP编程,实现客户端与服务器的聊天(socket)

    参考大神blog:自己再写一个 https://blog.csdn.net/qq_31187881/article/details/79067644

  3. 关于logback

    1 logback是一个日志框架 2 logback的构成 LogBack被分为3个组件,logback-core, logback-classic 和 logback-access. 其中logba ...

  4. 函数的光滑化或正则化 卷积 应用 两个统计独立变量X与Y的和的概率密度函数是X与Y的概率密度函数的卷积

    http://graphics.stanford.edu/courses/cs178/applets/convolution.html Convolution is an operation on t ...

  5. Android 常用的权限

    添加WiFi以及访问网络的权限:    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE&q ...

  6. ABAP 面向对象(Object Orientation) OO

    [转自 http://blog.sina.com.cn/s/blog_7c7b16000101bhof.html]在程序中, 对象的识别和寻址是通过对象引用来实现的, 对象引用变量可以访问对象的属性和 ...

  7. ubuntu sudo-update出错Encountered a section with no Package: header

    Reading package lists... Error! E: Encountered a section with no Package: header E: Problem with Mer ...

  8. MySQL常用代码

    create database 数据库名 create table CeShi1( Uid varchar(50) primary key, Pwd varchar(50), Name varchar ...

  9. Data Structure Trie: suffix problem

    http://www.geeksforgeeks.org/suffix-array-set-1-introduction/ http://www.geeksforgeeks.org/pattern-s ...

  10. flex 坐标系

    全局坐标(舞台坐标)  本地坐标 内容坐标系  地图坐标(MapPoint) flash和flex针对不同的目的,提供了3种不同的坐标系 全局的就是(stage级别的) 本地坐标系(组件级别的) 内容 ...