超级钢琴的树上版本, 类似做法即可, 只不过区间转为dfs序了, 用点分求一下, 复杂度$O(nlog^2n)$

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 1e6+10;
int n, m, sum, rt;
struct _ {int to,w;};
vector<_> g[N];
int mx[N], sz[N], vis[N];
#define go for (int i=0,y,w; i<g[x].size()?w=g[x][i].w,y=g[x][i].to:0; ++i)
void getrt(int x, int fa) {
mx[x]=0,sz[x]=1;
go if (!vis[y]&&y!=fa) {
getrt(y,x),sz[x]+=sz[y];
mx[x]=max(mx[x],sz[y]);
}
mx[x]=max(mx[x],sum-sz[x]);
if (mx[rt]>mx[x]) rt=x;
} int s[N], L, R;
pii a[N]; void dfs(int x, int f, int d, int rt) {
s[++*s]=d, a[*s]=pii(L,R);
go if (!vis[y]&&y!=f) dfs(y,x,d+w,rt);
} void solve(int x) {
vis[x]=1,s[++*s]=0,L=R=*s;
go if (!vis[y]) dfs(y,x,w,x),R=*s;
go if (!vis[y]) {
mx[rt=0]=n,sum=sz[y];
getrt(y,0),solve(rt);
}
} int Log[N], f[N][20];
void init(int n) {
Log[0]=-1;
REP(i,1,n) f[i][0]=i,Log[i]=Log[i>>1]+1;
for (int j=1; (1<<j)<=n; ++j) {
for (int i=0; i+(1<<j)-1<=n; ++i) {
int x=f[i][j-1], y=f[i+(1<<(j-1))][j-1];
f[i][j]=s[x]>s[y]?x:y;
}
}
}
int RMQ(int l, int r) {
int k=Log[r-l+1];
int x=f[l][k],y=f[r-(1<<k)+1][k];
return s[x]>s[y]?x:y;
}
struct node {
int l,r,pos,opt,v;
node () {}
node (int l, int r, int pos) : l(l),r(r),pos(pos) {
opt = RMQ(l,r);
v = s[pos]+s[opt];
}
bool operator < (const node & rhs) const {
return v < rhs.v;
}
}; priority_queue<node> q;
int main() {
scanf("%d%d", &n, &m);
REP(i,2,n) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
g[u].pb({v,w}),g[v].pb({u,w});
}
sum=mx[0]=n,getrt(1,0),solve(rt);
init(*s);
REP(i,1,*s) if (a[i].x) q.push(node(a[i].x,a[i].y,i));
REP(i,1,m) {
node t = q.top();
printf("%d\n", t.v);q.pop();
if (t.opt!=t.l) q.push(node(t.l,t.opt-1,t.pos));
if (t.opt!=t.r) q.push(node(t.opt+1,t.r,t.pos));
}
}

bzoj 1267 Kth Number I (点分治,堆)的更多相关文章

  1. bzoj 1095 Hide 捉迷藏 - 动态点分治 -堆

    Description 捉迷藏 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子们决定在家里玩捉迷藏游戏.他们的家很大且构造很奇特,由N个屋子和N-1条双 ...

  2. POJ P2104 K-th Number

    You are working for Macrohard company in data structures department. After failing your previous tas ...

  3. POJ 2104:K-th Number 整体二分

    感觉整体二分是个很有趣的东西. 在别人的博客上看到一句话 对于二分能够解决的询问,如果有多个,那么如果支持离线处理的话,那么就可以使用整体二分了 树套树写了一天还是WA着,调得焦头烂额,所以决定学cd ...

  4. POJ2104 K-th Number(主席树)

    题目 Source http://poj.org/problem?id=2104 Description You are working for Macrohard company in data s ...

  5. POJ2104 K-th Number[主席树]【学习笔记】

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 51440   Accepted: 17594 Ca ...

  6. poj[2104]K-th Number

    Description You are working for Macrohard company in data structures department. After failing your ...

  7. [划分树] POJ 2104 K-th Number

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 51732   Accepted: 17722 Ca ...

  8. poj 2104 K-th Number(可持久线段树)

    K-th Number 持久化:http://www.cnblogs.com/tedzhao/archive/2008/11/12/1332112.html 结构:http://www.docin.c ...

  9. [POJ2104]K-th Number

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 34048   Accepted: 10810 Ca ...

随机推荐

  1. 数据仓库基础(八)Informatica 小例子

    本文转载自:http://www.cnblogs.com/evencao/p/3147843.html 之前看了一段数据库的基础,感觉自己对数据库的基础挺薄弱的.以后再学习其他东西的时候也需要经常能学 ...

  2. Ubuntu系统下使用Jenkins进行项目的自动构建还是项目回滚方法

    上面虽然实现了项目的自动部署,但是有时部署失败的时候我们需要回滚到指定版本的构建,这样才能更灵活的进行项目的构建部署.我们可以选择“参数化的构建过程”进行传递不同的参数来选择是进行新的构建还是回滚 如 ...

  3. Struts2 Spring Hibernate 框架整合 Annotation MavenProject

    项目结构目录 pom.xml       添加和管理jar包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns ...

  4. Python入门之Python Colorama模块

    Python的Colorama模块,可以跨多终端,显示字体不同的颜色和背景,只需要导入colorama模块即可,不用再每次都像linux一样指定颜色: 官方参考:https://pypi.org/pr ...

  5. Linux查看文件大小命令

    Linux查看文件大小命令 du命令 (1)du -b filepath 参数-b表示以字节计数 du -b filepath 参数-b表示以字节计数 #示例: $ du -b ~/Downloads ...

  6. Linux解压文件到指定目录

    Linux解压文件到指定目录 tar在Linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数:-c :create 建立压缩档案的参数:-x : 解压缩压缩 ...

  7. Android实践项目汇报(总结)

    天气客户端开发报告 1    系统需求分析 1.1功能性需求分析 天气预报客户端,最基本就是为用户提供准确的天气预报信息.天气查询结果有两种:一种是当天天气信息,信息结果比较详细,除温度.天气状况外还 ...

  8. bzoj 1010 玩具装箱toy -斜率优化

    P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1...N的N件玩具,第i件玩具 ...

  9. flash,sram

    flash写入的内容不会因电源关闭而失去,读取速度慢,成本较低,一般用作程序存储器或者低速数据读取的情况. sram有最快的读写速度,但电源掉落后其内容也会失去,价格昂贵,一般用作cpu的二级缓存,内 ...

  10. cogs 1962. [HAOI2015]树上染色

    ★★☆   输入文件:haoi2015_t1.in   输出文件:haoi2015_t1.out   简单对比 时间限制:1 s   内存限制:256 MB [题目描述] 有一棵点数为N的树,树边有边 ...