2015 Zhejiang province race b question. I used the enumeration + two points: Time complexity O (64*n), 1900ms ran over. There should be a better way.
#include <cstdio>#include<cstring>#include<cmath>#include<string>#include<vector>#include<queue>#include<algorithm>#include<iostream>using namespacestd;Const intMAXN = +;int Base[MAXN], tot;intT, N;inta[100000+Ten];inttmp, L, R, Mid;Long Longans;intMain () {scanf ("%d", &T); while(t--) {scanf ("%d", &N); for(inti =1; I <= N; i++) scanf ("%d", &A[i]); Sort (a+1, A +1+N); Ans=0; for(inti =1; I <= N; i++) {memset (Base,0,sizeof Base); Tot=0; TMP =A[i]; while(TMP)Base[tot++] = tmp%2, TMP = TMP/2; for(intj = Tot-1; J >=0; j--) { if(Base[j] = =0) { intMIN = (1<<j); intMAX = (1<< (j +1)) -1; intPOS1 =-1, Pos2 =-1; L=1, r =N, Mid; while(L <=r) {Mid= (L + r)/2; if(A[mid] >= MIN) R = mid-1, POS1 =mid; ElseL = mid +1; } l=1, r =N, Mid; while(L <=r) {Mid= (L + r)/2; if(A[mid] <= MAX) L = mid +1, Pos2 =mid; ElseR = Mid-1; } if(POS1! =-1&& Pos2! =-1) ans = ans + (Long Long) (POS2-POS1 +1); }}} printf ("%lld\n", ans); } return 0;}
ZOJ 3870 Team Formation