https://cn.vjudge.net/problem/SPOJ-COT

插上 大佬的代码 和 我的。。。以后再看吧。。。

Count on a tree

大佬:http://www.cnblogs.com/Sunnie69/p/5511684.html

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std; const int maxn=+;
int n,m,cnt,num;
int a[maxn],id[maxn],b[maxn],head[maxn],p[maxn],f[maxn],root[maxn];
bool vis[maxn];
struct edge{
int to,next;
edge(){}
edge(int a,int b):to(a),next(b){}
}g[maxn<<];
struct Qry{
int u,v,k,lca;
Qry(){}
Qry(int a,int b,int c,int d):u(a),v(b),k(c),lca(d){}
}Q[maxn];
struct node{ int l,r,s; }t[maxn*];
struct qry{
int v,id;
qry(){}
qry(int a,int b):v(a),id(b){}
};
vector <qry> q[maxn]; inline int find(int x){ return x==f[x]?x:f[x]=find(f[x]); }
void add_edge(int u,int v){
g[++cnt]=edge(v,head[u]); head[u]=cnt;
g[++cnt]=edge(u,head[v]); head[v]=cnt;
}
void update(int l,int r,int &pos,int d){
t[++num]=t[pos]; pos=num; t[pos].s++;
if(l==r) return;
int mid=l+(r-l)/;
if(d<=mid) update(l,mid,t[pos].l,d);
else update(mid+,r,t[pos].r,d);
}
bool cmp(int x,int y){ return a[x]<a[y]; }
void dfs(int u){
f[u]=u; root[u]=root[p[u]]; update(,n,root[u],b[u]);
for(int i=head[u];i;i=g[i].next){
if(g[i].to!=p[u]){
p[g[i].to]=u;
dfs(g[i].to);
f[g[i].to]=u;
}
}
vis[u]=true;
int size=q[u].size();
for(int i=;i<size;i++) if(vis[q[u][i].v]) Q[q[u][i].id].lca=find(q[u][i].v);
}
void init(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&a[i]), id[i]=i;
sort(id+,id+n+,cmp);
for(int i=;i<=n;i++) b[id[i]]=i;
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v);
}
for(int i=;i<=m;i++){
scanf("%d%d%d",&Q[i].u,&Q[i].v,&Q[i].k);
q[Q[i].u].push_back(qry(Q[i].v,i)); q[Q[i].v].push_back(qry(Q[i].u,i));
}
}
int query(int l,int r,int x,int y,int ra,int a,int k){
if(l==r) return l;
int mid=l+(r-l)/;
int s=t[t[x].l].s+t[t[y].l].s-*t[t[ra].l].s;
if(b[a]>=l&&b[a]<=mid) s++;
if(k<=s) return query(l,mid,t[x].l,t[y].l,t[ra].l,a,k);
else return query(mid+,r,t[x].r,t[y].r,t[ra].r,a,k-s);
}
void solve(){
dfs();
for(int i=;i<=m;i++){
if(Q[i].u==Q[i].v){ printf("%d\n",a[Q[i].u]); continue; }
printf("%d\n",a[id[query(,n,root[Q[i].u],root[Q[i].v],root[Q[i].lca],Q[i].lca,Q[i].k)]]);
}
}
int main(){
init();
solve();
return ;
}

蒟蒻:

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(a, n) for(int i=a; i<=n; i++)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int n, m, x, y, k, cnt;
int root[maxn], a[maxn], f[maxn], head[maxn], vis[maxn], pre[maxn];
struct node {int l, r, sum;}T[maxn];
struct edge {int v, next, lca;}Edge[maxn];
vector<int> v;
int getid(int x) { return lower_bound(v.begin(), v.end(), x) - v.begin() + ;}
vector<int> G[maxn]; struct quer {int u, v, id, lca, k;}Q[maxn];
vector<quer> q[maxn];
void add_(int u, int v)
{
Edge[cnt].v = v;
Edge[cnt].next = head[u];
head[u] = cnt++;
}
void add(int u, int v)
{
add_(u, v);
add_(v, u);
} int find(int x)
{
return f[x] == x?x:(f[x] == find(f[x]));
} void update(int l, int r, int& pos, int d)
{
T[++cnt] = T[pos], T[cnt].sum++, pos = cnt;
if(l == r) return;
int mid = l + (r - l) / ;
if(mid >= d) update(l, mid, T[pos].l, d);
else update(mid+, r, T[pos].r, d);
} void lca(int u)
{
f[u] = u;
root[u] = root[pre[u]];
update(, n, root[u], getid(u));
for(int i=head[u]; i!=-; i=Edge[i].next)
{
int v = Edge[i].v;
if(v == pre[u]) continue;
pre[v] = u;
lca(v);
f[v] = u;
}
vis[u] = true;
int size = q[u].size();
rap(, size-)
{
if(vis[q[u][i].v])
{
Q[q[u][i].id].lca = find(q[u][i].v);
}
}
} int query(int l, int r, int x, int y, int ra, int lca, int k)
{
if(l == r) return l;
int mid = l + (r - l) / ;
int sum = T[T[y].l].sum + T[T[x].l].sum - *T[T[ra].l].sum;
int lca_id = getid(lca);
if(lca_id >= l && lca_id <= mid) sum++;
if(sum >= k) return query(l, mid, T[x].l, T[y].l, T[ra].l, lca, k);
else return query(mid+, r, T[x].r, T[y].r, T[ra].r, lca, k - sum);
} int main()
{
mem(head, -);
cnt = ;
scanf("%d%d", &n, &m);
rap(, n)
{
scanf("%d", &a[i]);
v.push_back(a[i]);
}
sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());
rap(, n-)
{
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
}
rap(, m)
{
scanf("%d%d%d", &Q[i].u, &Q[i].v, &Q[i].k);
Q[i].id = i;
q[Q[i].u].push_back(Q[i]);
q[Q[i].v].push_back(Q[i]);
}
lca();
rap(, m)
{
if(Q[i].u == Q[i].v)
{
printf("%d\n",a[Q[i].u]);
continue;
} printf("%d\n",v[query(, n, root[Q[i].u], root[Q[i].v], root[Q[i].lca], Q[i].lca, Q[i].k) - ]);
}
return ;
}

🔺Count on a tree SPOJ - COT (无能为力。。。)的更多相关文章

  1. Count on a tree SPOJ - COT (主席树,LCA)

    You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer ...

  2. Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式)

    Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式) 题外话,这是我第40篇随笔,纪念一下.<( ̄︶ ̄)↗[GO!] 题意 是说有棵树,每个节点上 ...

  3. SPOJ - COT Count on a tree

    地址:http://www.spoj.com/problems/COT/en/ 题目: COT - Count on a tree #tree You are given a tree with N  ...

  4. spoj cot: Count on a tree 主席树

    10628. Count on a tree Problem code: COT You are given a tree with N nodes.The tree nodes are number ...

  5. SPOJ 10628 COT - Count on a tree(在树上建立主席树)(LCA)

    COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...

  6. SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)

    COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to  ...

  7. SPOJ Count on a tree

    Count on a tree Time Limit:129MS     Memory Limit:1572864KB     64bit IO Format:%lld & %llu Subm ...

  8. SPOJ Count on a tree(主席树+LCA)

    一.题目 COT - Count on a tree You are given a tree with N nodes. The tree nodes are numbered from 1 to  ...

  9. SPOJ 10628 Count on a tree(Tarjan离线 | RMQ-ST在线求LCA+主席树求树上第K小)

    COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to  ...

随机推荐

  1. Nginx入门篇(二)之Nginx部署与配置文件解析

    一.Nginx编译安装 ()查看系统环境 [root@localhost tools]# cat /etc/redhat-release CentOS Linux release (Core) [ro ...

  2. 【MySQL高级特性】高性能MySQL第七章

    2017-07-25 14:15:43 前言:MYSQL从5.0和5.1版本开始引入了很多高级特性,例如分区.触发器等,这对有其他关系型数据库使用 背景的用户来说可能并不陌生.这些新特性吸引了很多用户 ...

  3. 牛客小白月赛9 A签到(分数取模,逆元)

    传送门 对分母求一下逆元,把除法取模变成乘法取模,逆元介绍看这里 这种方法只适合模为质数的情况 #include<bits/stdc++.h> using namespace std; ; ...

  4. CentOS7.2最小化安装后系统优化

    系统初始化技术的演变 1.sysvinit技术 (1)Linux系统的第一个进程(pid=1)为init: Linux 操作系统的启动首先从 BIOS 开始,接下来进入 boot loader,由 b ...

  5. SQLAlchemy 简单笔记

    ORM 江湖##### 曾几何时,程序员因为惧怕SQL而在开发的时候小心翼翼的写着sql,心中总是少不了恐慌,万一不小心sql语句出错,搞坏了数据库怎么办?又或者为了获取一些数据,什么内外左右连接,函 ...

  6. Ubuntu系统下在PyCharm里用virtualenv集成TensorFlow

    我的系统环境 Ubuntu 18.04 Python3.6 PyCharm 2018.3.2 community(免费版) Java 1.8 安装前准备 由于众所周知的原因,安装中需要下载大量包,尽量 ...

  7. NO.2:自学python之路------变量类型、列表、字典

    引言 本周初步认识了库,并学习了Python中各种类型的变量和常用操作.并完成了较为完善的用户与商家购物界面设计. 正文 模块: Python有标准库和第三方库.第三方库需要安装才能使用.大量的库可以 ...

  8. PCA(主成分析)

    PCA通过将高维空间向量映射到低维,对于数据进行处理

  9. 深度学习-tensorflow学习笔记(2)-MNIST手写字体识别

    深度学习-tensorflow学习笔记(2)-MNIST手写字体识别超级详细版 这是tf入门的第一个例子.minst应该是内置的数据集. 前置知识在学习笔记(1)里面讲过了 这里直接上代码 # -*- ...

  10. 如何开发一个 PyCharm 插件

    PyCharm 是很多 Python 开发者优先选择的 IDE,功能强大,跨平台,提供免费社区版,非常良心.如果你想自己给PyCharm添加一些功能怎么办呢?有两个办法: 通过提需求实现,到 JetB ...