Test instructions: Give the number of N, and then give Q a query, to find the number of reverse order L to R
Write your own time, is each inquiry is re-inserted to find sum (r)-sum (L)
Decisive T
Later, I saw someone else's code----
Pre-processing, ask all possible intervals (1000*1000), then ask O (1)
And then want to write this time out, because there are too many inquiries----
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <Set>9#include <queue>Ten#include <algorithm> One using namespacestd; A -typedefLong LongLL; - Const intINF = (1<< -)-1; the Const intMod=1000000007; - Const intmaxn=1005; - - intA[MAXN]; + intC[MAXN];//tree-like array - intn,m; + intANS[MAXN][MAXN]; A at structnode{ - intX,id; - }P[MAXN]; - - intCMP (node N1,node n2) { - returnn1.x <n2.x; in } - to intLowbit (intx) {returnX & (-x);} + - intSumintx) { the intret=0; * while(x>0){ $RET + = c[x];x-=lowbit (x);Panax Notoginseng } - returnret; the } + A voidAddintXintd) { the while(x<=N) { +c[x]+=d;x+=lowbit (x); - } $ } $ - intMain () { - while(SCANF ("%d%d", &n,&m)! =EOF) { theMemset (c,0,sizeof(c)); -Memset (A,0,sizeof(a));Wuyimemset (ans,0,sizeof(ans)); the for(intI=1; i<=n;i++) { -scanf"%d",&p[i].x); WuP[i].id=i; - } AboutSort (p+1, p+n+1, CMP); $ for(intI=1, j=0; i<=n;i++){ - if(i==1|| P[i].x! = p[i-1].x) J + +; -a[p[i].id]=J; - } A + //for (int i=1;i<=n;i++) the //printf ("a[%d]=%d\n", I,a[i]); - $ for(intI=1; i<=n;i++){ theMemset (c,0,sizeof(c)); the intres=0; the for(intj=i;j<=n;j++){ theres+= (J-i)-sum (a[j]); -Add (A[j],1); inANS[I][J] =Res; the } the } About the while(m--){ the intL,r; thescanf"%d%d",&l,&R); +printf"%d\n", Ans[l][r]); - } the } Bayi return 0; the}
View Code
HDU 5273 Dylans loves sequence "tree-like array"