1. 程式人生 > >11.05T2 線段樹+卡特蘭數

11.05T2 線段樹+卡特蘭數

#3876 快速排序

 

描述
20181105121038_34071
輸入
20181105121139_16153
輸出

對於每個詢問,輸出一行三個整數,分別表示最大差距、最小差距和方案數。

樣例輸入[複製]
3 3
1 2 3 4 5 6
1 1 6
0 1 6 10
1 1 6
樣例輸出[複製]
9 3 5
9 3 5
提示

n,m<=5e5,-5e5<=val<=5e5,區間為偶數長度

    考場程式碼 code:
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<algorithm>
  4 #define N 2000005
  5 #define min(i,j) i>j?j:i
  6 #define lc (p<<1)
  7 #define rc (p<<1|1)
  8 using namespace std;
  9 const int
mod=1e9+7; 10 long long a[N],cat[N]; 11 struct SEGMENT_TREE { 12 int type; 13 struct node { 14 int l,r; 15 long long sum,lazy; 16 } t[N]; 17 inline void pushup(int p) { 18 t[p].sum=t[lc].sum+t[rc].sum; 19 } 20 inline void pushnow(int
p,int v) { 21 t[p].sum+=(t[p].r-t[p].l+1)*v; 22 t[p].lazy=t[p].lazy+v; 23 } 24 inline void pushdown(int p) { 25 if(t[p].lazy) { 26 pushnow(lc,t[p].lazy); 27 pushnow(rc,t[p].lazy); 28 t[p].lazy=0; 29 } 30 } 31 void build(int p,int l,int r) { 32 t[p].l=l,t[p].r=r; 33 if(l==r) { 34 if(type==3) { 35 t[p].sum=a[l]; 36 } else 37 t[p].sum=a[2*l-type]; 38 t[p].lazy=0; 39 return; 40 } 41 int mid=l+r>>1; 42 build(lc,l,mid); 43 build(rc,mid+1,r); 44 pushup(p); 45 } 46 void update(int p,int ql,int qr,int v) { 47 if(ql<=t[p].l&&t[p].r<=qr) { 48 pushnow(p,v); 49 return; 50 } 51 pushdown(p); 52 int mid=t[p].l+t[p].r>>1; 53 if(ql<=mid)update(lc,ql,qr,v); 54 if(qr>mid)update(rc,ql,qr,v); 55 pushup(p); 56 } 57 int query(int p,int ql,int qr) { 58 if(ql<=t[p].l&&t[p].r<=qr) { 59 return t[p].sum; 60 } 61 pushdown(p); 62 long long ans=0; 63 int mid=t[p].l+t[p].r>>1; 64 if(ql<=mid)ans+=query(lc,ql,qr); 65 if(qr>mid)ans+=query(rc,ql,qr); 66 pushup(p); 67 return ans; 68 } 69 } A,B,C; 70 int f[12]; 71 int n; 72 long long inv[N]; 73 inline void pre() { 74 inv[1]=1;int n1=n+1; 75 for(register int i=2;i<=n1;i++) 76 { 77 inv[i]=inv[mod%i]*(-mod/i)%mod; 78 inv[i]=inv[i]<0?inv[i]+mod:inv[i]; 79 } 80 cat[1]=1; 81 for(register int i=2; i<=n; i++)cat[i]=((cat[i-1]*((i<<2)-2)%mod)*inv[i+1])%mod; 82 } 83 inline int read(){ 84 int x=0; 85 bool f=true; 86 char c=getchar(); 87 while(!isdigit(c)){ 88 if(c=='-')f=false; 89 c=getchar(); 90 } 91 while(isdigit(c)){ 92 x=(x<<3)+(x<<1)+(c^48); 93 c=getchar(); 94 } 95 return f?x:-x; 96 } 97 int main() { 98 // freopen("sort.in","r",stdin); 99 // freopen("sort.out","w",stdout); 100 int m,n2,md,l2,r2,op,l,r,val; 101 long long max0,temp1; 102 n=read(),m=read();n2=n<<1; 103 pre(); 104 for(register int i=1; i<=n2; i++) 105 a[i]=read(); 106 A.type=1,B.type=0,C.type=3; 107 A.build(1,1,n); 108 B.build(1,1,n); 109 C.build(1,1,n2); 110 while(m--) { 111 op=read();l=read(),r=read();md=l+r>>1;l2=l>>1;r2=r>>1; 112 if(op) { 113 max0=C.query(1,md+1,r)-C.query(1,l,md); 114 if(l&1) 115 temp1=B.query(1,l2+1,r2)-A.query(1,l2+1,r2); 116 else 117 temp1=A.query(1,l2+1,r2+1)-B.query(1,l2,r2); 118 printf("%lld %lld %lld\n",max0,temp1,cat[(r-l+1)>>1]); 119 } 120 else{ 121 val=read(); 122 C.update(1,l,r,val); 123 if(l&1){ 124 B.update(1,l2+1,r2,val); 125 A.update(1,l2+1,r2,val); 126 } 127 else{ 128 B.update(1,l2,r2,val); 129 A.update(1,l2+1,r2+1,val); 130 } 131 } 132 } 133 return 0; 134 }

over