Aragorn's Story
直接套 线段树+树剖 板子
代码:
// Created by CAD on 2019/8/12.
#include <bits/stdc++.h>
#define lson (p<<1)
#define rson (p<<1|1)
using namespace std;
using ll=long long;
const int maxn=5e4+5;
ll d[maxn<<2],laz[maxn<<2];
int wt[maxn],w[maxn],head[maxn],dep[maxn];
int id[maxn],son[maxn],f[maxn],siz[maxn],top[maxn];
int tot,cnt,n;
void build(int l,int r,int p)
{
laz[p]=0;
if(l==r)
{
d[p]=wt[l];
return ;
}
int m=(l+r)>>1;
build(l,m,lson),build(m+1,r,rson);
d[p]=d[lson]+d[rson];
}
void pushdown(int s, int t,int p)
{
int m=(s+t)>>1;
d[lson]+=(m-s+1)*laz[p],d[rson]+=(t-m)*laz[p];
laz[lson]+=laz[p],laz[rson]+=laz[p];
laz[p]=0;
}
void update(int l,int r,int s,int t,int c,int p)
{
if(l<=s&&t<=r)
{
d[p]+=(t-s+1)*c;
laz[p]+=c;
return ;
}
if(laz[p]) pushdown(s,t,p);
int m=(s+t)>>1;
if(l<=m) update(l,r,s,m,c,lson);
if(r>m) update(l,r,m+1,t,c,rson);
d[p]=d[lson]+d[rson];
}
ll getsum(int l,int r,int s,int t,int p)
{
if(l<=s&&t<=r)
return d[p];
if(laz[p]) pushdown(s,t,p);
int m=(s+t)>>1;
if(l<=m) return getsum(l,r,s,m,lson);
else return getsum(l,r,m+1,t,rson);
}
struct edge{
int to,next;
}e[maxn<<1];
void add(int u,int v)
{
e[++cnt].to=v;
e[cnt].next=head[u];
head[u]=cnt;
}
void dfs1(int u,int fa,int deep)
{
dep[u]=deep;
f[u]=fa;
siz[u]=1;
int maxson=-1;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].to;
if(v==fa) continue;
dfs1(v,u,deep+1);
siz[u]+=siz[v];
if(siz[v]>maxson)
maxson=siz[v],son[u]=v;
}
}
void dfs2(int u,int topf)
{
top[u]=topf;
id[u]=++tot;
wt[tot]=w[u];
if(!son[u]) return ;
dfs2(son[u],topf);
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].to;
if(v==f[u]||v==son[u]) continue;
dfs2(v,v);
}
}
void updrange(int x,int y,int k)
{
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
update(id[top[x]],id[x],1,n,k,1);
x=f[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
update(id[x],id[y],1,n,k,1);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int m,p;
while(cin>>n>>m>>p)
{
cnt=tot=0;
for(int i=1;i<=n;++i) head[i]=0,son[i]=0,siz[i]=0;
for(int i=1;i<=n;++i) cin>>w[i];
for(int i=1,u,v;i<=m;++i) cin>>u>>v,add(u,v),add(v,u);
dfs1(1,0,1);
dfs2(1,1);
build(1,n,1);
for(int i=1;i<=p;++i)
{
string op;
cin>>op;
int x,y,k;
if(op=="I")
cin>>x>>y>>k,updrange(x,y,k);
else if(op=="D")
cin>>x>>y>>k,updrange(x,y,-k);
else if(op=="Q")
cin>>x,cout<<getsum(id[x],id[x],1,n,1)<<endl;
}
}
return 0;
}
Aragorn's Story的更多相关文章
- HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树
HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...
- hdu 3966 Aragorn's Story 树链剖分 按点
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3966 Aragorn's Story(树链剖分+树状数组)
pid=3966" target="_blank" style="">题目链接:hdu 3966 Aragorn's Story 题目大意:给定 ...
- HDU 3966 Aragorn's Story(树链剖分)
HDU Aragorn's Story 题目链接 树抛入门裸题,这题是区间改动单点查询,于是套树状数组就OK了 代码: #include <cstdio> #include <cst ...
- HDOJ 3966 Aragorn's Story
树链拆分+树阵 (进入坑....) Aragorn's Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/327 ...
- HDU - 3966 Aragorn's Story(树链剖分入门+线段树)
HDU - 3966 Aragorn's Story Time Limit: 3000MS Memory Limit: 32768KB 64bit IO Format: %I64d & ...
- HDU 3966 Aragorn's Story 动态树 树链剖分
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- [hdu3966]Aragorn's Story
传送门 题目描述 Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One ...
- Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组
Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...
- codeforces 148E Aragorn's Story 背包DP
Aragorn's Story Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/probl ...
随机推荐
- 多个div排列在同一行而不换行
有时候,我们可能会产生多个div标签横向排列而不换行的需求,具体有以下几种实现方法: 1. 同级div设置display:inline-block,父级div强制不换行例如: <html> ...
- Django之自定义标签,过滤器,以及inclusion_tag
目录 Django之自定义标签,过滤器,以及inclusion_tag 自定义过滤器 自定义标签 inclusion_tag inclusion_tag() 项目实例: inclusion_tag() ...
- 生成ini文件
setProfileString是无法直接生成ini文件的,如果不存在这个ini文件需要先创建,然后再setProfileString.示例代码//保存连接参数到配置文件if not FileExis ...
- The Party and Sweets CodeForces - 1159C (拓排)
优化连边然后拓排. #include <iostream> #include <sstream> #include <algorithm> #include < ...
- springboot 配合多个cachemanager
springboot集成 redis需要引入 官方推进用lettuce连接池,Lettuce的连接是基于Netty的,连接实例(StatefulRedisConnection)可以在多个线程间并发访问 ...
- apply,call 和 bind 有什么区别
三者都可以把函数应用到其他对象上,不是自身对象,apply,call是直接执行函数调用,bind是绑定,执行需要再次调用,apply和call的区别是apply接受数组作为参数,而call是接受逗号分 ...
- 用原生JS写省市二级联动
HTML代码 <select id="s1"> <option value="0">~请选择省份~</option> < ...
- this —— javascript
目录 为什么要讨论this this是什么 如何改变this的指向 箭头函数中的this 为什么要讨论this 代码一: function fun1(){ var aa = 'I am aa'; co ...
- Halide安装指南release版本
Halide安装指南 本版本针对Halide release版本 by jourluohua 使用的是Ubuntu 16.04 64位系统默认Gcc 4.6 由于halide中需要C++ 11中的特性 ...
- NDK: ant 错误 [javah] Exception in thread "main" java.lang.NullPointerException 多种解决办法
1.错误提示内容 2.ant脚本对应的内容 <?xml version="1.0" encoding="UTF-8"?> <!-- ===== ...