题意:一棵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的更多相关文章

  1. 【树】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 ...

  2. LeetCode之“树”:Balanced Binary Tree

    题目链接 题目要求: Given a binary tree, determine if it is height-balanced. For this problem, a height-balan ...

  3. 【树】Construct Binary Tree from Inorder and Postorder Traversal

    题目: Given inorder and postorder traversal of a tree, construct the binary tree. 思路: 后序序列的最后一个元素就是树根, ...

  4. 【树】Construct Binary Tree from Preorder and Inorder Traversal

    题目: Given preorder and inorder traversal of a tree, construct the binary tree. 思路: 线序序列的第一个元素就是树根,然后 ...

  5. ✡ leetcode 156. Binary Tree Upside Down 旋转树 --------- java

    156. Binary Tree Upside Down Add to List QuestionEditorial Solution My Submissions   Total Accepted: ...

  6. LeetCode之“树”:Validate Binary Search Tree

    题目链接 题目要求: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is ...

  7. 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树

    平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...

  8. PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

  9. 【树】Binary Tree Zigzag Level Order Traversal

    题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from lef ...

随机推荐

  1. LCD实验学习笔记(八):中断

    s3c2440有60个中断源(其中15个为子中断源). 31个32位的通用寄存器,6个程序状态寄存器.有6种工作模式(系统/用户模式,快中断模式,管理模式,数据访问中止模式,中断模式,未定指令中止模式 ...

  2. 【Matlab】使用Matlab运行Windows命令

    可以使用Matlab的一些命令来帮助程序运行.比如说 ! calc % 打开计算器 ! mspaint % 打开画图 dos calc % 打开计算器 比如一个程序要运行很长时间,而我们又不能一直守在 ...

  3. Win10默认浏览器怎么设置

    1.首先在Win10桌面左下角的开始菜单图标上右键单击鼠标,在弹出的菜单选项中,点击进入“控制面板”,如下图所示. 接下来就可以找到“默认程序”设置了,找到后点击进入设置,如下图所示. 打开Win10 ...

  4. 004 ConcurrentHashMap原理

    下面这部分内容转载自: http://www.haogongju.net/art/2350374 JDK5中添加了新的concurrent包,相对同步容器而言,并发容器通过一些机制改进了并发性能.因为 ...

  5. c basic library framework - simplec 2.0.0

    前言 - simplec 单元测试 流程介绍 一个关于C基础库 simplec 2.0.0 发布了. 详细的文档介绍请参照 README.md. 说的再多都无用, 抵不上 gdb 一个 b r n. ...

  6. linux 系统调用exec()

    系统调用execve()对当前进程进行替换,替换者为一个指定的程序,其参数包括文件名(filename).参数列表(argv)以及环境变量(envp).exec函数族当然不止一个,但它们大致相同,在 ...

  7. saltstack安装和配置

    [root@web9 salt]# vi filetest.sls //ADD file_test: file.managed: - name: /tmp/lulu.com - source: sal ...

  8. Web开发中,页面渲染方案

    转载自:http://www.jianshu.com/p/d1d29e97f6b8 (在该文章中看到一段感兴趣的文字,转载过来) 在Web开发中,有两种主流的页面渲染方案: 服务器端渲染,通过页面渲染 ...

  9. Tomcat手动指定jdk路径

    Windows系统: 修改bin下的setclasspath.bat,在开头处添加: rem set specific jdkset JAVA_HOME=%CATALINA_HOME%\jdk1.8. ...

  10. Django在根据models生成数据库表时报错

    报错信息: E:\Python\s6day103>python manage.py makemigrations Traceback (most recent call last): File ...