题意:一棵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. python基础===tkinter学习链接

    http://effbot.org/tkinterbook/tkinter-classes.htm

  2. sunos kernel src

    https://github.com/eocallaghan/AuroraUX-SunOS https://github.com/zoyanhui/coroutine-libtask https:// ...

  3. C#数据没初始化,使用会报错,可以初始化null

    protected void Page_Load(object sender, EventArgs e) { string[] A; if (B== 0) { A = new string[] {1, ...

  4. 【bzoj4459】JSOI2013丢番图

    某JSOI夏令营出题人啊,naive! 你还是得学习个,搬这种原题不得被我一眼看穿? 求个n^2的约数除以二,向上取整. #include<bits/stdc++.h> using nam ...

  5. 设计模式之笔记--抽象工厂模式(Abstract Factory)

    抽象工厂模式(Abstract Factory) 定义 抽象工厂模式(Abstract Factory),提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 类图 描述 多个抽象产品 ...

  6. Struts2学习笔记04 之 拦截器

    一.创建拦截器组件 1. 创建一个类,实现Interceptor接口,并实现intercept方法 2.注册拦截器 3.引用拦截器 二.拦截器栈 预置拦截器: 默认引用拦截器 拦截器调用顺序: Fil ...

  7. linux命令(34):less命令

    1.命令格式: less [参数]  文件 2.命令功能: less 与 more 类似,但使用 less 可以随意浏览文件,而 more 仅能向前移动,却不能向后移动,而且 less 在查看之前不会 ...

  8. Asp.net 模板下载和导入到DataTable中

    HTML页面: <tr> <td colspan=" style="text-align: left; border: 1px;"> <as ...

  9. hdu 2147(巴什博弈+NP图)

    kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/10000 K (Java/Others)Total ...

  10. GT-----如何做Android应用流量测试?

    1.如何判断一个应用的流量偏高? 如果看流量的绝对值看不出高低,那就找几个同类型的产品对比一下,如果完成同样的事物,被测应用比同类产品高很多,那就偏高了,可能有优化的空间. 2.如何找到有效的优化点? ...