Main topic:
Three times the number of occurrences of a number in a range
Thinking Analysis:
It's really hard to get past the water, all sorts of optimizations.
The map cannot be used and the lowerbound will be processed first after discretization.
Optimize the input.
。
。
The time card is very tight.
。
The topic is directly used by the team water.
If you time out, it's best to try the top three optimizations.
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <map > #include <cmath> #define MAXN 100005using namespace Std;typedef long long ll;int app[maxn];int pos[maxn];int sav E[maxn];int x[maxn];map<int,int>mymap;struct foo{int l,r,index; LL ans; BOOL operator < (const foo &CMP) const {if (POS[L]==POS[CMP.L]) return r<cmp.r; Return pos[l]<pos[cmp.l]; }}q[maxn];int n;inline void scanf_ (int &num) {char in; BOOL Neg=false; while ((In=getchar () > ' 9 ' | | in< ' 0 ') && in!= '-'); if (in== '-') {neg=true; while (In=getchar ()) > ' 9 ' | | in< ' 0 '); } num=in-' 0 '; while (In=getchar (), in>= ' 0 ' &&in<= ' 9 ') num*=10,num+=in-' 0 '; if (neg) num=0-num;} BOOL cmp_id (const foo &a,const foo &b) {return a.index<b.index;} int sz;void Modify (int p,ll &ans,int add) {ans-= (LL) app[x[p]]*app[x[p]]*app[x[p]]; ApP[x[p]]+=add; ans+= (LL) app[x[p]]*app[x[p]]*app[x[p]];} int main () {while (scanf ("%d", &n)!=eof) {memset (app,0,sizeof app); int SIZE = (int) sqrt (n*1.0); for (int i=1;i<=n;i++) {scanf_ (save[i]); X[i]=save[i]; pos[i]= (i-1)/size+1; } int m; Scanf_ (m); for (int i=0;i<m;i++) {scanf_ (Q[I].L); Scanf_ (Q[I].R); Q[i].index=i; } sort (save+1,save+n+1); Sz=unique (save+1,save+n+1)-save; for (int i=1;i<=n;i++) {X[i] = Lower_bound (save+1,save+sz,x[i])-save; } sort (q,q+m); LL ans=0; for (int i=0,l=1,r=0;i<m;i++) {if (R<Q[I].R) {for (r=r+1;r<=q[i].r;r++ ) modify (r,ans,1); r--; } if (R>Q[I].R) {for (; r>q[i].r;r--) {mOdify (r,ans,-1); }} if (L<Q[I].L) {for (; l<q[i].l;l++) Modify (L,an S,-1); } if (L>Q[I].L) {for (l=l-1;l>=q[i].l;l--) modify (l,ans,1); l++; } if (Q[I].L==Q[I].R) {Q[i].ans=1; Continue } Q[i].ans=ans; } sort (q,q+m,cmp_id); for (int i=0;i<m;i++) printf ("%i64d\n", Q[i].ans); } return 0;}
Nbut 1457 Sona (Mo team algorithm)