Analysis: a^b+2 (a&b) =a+b so->a^ (-B) +2 (a& (-B)) =a-b
Then the tree-like array can be discussed
Links: http://www.ifrog.cc/acm/problem/1023
Spit Groove: This problem is originally mod (2^40), obviously want to use fast ride ah, but with the later Crazy T, do not have to cross, do not know the question of the person
#include <iostream>#include<algorithm>#include<cmath>#include<vector>#include<string>#include<cstdio>#include<cstring>using namespaceStd;typedefLong LongLL;Const intN = 1e5+5;ConstLL mod = (1ll<< +);intt,n,m,a[n],b[n],p[n],cnt,kase; LL c[4][n],sum[4];inlinevoidinit () { for(intI=0; i<4; ++i) for(intj=0; j<=cnt;++j) c[i][j]=0; sum[0]=sum[1]=sum[2]=sum[3]=0;} InlinevoidUp (LL &X,ll T) {x+=T; if(X>=mod) x-=MoD;} InlinevoidAddintPosintX,ll T) { for(intI=x; i<=cnt; i+=i& (-i)) Up (c[pos][i],t);} Inline LL Ask (intPosintx) {LL ret=0; for(intI=x; I i-=i& (-i)) Up (Ret,c[pos][i]); returnret;} ll KSC (ll X,ll y) {ll ret=0; while(y) {if(y&1) up (ret,x); Y>>=1; Up (x,x); } returnret;}intMain () {scanf ("%d",&T); while(t--) {scanf ("%d%d",&n,&m); for(intI=1; i<=n; ++i) scanf ("%d",&A[i]); for(intI=1; i<=m; ++i) scanf ("%d", &b[i]), p[i]=C[i]; Sort (P+1, p+1+m); CNT= Unique (p+1, p+1+M)-p-1; intPtr=1; LL ret=0; Init (); for(intI=1; i<=n; ++i) {intPOS; for(; ptr<=m&&ptr<i; + +ptr) {POS= Lower_bound (p+1, p+1+CNT,B[PTR])-p; Add (0Pos1); ++sum[0]; Add (1, pos,ptr); Up (sum[1],ptr); Add (2, Pos,b[ptr]); Up (sum[2],b[ptr]); Add (3, pos,1ll*b[ptr]*ptr%mod); Up (sum[3],1ll*b[ptr]*ptr%MoD); } /**j<i,b[j]<a[i]**/POS= Lower_bound (p+1, p+1+cnt,a[i])-p; --POS; LL tmp=ask (0, POS); if(tmp!=0) {tmp= 1ll*i*a[i]%mod*tmp%mod;up (ret,tmp); //Up (RET,KSC (1ll*i*a[i]%mod,tmp));TMP =-(Ask (1, POS) *a[i]%MoD); //tmp =-KSC (Ask (1,pos), a[i]);Up (TMP,MOD); Up (RET,TMP); TMP=-(Ask (2, POS) *i%MoD); //tmp =-KSC (Ask (2,pos), i);Up (TMP,MOD); Up (RET,TMP); Up (Ret,ask (3, POS)); } /*********/ /**j<i,b[j]>a[i]**/POS= Upper_bound (p+1, p+1+cnt,a[i])-p; if(pos==cnt+1)Continue; --POS; TMP= sum[0]-ask (0, POS); if(tmp==0)Continue; TMP=-(1ll*i*a[i]%mod*tmp%MoD); //tmp=-KSC (1ll*i*a[i]%mod,tmp);Up (TMP,MOD); Up (RET,TMP); TMP= (sum[1]-ask (1, POS) +mod)%MoD; TMP= tmp*a[i]%MoD; //tmp = KSC (Tmp,a[i]);Up (ret,tmp); TMP= (sum[2]-ask (2, POS) +mod)%MoD; TMP= tmp*i%MoD; //tmp = KSC (tmp,i);Up (ret,tmp); TMP= (sum[3]-ask (3, POS) +mod)%MoD; TMP= (mod-tmp)%MoD; Up (RET,TMP); /*********/} init (); PTR=m; for(intI=n; I --i) {intPOS; for(; ptr>i&&ptr;--ptr) {POS= Lower_bound (p+1, p+1+CNT,B[PTR])-p; Add (0Pos1); ++sum[0]; Add (1, pos,ptr); Up (sum[1],ptr); Add (2, Pos,b[ptr]); Up (sum[2],b[ptr]); Add (3, pos,1ll*b[ptr]*ptr%mod); Up (sum[3],1ll*b[ptr]*ptr%MoD); } /**j>i,b[j]>a[i]**/POS= Upper_bound (p+1, p+1+cnt,a[i])-p; --POS; if(pos!=CNT) {LL tmp= sum[0]-ask (0, POS); if(tmp!=0) {tmp= 1ll*i*a[i]%mod*tmp%mod;up (ret,tmp); //Up (RET,KSC (1ll*i*a[i]%mod,tmp));TMP = (sum[1]-ask (1, POS) +mod)%MoD; TMP=-(tmp*a[i]%MoD); //tmp =-KSC (Tmp,a[i]);Up (TMP,MOD); Up (RET,TMP); TMP= (sum[2]-ask (2, POS) +mod)%MoD; TMP=-(tmp*i%MoD); //tmp =-KSC (tmp,i);Up (TMP,MOD); Up (RET,TMP); TMP= (sum[3]-ask (3, POS) +mod)%MoD; Up (RET,TMP); } } /*********/ /**j>i,b[j]<a[i]**/POS= Lower_bound (p+1, p+1+cnt,a[i])-p; --POS; LL tmp= Ask (0, POS); if(tmp==0)Continue; TMP=-(1ll*i*a[i]%mod*tmp%MoD); //tmp=-KSC (1ll*i*a[i]%mod,tmp);Up (TMP,MOD); Up (RET,TMP); TMP= Ask (1, POS); TMP= tmp*a[i]%MoD; //tmp = KSC (Tmp,a[i]);Up (ret,tmp); TMP= Ask (2, POS); TMP= tmp*i%MoD; //tmp = KSC (tmp,i);Up (ret,tmp); TMP= Ask (3, POS); TMP= (mod-tmp)%MoD; Up (RET,TMP); /*********/} printf ("Case #%d:%lld\n",++Kase,ret); } return 0;}
View Code
Linglong College OJ 1023-magic boy Bi Luo and his excited math problem tree-like array violence