AC日记——Aragorn's Story HDU 3966
Aragorn's Story
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10510 Accepted Submission(s):
2766
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.
1 2 3
2 1
2 3
I 1 3 5
Q 2
D 1 2 2
Q 1
Q 3
4
8
1.The number of enemies may be negative.
2.Huge input, be careful.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> #define maxn 50001 using namespace std; struct TreeNodeType {
int l,r,dis,mid,flag; void clear()
{
l=,r=,dis=,mid=,flag=;
}
};
struct TreeNodeType tree[maxn<<]; struct EdgeType {
int to,next;
};
struct EdgeType edge[maxn<<]; int if_z,n,m,q,cnt,tot,Enum,deep[maxn],size[maxn],belong[maxn];
int flag[maxn],head[maxn],dis[maxn],dis_[maxn],f[maxn]; char Cget; inline void read_int(int &now)
{
now=,if_z=,Cget=getchar();
while(Cget<''||Cget>'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
now*=if_z;
} inline void edge_add(int from,int to)
{
edge[++Enum].to=from,edge[Enum].next=head[to],head[to]=Enum;
edge[++Enum].to=to,edge[Enum].next=head[from],head[from]=Enum;
} void search(int now,int fa)
{
int pos=tot++;
deep[now]=deep[fa]+,f[now]=fa;
for(int i=head[now];i;i=edge[i].next)
{
if(edge[i].to==fa) continue;
search(edge[i].to,now);
}
size[now]=tot-pos;
} void search_(int now,int chain)
{
int pos=;
flag[now]=++tot,dis_[flag[now]]=dis[now];
belong[now]=chain;
for(int i=head[now];i;i=edge[i].next)
{
if(flag[edge[i].to]) continue;
if(size[edge[i].to]>size[pos]) pos=edge[i].to;
}
if(pos!=) search_(pos,chain);
else return ;
for(int i=head[now];i;i=edge[i].next)
{
if(flag[edge[i].to]) continue;
search_(edge[i].to,edge[i].to);
}
} inline void tree_up(int now)
{
tree[now].dis=tree[now<<].dis+tree[now<<|].dis;
} inline void tree_down(int now)
{
if(tree[now].l==tree[now].r) return ;
tree[now<<].dis+=(tree[now<<].r-tree[now<<].l+)*tree[now].flag;
tree[now<<].flag+=tree[now].flag;
tree[now<<|].dis+=(tree[now<<|].r-tree[now<<|].l+)*tree[now].flag;
tree[now<<|].flag+=tree[now].flag;
tree[now].flag=;
} void tree_build(int now,int l,int r)
{
tree[now].clear();
tree[now].l=l,tree[now].r=r;
if(l==r)
{
tree[now].dis=dis_[++tot];
return ;
}
tree[now].mid=(l+r)>>;
tree_build(now<<,l,tree[now].mid);
tree_build(now<<|,tree[now].mid+,r);
tree_up(now);
} void tree_change(int now,int l,int r,int x)
{
if(tree[now].l==l&&tree[now].r==r)
{
tree[now].dis+=(r-l+)*x;
tree[now].flag+=x;
return ;
}
if(tree[now].flag) tree_down(now);
if(l>tree[now].mid) tree_change(now<<|,l,r,x);
else if(r<=tree[now].mid) tree_change(now<<,l,r,x);
else
{
tree_change(now<<,l,tree[now].mid,x);
tree_change(now<<|,tree[now].mid+,r,x);
}
tree_up(now);
} int tree_query(int now,int to)
{
if(tree[now].l==tree[now].r&&tree[now].l==to)
{
return tree[now].dis;
}
if(tree[now].flag) tree_down(now);
tree_up(now);
if(to>tree[now].mid) return tree_query(now<<|,to);
else return tree_query(now<<,to);
} inline void solve_change(int x,int y,int z)
{
while(belong[x]!=belong[y])
{
if(deep[belong[x]]<deep[belong[y]]) swap(x,y);
tree_change(,flag[belong[x]],flag[x],z);
x=f[belong[x]];
}
if(deep[x]<deep[y]) swap(x,y);
tree_change(,flag[y],flag[x],z);
} int main()
{
while(scanf("%d%d%d",&n,&m,&q)==)
{
memset(head,,sizeof(head));
memset(size,,sizeof(size));
memset(flag,,sizeof(flag));
tot=,cnt=,Enum=;
for(int i=;i<=n;i++) read_int(dis[i]);
int from,to,cur;
for(int i=;i<=m;i++)
{
read_int(from),read_int(to);
edge_add(from,to);
}
search(,),tot=,search_(,);
tot=,tree_build(,,n);
char type;
for(int i=;i<=q;i++)
{
cin>>type;
if(type=='I')
{
read_int(from),read_int(to),read_int(cur);
solve_change(from,to,cur);
}
if(type=='D')
{
read_int(from),read_int(to),read_int(cur);
solve_change(from,to,-cur);
}
if(type=='Q')
{
read_int(from);
printf("%d\n",tree_query(,flag[from]));
}
}
}
return ;
}
AC日记——Aragorn's Story HDU 3966的更多相关文章
- Aragorn's Story HDU - 3966 -树剖模板
HDU - 3966 思路 :树链剖分就是可以把一个路径上的点映射成几段连续的区间上.这样对于连续的区间可以用线段树维护, 对于每一段连续的区间都可以通过top [ ]数组很快的找到这段连续区间的头. ...
- A - Aragorn's Story HDU - 3966 树剖裸题
这个题目是一个比较裸的树剖题,很好写. http://acm.hdu.edu.cn/showproblem.php?pid=3966 #include <cstdio> #include ...
- AC日记——Dylans loves tree hdu 5274
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树
HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...
- hdu 3966 Aragorn's Story(树链剖分+树状数组)
pid=3966" target="_blank" style="">题目链接:hdu 3966 Aragorn's Story 题目大意:给定 ...
- 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) ...
- Hdu 3966 Aragorn's Story (树链剖分 + 线段树区间更新)
题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2 ...
- HDU 3966 (树链剖分+线段树)
Problem Aragorn's Story (HDU 3966) 题目大意 给定一颗树,有点权. 要求支持两种操作,将一条路径上的所有点权值增加或减少ai,询问某点的权值. 解题分析 树链剖分模板 ...
随机推荐
- CentOS 7 忘记root密码解决方法
CentOS 7 root密码的重置方式和CentOS 6完全不一样,CentOS 7与之前的版本6变化还是比较大的,以进入单用户模式修改root密码为例: 1.重启机器,进入grub菜单的时候按e ...
- python数据类型之字符串(str)和其常用方法
字符串是有序的,不可变的. 下面的例子说明了字符串是不可变的 name = 'alex' name = 'Jack' """ 并没有变,只是给name开启了一块新内存,储 ...
- WZK的减肥计划
WZK 的减肥计划(plan.cpp/ plan.in/ plan.out)问题描述:WZK 发现他的体重正迅猛的上升着,对此他感到非常焦虑,想要制定出一套完美的减肥计划. 于是 WZK 翻阅资料,查 ...
- INDEX && PRIMARY KEY && UNIQUE KEY
When I have do some sql tody, some confusion come up to me. Its about the index && PRIMARY K ...
- 03008_ServletContext
1.什么是ServletContext? (1)ServletContext代表是一个web应用的环境(上下文)对象,ServletContext对象 内部封装是该web应用的信息,Servle ...
- loj2027 「SHOI2016」黑暗前的幻想乡
矩阵树定理+模意义下整数高斯消元 #include <algorithm> #include <iostream> #include <cstring> #incl ...
- luogu2698 [USACO12MAR]花盆Flowerpot
单调队列+二分答案 #include <algorithm> #include <iostream> #include <cstring> #include < ...
- dubbo rpc filter实现剖析(二)
2.6.3版本,之前读的是2.4.9版本 本篇主要阐述dubbo rpc的filter的实现,包括作用,用法,原理,与Spring Cloud在这些能力的对比. 整个filter列表的获取过程在 co ...
- Leetcode 476.数字的补数
数字的补数 给定一个正整数,输出它的补数.补数是对该数的二进制表示取反. 注意: 给定的整数保证在32位带符号整数的范围内. 你可以假定二进制数不包含前导零位. 示例 1: 输入: 5 输出: 2 解 ...
- 薛XX后代的IQ CSU1597【循环节】或【快速幂】
薛先生想改变后代的IQ,为此他发明了一种药,这种药有三种属性:A, B,P.他父亲的智商为X,薛先生的智商为Y,用了这种药之后,薛先生的孩子的智商就可以变为(AX+BY) mod P.后代的智商以此类 ...