64分超小细节求助!
查看原帖
64分超小细节求助!
407223
TulipeNoire楼主2021/9/5 14:01
#include <bits/stdc++.h>
using namespace std;
int n,m,p[200005],pos[200005],l=1,r,s,L[1005],R[1005],T;
long long first[200005],last[200005],first2[200005],last2[200005],res,ans[200005];
bool vis[200005];
struct P { 
	int l, r, idx; 
} a[200005];
struct Node {
    int p, idx;
} k[200005];
bool cmp(P x, P y) {
    if (pos[x.l] == pos[y.l]) return x.r < y.r;
    return pos[x.l] < pos[y.l];
}
bool rul(Node x, Node y) { 
	return x.p < y.p; 
}
int main() {
    scanf("%d", &n);
    s = sqrt(n), T = n / s;
    for (int i = 1; i <= T; i++) {
        if (i * s > n) break;
        L[i] = (i - 1) * s + 1;
        R[i] = i * s;
    }
    if (R[T] < n) T++, L[T] = R[T - 1] + 1, R[T] = n;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &p[i]);
        k[i].p = p[i], k[i].idx = i;
        pos[i] = i / s + 1;
    }
    sort(k + 1, k + n + 1, rul);
    int temp = 0, tot = 0;
    for (int i = 1; i <= n; i++) {
        if (k[i].p != temp) temp = k[i].p, tot++;
        p[k[i].idx] = tot;
    }
    scanf("%d", &m);
    for (int i = 1; i <= m; i++) {
        scanf("%d %d", &a[i].l, &a[i].r);
        a[i].idx = i;
    }
    sort(a + 1, a + m + 1, cmp);
    int now = 0;
    for (int i = 1; i <= m; i++) {
        if (pos[a[i].l] == pos[a[i].r]) {
            long long Tmp = 0;
            for (int j = a[i].l; j <= a[i].r; j++) last2[p[j]] = j;
            for (int j = a[i].r; j >= a[i].l; j--) first2[p[j]] = j;
            for (int j = a[i].l; j <= a[i].r; j++) Tmp = max(Tmp, last2[p[j]] - first2[p[j]]);
            for (int j = a[i].l; j <= a[i].r; j++) last2[p[j]] = 0;
            for (int j = a[i].r; j >= a[i].l; j--) first2[p[j]] = 0;
            ans[a[i].idx] = Tmp;
            continue;
        }
        if (pos[a[i].l] != now) {
            memset(first, 0, sizeof(first));
            memset(last, 0, sizeof(last));
            l = R[pos[a[i].l]]+1;
            r = R[pos[a[i].l]];
            res = 0;
            now = pos[a[i].l];
        }
        while (r < a[i].r) {
            r++;
            last[p[r]] = r;
            if (!first[p[r]]) first[p[r]] = r;
            res = max(res, last[p[r]] - first[p[r]]);
        }
        long long tmp = res;
        int l2 = l;
        while (l2 > a[i].l) {
            l2--;
            if (!last2[p[l2]]) last2[p[l2]]=l2;
            if (last[p[l2]]||last2[p[l2]])
                tmp = max(tmp, max(last[p[l2]],last2[p[l2]]) - l2);
        }
        ans[a[i].idx] = tmp;
        while (l2 < a[i].l) {
        	last2[p[l2]]=0;
        	l2++;
		}
    }
    for (int i = 1; i <= m; i++) {
        printf("%lld\n", ans[i]);
    }
    return 0;
}

每个点都是在几万行或者十几万行上错的。。。

2021/9/5 14:01
加载中...