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,询问某点的权值. 解题分析 树链剖分模板 ...
随机推荐
- vue_music:封装scroll.vue组件
在项目中经常用到滚动,结合Better-scroll封装了sroll.vue组件参考链接:https://ustbhuangyi.github.io...http://www.imooc.com/ar ...
- VUE2.0声明周期钩子:不同阶段不同钩子的开启
- CDH4 journalnode方式手工安装手册之二
一. Hadoop配置修改 修改core-site.xml文件 <configuration> <property> ...
- 搭建基于金山快盘的Git服务器
最近迷上了Git,这货堪称神器,用了它就再也不想用其他VCS了,就像上了高速就不想再走国道一样. 一般人使用Git+Github来搭建进行本地远程交互,不过Github弄个私人仓库是要刀乐思的,如果你 ...
- P3391 【模板】文艺平衡树FHQ treap
P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转 ...
- BNU 3692 I18n 模拟
题意: 在一篇文章中,单词可以缩写.例如单词Internationalization可以缩写为I18n,缩写的两端是原单词的首尾字母,中间的数字是被省略的字母的个数. 现在给你一篇缩写的文章,输出展开 ...
- 新线程 handler
class CalculateThread extends Thread { private Handler handler; @Override public void run() { super. ...
- github仓库主页介绍
- [Cake] 2. dotnet 全局工具 cake
在上篇博客[Cake] 1. CI中的Cake中介绍了如何在CI中利用Cake来保持与CI/CD环境的解耦. 1. 简化cake的安装 当时dotnet 2.1还未正式发布,dotnet 还没有工具的 ...
- Selenium WebDriver- 操作 IFrame 中的页面元素
#encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver i ...