HDU 3966 Aragorn's Story 树链剖分

Link: http://acm.hdu.edu.cn/showproblem.php?pid=3966

这题注意要手动扩栈。

这题我交g++无限RE,即使手动扩栈了,但交C++就过了。

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
using namespace std;
const int maxn = 5e4+;
#ifdef WIN32
#define LL __int64
#else
#define LL long long
#endif int A[maxn],totw;
struct node
{
int fa,son,top,w,size,deep;
}tree[maxn];
vector<int> vt[maxn];
int tr[maxn]; void dfs_1(int p,int last,int de)
{
tree[p].son = -;
tree[p].fa = last;
tree[p].deep = de;
tree[p].size = ;
int num = vt[p].size();
for(int i = ;i < num;++i)
if(vt[p][i] != last)
{
dfs_1(vt[p][i],p,de+);
tree[p].size += tree[vt[p][i]].size;
if(- == tree[p].son || tree[vt[p][i]].size > tree[tree[p].son].size)
tree[p].son = vt[p][i];
}
}
void dfs_2(int p,int top)
{
tree[p].w = ++totw;
tree[p].top = top;
if(tree[p].son != -)
dfs_2(tree[p].son,top);
else return ;
int num = vt[p].size();
for(int i = ;i < num;++i)
{
if(vt[p][i] != tree[p].fa && vt[p][i] != tree[p].son)
dfs_2(vt[p][i],vt[p][i]);
}
} void sol()
{
totw = ;
memset(tr,,sizeof(tr));
memset(tree,,sizeof(tree));
dfs_1(,,);
dfs_2(,);
}
int lowbit(int x)
{
return x & (-x);
}
int query(int p)
{
p = tree[p].w;
int tot = ;
while(p >= )
{
tot += tr[p];
p -= lowbit(p);
}
return tot;
}
void add(int n,int p,int d)
{
while(p <= n)
{
tr[p] += d;
p += lowbit(p);
}
}
void update_sec(int n,int u,int v,int d)
{
if(tree[u].w > tree[v].w) swap(u,v);
add(n,tree[u].w,d);
add(n,tree[v].w+,-d);
}
void update(int n,int u,int v,int d)
{
/*
u,v不断靠近根结点
*/
while(tree[u].top != tree[v].top)
{
if(tree[tree[u].top].deep < tree[tree[v].top].deep) swap(u,v);
update_sec(n,tree[u].top,u,d);
u = tree[tree[u].top].fa;
}
update_sec(n,u,v,d);
} int main()
{
// freopen("in.txt","r",stdin);
int n,m,p;
while(scanf("%d%d%d",&n,&m,&p)!=EOF)
{
for(int i = ;i <= n;++i)
scanf("%d",&A[i]);
for(int i = ;i <= n;++i)
vt[i].clear();
int x,y;
for(int i = ;i < m;++i)
{
scanf("%d%d",&x,&y);
vt[x].push_back(y);
vt[y].push_back(x);
}
sol();
char oper[];
while(p--)
{
scanf("%s",oper);
int x,y,z;
if('Q' == oper[])
{
scanf("%d",&x);
printf("%d\n",A[x] + query(x));
}
else if('I' == oper[])
{
scanf("%d%d%d",&x,&y,&z);
update(n,x,y,z);
}
else if('D' == oper[])
{
scanf("%d%d%d",&x,&y,&z);
update(n,x,y,-z);
}
}
}
return ;
}
上一篇:【MFC】MFC DLEdit 设计属于自己的编辑框_鼠标悬停


下一篇:WebService接口定义及调用