通过 poj3368 问题讨论:RMQ问题的 tarjan_lca 求解
来源:互联网 发布:无主之地mac下载 编辑:程序博客网 时间:2024/06/06 15:03
首先是tarjan_lca 算法解决 RMQ 问题的思路
如何建立??
tarjan_lca:
探讨:
Frequent valuesTime Limit: 2000MS Memory Limit: 65536KTotal Submissions: 10390 Accepted: 3811
Description
You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, determine the most frequent value among the integers ai , ... , aj.
Input
The input consists of several test cases. Each test case starts with a line containing two integers n and q (1 ≤ n, q ≤ 100000). The next line contains n integers a1 , ... , an (-100000 ≤ ai ≤ 100000, for each i ∈ {1, ..., n}) separated by spaces. You can assume that for each i ∈ {1, ..., n-1}: ai ≤ ai+1. The following q lines contain one query each, consisting of two integers i and j (1 ≤ i ≤ j ≤ n), which indicate the boundary indices for the
query.
The last test case is followed by a line containing a single 0.
Output
For each query, print one line with one integer: The number of occurrences of the most frequent value within the given range.
int find_fa(int p){//int t = p, kp = p;for (; p != father[p]; p = father[p]);/*for (; kp != p; t = kp){kp = father[t];father[t] = p;}*/return p;}不是明显很慢吗???
#include <string.h>#include <stdio.h>struct rec_tree{int up, l, r;rec_tree():up(-1), l(-1), r(-1){}};struct rec_query{int u, qk, e;};const int maxn = 100005;rec_treetree[maxn];rec_queryquery[3*maxn];intans[maxn], head[maxn], a[maxn], nset[maxn], w[maxn], ql[maxn], qr[maxn], father[maxn];int tot, n, m, q, root;boolvis[maxn];void init(){memset(head, -1, sizeof(head));memset(vis, false, sizeof(vis));tot = 0;m = 0;}void add_query(int u1, int v1, int kv){query[tot].u = v1; query[tot].qk = kv; query[tot].e = head[u1]; head[u1] = tot++;query[tot].u = u1; query[tot].qk = kv; query[tot].e = head[v1]; head[v1] = tot++;}void readd(){for (int i = 0; i < n; ++i) scanf("%d", &a[i]);int j, k = 0;while (k < n){j = k;while (j < n && a[j] == a[j+1]) nset[j++] = m;nset[j] = m;w[m++] = j-k+1;k = j+1;}for (int i = 0; i < q; ++i){scanf("%d%d", &ql[i], &qr[i]);if (nset[ql[i]-1]+1 < nset[qr[i]-1]) add_query(nset[ql[i]-1]+1, nset[qr[i]-1]-1, i);}for (int i = 0; i < m; ++i)tree[i] = rec_tree();int l = 0, tp;root = 0;for (int i = 1; i < m; ++i){tree[l].r = i;tree[i].up = l;while (l != -1 && w[i] > w[l]){tp = tree[l].up;tree[i].up = tree[l].up; if (tree[l].up != -1) tree[tree[l].up].r = i;else root = i;tree[l].up = i;if (tree[i].l == -1){tree[i].l = l;tree[l].r = -1;}else {k = tree[i].l; tree[i].l = l;tree[l].r = k; tree[k].up = l;}l = tp;}l = i;}for (int i = 0; i < m; ++i) father[i] = i;}int find_fa(int p){int t = p, kp = p;for (; p != father[p]; p = father[p]);for (; kp != p; t = kp){kp = father[t];father[t] = p;}return p;}void tarjan_lca(int p){if (tree[p].l != -1){tarjan_lca(tree[p].l);father[tree[p].l] = p;}if (tree[p].r != -1){tarjan_lca(tree[p].r);father[tree[p].r] = p;}vis[p] = true;int tp = head[p];while (tp != -1){if (vis[query[tp].u]) ans[query[tp].qk] = w[find_fa(query[tp].u)];tp = query[tp].e;}}int main(){scanf("%d", &n);while (n != 0){scanf("%d", &q);init();readd();tarjan_lca(root);for (int i = 1; i < m; ++i)w[i] += w[i-1];int tm, maxt;for (int i = 0; i < q; ++i){if (nset[ql[i]-1] == nset[qr[i]-1])printf("%d\n", qr[i] - ql[i]+1);else if (nset[ql[i]-1]+1 == nset[qr[i]-1]){maxt = w[nset[ql[i]-1]]-ql[i]+1;if (qr[i]-w[nset[qr[i]-1]-1] > maxt) maxt = qr[i]-w[nset[qr[i]-1]-1];printf("%d\n", maxt);/*std::max(w[nset[ql[i]-1]]-ql[i]+1, qr[i]-w[nset[qr[i]-1]-1]*/}else {tm = w[nset[ql[i]-1]]-ql[i]+1;if (qr[i]-w[nset[qr[i]-1]-1] > tm) tm = qr[i]-w[nset[qr[i]-1]-1];//std::max(w[nset[ql[i]-1]]-ql[i]+1, qr[i]-w[nset[qr[i]-1]-1]);if (ans[i] > tm) tm = ans[i];printf("%d\n", tm);/*std::max(tm, ans[i])*/}}scanf("%d",&n);}}
- 通过 poj3368 问题讨论:RMQ问题的 tarjan_lca 求解
- POJ3368 离散化+ST算法求解RMQ
- RMQ poj3368
- ST(Sparse Table)算法求解RMQ问题
- RMQ的问题
- poj3368(线段树,RMQ)
- poj3368--Frequent values(RMQ)
- POJ3368&&HDOJ1086Frequent values【RMQ】
- RMQ模板题 poj3368
- poj3368 rmq变形
- RMQ 问题 POJ 3264 求解任意指定区间内的最小值和最大值
- Sprase-Table(S-T)算法求解RMQ问题
- RMQ问题
- RMQ问题
- RMQ问题
- RMQ问题
- RMQ问题
- RMQ问题
- ORACLE性能优化31条
- java基础-demo(黑马程序员)
- Maven项目导入Eclipse
- 内核编译Openwrt制作ipk动态更新文件
- ServiceGUI
- 通过 poj3368 问题讨论:RMQ问题的 tarjan_lca 求解
- Ubuntu:安装MySql经GUI客户端MySql-Workbench
- 生成excel文档即便没有安装微软Excel
- 自动化脚本过程中出现This element neither has attached source nor attached Javadoc...的解决方法
- 在cocos2d中的四种坐标
- Extjs4.1.x 框架搭建 采用Application动态按需加载MVC各模块
- php学习 第二十七节
- Android中长度单位的介绍
- MajiangService