HDU - 3966-Aragorn' Story(树链剖分+线段树)

时间:2023-02-03 11:39:09

链接:https://vjudge.net/problem/HDU-3966

题意:

Our protagonist is the handsome human prince Aragorn 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.

思路:

第一次写树链剖分, 直接上模板。

代码:

#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
#include <cstdio>
#include <set>
#include <iterator>
#include <cstring>
#include <assert.h>
using namespace std; typedef long long LL;
const int MAXN = 5e4+10;
vector<int> G[MAXN];
int Dis[MAXN], Fa[MAXN], Top[MAXN], Size[MAXN];
int Son[MAXN], Id[MAXN], Rk[MAXN];
int Seg[MAXN*4], A[MAXN], Add[MAXN*4];
int n, m, p;
int x, y;
int cnt = 0; void Init()
{
for (int i = 1;i <= n;i++)
G[i].clear();
memset(Seg, 0, sizeof(Seg));
memset(Son, 0, sizeof(Son));
memset(Add, 0, sizeof(Add));
cnt = 0;
} void Dfs1(int x, int u, int dep)
{
Dis[u] = dep;
Fa[u] = x;
Size[u] = 1;
for (int i = 0;i < G[u].size();i++)
{
int v = G[u][i];
if (v == x)
continue;
Dfs1(u, v, dep+1);
Size[u] += Size[v];
if (Size[v] > Size[Son[u]])
Son[u] = v;
}
} void Dfs2(int u, int top)
{
Top[u] = top;
Id[u] = ++cnt;
Rk[cnt] = u;
if (!Son[u])
return;
Dfs2(Son[u], top);
for (int i = 0;i < G[u].size();i++)
{
int v = G[u][i];
if (v != Son[u] && v != Fa[u])
Dfs2(v, v);
}
} void PushUp(int root)
{
Seg[root] = Seg[root<<1]+Seg[root<<1|1];
} void PushDown(int root, int l, int r)
{
if (Add[root])
{
int mid = (l+r)/2;
Add[root<<1] += Add[root];
Add[root<<1|1] += Add[root];
Seg[root<<1] += (mid-l+1)*Add[root];
Seg[root<<1|1] += (r-mid)*Add[root];
Add[root] = 0;
}
} void Build(int root, int l, int r)
{
if (l == r)
{
Seg[root] = A[Rk[l]];
return;
}
int mid = (l+r)/2;
Build(root<<1, l, mid);
Build(root<<1|1, mid+1, r);
PushUp(root);
} int Query(int root, int l, int r, int ql, int qr)
{
if (r < ql || qr < l)
return 0;
if (ql <= l && r <= qr)
return Seg[root];
PushDown(root, l, r);
int mid = (l+r)/2;
int res = 0;
res += Query(root<<1, l, mid, ql, qr);
res += Query(root<<1|1, mid+1, r, ql, qr);
PushUp(root);
return res;
} void Update(int root, int l, int r, int ql, int qr, int c)
{
if (r < ql || qr < l)
return;
if (ql <= l && r <= qr)
{
Seg[root] += (r-l+1)*c;
Add[root] += c;
return;
}
PushDown(root, l, r);
int mid = (l+r)/2;
Update(root<<1, l, mid, ql, qr, c);
Update(root<<1|1, mid+1, r, ql, qr, c);
PushUp(root);
} void UpdateLine(int l, int r, int c)
{
while (Top[l] != Top[r])
{
if (Dis[Top[l]] < Dis[Top[r]])
swap(l, r);
Update(1, 1, n, Id[Top[l]], Id[l], c);
l = Fa[Top[l]];
}
if (Id[l] < Id[r])
Update(1, 1, n, Id[l], Id[r], c);
else
Update(1, 1, n, Id[r], Id[l], c);
} int main()
{
// freopen("test.in", "r", stdin);
while (~scanf("%d%d%d", &n, &m, &p))
{
Init();
for (int i = 1;i <= n;i++)
scanf("%d", &A[i]);
for (int i = 1;i < n;i++)
{
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
Dfs1(0, 1, 1);
Dfs2(1, 1);
Build(1, 1, n);
// for (int i = 1;i <= n;i++)
// cout << Dis[i] << ' ';
// cout << endl;
// for (int i = 1;i <= n;i++)
// cout << Id[i] << ' ';
// cout << endl;
// for (int i = 1;i <= n;i++)
// cout << Top[i] << ' ' ;
// cout << endl;
char op[10];
int l, r, v, w;
while (p--)
{
scanf("%s", op);
if (op[0] == 'I')
{
scanf("%d%d%d", &l, &r, &v);
// cout << Top[l] << ' ' << Top[r] << endl;
UpdateLine(l, r, v);
}
else if (op[0] == 'D')
{
scanf("%d%d%d", &l, &r, &v);
v = -v;
UpdateLine(l, r, v);
}
else
{
scanf("%d", &w);
printf("%d\n", Query(1, 1, n, Id[w], Id[w]));
}
}
} return 0;
}

  

HDU - 3966-Aragorn' Story(树链剖分+线段树)的更多相关文章

  1. Aragorn&&num;39&semi;s Story 树链剖分&plus;线段树 &amp&semi;&amp&semi; 树链剖分&plus;树状数组

    Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...

  2. HDU 2460 Network(双连通&plus;树链剖分&plus;线段树)

    HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...

  3. 【BZOJ-2325】道馆之战 树链剖分 &plus; 线段树

    2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1153  Solved: 421[Submit][Statu ...

  4. 【BZOJ2243】&lbrack;SDOI2011&rsqb;染色 树链剖分&plus;线段树

    [BZOJ2243][SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的 ...

  5. BZOJ2243 &lpar;树链剖分&plus;线段树)

    Problem 染色(BZOJ2243) 题目大意 给定一颗树,每个节点上有一种颜色. 要求支持两种操作: 操作1:将a->b上所有点染成一种颜色. 操作2:询问a->b上的颜色段数量. ...

  6. POJ3237 &lpar;树链剖分&plus;线段树)

    Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...

  7. bzoj4034 (树链剖分&plus;线段树)

    Problem T2 (bzoj4034 HAOI2015) 题目大意 给定一颗树,1为根节点,要求支持三种操作. 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子 ...

  8. HDU4897 (树链剖分&plus;线段树)

    Problem Little Devil I (HDU4897) 题目大意 给定一棵树,每条边的颜色为黑或白,起始时均为白. 支持3种操作: 操作1:将a->b的路径中的所有边的颜色翻转. 操作 ...

  9. Aizu&Tab;2450 Do use segment tree 树链剖分&plus;线段树

    Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...

  10. 【POJ3237】Tree(树链剖分&plus;线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

随机推荐

  1. 给WordPress Page页面添加摘要输入框

    默认情况下 WordPress Page 编辑页面没有摘要(Excerpt)输入框,所以对 WordPress 进行 SEO 的时候比较麻烦. 这个时候我们就可以通过以下代码给我 WordPress ...

  2. mysql 1067 进程意外终止 无法启动

    查看日志 data/XXX.err 发现如下错误 [ERROR] InnoDB: Attempted to open a previously opened tablespace. Previous ...

  3. 开发方式-----C语言

    上期我们已经把C语言的开发平台搭建好了,还有不清楚地可以查看我上一篇的笔记,这次我们就要进行编辑C语言,那么它到底是怎么实现开发的呢?这一期我就来演示一次开发方式,至于说明为什么会这样或者这个是什么意 ...

  4. EBS CAS SSO测试

    https://wiki.jasig.org/display/CAS/CASifying+Oracle+Portal https://wenku.baidu.com/view/5f110a85b9d5 ...

  5. python中使用queue实现约瑟夫环(约瑟夫问题)求解

    约瑟夫问题:是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围. 从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又出列: 依 ...

  6. &lpar;25&rpar;线程---local数据隔离

    线程之间本身是数据共享的,当多个线程同时修改一份数据的时候,数据就可能不 准确,特别是线程量特别大的时候,为了保证数据准确性: (1) 通过线程锁Lock (2)通过local数据隔离 from th ...

  7. html中的a标签

    <a> 标签定义超链接,用于从一张页面链接到另一张页面.最重要的属性是 href 属性,它指示链接的目标,<href="#">表示跳转到自己.我们通常通过C ...

  8. request&period;getParameter&lpar;&rpar;和request&period;getAttribute&lpar;&rpar;的区别

    request.getParameter("val_1");这是获取请求的参数,比如你在url上看到的?id=12&name=abc就是参数,如果是post请求,就看不到. ...

  9. Interceptor for &lbrace;http&colon;&sol;&sol;cxf&period;liuyang&period;com&sol;&rcub;IHiServiceService has thrown exception&comma; unwinding now org&period;apache&period;cxf&period;binding&period;soap&period;SoapFault&colon; Error reading XMLStreamReader&period;

    Jquery同域访问:客户端连接服务器访问跨域访问:通过本地html文档,浏览器点击开访问(jquery不支持此访问) 用域名的方式访问http://localhost:8080/CXF_09_jqu ...

  10. php cache类代码&lpar;php数据缓存类&rpar;

    如果访问量大的话会给数据库造成很大的负担,所以对于变化不经常的内容要做好php 数据cache(缓存)是十分必要的,我做了一个简单的php“文件缓存”的类,希望对大家有所帮助. 思路是这样的: 对于一 ...