题意:一棵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. Exponial (欧拉定理+指数循环定理+欧拉函数+快速幂)

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2021 Description Everybody loves big numbers ...

  2. react-native中使用Echarts,自己使用WebView封装Echarts经验

    1.工作中遇到的问题 我们在使用react-native肯定遇到过各种奇葩的问题,比如引入Echarts时候莫名报错,但是Echarts官网明显告诉我们可以懒加载的,这是因为基本上js大部分原生的组件 ...

  3. Python学习笔记 - day12 - Python操作NoSQL

    NoSQL(非关系型数据库) NoSQL,指的是非关系型的数据库.NoSQL有时也称作Not Only SQL的缩写,是对不同于传统的关系型数据库的数据库管理系统的统称.用于超大规模数据的存储.(例如 ...

  4. Linux时间子系统之一:clock source(时钟源)【转】

    转自:http://blog.csdn.net/droidphone/article/details/7975694 clock source用于为linux内核提供一个时间基线,如果你用linux的 ...

  5. gnu app url[web][5星]

    http://www.gnu.org/software/software.zh-cn.html http://linux.chinaunix.net/news/2010/12/07/1175310.s ...

  6. Timer类

    构造方法:public Timer() 创建一个新计时器.相关的线程不 作为守护程序运行. public Timer(boolean isDaemon) 创建一个新计时器,可以指定其相关的线程作为守护 ...

  7. idea和maven使用的点点滴滴

    1.如果在使用maven打包时想跳过单元测试,可以在pom文件中配置如下代码 <build> <plugins> <plugin> <groupId>o ...

  8. HA集群

    //硬件准备: .两个机器,相同系统 .网卡ip为:aming 192.168.11.24 aming1 192.168.11.23 //实验准备: . hostname : aming , amin ...

  9. 如何消除类型是submit类型的按钮的默认文字 ‘确认提交’

    只需要加上value="" 即可.默认的文字就可以去掉了.

  10. webIcon

    webIcon是我在拿别人的模板参考的时候我发现的一个东西,觉得挺不错的一个东西,但是后来发现用webIcon其实我也不知道是好还是不好,因为要用到字体,字体文件其实挺大的,所以当你要的图标不多的时候 ...