【虚树】hdu6161 Big binary tree
题意:一棵n个结点的完全二叉树,初始i号结点的权值为i。有两种操作:单点修改;询问经过某个结点的路径中,权值和最大的路径的权值和是多少。
修改的时候,暴力修改到根节点的路径上的点的f(x)即可。
跟虚树的思想只是有点点像而已,实际上不是一个东西啦。
队友的代码:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; struct nod{
int lx,d;
long long c;
}q[110000]; struct nod2{
int sum[40],s[40],l;
}ceng;
int ls[2800000],i,n,m,all,wei[40];
long long t[2800000],v[2800000];
char s[100]; int fen(int a)
{
if (a==0) return 0;
int l=1,r=all,mid;
while (l!=r)
{
mid=(l+r)/2;
if (ls[mid]>=a) r=mid; else l=mid+1;
}
return l;
} void add(int a)
{
while (a)
{
ls[++all]=a;
a=a/2;
}
} void quchong()
{
int l,r,now=0;
l=1;
while (l<=all)
{
r=l;
while (r<=all && ls[l]==ls[r]) r++;
ls[++now]=ls[l];
l=r;
}
all=now;
} long long getv(int d)
{
if (d>n) return 0;
int k=fen(d),c,y,ans;
if (ls[k]==d) return t[k];
y=d; c=0;
while (y!=0)
{
c++;
y=y/2;
}
if (d==ceng.s[c]) return ceng.sum[c];
ans=0; y=d;
while (y<=n)
{
ans+=y;
y=y*2+1;
}
return ans; } void modify(int d,long long a)
{
d=fen(d); v[d]=a;
while (d!=0)
{
t[d]=max(getv(ls[d]*2),getv(ls[d]*2+1))+v[d];
d=fen(ls[d]/2);
}
} void getceng(int d)
{
int i;
ceng.l=0;
while (d)
{
ceng.s[++ceng.l]=d;
d=d/2;
}
for (i=1;i<=ceng.l/2;i++) swap(ceng.s[i],ceng.s[ceng.l-i+1]);
ceng.sum[ceng.l]=ceng.s[ceng.l];
for (i=ceng.l-1;i>=1;i--) ceng.sum[i]=ceng.s[i]+ceng.sum[i+1];
} long long getans(int d)
{
long long ans,p,sum=0;
int pre;
d=fen(d); p=t[d];
ans=getv(ls[d]*2)+getv(ls[d]*2+1)+v[d];
pre=d; d=fen(ls[d]/2);
while (d)
{
sum+=v[d];
if (ls[d]*2==ls[pre]) ans=max(ans,p+sum+getv(ls[d]*2+1)); else ans=max(ans,p+sum+getv(ls[d]*2));
pre=d;
d=fen(ls[d]/2);
}
return ans;
} void chushi()
{
int i;
for (i=all;i>=1;i--)
{
v[i]=ls[i];
t[i]=max(getv(ls[i]*2),getv(ls[i]*2+1))+v[i];
}
} int main()
{
wei[0]=1;
for (i=1;i<=30;i++) wei[i]=wei[i-1]*2;
while (scanf("%d%d",&n,&m)!=EOF)
{
all=0;
getceng(n);
for (i=1;i<=m;i++)
{
scanf("%s",&s);
if (s[0]=='q')
{
scanf("%d",&q[i].d);
q[i].lx=0;
}
if (s[0]=='c')
{
scanf("%d%lld",&q[i].d,&q[i].c);
q[i].lx=1;
}
add(q[i].d);
}
sort(ls+1,ls+all+1);
quchong();
chushi();
for (i=1;i<=m;i++)
{
if (q[i].lx==1) modify(q[i].d,q[i].c);
if (q[i].lx==0) printf("%lld\n",getans(q[i].d));
}
}
}
【虚树】hdu6161 Big binary tree的更多相关文章
- 【树】Flatten Binary Tree to Linked List(先序遍历)
题目: Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 ...
- LeetCode之“树”:Balanced Binary Tree
题目链接 题目要求: Given a binary tree, determine if it is height-balanced. For this problem, a height-balan ...
- 【树】Construct Binary Tree from Inorder and Postorder Traversal
题目: Given inorder and postorder traversal of a tree, construct the binary tree. 思路: 后序序列的最后一个元素就是树根, ...
- 【树】Construct Binary Tree from Preorder and Inorder Traversal
题目: Given preorder and inorder traversal of a tree, construct the binary tree. 思路: 线序序列的第一个元素就是树根,然后 ...
- ✡ leetcode 156. Binary Tree Upside Down 旋转树 --------- java
156. Binary Tree Upside Down Add to List QuestionEditorial Solution My Submissions Total Accepted: ...
- LeetCode之“树”:Validate Binary Search Tree
题目链接 题目要求: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is ...
- 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树
平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...
- PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- 【树】Binary Tree Zigzag Level Order Traversal
题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from lef ...
随机推荐
- mysql in/no in/like
% 任意字符 _ 任意一个字符 in (value,......) 在这里 not in (value,......) 不在这里 mysql> select 'a' not in (1,2,3, ...
- ImportError: libQtTest.so.4: cannot open shared
错误: import cv2 File , in <module> from .cv2 import * ImportError: libQtTest.so.: cannot open s ...
- 写个dump_stack【转】
转自:http://blog.chinaunix.net/uid-27714502-id-3434761.html 简单实现dump_stack 0.首先确保你能写个内核模块:打印"hell ...
- python基础===通过菲波那契数列,理解函数
def fib(n): # write Fibonacci series up to n """Print a Fibonacci series up to n.&quo ...
- 在Perl中采用open进行管道操作
在Perl中采用open进行管道操作 http://blog.sina.com.cn/s/blog_4840fe2a0100b8na.html perl exec管道和子进程 http://blog. ...
- webapi-2 接口参数
1. 实例 using System; using System.Collections.Generic; using System.Linq; using System.Net; using Sys ...
- Github精选 – 适合移动端的HTML5 Datepicker
2016-01-12 (updated: 2017-01-07) 15731 通过 HTML5 的 <input> 新属性可以简单方便地调用手机的原生 Datepicker,但功能较弱, ...
- MySQL乐观锁
MySQL悲观锁是依靠数据库的锁机制来实现,以实现最大程度上的独占性.但由于现代的web系统一般都是高并发的,所以悲观锁在这样的情况下的适用性不高,所以我们有了和悲观锁相对应的乐观锁. 乐观锁,是说假 ...
- find命令的基本用法
linux 中find 常用示例解析 find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path…] [expression] 其实[-H] [-L] [- ...
- 简述MapReduce计算框架原理
1. MapReduce基本编程模型和框架 1.1 MapReduce抽象模型 大数据计算的核心思想是:分而治之.如下图所示.把大量的数据划分开来,分配给各个子任务来完成.再将结果合并到一起输出.注: ...