题意:一棵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. 使用SQL Server连接xml接口,读取并解析数据

    --数据源格式,放到任意程序中部署接口即可--<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmln ...

  2. scikit-learn中的岭回归(Ridge Regression)与Lasso回归

    一.岭回归模型 岭回归其实就是在普通最小二乘法回归(ordinary least squares regression)的基础上,加入了正则化参数λ. 二.如何调用 class sklearn.lin ...

  3. 2018DDCTF Misc部分WP

    题目给出了这样一串字符:d4e8e1f4a0f7e1f3a0e6e1f3f4a1a0d4e8e5a0e6ece1e7a0e9f3baa0c4c4c3d4c6fbb9e1e6b3e3b9e4b3b7b7 ...

  4. C++之编译器与链接器工作原理

    原文来自:http://blog.sina.com.cn/s/blog_5f8817250100i3oz.html 这里并没不是讨论大学课程中所学的<编译原理>,只是写一些我自己对C++编 ...

  5. mysql 1366的错误 字符集错误解决方案

    最近用mysqlalchmy的时候遇到了 sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1366, "Incorrec ...

  6. 分割线用CSS样式做出来的效果

    一:单个标签实现分隔线:. ; ; line-height: 1px; border-left: 200px solid #ddd; border-right: 200px solid #ddd; t ...

  7. node起server--axios做前端请求----进行CORS--跨域请求

    CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing). 它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从 ...

  8. 883H - Palindromic Cut(思维+STL)

    题目链接:http://codeforces.com/problemset/problem/883/H 题目大意:给一段长度为n的字符串s,想让你把s切成几段长度相同的回文串,可以改变s中字符的排列, ...

  9. redis之(九)redis的事务机制

    [一]什么是redis的事务 --->redis的事务是一组命令的集合. --->redis的事务是保证一组命令,要么都执行,要么都不执行.但不支持一组命令中,其中一个或多个执行失败,不支 ...

  10. 微信小程序-如何自定义导航栏(navigationStyle)?

    小程序是越来越开放了,微信版本 6.6.0可以自定义导航? 先了解下app.json中window配置navigationStyle属性,即导航栏样式,仅支持 default/custom.custo ...