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. toString()方法简单分析

    问题描述 今天在使用spotbugs代码走查时发现这样一个问题,如下, String[] myArray=new String[] {"1","2"," ...

  2. Maven学习(七)-----Maven添加远程仓库

    Maven添加远程仓库 默认情况下,Maven从Maven中央仓库下载所有依赖关系.但是,有些库丢失在中央存储库,只有在Java.net或JBoss的储存库远程仓库中能找到. 1. Java.net资 ...

  3. Python基本编程题

    问题1:仅使用 Python 基本语法,即不使用任何模块,编写 Python 程序计算下列数学表达式的结果并输出,小数点后保留3位.‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬ ...

  4. Python登录,输入三次密码

    第一段python代码,写了一天,总算不报错了,值得纪念. 基本要求: 写一个登录界面,登录三次锁定用户 1. 包含一个用户信息文件,用户名和密码 2.黑名单文件 过程: 1.先检查是否在黑名单中,如 ...

  5. 印度电商Snapdeal获投$1.34亿 eBay领投

    据消息人士透露,eBay领投1.337亿美元,投资印度最大在线购物网站Snapdeal,最终或有可能全权收购该网站.据悉,在此次投资中,大部分资金来自eBay. 今年1月,曾有报道称,Snapdeal ...

  6. python2/3 发送https请求时,告警关闭方法

    问题: 使用Python3 requests发送HTTPS请求,已经关闭认证(verify=False)情况下,控制台会输出以下错误: InsecureRequestWarning: Unverifi ...

  7. redis 学习记录

    http://www.yiibai.com/redis/redis_quick_guide.html Redis 是一款依据BSD开源协议发行的高性能Key-Value存储系统(cache and s ...

  8. Python中用字符串导入module

    在Python中,无法通过字符串来导入一个module文件: import "string" # Error x = "string" import x # 不 ...

  9. ORM(object relational Maping)

    ORM即对象关系映射,是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是通过使用描述对象和数据库之间映射的元数据,将java程序中的对象自动持久化到关系数据库中.本质上 ...

  10. 基础系列(5)—— C#控制语句

    语句是程序中最小程序指令.C#语言中可以使用多种类型的语句,每一种类型的语句又可以通过多个关键字实现.以下是C# 语言中使用的主要控制语句 类别 关键字 选择语句  if.else.switch.ca ...