Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
崩了,无限RE,哪位大佬帮我看看#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<algorithm> using namespace std; const int MaxN = 2e5; const int INF = 1e6; int n, q, a, b, t; int num[MaxN], pos[MaxN]; struct NOOD{ int l, r, cnt; }ans[200010]; int f[MaxN + 10][50]; void Init() { memset(ans, 0, sizeof(ans)); memset(f, 0, sizeof(f)); } void RMQ() { for(int j = 1; 1 << j <= t; j++) for(int i = 1; i + (1 << j) - 1 <= t; i++){ f[i][j] = max(f[i][j - 1], f[i + 1 << (j - 1)][j - 1]); } } int sum(int l, int r) { if(l > r)return 0; int k = log(double(r - l + 1)) / log(2.0); return max(f[l][k], f[r - (1 << k) + 1][k]); } int main() { while(~scanf("%d%d", &n, &q) && n) { int p = 1; t = 1; Init(); pos[1] = 1; for(int i = 1; i <= n; i++)scanf("%d", &num[i]); for(int i = 2; i <= n + 1; i++) { if(num[i] != num[i - 1]){ ans[t].l = p; ans[t].r = i - 1; ans[t].cnt = i - p; f[t][0] = ans[t].cnt; p = i; t++; } pos[i] = t; } t--; RMQ(); for(int i = 1; i <= q; i++) { scanf("%d%d", &a, &b); if(pos[a] == pos[b])printf("%d\n", b - a + 1); else if(pos[b] - pos[a] == 1) printf("%d\n", max(ans[pos[a]].r - a + 1, b - ans[pos[b]].l + 1)); else { int ant = max(ans[pos[a]].r - a + 1, b - ans[pos[b]].l + 1); ant = max(ant, sum(pos[a] + 1, pos[b] - 1)); printf("%d\n", ant); } } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator