zoukankan      html  css  js  c++  java
  • bzoj4046

    分组赛的题……madan原题,考试想不出来真是SB得不行

    首先,从大往小加边,每次加边如果成环必然弹出环上最大边

    考虑询问[x,y],如果边权在[x,y]的边弹出了小于等于y的边j,说明j不在最小生成树上

    其余边权在[x,y]的边都在最小生成树上

    因此我们每次只要询问,在一段区间内边权小于等于y的边权和是多少,显然用主席树维护

    处理弹出的最大边可以暴力,因为n不大

    但我写了lct还是t真是简直了……

      1 const inf=10000007;
      2 type way=record
      3        x,y,z:longint;
      4      end;
      5      node=record
      6        l,r,s:longint;
      7      end;
      8 
      9 var tree:array[0..210010*20] of node;
     10     a:array[0..100010] of way;
     11     h,st,w,f,c,fa,mx:array[0..120010] of longint;
     12     son:array[0..120010,1..2] of longint;
     13     rev:array[0..120010] of boolean;
     14     ans,j,len,wh,p,i,t,tt,n,m,q,x,y:longint;
     15 
     16 procedure swap(var a,b:longint);
     17   var c:longint;
     18   begin
     19     c:=a;
     20     a:=b;
     21     b:=c;
     22   end;
     23 
     24 procedure sort(l,r:longint);
     25   var i,j,x:longint;
     26       y:way;
     27   begin
     28     i:=l;
     29     j:=r;
     30     x:=a[(l+r) shr 1].z;
     31     repeat
     32       while a[i].z>x do inc(i);
     33       while x>a[j].z do dec(j);
     34       if not(i>j) then
     35       begin
     36         y:=a[i]; a[i]:=a[j]; a[j]:=y;
     37         inc(i);
     38         dec(j);
     39       end;
     40     until i>j;
     41     if l<j then sort(l,j);
     42     if i<r then sort(i,r);
     43   end;
     44 
     45 function max(a,b:longint):longint;
     46   begin
     47     if a>b then exit(a) else exit(b);
     48   end;
     49 
     50 function getf(x:longint):longint;
     51   begin
     52     if f[x]<>x then f[x]:=getf(f[x]);
     53     exit(f[x]);
     54   end;
     55 
     56 function root(x:longint):boolean;
     57   begin
     58     exit((son[fa[x],1]<>x) and (son[fa[x],2]<>x));
     59   end;
     60 
     61 procedure update(x:longint);
     62   var l,r:longint;
     63   begin
     64     l:=son[x,1]; r:=son[x,2];
     65     mx[x]:=x;
     66     if w[mx[l]]>w[mx[x]] then mx[x]:=mx[l];
     67     if w[mx[r]]>w[mx[x]] then mx[x]:=mx[r];
     68   end;
     69 
     70 procedure rotate(x,w:longint);
     71   var y:longint;
     72   begin
     73     y:=fa[x];
     74     if not root(y) then
     75     begin
     76       if son[fa[y],1]=y then son[fa[y],1]:=x
     77       else son[fa[y],2]:=x;
     78     end;
     79     fa[x]:=fa[y];
     80     son[y,3-w]:=son[x,w];
     81     if son[x,w]<>0 then fa[son[x,w]]:=y;
     82     son[x,w]:=y;
     83     fa[y]:=x;
     84     update(y);
     85   end;
     86 
     87 procedure push(x:longint);
     88   begin
     89     if rev[x] then
     90     begin
     91       rev[son[x,1]]:=not rev[son[x,1]];
     92       rev[son[x,2]]:=not rev[son[x,2]];
     93       swap(son[x,1],son[x,2]);
     94       rev[x]:=false;
     95     end;
     96   end;
     97 
     98 procedure splay(x:longint);
     99   var i,y,t:longint;
    100       fl:boolean;
    101   begin
    102     i:=x;
    103     t:=0;
    104     while not root(i) do
    105     begin
    106       inc(t);
    107       st[t]:=i;
    108       i:=fa[i];
    109     end;
    110     inc(t);
    111     st[t]:=i;
    112     for i:=t downto 1 do
    113       push(st[i]);
    114     if t=1 then exit;
    115     fl:=true;
    116     while fl do
    117     begin
    118       y:=fa[x];
    119       if y=st[t] then
    120       begin
    121         if son[y,1]=x then rotate(x,2)
    122         else rotate(x,1);
    123         fl:=false;
    124       end
    125       else begin
    126         if fa[y]=st[t] then fl:=false;
    127         if son[fa[y],1]=y then
    128         begin
    129           if son[y,1]=x then rotate(y,2)
    130           else rotate(x,1);
    131           rotate(x,2);
    132         end
    133         else begin
    134           if son[y,1]=x then rotate(x,2)
    135           else rotate(y,1);
    136           rotate(x,1);
    137         end;
    138       end;
    139     end;
    140     update(x);
    141   end;
    142 
    143 procedure access(x:longint);
    144   var y:longint;
    145   begin
    146     y:=0;
    147     repeat
    148       splay(x);
    149       son[x,2]:=y;
    150       update(x);
    151       y:=x;
    152       x:=fa[x];
    153     until x=0;
    154   end;
    155 
    156 procedure makeroot(x:longint);
    157   begin
    158     access(x);
    159     splay(x);
    160     rev[x]:=not rev[x];
    161   end;
    162 
    163 procedure link(x,y:longint);
    164   begin
    165     makeroot(x);
    166     fa[x]:=y;
    167   end;
    168 
    169 procedure cut(x,y:longint);
    170   begin
    171     makeroot(x);
    172     access(y);
    173     splay(y);
    174     son[y,1]:=0; fa[x]:=0;
    175   end;
    176 
    177 function get(x,y:longint):longint;
    178   begin
    179     makeroot(x);
    180     access(y);
    181     splay(y);
    182     exit(mx[y]);
    183   end;
    184 
    185 function build(l,r:longint):longint;
    186   var m,q:longint;
    187   begin
    188     inc(t); q:=t;
    189     tree[q].s:=0;
    190     if l<>r then
    191     begin
    192       m:=(l+r) shr 1;
    193       tree[q].l:=build(l,m);
    194       tree[q].r:=build(m+1,r);
    195     end;
    196     exit(q);
    197   end;
    198 
    199 function add(l,r,last,x,y:longint):longint;
    200   var m,q:longint;
    201   begin
    202     inc(t); q:=t;
    203     if l=r then tree[q].s:=tree[last].s+c[x]*y
    204     else begin
    205       m:=(l+r) shr 1;
    206       if x<=m then
    207       begin
    208         tree[q].r:=tree[last].r;
    209         tree[q].l:=add(l,m,tree[last].l,x,y);
    210       end
    211       else begin
    212         tree[q].l:=tree[last].l;
    213         tree[q].r:=add(m+1,r,tree[last].r,x,y);
    214       end;
    215       tree[q].s:=tree[tree[q].l].s+tree[tree[q].r].s;
    216     end;
    217     exit(q);
    218   end;
    219 
    220 function ask(l,r,x,y,k:longint):longint;
    221   var m:longint;
    222   begin
    223     if l=r then
    224     begin
    225       if c[l]<=k then exit(tree[y].s-tree[x].s)
    226       else exit(0);
    227     end
    228     else begin
    229       m:=(l+r) shr 1;
    230       if k<=c[m] then exit(ask(l,m,tree[x].l,tree[y].l,k))
    231       else exit(tree[tree[y].l].s-tree[tree[x].l].s+ask(m+1,r,tree[x].r,tree[y].r,k));
    232     end;
    233   end;
    234 
    235 function find1(l,r,x:longint):longint;
    236   var m:longint;
    237   begin
    238     find1:=r+1;
    239     while l<=r do
    240     begin
    241       m:=(l+r) shr 1;
    242       if (c[a[m-1].z]>x) and (c[a[m].z]<=x) then exit(m);
    243       if c[a[m].z]<=x then r:=m-1 else l:=m+1;
    244     end;
    245   end;
    246 
    247 function find2(l,r,x:longint):longint;
    248   var m:longint;
    249   begin
    250     find2:=l-1;
    251     while l<=r do
    252     begin
    253       m:=(l+r) shr 1;
    254       if (c[a[m].z]>=x) and (c[a[m+1].z]<x) then exit(m);
    255       if c[a[m].z]>=x then l:=m+1 else r:=m-1;
    256     end;
    257   end;
    258 
    259 begin
    260   readln(tt);
    261   while tt>0 do
    262   begin
    263     dec(tt);
    264     readln(n,m);
    265     for i:=1 to n+m do
    266     begin
    267       f[i]:=i;
    268       fa[i]:=0; son[i,1]:=0; son[i,2]:=0;
    269       w[i]:=0; rev[i]:=false;
    270     end;
    271     for i:=1 to m do
    272       readln(a[i].x,a[i].y,a[i].z);
    273     sort(1,m);
    274     for i:=1 to m do
    275       w[n+i]:=a[i].z;
    276     p:=1;
    277     c[1]:=a[m].z;
    278     a[m].z:=1;
    279     for i:=m-1 downto 1 do
    280     begin
    281       if a[i].z<>c[p] then
    282       begin
    283         inc(p);
    284         c[p]:=a[i].z;
    285       end;
    286       a[i].z:=p;
    287     end;
    288     t:=0;
    289     h[0]:=build(1,p);
    290     for i:=1 to m do
    291     begin
    292       x:=getf(a[i].x);
    293       y:=getf(a[i].y);
    294       h[i]:=h[i-1];
    295       if x<>y then f[x]:=y
    296       else begin
    297         wh:=get(a[i].x,a[i].y);
    298         h[i]:=add(1,p,h[i],a[wh-n].z,-1);
    299         cut(a[wh-n].x,wh);
    300         cut(a[wh-n].y,wh);
    301       end;
    302       h[i]:=add(1,p,h[i],a[i].z,1);
    303       link(a[i].x,n+i);
    304       link(a[i].y,n+i);
    305     end;
    306     c[p+1]:=inf;
    307     a[0].z:=p+1;
    308     a[m+1].z:=0;
    309     c[0]:=-1;
    310     readln(q);
    311     ans:=0;
    312     for i:=1 to q do
    313     begin
    314       readln(x,y);
    315       x:=x-ans;
    316       y:=y-ans;
    317       len:=y;
    318       if x>y then ans:=0
    319       else begin
    320         x:=find2(1,m,x);
    321         y:=find1(1,m,y);
    322         ans:=ask(1,p,h[y-1],h[x],len);
    323       end;
    324       writeln(ans);
    325     end;
    326   end;
    327 end.
    View Code
  • 相关阅读:
    POJ3320 Jessica's Reading Problem
    POJ3320 Jessica's Reading Problem
    CodeForces 813B The Golden Age
    CodeForces 813B The Golden Age
    An impassioned circulation of affection CodeForces
    An impassioned circulation of affection CodeForces
    Codeforces Round #444 (Div. 2) B. Cubes for Masha
    2013=7=21 进制转换
    2013=7=15
    2013=7=14
  • 原文地址:https://www.cnblogs.com/phile/p/4573454.html
Copyright © 2011-2022 走看看