zoukankan      html  css  js  c++  java
  • bzoj3262

    三维裸的做法是一维排序,剩下树套树,可我好像还没写过树套树
    先说cdq分治吧,先对一维排序,相当于原来修改询问里的时间线
    在这上面分治、划分,计算前半部分对后半部分的影响,显然可以按第二维的顺序维护树状数组

      1 type node=record
      2        a,b,c,s,p:longint;
      3      end;
      4 
      5 var a,b,q:array[0..100010] of node;
      6     ans,c:array[0..200010] of longint;
      7     n,m,i,t,j:longint;
      8 
      9 procedure swap(var a,b:node);
     10   var c:node;
     11   begin
     12     c:=a;
     13     a:=b;
     14     b:=c;
     15   end;
     16 
     17 function cmp(a,b:node):boolean;
     18   begin
     19     if a.a<b.a then exit(true);
     20     if a.a>b.a then exit(false);
     21     if (a.b<b.b) or (a.b=b.b) and (a.c<b.c) then exit(true);
     22     exit(false);
     23   end;
     24 
     25 procedure sorta(l,r:longint);
     26   var i,j:longint;
     27       x:node;
     28   begin
     29     i:=l;
     30     j:=r;
     31     x:=a[(l+r) shr 1];
     32     repeat
     33       while cmp(a[i],x) do inc(i);
     34       while cmp(x,a[j]) do dec(j);
     35       if not(i>j) then
     36       begin
     37         swap(a[i],a[j]);
     38         inc(i);
     39         dec(j);
     40       end;
     41     until i>j;
     42     if l<j then sorta(l,j);
     43     if i<r then sorta(i,r);
     44   end;
     45 
     46 function lowbit(x:longint):longint;
     47   begin
     48     exit(x and (-x));
     49   end;
     50 
     51 procedure add(x,w:longint);
     52   begin
     53     while x<=m do
     54     begin
     55       inc(c[x],w);
     56       x:=x+lowbit(x);
     57     end;
     58   end;
     59 
     60 function ask(x:longint):longint;
     61   begin
     62     ask:=0;
     63     while x>0 do
     64     begin
     65       ask:=ask+c[x];
     66       x:=x-lowbit(x);
     67     end;
     68   end;
     69 
     70 procedure cdq(l,r:longint);
     71   var m,i,j,l1,l2:longint;
     72   begin
     73     if l=r then exit;
     74     m:=(l+r) shr 1;
     75     cdq(l,m);
     76     cdq(m+1,r);
     77     j:=l;
     78     for i:=m+1 to r do
     79     begin
     80       while (j<=m) and (b[j].b<=b[i].b) do
     81       begin
     82         add(b[j].c,b[j].p);
     83         inc(j);
     84       end;
     85       inc(b[i].s,ask(b[i].c));
     86     end;
     87     for i:=l to j-1 do
     88       add(b[i].c,-b[i].p);
     89     l1:=l; l2:=m+1;
     90     for i:=l to r do
     91       if ((l1<=m) and (b[l1].b<b[l2].b)) or (l2>r) then
     92       begin
     93         q[i]:=b[l1];
     94         inc(l1);
     95       end
     96       else begin
     97         q[i]:=b[l2];
     98         inc(l2);
     99       end;
    100     for i:=l to r do
    101       b[i]:=q[i];
    102   end;
    103 
    104 begin
    105   readln(n,m);
    106   for i:=1 to n do
    107     readln(a[i].a,a[i].b,a[i].c);
    108   sorta(1,n);
    109   i:=0;
    110   while i<n do
    111   begin
    112     inc(i);
    113     j:=i+1;
    114     while (a[j].a=a[i].a) and (a[j].b=a[i].b) and (a[j].c=a[i].c) do inc(j);  //这步不能少,因为要保证后半部分序列对前半部分没有影响
    115     inc(t);
    116     b[t]:=a[i];
    117     b[t].p:=j-i;
    118     i:=j-1;
    119   end;
    120   cdq(1,t);
    121   for i:=1 to t do
    122     inc(ans[b[i].s+b[i].p-1],b[i].p);
    123   for i:=0 to n-1 do
    124     writeln(ans[i]);
    125 end.
    View Code
  • 相关阅读:
    Redis事务和锁
    11/6笔记 补充(Redis持久化,RDB&&AOF)
    11/6随笔
    Redis 安装教程
    Redis通用指令和第一个Jedis程序的实现
    Redis学习第二天
    SpringBoot学习笔记
    1000行代码手写服务器
    log4j创建实例异常
    寒假阅读人月神话3
  • 原文地址:https://www.cnblogs.com/phile/p/4472947.html
Copyright © 2011-2022 走看看