zoukankan      html  css  js  c++  java
  • 蒟蒻的省选复习(不如说是noip普及组复习)————连载中

    1.筛法

     1 var
     2     n,m,i,x,tot,j                                 :longint;
     3     prime                                         :array[0..10000050] of boolean;
     4     p                                             :array[0..10000050] of longint;
     5 begin
     6     read(n,m);
     7     for i:=2 to n do
     8     begin
     9         if not prime[i] then
    10         begin
    11             inc(tot);
    12             p[tot]:=i;
    13         end;
    14         for j:=1 to tot do
    15         begin
    16             if (i*p[j]>n) then break;
    17             prime[i*p[j]]:=true;
    18             if i mod p[j]=0 then break;
    19         end;
    20     end;
    21     //for i:=1 to tot do prime[p[i]]:=true;
    22     for i:=1 to m do
    23     begin
    24         read(x);
    25         if x=1 then writeln('No') else 
    26         if (prime[x]) then writeln('No') else writeln('Yes');
    27     end;
    28 end.
    Prime
     1 var
     2     n,i,j,tot                                   :longint;
     3     nprime                                     :array[0..10000050] of boolean;
     4     p,phi                                    :array[0..10000050] of int64;
     5 begin
     6     read(n);
     7     phi[1]:=1;
     8     for i:=2 to n do
     9     begin
    10         if not nprime[i] then
    11         begin
    12             phi[i]:=i-1;
    13             inc(tot);
    14             p[tot]:=i;
    15         end;
    16         for j:=1 to tot do
    17         begin
    18             if (i*p[j]>n) then break;
    19             nprime[i*p[j]]:=true;
    20             if (i mod p[j]=0) then
    21             begin
    22                 phi[i*p[j]]:=phi[i]*p[j];
    23                 break;
    24             end else phi[i*p[j]]:=phi[i]*(p[j]-1);
    25         end;
    26     end;
    27     for i:=n to n do write(phi[i],' ');
    28 end.
    Phi
     1 var
     2     n,i,j,tot                                 :longint;
     3     nprime                                     :array[0..1000050] of boolean;
     4     p,miu                                     :array[0..1000050] of longint;
     5 begin
     6     read(n);
     7     miu[1]:=1;
     8     for i:=2 to n do
     9     begin
    10         if not nprime[i] then
    11         begin
    12             miu[i]:=-1;
    13             inc(tot);
    14             p[tot]:=i;
    15         end;
    16         for j:=1 to tot do
    17         begin
    18             if i*p[j]>n then break;
    19             nprime[i*p[j]]:=true;
    20             if (i mod p[j]=0) then
    21             begin
    22                 miu[i*p[j]]:=0;
    23                 break;
    24             end else miu[i*p[j]]:=-miu[i];
    25         end;
    26     end;
    27     for i:=1 to n do write(miu[i],' ');
    28 end.
    Miu
      1 const maxn=2000000;
      2 var
      3     N,L,T,LL                                    :int64;
      4     nprime                                         :Array[0..2000050] of boolean;
      5     p                                             :array[0..200050] of longint;
      6     pre                                         :array[0..2000050] of longint;
      7     pr                                             :array[0..2000050] of longint;
      8     las,other,len                                 :array[0..2000050] of int64;
      9     la,oth,le                                     :array[0..2000050] of longint;
     10     miu                                         :array[0..2000050] of longint;
     11     phi                                            :array[0..2000050] of int64;
     12     tot,i                                         :longint;
     13     a,b                                         :double;
     14 
     15 procedure pred();
     16 var
     17     i,j                                     :longint;
     18 begin
     19     miu[1]:=1;
     20     phi[1]:=1;
     21     for i:=2 to maxn do
     22     begin
     23         if not nprime[i] then 
     24         begin
     25             miu[i]:=-1;
     26             phi[i]:=i-1;
     27             inc(tot);
     28             p[tot]:=i;
     29         end;
     30         for j:=1 to tot do
     31         begin
     32             if i*p[j]>maxn then break;
     33             nprime[i*p[j]]:=true;
     34             if i mod p[j]=0 then
     35             begin
     36                 miu[i*p[j]]:=0;
     37                 phi[i*p[j]]:=int64(phi[i]*p[j]);
     38                 break;
     39             end else 
     40             begin
     41                 miu[i*p[j]]:=-miu[i];
     42                 phi[i*p[j]]:=phi[i]*(p[j]-1);
     43             end;
     44         end;
     45     end;
     46     for i:=1 to maxn do miu[i]:=miu[i-1]+miu[i];
     47     for i:=1 to maxn do phi[i]:=phi[i-1]+phi[i];
     48 end;
     49 
     50 procedure add(u,v,r:int64);
     51 begin
     52     inc(L);
     53     pre[L]:=las[u];
     54     las[u]:=L;
     55     other[L]:=v;
     56     len[L]:=r;
     57 end;
     58 
     59 function cal1(x:int64):int64;
     60 var
     61     ans,z,i,j,last                                :int64;
     62     pp,q,k,di                                    :int64;
     63 begin
     64     if x<=maxn then exit(phi[x]);
     65     k:=x mod maxn;
     66     ans:=0;
     67     pp:=las[k];
     68     while pp<>0 do
     69     begin
     70         q:=other[pp];
     71         if q=x then exit(len[pp]);
     72         pp:=pre[pp];
     73     end;
     74     i:=2;
     75     while i<=x do
     76     begin
     77         di:=x div i;
     78         last:=x div di;
     79         ans:=ans+(last-i+1)*cal1(di);
     80         i:=last+1;
     81     end;
     82     ans:=int64(x*(x+1) div 2-ans);
     83     add(k,x,ans);
     84     exit(ans);
     85 end;
     86 
     87 procedure ad(u,v,r:longint);
     88 begin
     89     inc(LL);
     90     pr[LL]:=la[u];
     91     la[u]:=LL;
     92     oth[LL]:=v;
     93     le[LL]:=r;
     94 end;
     95 
     96 function cal2(x:longint):longint;
     97 var
     98     last,i,di                                 :int64;
     99     ans                                     :longint;
    100     pp,q,k                                     :longint;
    101 begin
    102     if x<=maxn then exit(miu[x]);
    103     k:=x mod maxn;
    104     ans:=0;
    105     pp:=la[k];
    106     while pp<>0 do
    107     begin
    108         q:=oth[pp];
    109         if q=x then exit(le[pp]);
    110         pp:=pr[pp];
    111     end;
    112     i:=2;
    113     while i<=x do
    114     begin
    115         di:=x div i;
    116         last:=x div di;
    117         ans:=ans+(last-i+1)*cal2(di);
    118         i:=last+1;
    119     end;
    120     ans:=1-ans;
    121     ad(k,x,ans);
    122     exit(ans);
    123 end;
    124 
    125 begin
    126     read(T);
    127     pred;
    128     for i:=1 to T do
    129     begin
    130         read(N);
    131         writeln(cal1(N),' ',cal2(N));
    132     end;
    133     //writeln(a);
    134 end.
    杜教筛-bzoj3944
     1 var
     2     N,i,j,tot,w                                :longint;
     3     nprime                                     :array[0..1000050] of boolean;
     4     p                                         :array[0..1000050] of longint;
     5     f,t                                     :array[0..1000050] of int64;
     6 begin
     7     read(N);
     8     f[1]:=1;
     9     for i:=2 to N do
    10     begin
    11         if not nprime[i] then
    12         begin
    13             inc(tot);
    14             p[tot]:=i;
    15             f[i]:=i+1;
    16             t[i]:=i;
    17         end;
    18         for j:=1 to tot do
    19         begin
    20             if i*p[j]>N then break;
    21             nprime[i*p[j]]:=true;
    22             if (i mod p[j]=0) then
    23             begin
    24                 t[i*p[j]]:=t[i]*p[j];
    25                 if i*p[j]=t[i*p[j]] then
    26                 begin
    27                     w:=1;
    28                     while w<=i*p[j] do
    29                     begin
    30                         f[i*p[j]]:=f[i*p[j]]+w;
    31                         w:=w*p[j];
    32                     end;
    33                 end else f[i*p[j]]:=f[i*p[j] div t[i*p[j]]]*f[t[i*p[j]]];
    34                 break;
    35             end;
    36             t[i*p[j]]:=p[j];
    37             f[i*p[j]]:=f[i]*f[p[j]];
    38         end;
    39     end;
    40     for i:=1 to N do write(f[i],' ');
    41 end.
    积性函数筛--因数和

    2.平衡树

      1 var
      2     N,i,opt,x,t,tot                                 :longint;
      3     left,right,size,key                             :Array[0..100050] of longint;
      4 
      5 procedure lrot(var t:longint);
      6 var
      7     k                                                 :longint;
      8 begin
      9     k:=right[t];
     10     right[t]:=left[k];
     11     left[k]:=t;
     12     size[k]:=size[t];
     13     size[t]:=size[left[t]]+size[right[t]]+1;
     14     t:=k;
     15 end;
     16 
     17 procedure rrot(var t:longint);
     18 var
     19     k                                                 :longint;
     20 begin
     21     k:=left[t];
     22     left[t]:=right[k];
     23     right[k]:=t;
     24     size[k]:=size[t];
     25     size[t]:=size[left[t]]+size[right[t]]+1;
     26     t:=k;
     27 end;
     28 
     29 procedure maintain(var t:longint; flag:boolean);
     30 begin
     31     if not flag then
     32     begin
     33         if size[left[left[t]]]>size[right[t]] then rrot(t) else
     34         if size[right[left[t]]]>size[right[t]] then
     35         begin
     36             lrot(left[t]);
     37             rrot(t);
     38         end else exit;
     39     end else
     40     begin
     41         if size[right[right[t]]]>size[left[t]] then lrot(t) else
     42         if size[left[right[t]]]>size[left[t]] then
     43         begin
     44             rrot(right[t]);
     45             lrot(t);
     46         end else exit;
     47     end;
     48     maintain(left[t],false);
     49     maintain(right[t],true);
     50     maintain(t,false);
     51     maintain(t,true);
     52 end;
     53 
     54 procedure insert(var t:longint; v:longint);
     55 begin
     56     if t=0 then
     57     begin
     58         inc(tot);
     59         t:=tot;
     60         left[t]:=0;
     61         right[t]:=0;
     62         size[t]:=1;
     63         key[t]:=v;
     64         exit;
     65     end;
     66     inc(size[t]);
     67     if v<key[t] then insert(left[t],v) else insert(right[t],v);
     68     maintain(t,v>=key[t]);
     69 end;
     70 
     71 function delete(var t:longint; v:longint):longint;
     72 begin
     73     dec(size[t]);
     74     if (v=key[t]) or ((v<key[t]) and (left[t]=0)) or ((v>key[t]) and (right[t]=0)) then
     75     begin
     76         delete:=key[t];
     77         if (left[t]=0) or (right[t]=0) then t:=left[t]+right[t] else key[t]:=delete(left[t],v+1);
     78     end else
     79     if v<key[t] then exit(delete(left[t],v)) else exit(delete(right[t],v));
     80 end;
     81 
     82 function rank(var t:longint; v:longint):longint;
     83 begin
     84     if t=0 then exit(1);
     85     if v<=key[t] then exit(rank(left[t],v));
     86     exit(rank(right[t],v)+size[left[t]]+1);
     87 end;
     88 
     89 function select(var t:longint; v:longint):longint;
     90 begin
     91     if v=size[left[t]]+1 then exit(key[t]);
     92     if v<size[left[t]]+1 then exit(select(left[t],v));
     93     exit(select(right[t],v-size[left[t]]-1));
     94 end;
     95 
     96 function pred(var t:longint; v:longint):longint;
     97 begin
     98     if t=0 then exit(-1);
     99     if v<=key[t] then exit(pred(left[t],v));
    100     pred:=pred(right[t],v);
    101     if pred=-1 then exit(key[t]);
    102 end;
    103 
    104 function succ(var t:longint; v:longint):longint;
    105 begin
    106     if t=0 then exit(-1);
    107     if v>=key[t] then exit(succ(right[t],v));
    108     succ:=succ(left[t],v);
    109     if succ=-1 then exit(key[t]);
    110 end;
    111 
    112 begin
    113     read(N);
    114     for i:=1 to N do
    115     begin
    116         read(opt,x);
    117         if (opt=1) then insert(t,x) else 
    118         if (opt=2) then x:=delete(t,x) else
    119         if (opt=3) then writeln(rank(t,x)) else
    120         if (opt=4) then writeln(select(t,x)) else
    121         if (opt=5) then writeln(pred(t,x)) else
    122         if (opt=6) then writeln(succ(t,x));
    123     end;
    124 end.
    SBT
      1 {$Q-}
      2 {$inline on}
      3 const inf=1000000000;
      4 var
      5     n,m,rt,tot,i,j,k,top                    :longint;
      6     size,key,psum,ssum,msum,sum,a,st        :array[0..500050] of longint;
      7     son                                     :array[0..500050,0..2] of longint;
      8     lazy,tag,father                         :array[0..500050] of longint;
      9     c1,c2,c3                                :char;
     10      
     11 function max(x,y:longint):longint;inline;
     12 begin
     13     if x>y then exit(x) else exit(y);
     14 end;
     15  
     16 procedure swap(var x,y:longint);inline;
     17 var
     18     z                                       :longint;
     19 begin
     20     z:=x;
     21     x:=y;
     22     y:=z;
     23 end;
     24  
     25      
     26 function nw():longint;inline;
     27 var
     28     x                                       :longint;
     29 begin
     30     if (top>0) then
     31     begin
     32         x:=st[top];
     33         dec(top);
     34     end else
     35     begin
     36         inc(tot);
     37         x:=tot;
     38     end;
     39     lazy[x]:=0;
     40     tag[x]:=0;
     41     son[x,0]:=0;
     42     son[x,1]:=0;
     43     exit(x);
     44 end;
     45  
     46 function which(x:longint):longint;inline;
     47 begin
     48     if x=son[father[x],1] then exit(1) else exit(0);
     49 end;
     50  
     51 procedure update(x:longint);inline;
     52 begin
     53     size[x]:=size[son[x,0]]+size[son[x,1]]+1;
     54     sum[x]:=sum[son[x,0]]+sum[son[x,1]]+key[x];
     55     psum[x]:=max(psum[son[x,0]],key[x]+sum[son[x,0]]+max(0,psum[son[x,1]]));
     56     ssum[x]:=max(ssum[son[x,1]],key[x]+sum[son[x,1]]+max(0,ssum[son[x,0]]));
     57     msum[x]:=max(max(msum[son[x,0]],msum[son[x,1]]),max(ssum[son[x,0]],0)+max(psum[son[x,1]],0)+key[x]);
     58 end;
     59  
     60  
     61 procedure rot(x:longint);inline;
     62 var
     63     f,gf,opt                                :longint;
     64 begin
     65     f:=father[x];
     66     gf:=father[f];
     67     opt:=which(x);
     68     if gf<>0 then son[gf,which(f)]:=x;
     69     father[x]:=gf;
     70     son[f,opt]:=son[x,opt xor 1];
     71     father[son[x,opt xor 1]]:=f;
     72     son[x,opt xor 1]:=f;
     73     father[f]:=x;
     74     update(f);
     75     update(x);
     76 end;
     77  
     78 procedure splay(x,go:longint);inline;
     79 begin
     80     while father[x]<>go do
     81     begin
     82         if (father[father[x]]<>go) then
     83         begin
     84             if which(x)=which(father[x]) then rot(father[x]) else rot(x);
     85         end;
     86         rot(x);
     87     end;
     88     if go=0 then rt:=x;
     89 end;
     90  
     91 procedure build(x,l,r:longint);
     92 var
     93     mid                                     :longint;
     94 begin
     95     mid:=(l+r)>>1;
     96     key[x]:=a[mid];
     97     if (l=r) then
     98     begin
     99         size[x]:=1;
    100         sum[x]:=key[x];
    101         psum[x]:=key[x];
    102         ssum[x]:=key[x];
    103         msum[x]:=key[x];
    104         exit;
    105     end;
    106     if (l<mid) then
    107     begin
    108         son[x,0]:=nw;
    109         father[son[x,0]]:=x;
    110         build(son[x,0],l,mid-1);
    111     end;
    112     if (r>mid) then
    113     begin
    114         son[x,1]:=nw;
    115         father[son[x,1]]:=x;
    116         build(son[x,1],mid+1,r);
    117     end;
    118     update(x);
    119 end;
    120  
    121 procedure paint(x:longint);inline;
    122 begin
    123     lazy[x]:=lazy[x] xor 1;
    124     swap(son[x,0],son[x,1]);
    125     swap(psum[x],ssum[x]);
    126 end;
    127  
    128 procedure paintt(x,v:longint);inline;
    129 begin
    130     tag[x]:=1;
    131     lazy[x]:=0;
    132     key[x]:=v;
    133     sum[x]:=size[x]*v;
    134     msum[x]:=max(v,sum[x]);
    135     psum[x]:=msum[x];
    136     ssum[x]:=msum[x];
    137 end;
    138  
    139 procedure p_d(x:longint);inline;
    140 begin
    141     if (lazy[x]<>0) then
    142     begin
    143         if son[x,0]<>0 then paint(son[x,0]);
    144         if son[x,1]<>0 then paint(son[x,1]);
    145         lazy[x]:=0;
    146     end;
    147     if (tag[x]<>0) then
    148     begin
    149         if son[x,0]<>0 then paintt(son[x,0],key[x]);
    150         if son[x,1]<>0 then paintt(son[x,1],key[x]);
    151         tag[x]:=0;
    152     end;
    153 end;
    154  
    155 function kth(x,k:longint):longint;inline;
    156 begin
    157     if x=0 then exit(-1);
    158     p_d(x);
    159     if (size[son[x,0]]+1=k) then exit(x);
    160     if (size[son[x,0]]+1<k) then exit(kth(son[x,1],k-size[son[x,0]]-1));
    161     if (size[son[x,0]]+1>k) then exit(kth(son[x,0],k));
    162 end;
    163  
    164 procedure ins();
    165 var
    166     i,k,t,p,x                               :longint;
    167 begin
    168     read(k,t);
    169     for i:=1 to t do read(a[i]);
    170     p:=kth(rt,k+1); splay(p,0);
    171     x:=kth(rt,k+2); splay(x,p);
    172     son[x,0]:=nw;
    173     father[son[x,0]]:=x;
    174     build(son[x,0],1,t);
    175     update(x);
    176     update(p);
    177 end;
    178  
    179 procedure erase(x:longint);
    180 begin
    181     if x=0 then exit;
    182     inc(top);
    183     st[top]:=x;
    184     if (son[x,0]<>0) then erase(son[x,0]);
    185     if (son[x,1]<>0) then erase(son[x,1]);
    186 end;
    187  
    188 procedure del();
    189 var
    190     k,t,p,x                                 :longint;
    191 begin
    192     read(k,t);
    193     p:=kth(rt,k); splay(p,0);
    194     x:=kth(rt,k+t+1); splay(x,p);
    195     erase(son[x,0]);
    196     father[son[x,0]]:=0;
    197     son[x,0]:=0;
    198     update(x);
    199     update(p);
    200 end;
    201  
    202 procedure rev();
    203 var
    204     k,t,p,x                                 :longint;
    205 begin
    206     read(k,t);
    207     p:=kth(rt,k); splay(p,0);
    208     x:=kth(rt,k+t+1); splay(x,p);
    209     paint(son[x,0]);
    210     update(x);
    211     update(p);
    212 end;
    213  
    214  
    215 procedure make();
    216 var
    217     k,t,v,p,x                               :longint;
    218 begin
    219     read(k,t,v);
    220     p:=kth(rt,k); splay(p,0);
    221     x:=kth(rt,k+t+1); splay(x,p);
    222     paintt(son[x,0],v);
    223     update(x);
    224     update(p);
    225 end;
    226  
    227 function qsum():longint;
    228 var
    229     k,t,p,x                                 :longint;
    230 begin
    231     read(k,t);
    232     p:=kth(rt,k); splay(p,0);
    233     x:=kth(rt,k+t+1); splay(x,p);
    234     exit(sum[son[x,0]]);
    235 end;
    236  
    237 begin
    238     //assign(input,'testdata.in');reset(input);
    239     read(n,m);
    240     for i:=1 to n do read(a[i+1]);
    241     rt:=1; tot:=1;
    242     a[1]:=-inf; a[n+2]:=-inf; psum[0]:=-inf; ssum[0]:=-inf; msum[0]:=-inf;
    243     build(rt,1,n+2);
    244     readln;
    245     for i:=1 to m do
    246     begin
    247         read(c1,c2,c3);
    248         if (c1='I') then    
    249         begin
    250             while (c3<>' ') do read(c3);
    251             ins();
    252         end;
    253         if (c1='D') then
    254         begin
    255             while (c3<>' ') do read(c3);
    256             del();
    257         end;
    258         if (c1='R') then
    259         begin
    260             while (c3<>' ') do read(c3);
    261             rev();
    262         end;
    263         if (c3='K') then
    264         begin
    265             while (c3<>' ') do read(c3);
    266             make();
    267         end;
    268         if (c1='G') then
    269         begin
    270             while (c3<>' ') do read(c3);
    271             writeln(qsum());
    272         end;
    273         if (c3='X') then
    274         begin
    275             readln;
    276             writeln(msum[rt]);
    277             continue;
    278         end;
    279         readln;
    280     end;
    281 end.
    Splay

    3.线段树

      1 type
      2     rec                                         =record
      3         l,r                                     :longint;
      4         sum,lz,tg                                 :int64;
      5     end;
      6 
      7 var
      8     t                                             :array[0..300050] of rec;
      9     a                                             :array[0..100050] of int64;
     10     N,M,mo,i,opt,x,y                             :longint;
     11     z                                             :int64;
     12 
     13 procedure build(x,l,r:longint);
     14 var
     15     mid                                         :longint;
     16 begin
     17     t[x].l:=l; t[x].r:=r; t[x].tg:=1;
     18     if (t[x].l=t[x].r) then
     19     begin
     20         t[x].sum:=a[l] mod mo;
     21         exit;
     22     end;
     23     mid:=(t[x].l+t[x].r)>>1;
     24     build(x*2,l,mid);
     25     build(x*2+1,mid+1,r);
     26     t[x].sum:=(t[x*2].sum+t[x*2+1].sum) mod mo;
     27 end;
     28 
     29 procedure update(x:longint);
     30 begin
     31     t[x].sum:=(t[x].sum*t[x].tg+t[x].lz*(t[x].r-t[x].l+1)) mod mo;
     32     if (t[x].l=t[x].r) then
     33     begin
     34         t[x].tg:=1;
     35         t[x].lz:=0;
     36         exit;
     37     end;
     38     t[x*2].tg:=(t[x*2].tg*t[x].tg) mod mo;
     39     t[x*2+1].tg:=(t[x*2+1].tg*t[x].tg) mod mo;
     40     t[x*2].lz:=(t[x*2].lz*t[x].tg+t[x].lz) mod mo;
     41     t[x*2+1].lz:=(t[x*2+1].lz*t[x].tg+t[x].lz) mod mo;
     42     t[x].tg:=1;
     43     t[x].lz:=0;
     44 end;
     45 
     46 procedure c_tg(x,l,r:longint; y:int64);
     47 var
     48     mid                                         :longint;
     49 begin
     50     if (t[x].lz<>0) or (t[x].tg<>1) then update(x);
     51     if (t[x].l=l) and (t[x].r=r) then
     52     begin
     53         t[x].tg:=(t[x].tg*y) mod mo;
     54         exit;
     55     end;
     56     mid:=(t[x].l+t[x].r)>>1;
     57     if l>mid then c_tg(x*2+1,l,r,y) else
     58     if r<=mid then c_tg(x*2,l,r,y) else
     59     begin
     60         c_tg(x*2,l,mid,y);
     61         c_tg(x*2+1,mid+1,r,y);
     62     end;
     63     t[x].sum:=(t[x*2].sum*t[x*2].tg+t[x*2].lz*(t[x*2].r-t[x*2].l+1)+
     64         t[x*2+1].sum*t[x*2+1].tg+t[x*2+1].lz*(t[x*2+1].r-t[x*2+1].l+1)) mod mo;
     65 end;
     66 
     67 procedure c_lz(x,l,r:longint; y:int64);
     68 var
     69     mid                                         :longint;
     70 begin
     71     if (t[x].lz<>0) or (t[x].tg<>1) then update(x);
     72     if (t[x].l=l) and (t[x].r=r) then
     73     begin
     74         t[x].lz:=(t[x].lz+y) mod mo;
     75         exit;
     76     end;
     77     mid:=(t[x].l+t[x].r)>>1;
     78     if l>mid then c_lz(x*2+1,l,r,y) else
     79     if r<=mid then c_lz(x*2,l,r,y) else
     80     begin
     81         c_lz(x*2,l,mid,y);
     82         c_lz(x*2+1,mid+1,r,y);
     83     end;
     84     t[x].sum:=(t[x*2].sum*t[x*2].tg+t[x*2].lz*(t[x*2].r-t[x*2].l+1)+
     85         t[x*2+1].sum*t[x*2+1].tg+t[x*2+1].lz*(t[x*2+1].r-t[x*2+1].l+1)) mod mo;
     86 end;
     87 
     88 function find(x,l,r:longint):int64;
     89 var
     90     mid                                         :longint;
     91 begin
     92     if (t[x].lz<>0) or (t[x].tg<>1) then update(x);
     93     if (t[x].l=l) and (t[x].r=r) then exit(t[x].sum mod mo);
     94     mid:=(t[x].l+t[x].r)>>1;
     95     if l>mid then exit(find(x*2+1,l,r)) else
     96     if r<=mid then exit(find(x*2,l,r)) else
     97         exit((find(x*2,l,mid)+find(x*2+1,mid+1,r)) mod mo);
     98 end;
     99 
    100 begin
    101     read(N,M,mo);
    102     for i:=1 to N do read(a[i]);
    103     build(1,1,N);
    104     for i:=1 to M do
    105     begin
    106         read(opt);
    107         if opt=1 then
    108         begin
    109             read(x,y,z);
    110             c_tg(1,x,y,z);
    111         end else
    112         if opt=2 then
    113         begin
    114             read(x,y,z);
    115             c_lz(1,x,y,z);
    116         end else
    117         begin
    118             read(x,y);
    119             writeln(find(1,x,y));
    120         end;
    121     end;
    122 end.
    View Code

    4.树链剖分

      1 type
      2     rec                             =record
      3         l,r                         :longint;
      4         sum,lazy                     :int64;
      5     end;
      6 
      7 var
      8     N,M,R,mo,i,x,y,opt,L,tot        :longint;
      9     z                                 :int64;
     10     t                                 :array[0..300050] of rec;
     11     a,key                            :array[0..100050] of int64;
     12     size,num,dep,mson,top,father    :array[0..100050] of longint;
     13     vis                             :array[0..100050] of boolean;
     14     pre,last,other                     :array[0..200050] of longint;
     15 
     16 procedure swap(var x,y:longint);
     17 var
     18     z                                 :longint;
     19 begin
     20     z:=x;
     21     x:=y;
     22     y:=z;
     23 end;
     24 
     25 procedure add(u,v:longint);
     26 begin
     27     inc(L);
     28     pre[L]:=last[u];
     29     last[u]:=L;
     30     other[L]:=v;
     31 end;
     32 
     33 procedure dfs(x,fa:longint);
     34 var
     35     p,q                             :longint;
     36 begin
     37     size[x]:=1;
     38     p:=last[x];
     39     while p<>0 do
     40     begin
     41         q:=other[p];
     42         if q<>fa then
     43         begin
     44             father[q]:=x;
     45             dfs(q,x);
     46             size[x]:=size[x]+size[q];
     47             if size[q]>size[mson[x]] then mson[x]:=q;
     48         end;
     49         p:=pre[p];
     50     end;
     51 end;
     52 
     53 procedure make(x,depp,topp:longint);
     54 var
     55     p,q                             :longint;
     56 begin
     57     inc(tot);
     58     num[x]:=tot;
     59     a[tot]:=key[x];
     60     dep[x]:=depp;
     61     top[x]:=topp;
     62     if mson[x]<>0 then
     63     begin
     64         vis[mson[x]]:=true;
     65         make(mson[x],depp,topp);
     66     end;
     67     p:=last[x];
     68     while p<>0 do
     69     begin
     70         q:=other[p];
     71         if (vis[q]) or (q=mson[x]) then
     72         begin
     73             p:=pre[p];
     74             continue;
     75         end;
     76         vis[q]:=true;
     77         make(q,depp+1,q);
     78         p:=pre[p];
     79     end;
     80 end;
     81 
     82 procedure build(x,l,r:longint);
     83 var
     84     mid                             :longint;
     85 begin
     86     t[x].l:=l; t[x].r:=r;
     87     if (t[x].l=t[x].r) then
     88     begin
     89         t[x].sum:=a[l] mod mo;
     90         exit;
     91     end;
     92     mid:=(t[x].l+t[x].r)>>1;
     93     build(x*2,l,mid);
     94     build(x*2+1,mid+1,r);
     95     t[x].sum:=(t[x*2].sum+t[x*2+1].sum) mod mo;
     96 end;
     97 
     98 procedure update(x:longint);
     99 begin
    100     t[x].sum:=(t[x].sum+t[x].lazy*(t[x].r-t[x].l+1)) mod mo;
    101     if (t[x].l=t[x].r) then
    102     begin
    103         t[x].lazy:=0;
    104         exit;
    105     end;
    106     t[x*2].lazy:=(t[x*2].lazy+t[x].lazy) mod mo;
    107     t[x*2+1].lazy:=(t[x*2+1].lazy+t[x].lazy) mod mo;
    108     t[x].lazy:=0;
    109 end;
    110 
    111 procedure change(x,l,r:longint;y:int64);
    112 var
    113     mid                             :longint;
    114 begin
    115     if (t[x].l=l) and (t[x].r=r) then
    116     begin
    117         t[x].lazy:=(t[x].lazy+y) mod mo;
    118         exit;
    119     end;
    120     if (t[x].lazy<>0) then update(x);
    121     mid:=(t[x].l+t[x].r)>>1;
    122     if l>mid then change(x*2+1,l,r,y) else
    123     if r<=mid then change(x*2,l,r,y) else
    124     begin
    125         change(x*2,l,mid,y);
    126         change(x*2+1,mid+1,r,y);
    127     end;
    128     t[x].sum:=(t[x*2].sum+t[x*2].lazy*(t[x*2].r-t[x*2].l+1)+t[x*2+1].sum+t[x*2+1].lazy*(t[x*2+1].r-t[x*2+1].l+1)) mod mo;
    129 end;
    130 
    131 function find(x,l,r:longint):int64;
    132 var
    133     mid                             :longint;
    134 begin
    135     if (t[x].lazy<>0) then update(x);
    136     if (t[x].l=l) and (t[x].r=r) then exit(t[x].sum);
    137     mid:=(t[x].l+t[x].r)>>1;
    138     if l>mid then exit(find(x*2+1,l,r)) else
    139     if r<=mid then exit(find(x*2,l,r)) else
    140         exit((find(x*2,l,mid)+find(x*2+1,mid+1,r)) mod mo);
    141 end;
    142 
    143 procedure qchange(x,y:longint; z:int64);
    144 begin
    145     if dep[x]>dep[y] then swap(x,y);
    146     while dep[y]>dep[x] do
    147     begin
    148         change(1,num[top[y]],num[y],z);
    149         y:=father[top[y]];
    150     end;
    151     while top[x]<>top[y] do
    152     begin
    153         change(1,num[top[x]],num[x],z);
    154         change(1,num[top[y]],num[y],z);
    155         x:=father[top[x]];
    156         y:=father[top[y]];
    157     end;
    158     x:=num[x];
    159     y:=num[y];
    160     if x>y then swap(x,y);
    161     change(1,x,y,z);
    162 end;
    163 
    164 function qfind(x,y:longint):int64;
    165 var
    166     sum                             :int64;
    167 begin
    168     sum:=0;
    169     if dep[x]>dep[y] then swap(x,y);
    170     while dep[y]>dep[x] do
    171     begin
    172         sum:=(sum+find(1,num[top[y]],num[y])) mod mo;
    173         y:=father[top[y]];
    174     end;
    175     while top[x]<>top[y] do
    176     begin
    177         sum:=(sum+find(1,num[top[y]],num[y])) mod mo; 
    178         sum:=(sum+find(1,num[top[x]],num[x])) mod mo;
    179         y:=father[top[y]];        
    180         x:=father[top[x]];
    181     end;
    182     x:=num[x];
    183     y:=num[y];
    184     if x>y then swap(x,y);
    185     exit((sum+find(1,x,y)) mod mo);
    186 end;
    187 
    188 
    189 begin
    190     read(N,M,R,mo);
    191     for i:=1 to N do read(key[i]);
    192     for i:=1 to N-1 do
    193     begin
    194         read(x,y);
    195         add(x,y);
    196         add(y,x);
    197     end;
    198     dfs(R,0);
    199     //for i:=1 to n do writeln(size[i]);
    200     vis[R]:=true;
    201     make(R,1,R);
    202     build(1,1,N);
    203     for i:=1 to M do
    204     begin
    205         read(opt);
    206         if (opt=1) then
    207         begin
    208             read(x,y,z);
    209             qchange(x,y,z);
    210         end else
    211         if (opt=2) then
    212         begin
    213             read(x,y);
    214             writeln(qfind(x,y));
    215         end else
    216         if (opt=3) then
    217         begin
    218             read(x,z);
    219             change(1,num[x],num[x]+size[x]-1,z);
    220         end else
    221         begin
    222             read(x);
    223             writeln(find(1,num[x],num[x]+size[x]-1));
    224         end;
    225     end;
    226 end.
    View Code

    5.主席树

     1 type
     2     rec                                     =record
     3     l,r,sum                                 :longint;
     4 end;
     5 
     6 var
     7     N,M,i,j,x,y,z,tot                        :longint;
     8     a,rt,reco,num,b,map                        :array[0..50050] of longint;
     9     t                                         :Array[0..50050*20] of rec;
    10 
    11 procedure swap(var x,y:longint);
    12 var
    13     z                                         :longint;
    14 begin
    15     z:=x;
    16     x:=y;
    17     y:=z;
    18 end;
    19 
    20 procedure sort(l,r:longint);
    21 var
    22     i,j,x                                     :longint;
    23 begin
    24     i:=l; j:=r; x:=b[(l+r)>>1];
    25     while i<j do
    26     begin
    27         while b[i]<x do inc(i);
    28         while b[j]>x do dec(j);
    29         if i<=j then
    30         begin
    31             swap(b[i],b[j]);
    32             swap(num[i],num[j]);
    33             inc(i);
    34             dec(j);
    35         end;
    36     end;
    37     if i<r then sort(i,r);
    38     if j>l then sort(l,j);
    39 end;
    40 
    41 
    42 function build(x,y,z,l,r:longint):longint;
    43 var
    44     cur,mid                                    :longint;
    45 begin
    46     inc(tot);
    47     cur:=tot;
    48     t[cur]:=t[x];
    49     t[cur].sum:=t[x].sum+z;
    50     if (l=r) then exit(cur);
    51     mid:=(l+r)>>1;
    52     if y>mid then t[cur].r:=build(t[cur].r,y,z,mid+1,r) else t[cur].l:=build(t[cur].l,y,z,l,mid);
    53     exit(cur);
    54 end;
    55 
    56 function find(x,y,k,l,r:longint):longint;
    57 var
    58     mid,s                                     :longint;
    59 begin
    60     if l=r then exit(l);
    61     s:=t[t[y].r].sum-t[t[x].r].sum;
    62     mid:=(l+r)>>1;
    63     if k<=s then exit(find(t[x].r,t[y].r,k,mid+1,r)) else exit(find(t[x].l,t[y].l,k-s,l,mid));
    64 end;
    65 
    66 
    67 begin
    68     read(N);
    69     for i:=1 to N do 
    70     begin
    71         read(b[i]);
    72         reco[i]:=b[i];
    73         num[i]:=i;
    74     end;
    75     sort(1,N);
    76     for i:=1 to N do a[num[i]]:=i;
    77     for i:=1 to N do map[a[i]]:=reco[i];
    78     for i:=1 to N do rt[i]:=build(rt[i-1],a[i],1,1,n);
    79     read(M);
    80     for i:=1 to M do
    81     begin
    82         read(x,y,z);
    83         inc(x);
    84         inc(y);
    85         writeln(map[find(rt[x-1],rt[y],z,1,n)]);
    86     end;
    87 end.
    区间k大值

    6.最短路经

      1 var
      2     N,M,S,L,i,x,y,z,tot                            :longint;
      3     d,heap,pl                                    :array[0..10005] of longint;
      4     pre,last,other,len                             :array[0..500050] of longint;
      5 
      6 procedure swap(var x,y:longint);
      7 var
      8     z                                             :longint;
      9 begin
     10     z:=x;
     11     x:=y;
     12     y:=z;
     13 end;
     14 
     15 procedure add(u,v,r:longint);
     16 begin
     17     inc(L);
     18     pre[L]:=last[u];
     19     last[u]:=L;
     20     other[L]:=v;
     21     len[L]:=r;
     22 end;
     23 
     24 procedure up(x:longint);
     25 var
     26     i,j                                         :longint;
     27 begin
     28     i:=x;
     29     while i>>1>0 do
     30     begin
     31         j:=i>>1;
     32         if d[heap[j]]<=d[heap[i]] then break;
     33         swap(heap[i],heap[j]);
     34         pl[heap[i]]:=i;
     35         pl[heap[j]]:=j;
     36         i:=j;
     37     end;
     38 end;
     39 
     40 procedure down(x:longint);
     41 var
     42     i,j                                         :longint;
     43 begin
     44     i:=x;
     45     while i<<1<=tot do
     46     begin
     47         j:=i<<1;
     48         if (j+1<=tot) and (d[heap[j+1]]<d[heap[j]]) then inc(j);
     49         if d[heap[j]]>=d[heap[i]] then break;
     50         swap(heap[i],heap[j]);
     51         pl[heap[i]]:=i;
     52         pl[heap[j]]:=j;
     53         i:=j;
     54     end;
     55 end;
     56 
     57 procedure delete(x:longint);
     58 begin
     59     heap[x]:=heap[tot];
     60     dec(tot);
     61     down(x);
     62 end;
     63 
     64 procedure dij();
     65 var
     66     p,q,cur                                     :longint;
     67 begin
     68     for i:=1 to N do
     69     begin
     70         cur:=heap[1];
     71         delete(1);
     72         p:=last[cur];
     73         while p<>0 do
     74         begin
     75             q:=other[p];
     76             if d[q]>d[cur]+len[p] then
     77             begin
     78                 d[q]:=d[cur]+len[p];
     79                 up(pl[q]);
     80             end;
     81             p:=pre[p];
     82         end;
     83     end;
     84 end;
     85 
     86 begin
     87     read(N,M,S);
     88     for i:=1 to M do
     89     begin
     90         read(x,y,z);
     91         add(x,y,z);
     92     end;
     93     for i:=1 to N do d[i]:=maxlongint>>1; d[S]:=0;
     94     for i:=1 to N do heap[i]:=i;
     95     for i:=1 to N do pl[i]:=i;
     96     tot:=n;
     97     up(S);
     98     dij;
     99     for i:=1 to N do if d[i]=maxlongint>>1 then write(maxlongint,' ') else write(d[i],' ');
    100 end.
    Heap-Dijkstra
     1 const mo=20000;
     2 var
     3     N,M,S,L,i,x,y,z                            :longint;
     4     pre,last,other,len                         :array[0..5000050] of longint;
     5     d,que                                     :Array[0..200050] of longint;
     6     vis                                     :array[0..100050] of boolean;
     7 
     8 procedure add(u,v,r:longint);
     9 begin
    10     inc(L);
    11     pre[L]:=last[u];
    12     last[u]:=L;
    13     other[L]:=v;
    14     len[L]:=r;
    15 end;
    16 
    17 procedure spfa();
    18 var
    19     p,q,cur,h,t                             :longint;
    20 begin
    21     for i:=1 to N do d[i]:=maxlongint>>1; d[S]:=0;
    22     que[1]:=S;
    23     h:=0;
    24     t:=1;
    25     while h<>t do
    26     begin
    27         h:=h mod mo+1;
    28         cur:=que[h];
    29         vis[cur]:=false;
    30         p:=last[cur];
    31         while p<>0 do
    32         begin
    33             q:=other[p];
    34             if d[q]>d[cur]+len[p] then
    35             begin
    36                 d[q]:=d[cur]+len[p];
    37                 if not vis[q] then
    38                 begin
    39                     vis[q]:=true;
    40                     inc(t);
    41                     que[t]:=q;
    42                 end;
    43             end;
    44             p:=pre[p];
    45         end;
    46     end;
    47 end;
    48 
    49 
    50 begin
    51     read(N,M,S);
    52     for i:=1 to M do
    53     begin
    54         read(x,y,z);
    55         add(x,y,z);
    56     end;
    57     spfa;
    58     for i:=1 to N do if d[i]=maxlongint>>1 then write(maxlongint,' ') else write(d[i],' ');
    59 end.
    Spfa
     1 var
     2     i,j,k,x,y,z                                :longint;
     3     N,M,S                                     :longint;
     4     d                                         :array[0..500,0..500] of longint;
     5 
     6 function min(x,y:longint):longint;
     7 begin
     8     if x<y then exit(x) else exit(y);
     9 end;
    10 
    11 begin
    12     read(N,M,S);
    13     for i:=1 to N do
    14     for j:=1 to N do d[i,j]:=maxlongint>>1;
    15     for i:=1 to N do d[i,i]:=0;
    16     for i:=1 to M do
    17     begin
    18         read(x,y,z);
    19         d[x,y]:=min(d[x,y],z);
    20     end;
    21     for k:=1 to N do
    22     for i:=1 to N do
    23     for j:=1 to N do d[i,j]:=min(d[i,j],d[i,k]+d[k,j]);
    24     for i:=1 to N do write(d[S,i],' ');
    25 end.
    Floyd

    7.网络流

     1 var
     2     N,M,SS,TT,i,x,y,z,L                            :longint;
     3     ans                                         :int64;
     4     pre,last,other,len                             :array[0..300050] of longint;
     5     d,que                                         :array[0..10050] of longint;
     6 
     7 function min(x,y:longint):longint;
     8 begin
     9     if x<y then exit(x) else exit(y);
    10 end;
    11 
    12 procedure add_double(u,v,r:longint);
    13 begin
    14     inc(L);pre[L]:=last[u];last[u]:=L;other[L]:=v;len[L]:=r;
    15     inc(L);pre[L]:=last[v];last[v]:=L;other[L]:=u;len[L]:=0;
    16 end;
    17 
    18 function bfs():boolean;
    19 var
    20     h,t,p,q,cur                                 :longint;
    21 begin
    22     fillchar(d,sizeof(d),0);
    23     d[SS]:=1;
    24     que[1]:=SS;
    25     h:=0;
    26     t:=1;
    27     while h<t do
    28     begin
    29         inc(h);
    30         cur:=que[h];
    31         p:=last[cur];
    32         while p<>0 do
    33         begin
    34             if len[p]>0 then
    35             begin
    36                 q:=other[p];
    37                 if (d[q]=0) then
    38                 begin
    39                     d[q]:=d[cur]+1;
    40                     inc(t);
    41                     que[t]:=q;
    42                     if q=TT then exit(true);
    43                 end;
    44             end;
    45             p:=pre[p];
    46         end;
    47     end;
    48     exit(false);
    49 end;
    50 
    51 function dinic(x,flow:longint):longint;
    52 var
    53     p,q,res,c                                     :longint;
    54 begin
    55     if x=TT then exit(flow);
    56     res:=flow;
    57     p:=last[x];
    58     while p<>0 do
    59     begin
    60         q:=other[p];
    61         if (len[p]>0) and (res>0) and (d[q]=d[x]+1) then
    62         begin
    63             c:=dinic(q,min(len[p],res));
    64             dec(res,c);
    65             dec(len[p],c);
    66             inc(len[p xor 1],c);
    67             if res=0 then exit(flow);
    68         end;
    69         p:=pre[p];
    70     end;
    71     if flow=res then d[x]:=0;
    72     exit(flow-res);
    73 end;
    74 
    75 begin
    76     read(N,M,SS,TT);
    77     L:=1;
    78     for i:=1 to M do
    79     begin
    80         read(x,y,z);
    81         add_double(x,y,z);
    82     end;
    83     while bfs() do ans:=ans+dinic(SS,maxlongint>>1);
    84     writeln(ans);
    85 end.
    Dinic
     1 const mo=1000000;
     2 var
     3     N,M,SS,TT,L,i,x,y,z,w                             :longint;
     4     pre,last,other,len,cos,next,from                :Array[0..200050] of longint;
     5     d                                                 :Array[0..5050] of longint;
     6     vis                                             :Array[0..5050] of boolean;
     7     que                                             :array[0..1000050] of longint;
     8 
     9 function min(x,y:longint):longint;
    10 begin
    11     if x<y then exit(x) else exit(y);
    12 end;
    13 
    14 procedure add_double(u,v,r,w:longint);
    15 begin
    16     inc(L); pre[L]:=last[u]; last[u]:=L; other[L]:=v; len[L]:=r; cos[L]:=w; from[L]:=u;
    17     inc(L); pre[L]:=last[v]; last[v]:=L; other[L]:=u; len[L]:=0; cos[L]:=-w; from[L]:=v;
    18 end;
    19 
    20 function spfa():boolean;
    21 var
    22     p,q,cur,h,t,inf                                    :longint;
    23 begin
    24     fillchar(vis,sizeof(vis),false);
    25     fillchar(d,sizeof(d),63); d[SS]:=0; inf:=d[0];
    26     que[1]:=SS; h:=0; t:=1;
    27     while h<>t do
    28     begin
    29         h:=h mod mo+1;
    30         cur:=que[h];
    31         p:=last[cur];
    32         vis[cur]:=false;
    33         while p<>0 do
    34         begin
    35             if len[p]>0 then
    36             begin
    37                 q:=other[p];
    38                 if (d[q]>d[cur]+cos[p]) then
    39                 begin
    40                     next[q]:=p;
    41                     d[q]:=d[cur]+cos[p];
    42                     if not vis[q] then
    43                     begin
    44                         vis[q]:=true;
    45                         t:=t mod mo+1;
    46                         que[t]:=q;
    47                     end;
    48                 end;
    49             end;
    50             p:=pre[p];
    51         end;
    52     end;
    53     exit(d[TT]<inf);
    54 end;
    55 
    56 
    57 procedure mcmf();
    58 var
    59     p,q,sumflow,flow,sumcos                         :longint;
    60 begin
    61     sumflow:=0;
    62     sumcos:=0;
    63     while spfa do
    64     begin
    65         p:=next[TT];
    66         flow:=maxlongint;
    67         while p<>0 do
    68         begin
    69             flow:=min(flow,len[p]);
    70             p:=next[from[p]];
    71         end;
    72         p:=next[TT];
    73         while p<>0 do
    74         begin
    75             len[p]:=len[p]-flow;
    76             len[p xor 1]:=len[p xor 1]+flow;
    77             p:=next[from[p]];
    78         end;
    79         sumflow:=sumflow+flow;
    80         sumcos:=sumcos+flow*d[TT];
    81     end;
    82     writeln(sumflow,' ',sumcos);
    83 end;
    84 
    85 
    86 begin
    87     read(N,M,SS,TT);
    88     L:=1;
    89     for i:=1 to M do
    90     begin
    91         read(x,y,z,w);
    92         add_double(x,y,z,w);
    93     end;
    94     mcmf;
    95 end.
    Mcmf

    8.树状数组

     1 var
     2     N,M,opt,x,y,i                                 :longint;
     3     z                                             :int64;
     4     c                                             :Array[0..500050] of int64;
     5 
     6 function lowbit(t:longint):longint;
     7 begin
     8     exit(t and (-t));
     9 end;
    10 
    11 procedure ins(t:longint; x:int64);
    12 begin
    13     while t<=N do
    14     begin
    15         c[t]:=c[t]+x;
    16         t:=t+lowbit(t);
    17     end;
    18 end;
    19 
    20 function find(t:longint):int64;
    21 var
    22     sum                                         :int64;
    23 begin
    24     sum:=0;
    25     while t>0 do
    26     begin
    27         sum:=sum+c[t];
    28         t:=t-lowbit(t);
    29     end;
    30     exit(sum);
    31 end;
    32 
    33 begin
    34     read(N,M);
    35     for i:=1 to N do
    36     begin
    37         read(z);
    38         ins(i,z);
    39     end;
    40     for i:=1 to M do
    41     begin
    42         read(opt);
    43         if opt=1 then
    44         begin
    45             read(x,z);
    46             ins(x,z);
    47         end else
    48         begin
    49             read(x,y);
    50             writeln(find(y)-find(x-1));
    51         end;
    52     end;
    53 end.
    View Code

     9.分块

     1 type
     2     block                                 =record
     3         l,r,id                             :longint;
     4         sum,add                         :int64;
     5     end;
     6 
     7 var
     8     N,M                                 :longint;
     9     i,j,k,opt,x,y,len,tot                 :longint;
    10     z,ans                                :int64;
    11     a                                     :Array[0..100050] of int64;
    12     b                                     :array[0..10005] of block;
    13     bel                                 :array[0..100050] of longint;
    14 
    15 begin
    16     read(N,M);
    17     len:=trunc(sqrt(N));
    18     while true do
    19     begin
    20         inc(tot);
    21         b[tot].l:=b[tot-1].r+1;
    22         b[tot].r:=b[tot].l+len-1;
    23         if b[tot].r>=N then
    24         begin
    25             b[tot].r:=N;
    26             break;
    27         end;
    28     end;
    29     for i:=1 to N do read(a[i]);
    30     for i:=1 to tot do
    31     begin
    32         for j:=b[i].l to b[i].r do
    33         begin
    34             bel[j]:=i;
    35             b[i].sum:=b[i].sum+a[j];
    36         end;
    37     end;
    38     for k:=1 to M do
    39     begin
    40         read(opt);
    41         if opt=1 then
    42         begin
    43             read(x,y,z);
    44             for i:=bel[x]+1 to bel[y]-1 do b[i].add:=b[i].add+z;
    45             if bel[x]=bel[y] then
    46             begin
    47                 for i:=x to y do
    48                 begin
    49                     a[i]:=a[i]+z;
    50                     b[bel[i]].sum:=b[bel[i]].sum+z;
    51                 end;
    52             end else
    53             begin
    54                 for i:=x to b[bel[x]].r do
    55                 begin
    56                     a[i]:=a[i]+z;
    57                     b[bel[i]].sum:=b[bel[i]].sum+z;
    58                 end;
    59                 for i:=b[bel[y]].l to y do
    60                 begin
    61                     a[i]:=a[i]+z;
    62                     b[bel[i]].sum:=b[bel[i]].sum+z;
    63                 end;
    64             end;
    65         end else
    66         begin
    67             read(x,y);
    68             ans:=0;
    69             for i:=bel[x]+1 to bel[y]-1 do ans:=ans+b[i].sum+b[i].add*(b[i].r-b[i].l+1);
    70             if (bel[x]=bel[y]) then
    71             begin
    72                 for i:=x to y do ans:=ans+a[i]+b[bel[i]].add;
    73                 writeln(ans);
    74             end else
    75             begin
    76                 for i:=x to b[bel[x]].r do ans:=ans+a[i]+b[bel[i]].add;
    77                 for i:=b[bel[y]].l to y do ans:=ans+a[i]+b[bel[i]].add;
    78                 writeln(ans);
    79             end;
    80         end;
    81     end;
    82 end.
    View Code

     10.左偏树(可合并堆)

     1 var
     2     N,M,i,opt,x,y,fx,fy,rt                            :longint;
     3     father,npl,l,r,key                                :array[0..100050] of longint;
     4     flag                                             :Array[0..100050] of boolean;
     5 
     6 procedure swap(var x,y:longint);
     7 var
     8     z                                                 :longint;
     9 begin
    10     z:=x;
    11     x:=y;
    12     y:=z;
    13 end;
    14 
    15 function comb(r1,r2:longint):longint;
    16 begin
    17     if r1=0 then exit(r2);
    18     if r2=0 then exit(r1);
    19     if key[r1]>key[r2] then swap(r1,r2);
    20     r[r1]:=comb(r[r1],r2);
    21     if npl[l[r1]]<npl[r[r1]] then swap(l[r1],r[r1]);
    22     npl[r1]:=npl[r[r1]]+1;
    23     exit(r1);
    24 end;
    25 
    26 function gf(x:longint):longint;
    27 begin
    28     if father[x]=x then exit(x);
    29     father[x]:=gf(father[x]);
    30     exit(father[x]);
    31 end;
    32 
    33 begin
    34     read(N,M);
    35     for i:=1 to N do read(key[i]);
    36     for i:=1 to N do father[i]:=i;
    37     fillchar(flag,sizeof(flag),true);
    38     for i:=1 to M do
    39     begin
    40         read(opt);
    41         if opt=1 then
    42         begin
    43             read(x,y);
    44             if (not flag[x]) or (not flag[y]) then continue;
    45             fx:=gf(x);
    46             fy:=gf(y);
    47             if fx=fy then continue;
    48             rt:=comb(fx,fy);
    49             father[fx]:=rt;
    50             father[fy]:=rt;
    51         end else
    52         begin
    53             read(x);
    54             if not flag[x] then
    55             begin
    56                 writeln(-1);
    57                 continue;
    58             end;
    59             fx:=gf(x);
    60             flag[fx]:=false;
    61             writeln(key[fx]);
    62             rt:=comb(l[fx],r[fx]);
    63             father[l[fx]]:=rt;
    64             father[r[fx]]:=rt;
    65             father[fx]:=rt;
    66         end;
    67     end;
    68 end.
    View Code

     11.St表

     1 var
     2     N,M,i,j,x,y,k,len                        :longint;
     3     f                                         :Array[0..100005,0..15] of longint;
     4 
     5 function max(x,y:longint):longint;
     6 begin
     7     if x>y then exit(x) else exit(y);
     8 end;
     9 
    10 begin
    11     read(N);
    12     for i:=1 to N do read(f[i,0]);
    13     for j:=1 to 14 do
    14     for i:=1 to N do f[i,j]:=max(f[i,j-1],f[i+(1<<(j-1)),j-1]);
    15     read(M);
    16     for i:=1 to M do
    17     begin
    18         read(x,y);
    19         inc(x); inc(y);
    20         len:=y-x+1;
    21         k:=trunc(ln(len)/ln(2));
    22         writeln(max(f[x,k],f[y-(1<<k)+1,k]));
    23     end;
    24 end.
    View Code

     12.Kmp

     1 var
     2     s1,s2                                    :ansistring;
     3     next                                     :Array[0..1005] of longint;
     4     n,i,j,p,l1,l2                            :longint;
     5 begin
     6     readln(s1);
     7     readln(s2);
     8     l1:=length(s1);
     9     l2:=length(s2);
    10     p:=0;
    11     for i:=2 to l2 do
    12     begin
    13         while (p<>0) and (s2[p+1]<>s2[i]) do p:=next[p];
    14         if s2[p+1]=s2[i] then
    15         begin
    16             inc(p);
    17             next[i]:=p;
    18         end else next[i]:=0;
    19     end;
    20     p:=0;
    21     for i:=1 to l1 do
    22     begin
    23         while (p<>0) and (s2[p+1]<>s1[i]) do p:=next[p];
    24         if s2[p+1]=s1[i] then inc(p);
    25         if p=l2 then
    26         begin
    27             writeln(i-l2+1);
    28             p:=next[p];
    29         end;
    30     end;
    31     for i:=1 to l2 do write(next[i],' ');
    32 end.
    Kmp
     1 var
     2     next,ex                                    :array[0..1000050] of longint;
     3     N,i                                      :longint;
     4     ans                                     :int64;
     5     s                                         :ansistring;
     6 
     7 function max(x,y:longint):longint;
     8 begin
     9     if x>y then exit(x) else exit(y);
    10 end;
    11 
    12 procedure get_ex();
    13 var
    14     i,j,p,a,k                                 :longint;
    15 begin
    16     next[1]:=n;
    17     a:=0;
    18     p:=0;
    19     for i:=2 to n do
    20     begin
    21         j:=next[i-a+1];
    22         if i+j>p then
    23         begin
    24             j:=max(0,p-i+1);
    25             while (i+j<=n) and (s[i+j]=s[j+1]) do inc(j);
    26         end;
    27         next[i]:=j;
    28         if i+j-1>p then
    29         begin
    30             a:=i;
    31             p:=i+j-1;
    32         end;
    33     end;
    34     a:=0;
    35     p:=0;
    36     for i:=1 to n do
    37     begin
    38         j:=next[i-a+1];
    39         if i+j>p then
    40         begin
    41             j:=max(0,p-i+1);
    42             while (i+j<=n) and (j+1<=n) and (s[i+j]=s[j+1]) do inc(j);
    43         end;
    44         ex[i]:=j;
    45         if i+j-1>p then
    46         begin
    47             a:=i;
    48             p:=i+j-1;
    49         end;
    50     end;
    51 end;
    52 
    53 begin
    54     readln(s);
    55     N:=length(s);
    56     get_ex;
    57     for i:=1 to N do ans:=ans+ex[i];
    58     writeln(ans);
    59 end.
    Exkmp

     13.String-Hash

     1 const base=29;
     2 var
     3     T,N,i,j,L,LL,ans                        :longint;
     4     hash,np                                    :array[0..1000005] of qword;
     5     k                                         :qword;
     6     s1,s2                                     :ansistring;
     7     a,b                                     :Array[0..1000005] of longint;
     8 begin
     9     readln(T);
    10     np[0]:=1;
    11     for i:=1 to 1000000 do np[i]:=np[i-1]*base;
    12     while T>0 do
    13     begin
    14         dec(T);
    15         readln(s1); L:=length(s1);
    16         readln(s2); LL:=length(s2);
    17         ans:=0;
    18         for i:=1 to L do a[i]:=ord(s1[i])-ord('a')+1;
    19         for i:=1 to LL do b[i]:=ord(s2[i])-ord('a')+1;
    20         k:=0;
    21         for i:=L downto 1 do k:=k*base+a[i];
    22         hash[LL+1]:=0;
    23         for i:=LL downto 1 do hash[i]:=hash[i+1]*base+b[i];
    24         for i:=1 to LL do
    25         begin
    26             if i+L-1>LL then break;
    27             j:=i+L-1;
    28             if (hash[i]-hash[j+1]*np[L]=k) then inc(ans);
    29         end;
    30         writeln(ans);
    31     end;
    32 end.
    View Code

     14.后缀数组---最长回文子串

     1 var
     2     N,m,i,ans,jud                            :longint;
     3     a,t,y,sa,height,rank,c,r                :Array[0..400050] of longint;
     4     s                                         :ansistring;
     5 
     6 function max(x,y:longint):longint;
     7 begin
     8     if x>y then exit(x) else exit(y);
     9 end;
    10 
    11 procedure swap(var x,y:longint);
    12 var
    13     z                                         :longint;
    14 begin
    15     z:=x;
    16     x:=y;
    17     y:=z;
    18 end;
    19 
    20 function cmp(a,b,j:longint):boolean;
    21 begin
    22     exit((a+j<=n) and (b+j<=n) and (y[a]=y[b]) and (y[a+j]=y[b+j]));
    23 end;
    24 
    25 procedure getsa();
    26 var
    27     i,j,p                                     :longint;
    28 begin
    29     for i:=0 to m do c[i]:=0;
    30     for i:=1 to n do begin r[i]:=a[i]; inc(c[r[i]]); end;
    31     for i:=1 to m do c[i]:=c[i]+c[i-1];
    32     for i:=n downto 1 do begin sa[c[r[i]]]:=i; dec(c[r[i]]); end;
    33     j:=1;
    34     while j<=n do
    35     begin
    36         p:=0;
    37         for i:=n-j+1 to n do begin inc(p); y[p]:=i; end;
    38         for i:=1 to n do if sa[i]>j then begin inc(p); y[p]:=sa[i]-j; end;
    39         for i:=0 to m do c[i]:=0;
    40         for i:=1 to n do inc(c[r[y[i]]]);
    41         for i:=1 to m do c[i]:=c[i]+c[i-1];
    42         for i:=n downto 1 do begin sa[c[r[y[i]]]]:=y[i]; dec(c[r[y[i]]]); end;
    43         for i:=1 to n do swap(r[i],y[i]);
    44         p:=1;
    45         r[sa[1]]:=p;
    46         for i:=2 to n do if (cmp(sa[i],sa[i-1],j)) then r[sa[i]]:=p else begin inc(p); r[sa[i]]:=p; end;
    47         if p>=n then break;
    48         m:=p;
    49         j:=j<<1;
    50     end;
    51 end;
    52 
    53 procedure getheight();
    54 var
    55     i,j,k                                     :longint;
    56 begin
    57     for i:=1 to n do rank[sa[i]]:=i;
    58     k:=0;
    59     for i:=1 to n do
    60     begin
    61         if k>0 then dec(k);
    62         if rank[i]=1 then continue;
    63         j:=sa[rank[i]-1];
    64         while (i+k<=n) and (j+k<=n) and (a[i+k]=a[j+k]) do inc(k);
    65         height[rank[i]]:=k;
    66     end;
    67 end;
    68 
    69 begin
    70     readln(N); jud:=N;
    71     readln(s);
    72     for i:=1 to N do 
    73     begin
    74         a[i]:=ord(s[i])-ord('a')+1;
    75         m:=max(m,a[i]);
    76     end;
    77     a[N+1]:=0;
    78     for i:=1 to n do a[n*2+2-i]:=a[i];
    79     n:=n*2+1;
    80     getsa();
    81     getheight();
    82     for i:=1 to n do
    83         if (height[i]>ans) and (((sa[i]>jud) and (sa[i-1]<jud)) or ((sa[i]<jud) and (sa[i-1]>jud))) then ans:=height[i];
    84     writeln(ans);
    85 end.
    View Code

     15.FFT---大数乘法

      1 type
      2     cp                                         =record
      3         r,i                                 :double;
      4     end;
      5 
      6 var
      7     s1,s2                                     :ansistring;
      8     a,b                                        :array[0..1000050] of cp;
      9     sum                                        :array[0..1000050] of longint;
     10     l1,l2,l,i,j,t                             :longint;
     11 
     12 function max(x,y:longint):longint;
     13 begin
     14     if x>y then exit(x) else exit(y);
     15 end;
     16 
     17 function jia(a,b:cp):cp;
     18 var
     19     z                                         :cp;
     20 begin
     21     z.r:=a.r+b.r;
     22     z.i:=a.i+b.i;
     23     exit(z);
     24 end;
     25 
     26 function jian(a,b:cp):cp;
     27 var
     28     z                                         :cp;
     29 begin
     30     z.r:=a.r-b.r;
     31     z.i:=a.i-b.i;
     32     exit(z);
     33 end;
     34 
     35 function mul(a,b:cp):cp;
     36 var
     37     z                                         :cp;
     38 begin
     39     z.r:=a.r*b.r-a.i*b.i;
     40     z.i:=a.r*b.i+a.i*b.r;
     41     exit(z);
     42 end;
     43 
     44 procedure swap(var x,y:cp);
     45 var
     46     z                                         :cp;
     47 begin
     48     z:=x;
     49     x:=y;
     50     y:=z;
     51 end;
     52 
     53 procedure BRC(var a:array of cp; l:longint);
     54 var
     55     i,j,k                                     :longint;
     56 begin
     57     k:=0;
     58     for i:=1 to l-2 do
     59     begin
     60         j:=l>>1;
     61         while k>=j do
     62         begin
     63             k:=k xor j;
     64             j:=j>>1;
     65         end;
     66         k:=k or j;
     67         if i<k then swap(a[i],a[k]);
     68     end;
     69 end;    
     70 
     71 procedure FFT(var a:array of cp; l,flag:longint);
     72 var
     73     i,j,h                                    :longint;
     74     wn,w,t                                      :cp;
     75 begin
     76     BRC(a,l);
     77     h:=2;
     78     while h<=l do
     79     begin
     80         wn.r:=cos(flag*2*pi/h);
     81         wn.i:=sin(flag*2*pi/h);
     82         j:=0;
     83         while j<l do
     84         begin
     85             w.r:=1; w.i:=0; 
     86             for i:=j to j+(h>>1)-1 do
     87             begin
     88                 t:=mul(w,a[i+(h>>1)]);
     89                 a[i+(h>>1)]:=jian(a[i],t);
     90                 a[i]:=jia(a[i],t);
     91                 w:=mul(w,wn);
     92             end;
     93             j:=j+h;
     94         end;
     95         h:=h<<1;
     96     end;
     97     if flag=-1 then 
     98     begin
     99         for i:=0 to l-1 do a[i].r:=a[i].r/l;
    100     end;
    101 end;
    102 
    103 begin
    104     readln(s1); l1:=length(s1);
    105     readln(s2); l2:=length(s2);
    106     l:=1;
    107     while l<max(l1,l2)*2 do l:=l<<1;
    108     for i:=1 to l1 do
    109     begin
    110         a[l1-i].r:=ord(s1[i])-48;
    111         a[l1-i].i:=0;
    112     end;
    113     for i:=1 to l2 do
    114     begin
    115         b[l2-i].r:=ord(s2[i])-48;
    116         b[l2-i].i:=0;
    117     end;
    118     FFT(a,l,1);
    119     FFT(b,l,1);
    120     for i:=0 to l-1 do a[i]:=mul(a[i],b[i]);
    121     FFT(a,l,-1);
    122     for i:=0 to l-1 do sum[i]:=trunc(a[i].r+0.5);
    123     for i:=0 to l-1 do
    124     begin
    125         sum[i+1]:=sum[i+1]+(sum[i] div 10);
    126         sum[i]:=sum[i] mod 10;
    127     end;
    128     t:=l;
    129     while (sum[t]=0) and (t>0) do dec(t);
    130     for i:=t downto 0 do write(sum[i]);writeln;
    131 end.
    View Code

     16.Tarjan

     1 var
     2     iscut                                         :array[0..100050] of boolean;
     3     dfn,low                                     :array[0..100050] of longint;
     4     pre,last,other,len                             :Array[0..200050] of longint;
     5     N,M,i,x,y,L,time,ans                        :longint;
     6 
     7 function min(x,y:longint):longint;
     8 begin
     9     if x<y then exit(x) else exit(y);
    10 end;
    11 
    12 procedure add(u,v:longint);
    13 begin
    14     inc(L);
    15     pre[L]:=last[u];
    16     last[u]:=L;
    17     other[L]:=v;
    18 end;
    19 
    20 procedure tarjan(x,fa:longint);
    21 var
    22     tot,p,q                                        :longint;
    23 begin
    24     inc(time);
    25     dfn[x]:=time;
    26     low[x]:=time;
    27     tot:=0;
    28     p:=last[x];
    29     while p<>0 do
    30     begin
    31         q:=other[p];
    32         if dfn[q]=0 then
    33         begin
    34             inc(tot);
    35             tarjan(q,x);
    36             low[x]:=min(low[x],low[q]);
    37             if low[q]>=dfn[x] then iscut[x]:=true;
    38         end else if (dfn[q]<dfn[x]) and (q<>fa) then low[x]:=min(low[x],dfn[q]);
    39         p:=pre[p];
    40     end;
    41     if (fa<0) and (tot=1) then iscut[x]:=false;
    42 end;
    43 
    44 begin
    45     read(N,M);
    46     for i:=1 to M do
    47     begin
    48         read(x,y);
    49         add(x,y);
    50         add(y,x);
    51     end;
    52     for i:=1 to N do if (dfn[i]=0) then tarjan(i,-1);
    53     for i:=1 to N do if (iscut[i]) then inc(ans);
    54     writeln(ans);
    55     for i:=1 to N do if (iscut[i]) then write(i,' ');
    56 end.
    割点
     1 var
     2     T,N,M,L,i,x,y,time,ans,cnt,k            :longint;
     3     pre,last,other                             :array[0..200050] of longint;
     4     dfn,low                                 :array[0..15050] of longint;
     5     iscut                                     :Array[0..100050] of boolean;
     6 
     7 function min(x,y:longint):longint;
     8 begin
     9     if x<y then exit(x) else exit(y);
    10 end;
    11 
    12 procedure add(u,v:longint);
    13 begin
    14     inc(L);
    15     pre[L]:=last[u];
    16     last[u]:=L;
    17     other[L]:=v;
    18 end;
    19 
    20 procedure tarjan(x,fa:longint);
    21 var
    22     p,q                                     :longint;
    23 begin
    24     inc(time);
    25     dfn[x]:=time;
    26     low[x]:=time;
    27     p:=last[x];
    28     while p<>0 do
    29     begin
    30         q:=other[p];
    31         if dfn[q]=0 then
    32         begin
    33             tarjan(q,p);
    34             low[x]:=min(low[x],low[q]);
    35             if low[q]>dfn[x] then iscut[p>>1]:=true;
    36         end else if ((p xor 1)<>fa) then low[x]:=min(low[x],dfn[q]);
    37         p:=pre[p];
    38     end;
    39 end;
    40 
    41 
    42 begin
    43     read(T);
    44     for k:=1 to T do
    45     begin
    46         fillchar(dfn,sizeof(dfn),0);
    47         fillchar(low,sizeof(low),0);
    48         fillchar(last,sizeof(last),0);
    49         fillchar(iscut,sizeof(iscut),false);
    50         ans:=0;
    51         time:=0;
    52         L:=1;
    53         read(N,M);
    54         for i:=1 to M do
    55         begin
    56             read(x,y);
    57             add(x,y);
    58             add(y,x);
    59         end;
    60         for i:=1 to N do if (dfn[i]=0) then tarjan(i,-1);
    61         for i:=1 to M do if (iscut[i]) then inc(ans);
    62         writeln(ans);
    63         cnt:=0;
    64         for i:=1 to M do 
    65         begin
    66             if iscut[i] then 
    67             begin
    68                 inc(cnt);
    69                 if cnt<>ans then write(i,' ') else writeln(i);
    70             end;
    71         end;
    72         if k<>t then 
    73         begin
    74             writeln;
    75         end;
    76     end;
    77 end.
    割边

     17.Hungary

     1 var
     2     N,M,E,i,j,vis_cnt,x,y,ans                :longint;
     3     son                                     :Array[0..1005,0..1005] of longint;
     4     link                                     :Array[0..1005] of longint;
     5     vis                                     :array[0..1005] of longint;
     6 
     7 function dfs(x:longint):boolean;
     8 var
     9     i,j                                     :longint;
    10 begin
    11     for i:=1 to son[x,0] do
    12     begin
    13         if vis[son[x,i]]=vis_cnt then continue;
    14         vis[son[x,i]]:=vis_cnt;
    15         if (link[son[x,i]]=0) or (dfs(link[son[x,i]])) then
    16         begin
    17             link[son[x,i]]:=x;
    18             exit(true);
    19         end;
    20     end;
    21     exit(false);
    22 end;
    23 
    24 begin
    25     read(N,M,E);
    26     for i:=1 to E do
    27     begin
    28         read(x,y);
    29         if (x>N) or (y>M) then continue;
    30         inc(son[x,0]);
    31         son[x,son[x,0]]:=y;
    32     end;
    33     for i:=1 to N do
    34     begin
    35         inc(vis_cnt);
    36         if dfs(i) then inc(ans);
    37     end;
    38     writeln(ans);
    39 end.
    View Code
    1 二分图的一些定理:
    2 (1) 最小点覆盖=最大匹配
    3 (2) 最小边覆盖=图中点的个数-最大匹配
    4 (3) 最大独立集=图中点的个数-最大匹配
    5 (4) DAG最小不相交路径覆盖=原图的节点数-新图的匹配数 (把原图中的每个点拆成Vx和Vy,如果有一条边A-->B,那么就加边Ax-->By 这样就得到一个二分图)。
    6 (5) DAG最小可相交路径覆盖,先用Floyd求出原图的传递闭包,如果A,B联通,则连边A-->B,这样问题就转化成了DAG最小不相交路径覆盖问题了。
    balabala

    18.数论相关

    中国剩余定理内容
    对于同余方程组
    x=ai (mod mi)
    在模M=m1m2...mi 意义下有唯一解
    解为x=sigma(aiMi(Mi-1))
    其中Mi=M/mi,Mi-1为Mi关于mi的逆元
    中国剩余定理
    c(n,m)%p = c(n%p,m%p)*c(n/p,m/p)
    Lucas定理
    Millar Rabin素数测试内容:
    
    (1) 费马小定理:对于素数p和任意整数a,总有a^(p-1)=1 (mod p)
        相反,若数p满足a^(p-1)=1 (mod p) 那么p也几乎是素数
    
    (2) Miller Rabin测试:不断选取a,测试a^(p-1) ?=1 (mod p),若选取多个a测试都成立则p是素数,若有一次不成立则p是合数。
    
    选取前20个质数作为a进行测试就能保证在2^63范围内质数检测不出错
    Miller Rabin素数测试
     1 const eps=1e-8;
     2 var
     3     N,i,j,k,cur                                    :longint;
     4     p                                             :double;
     5     a                                             :array[0..105,0..105] of double;
     6 
     7 procedure swap(var x,y:double);
     8 var
     9     z                                             :double;
    10 begin
    11     z:=x;
    12     x:=y;
    13     y:=z;
    14 end;
    15 
    16 begin
    17     read(N);
    18     for i:=1 to N do
    19     for j:=1 to N+1 do read(a[i,j]);
    20     for i:=1 to N do
    21     begin
    22         cur:=i;
    23         for j:=i+1 to N do if abs(a[j,i])>abs(a[cur,i]) then cur:=j;
    24         if abs(a[cur,i])<=eps then
    25         begin
    26             writeln('No Solution');
    27             halt;
    28         end;
    29         if cur<>i then for j:=i to N+1 do swap(a[i,j],a[cur,j]);
    30         for j:=i+1 to N do
    31         begin
    32             p:=-(a[j,i]/a[i,i]);
    33             for k:=i to N+1 do a[j,k]:=a[j,k]+p*a[i,k];
    34         end;
    35         for j:=1 to i-1 do
    36         begin
    37             p:=-(a[j,i]/a[i,i]);
    38             for k:=i to N+1 do a[j,k]:=a[j,k]+p*a[i,k];
    39         end;
    40     end;
    41     for i:=1 to n do writeln(a[i,n+1]/a[i,i] :0 :2);
    42 end.
    高斯消元法
     1 var
     2     a,b                                     :longint;
     3     x,y                                     :longint;
     4 
     5 procedure exgcd(a,b:longint; var x,y:longint);
     6 var
     7     t                                         :longint;
     8 begin
     9     if b=0 then
    10     begin
    11         x:=1;
    12         y:=0;
    13         exit;
    14     end;
    15     exgcd(b,a mod b,x,y);
    16     t:=x;
    17     x:=y;
    18     y:=t-(a div b)*y;
    19 end;
    20 
    21 
    22 begin
    23     read(a,b);
    24     exgcd(a,b,x,y);
    25     while x<=0 do inc(x,b);
    26     writeln(x);
    27 end.
    Exgcd

     19.点分治

      1 var
      2     n,k,x,y,z,i,l,sum,l1,l2,vis_cnt            :longint;
      3     ans                                     :int64;
      4     pre,last,other,len                         :array[0..20050] of longint;
      5     vis                                     :array[0..10050] of longint;
      6     list,d,father,size                        :array[0..10050] of longint;
      7 
      8 function max(x,y:longint):longint;
      9 begin
     10     if x>y then exit(x) else exit(y);
     11 end;
     12 
     13 procedure swap(var x,y:longint);
     14 var
     15     z                                         :longint;
     16 begin
     17     z:=x;
     18     x:=y;
     19     y:=z;
     20 end;
     21 
     22 procedure add(u,v,r:longint);
     23 begin
     24     inc(l);
     25     pre[l]:=last[u];
     26     last[u]:=l;
     27     other[l]:=v;
     28     len[l]:=r;
     29 end;
     30 
     31 procedure sort(l,r:longint);
     32 var
     33     i,j,x                                     :longint;
     34 begin
     35     i:=l; j:=r; x:=d[list[(l+r)>>1]];
     36     while i<j do
     37     begin
     38         while d[list[i]]<x do inc(i);
     39         while d[list[j]]>x do dec(j);
     40         if i<=j then
     41         begin
     42             swap(list[i],list[j]);
     43             inc(i);
     44             deC(j);
     45         end;
     46     end;
     47     if i<r then sort(i,r);
     48     if j>l then sort(l,j);
     49 end;
     50 
     51 procedure dfs1(x,fa:longint);
     52 var
     53     p,q                                     :longint;
     54 begin
     55     size[x]:=1;
     56     inc(sum);
     57     list[sum]:=x;
     58     p:=last[x];
     59     while p<>0 do
     60     begin
     61         q:=other[p];
     62         if (q<>fa) and (vis[q]<>vis_cnt) then
     63         begin
     64             dfs1(q,x);
     65             father[q]:=x;
     66             size[x]:=size[x]+size[q];
     67         end;
     68         p:=pre[p];
     69     end;
     70 end;
     71 
     72 
     73 function get_root(x,fa:longint):longint;
     74 var
     75     i,pl,t,dd,q,p,cur                        :longint;
     76 begin
     77     sum:=0;
     78     dfs1(x,fa);
     79     t:=maxlongint;
     80     for i:=1 to sum do
     81     begin
     82         dd:=0;
     83         cur:=list[i];
     84         p:=last[cur];
     85         while p<>0 do
     86         begin
     87             q:=other[p];
     88             if (q<>father[cur]) and (vis[q]<>vis_cnt) then dd:=max(dd,size[q]);
     89             p:=pre[p];
     90         end;
     91         if cur<>x then dd:=max(dd,size[x]-size[cur]);
     92         if dd<t then 
     93         begin
     94             pl:=cur;
     95             t:=dd;
     96         end;
     97     end;
     98     exit(pl);
     99 end;
    100 
    101 procedure dfs2(x,fa,w:longint);
    102 var
    103     p,q                                     :longint;
    104 begin
    105     inc(l1);
    106     list[l1]:=x;
    107     d[x]:=w;
    108     p:=last[x];
    109     while p<>0 do
    110     begin
    111         q:=other[p];
    112         if (q<>fa) and (vis[q]<>vis_cnt) then dfs2(q,x,w+len[p]);
    113         p:=pre[p];
    114     end;
    115 end;
    116 
    117 function get_ans(l,r:longint):longint;
    118 var
    119     summ,i,j                                :longint;
    120 begin
    121     summ:=0;
    122     j:=r;
    123     for i:=l to r do
    124     begin
    125         while (d[list[i]]+d[list[j]]>k) and (j>i) do dec(j);
    126         if i>=j then break;
    127         summ:=summ+j-i;
    128     end;
    129     exit(summ);
    130 end;
    131 
    132 procedure work(x,fa:longint);
    133 var
    134     rt,p,q                                    :longint;
    135 begin
    136     rt:=get_root(x,fa);
    137     l1:=0;
    138     l2:=0;
    139     vis[rt]:=vis_cnt;
    140     p:=last[rt];
    141     while p<>0 do
    142     begin
    143         q:=other[p];
    144         if vis[q]=vis_cnt then
    145         begin
    146             p:=pre[p];
    147             continue;
    148         end;
    149         l2:=l1;
    150         dfs2(q,rt,len[p]);
    151         sort(l2+1,l1);
    152         ans:=ans-get_ans(l2+1,l1);
    153         p:=pre[p];
    154     end;
    155     inc(l1);
    156     list[l1]:=rt;
    157     d[rt]:=0;
    158     sort(1,l1);
    159     ans:=ans+get_ans(1,l1);
    160     p:=last[rt];
    161     while p<>0 do
    162     begin
    163         q:=other[p];
    164         if (vis[q]<>vis_cnt) then work(q,rt);
    165         p:=pre[p];
    166     end;
    167 end;
    168 
    169 begin
    170     read(n,k);
    171     while (n+k>0) do
    172     begin
    173         fillchar(last,sizeof(last),0);
    174         inc(vis_cnt);
    175         l:=0; ans:=0;
    176         for i:=1 to n-1 do
    177         begin
    178             read(x,y,z);
    179             add(x,y,z);
    180             add(y,x,z);
    181         end;
    182         work(1,-1);
    183         write(ans);
    184         read(n,k);
    185         if n+k>0 then writeln;
    186     end;
    187 end.
    poj1741
  • 相关阅读:
    Servlet生命周期、配置Servletcontext 对象
    Http协议、web、Tomcat服务器、Servlet入门
    XML、CDATA、DTD、Schema约束、DOM4j
    多线程 Upload、类加载器、反射、配置文件
    0624 网络通信协议 UDP、TCP
    线程类、线程池
    commons-IO、多线程
    Properties类 序列化和反序列化Object 、Print
    转换流OutputStreamWriter 缓冲流
    字节流Stream(Output 、Input)、字符流(Reader、Writer)
  • 原文地址:https://www.cnblogs.com/zoewilly/p/6718287.html
Copyright © 2011-2022 走看看