WA 18 求调
查看原帖
WA 18 求调
593595
_Aurore_楼主2023/2/2 13:16
#include<bits/stdc++.h>
#define int long long
#define MAXN 100001
using namespace std;
inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')
            f=-f;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
} 
const int len=500;
int n,a[MAXN],tot,m,b[MAXN];
map<int,int> h;
struct node{
    int l,r,id;
}q[MAXN];
int top;
struct Back{
    int ans,id;
}mo[MAXN];
bool cmp(node A,node B){
    int x=(A.l-1)/len,y=(B.l-1)/len;
    if(x!=y)
        return A.l<B.l;
    return A.r<B.r;
}
int sum,cnt[MAXN],ans[MAXN];
int res[MAXN];//这是处理单块内询问的 
void add(int x){
    cnt[a[x]]++;
    sum=max(sum,b[x]*cnt[a[x]]);
}
signed main(){
    n=read(),m=read();
    for(int i=1;i<=n;i++){
        a[i]=b[i]=read();
        int x=h[a[i]];
        if(!x){
            h[a[i]]=++tot;
            a[i]=tot;
        }
        else
            a[i]=x;
    }
    tot=0;
    for(int i=1;i<=m;i++){
        int l=read(),r=read();
        if(r-l+1<=len){
            int x=0;
            for(int j=l;j<=r;j++){
                res[a[j]]++;
                x=max(x,b[j]*res[a[j]]);
            }
            ans[i]=x;
            for(int j=l;j<=r;j++)
                res[a[j]]=0; 
        }
        else{
            q[++tot].l=l,q[tot].r=r;
            q[tot].id=i;
        }
    }
    sort(q+1,q+tot+1,cmp);
    int l=0,r=0;
    for(int i=1;i<=tot;i++){
        int x1=(q[i].r-1)/len,x2=(q[i].l-1)/len; 
        if((i==1)||(i==(q[i].l-1)/len!=(q[i-1].l-1)/len)){//在新的块内 
            int x=(q[i].l-1)/len;
            memset(cnt,0,sizeof(cnt));
            l=r=min(len*(x+1),n);
            sum=0;
            add(l);
            while(r<q[i].r)
                add(++r); 
            for(int j=l-1;j>=q[i].l;j--){
                ++top;
                mo[top].ans=sum;
                mo[top].id=a[i];
                add(i);
            }
            ans[q[i].id]=sum;
            while(top){
                sum=mo[top].ans;
                cnt[mo[top].id]--;
                top--;
            }
        }
        else{
            while(r<q[i].r)
                add(++r);
            for(int j=l-1;j>=q[i].l;j--){
                ++top;
                mo[top].ans=sum;
                mo[top].id=a[i];
                add(i);
            }
            ans[q[i].id]=sum;
            while(top){
                sum=mo[top].ans;
                cnt[mo[top].id]--;
                top--;
            }
        }
    }
    for(int i=1;i<=m;i++)
        cout<<ans[i]<<endl;
    return 0;
}
2023/2/2 13:16
加载中...