hdu 3966 Aragorn's Story 树链剖分 按点
Aragorn's Story
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3494 Accepted Submission(s):
973
comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who
want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his
kingdom and M edges connect them. It is guaranteed that for any two camps, there
is one and only one path connect them. At first Aragorn know the number of
enemies in every camp. But the enemy is cunning , they will increase or decrease
the number of soldiers in camps. Every time the enemy change the number of
soldiers, they will set two camps C1 and C2. Then, for C1, C2 and all camps on
the path from C1 to C2, they will increase or decrease K soldiers to these
camps. Now Aragorn wants to know the number of soldiers in some particular camps
real-time.
input.
For each case, The first line contains three integers N, M, P
which means there will be N(1 ≤ N ≤ 50000) camps, M(M = N-1) edges and P(1 ≤ P ≤
100000) operations. The number of camps starts from 1.
The next line
contains N integers A1, A2, ...AN(0 ≤ Ai ≤ 1000), means at first in camp-i has
Ai enemies.
The next M lines contains two integers u and v for each,
denotes that there is an edge connects camp-u and camp-v.
The next P
lines will start with a capital letter 'I', 'D' or 'Q' for each
line.
'I', followed by three integers C1, C2 and K( 0≤K≤1000), which
means for camp C1, C2 and all camps on the path from C1 to C2, increase K
soldiers to these camps.
'D', followed by three integers C1, C2 and K(
0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2,
decrease K soldiers to these camps.
'Q', followed by one integer C, which
is a query and means Aragorn wants to know the number of enemies in camp C at
that time.
of enemies in the specified camp.
Q 3
1.The number of enemies may be negative.
2.Huge input, be careful.
#pragma comment(linker,"/STACK:100000000,100000000")
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef __int64 LL;
const int maxn = 5e4+; int pos,cont;
int a[maxn];
int son[maxn];
int head[maxn];
int vis[maxn];
int w[maxn];
int dep[maxn];
int father[maxn];
int hxl[maxn];
int top[maxn];
struct Edge
{
int to;
int next;
}edge[maxn*]; void init()
{
pos = cont = ;
memset(son,-,sizeof(son));
memset(head,-,sizeof(head));
memset(hxl,,sizeof(hxl));
}
void addedge(int u,int v)
{
edge[cont].to = v;
edge[cont].next = head[u];
head[u] = cont;
++cont;
}
void dfs1(int u,int fre,int deep)
{
father[u] = fre;
dep[u] = deep;
vis[u] = ;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v = edge[i].to;
if(v!=fre)
{
dfs1(v,u,deep+);
vis[u]=vis[u]+vis[v];
if(son[u] == - || vis[v] > vis[son[u]])
son[u] = v;
}
}
}
void dfs2(int u,int t)
{
top[u] = t;
if(son[u]!=-)
{
w[u]=++pos;
dfs2(son[u],t);
}
else
{
w[u]=++pos;
return;
}
for(int i=head[u];i!=-;i=edge[i].next)
{
int v = edge[i].to;
if(v!=son[u] && v!=father[u])
dfs2(v,v);
}
}
void add(int x,int n,int num1)
{
for(int i=x;i<=n;i=i+(i&(-i)))
hxl[i] = hxl[i] + num1;
}
LL query(int x)
{
if(x==)return ;
LL sum1 = ;
while(x)
{
sum1=sum1+hxl[x];
x=x-(x&(-x));
}
return sum1;
}
void insert(int u,int v,int num1,int size1)
{
int topu=top[u],topv=top[v];
while(topu!=topv)
{
if(dep[topu]<dep[topv])
{
swap(u,v);
swap(topu,topv);
}
add(w[topu],pos,num1*size1);
add(w[u]+,pos,-*num1*size1);
u=father[topu];
topu = top[u];
}
if(dep[u]>dep[v]) swap(u,v);
add(w[u],pos,num1*size1);
add(w[v]+,pos,-*num1*size1);
}
int main()
{
int n,m,q,u,v,l,r;
while(scanf("%d%d%d",&n,&m,&q)>)
{
init();
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=m;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
addedge(,);
addedge(,);
dfs1(,,);
dfs2(,);
char str1[];
while(q--)
{
scanf("%s",str1);
if(str1[]=='I')
{
scanf("%d%d%d",&l,&r,&v);
insert(l,r,v,);
}
else if(str1[]=='D')
{
scanf("%d%d%d",&l,&r,&v);
insert(l,r,v,-);
}
else if(str1[]=='Q')
{
scanf("%d",&r);
printf("%I64d\n",query(w[r])+a[r]);
}
}
}
return ;
}
hdu 3966 Aragorn's Story 树链剖分 按点的更多相关文章
- HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树
HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...
- Hdu 3966 Aragorn's Story (树链剖分 + 线段树区间更新)
题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2 ...
- 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 树链剖分+BIT区间修改/单点询问
Aragorn's Story Description Our protagonist is the handsome human prince Aragorn comes from The Lord ...
- HDU 3966 Aragorn's Story 树链剖分
Link: http://acm.hdu.edu.cn/showproblem.php?pid=3966 这题注意要手动扩栈. 这题我交g++无限RE,即使手动扩栈了,但交C++就过了. #pragm ...
- hdu 3966 Aragorn's Story : 树链剖分 O(nlogn)建树 O((logn)²)修改与查询
/** problem: http://acm.hdu.edu.cn/showproblem.php?pid=3966 裸板 **/ #include<stdio.h> #include& ...
- HDU 3966 Aragorn's Story (树链剖分入门题)
树上路径区间更新,单点查询. 线段树和树状数组都可以用于本题的维护. 线段树: #include<cstdio> #include<iostream> #include< ...
- HDU 3966 Aragorn's Story (树链点权剖分,成段修改单点查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 树链剖分的模版,成段更新单点查询.熟悉线段树的成段更新的话就小case啦. //树链剖分 边权修 ...
- HDU 3966 Aragorn's Story 树链拋分
一.写在前面 终于开始开坑link-cut-tree这个了,对于网上找到的大佬的前进路线,进行了一番研发,发现实际上可以实现对于树链拋分的制作.经历了若干长时间之后终于打了出来(为什么每次学什么东西都 ...
随机推荐
- 输出单向链表中倒数第k个结点
描述 输入一个单向链表,输出该链表中倒数第k个结点,链表的倒数第0个结点为链表的尾指针. 链表结点定义如下: struct ListNode { int m_nKey; ListNode* ...
- shell 常用正则
shell常用正则表达式 “^\d+$” //非负整数(正整数 + 0) “^[0-9]*[1-9][0-9]*$” //正整数 “^((-\d+)|(0+))$” //非正整数(负整数 ...
- Android课程---Activity的创建
一.为了创建一个新的Activity,你需要继承Activity类,定义UI,实现功能.新的Activity的最基本的框架代码如下所示: package com.paad.myapplication; ...
- Nginx下TIME_WAIT过多的调优
查看Nginx并发状态 #netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' TIME_WAIT 1259SYN_ ...
- Java简单类(部门、领导、雇员关系)
class Dept { private int deptno ; private String dname ; private String loc ; private Emp emps [] ; ...
- java工具类目录
1 读取properties文件数据 http://www.cnblogs.com/xumin/p/3143762.html 2 发送邮件 3 发送短信 4
- Linux系统下Redis安装(一)
最近项目要使用Redis,特将这段时间将Redis的学习经验与大家分享,算是对这段时间学习成果的总结和技术提炼,不足之处还望大家批评指正. 项目背景: 有些很少改动的数据和经常使用的数据(例如系统中下 ...
- Servlet 生命周期、工作原理
按照单例的编码规则,Servlet本身只是一个Java,结构并不是单例结构. 只是Web容器在维护这些Servlet的时候只给创建一个实例存在JVM中,用户请求服务时,服务器只调用它已经实例化好的Se ...
- 中兴F412光猫超级密码破解、破解用户限制、关闭远程控制、恢复路由器拨号
不少家庭都改了光纤入户,那肯定少不了光猫的吧.今天以中兴F412光猫为例介绍下此型号光猫超级密码的破解方法.一.F412超级密码破解方法1.运行CMD,输入telnet 192.168.1.1: 2. ...
- Django,数据模型创建之数据库API参考(转载)
一旦 数据模型 创建完毕, 自然会有存取数据的需要.本文档介绍了由 models 衍生而来的数据库抽象API,及如何创建,得到及更新对象. 贯穿本参考, 我们都会引用下面的民意测验(Poll)应用程序 ...