1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| #include <iostream> #include <algorithm> #include <cmath> using namespace std; #define LL long long #define ll long long #define ULL unsigned long long #define Pair pair<LL,LL> #define ls rt<<1 #define rs rt<<1|1 #define Pi acos(-1.0) #define eps 1e-6 #define DBINF 1e100 #define mod 1000000007 #define MAXN 1e18 #define MS 1000009
LL n,m; LL a[MS]; LL b[MS],tb; LL c[MS]; struct node{ int l,r,id; }ask[MS]; LL sizee,bknum; LL belong[MS]; LL bkl[MS],bkr[MS];
LL cnp[MS]; LL cnt[MS]; LL ac[MS];
bool cmp(node t1,node t2){ if(belong[t1.l] ^ belong[t2.l]) return t1.l < t2.l; return t1.r < t2.r; }
void init_block(){ sizee = sqrt(n); bknum = n/sizee; for(int i=1;i<=bknum;i++){ bkl[i] = (i-1)*sizee+1; bkr[i] = i*sizee; } if(bkr[bknum] < n){ bknum++; bkl[bknum] = bkr[bknum-1]+1; bkr[bknum] = n; } for(int i=1;i<=bknum;i++){ for(int j=bkl[i];j<=bkr[i];j++){ belong[j] = i; } } }
int main() { ios::sync_with_stdio(false); cin >> n >> m; for(int i=1;i<=n;i++){ cin >> a[i]; b[i] = a[i]; } sort(b+1,b+n+1); tb = 1; for(int i=2;i<=n;i++){ if(b[i] != b[i-1]) b[++tb] = b[i]; } for(int i=1;i<=n;i++){ c[i] = lower_bound(b+1,b+tb+1,a[i]) - b; } for(int i=1;i<=m;i++){ int l,r; cin >> l >> r; ask[i] = {l,r,i}; } init_block(); sort(ask+1,ask+m+1,cmp); int L = 1,R = 0; int lastbk = 0; LL ans = 0; for(int i=1;i<=m;i++){ if(belong[ask[i].l] == belong[ask[i].r]){ LL tmp = 0; for(int j=ask[i].l;j<=ask[i].r;j++){ cnp[c[j]]++; tmp = max(tmp,cnp[c[j]]*a[j]); } ac[ask[i].id] = tmp; for(int j=ask[i].l;j<=ask[i].r;j++){ cnp[c[j]] = 0; } continue; } if(belong[ask[i].l] != lastbk){ for(;L<=bkr[belong[ask[i].l]];L++) cnt[c[L]]--; for(;R> bkr[belong[ask[i].l]];R--) cnt[c[R]]--; lastbk = belong[ask[i].l]; ans = 0; } while(R < ask[i].r){ cnt[c[++R]]++; ans = max(ans,cnt[c[R]]*a[R]); } LL tmp = ans; while(L > ask[i].l){ cnt[c[--L]]++; tmp = max(tmp,cnt[c[L]]*a[L]); } ac[ask[i].id] = tmp; while(L <= bkr[belong[ask[i].l]]) cnt[c[L++]]--; } for(int i=1;i<=m;i++){ cout << ac[i] << "\n"; } return 0; }
|