RMQ问题
2016-04-05 15:17
375 查看
RMQ:给出一个非降序整数数组给出一些(i,j)询问,回答i-j中的次数出现最多的值的次数
10 3
-1 -1 1 1 1 1 3 10 10 10
2 3
1 10
5 10
0
#include<cstdio> #include<cstring> #include<vector> #include<algorithm> #include<iostream> using namespace std; const int N = 1e5 + 10; int n, tot, Q; int dp [20]; int num , cnt , Left , Right ; void RMQ_Init() { memset(dp, 0, sizeof(dp)); for(int i = 1; i <= tot; i++) dp[i][0] = cnt[i]; for(int j = 1; (1<<j) <= n; j++) for(int i = 1; i + (1<<j) - 1 <= tot; i++) dp[i][j] = max(dp[i][j-1], dp[i+(1<<(j-1))][j-1]); } int RMQ(int L, int R) { if(L > R) return 0; int k = 0; while((1<<(k+1)) <= R - L + 1) k++; return max(dp[L][k], dp[R-(1<<k)+1][k]); } int main() { int v, last_v, i; while(~scanf("%d",&n)) { if(n == 0) break; scanf("%d",&Q); tot = 0; memset(Left, 0, sizeof(Left)); memset(Right, 0, sizeof(Right)); memset(cnt, 0, sizeof(cnt)); for(i = 1; i <= n; i++) { scanf("%d",&v); if(i == 1) { ++tot; last_v = v; Left[tot] = 1; } if(last_v == v) { num[i] = tot; cnt[tot]++; Right[tot]++; } else { num[i] = ++tot; cnt[tot]++; Left[tot] = Right[tot] = i; last_v = v; } } RMQ_Init(); int L, R; for(int i = 0; i < Q; i++) { scanf("%d%d",&L,&R); if(num[L] == num[R]) printf("%d\n", R - L + 1); else { int tmp1 = Right[num[L]] - L + 1; int tmp2 = R - Left[num[R]] + 1; int tmp3 = RMQ(num[L] + 1, num[R] - 1); printf("%d\n",max(tmp1, max(tmp2, tmp3))); } } } return 0; }
10 3
-1 -1 1 1 1 1 3 10 10 10
2 3
1 10
5 10
0
相关文章推荐
- 通过 poj3368 问题讨论:RMQ问题的 tarjan_lca 求解
- POJ3264-Balanced Lineup RMQ
- [BZOJ2006][NOI2010][RMQ/主席树][二叉堆]超级钢琴
- RMQ问题
- RMQ 模板
- [PKU3264]Balanced Lineup
- Balanced Lineup(RMQ)(POJ 3264)
- RANGE MINIUM/MAXIUM QUERY问题
- POJ3368/HDU1806/UVa11235 Frequent Values 游程编码+RMQ
- poj3264 Balanced Lineup(RMQ +st)
- RMQ Codeforces Round #322 (Div. 2) B. Luxurious Houses
- RMQ_Sparse Table & Segment Tree
- poj_3264 Balanced Lineup 线段树+点修改/RMQ
- RMQ问题
- bzoj-3676 回文串
- codeforces 487B B. Strip(rmq+线段树+二分)
- HDU_4123 && POJ_4003 Bob’s Race (dfs / bfs + RMQ + 尺取)
- POJ_4002 && HDU_4122 Alice's mooncake shop(RMQ)
- UVA 11235 Frequent values(RMQ)
- POJ 2452 Sticks Problem(RMQ+二分)