zoukankan      html  css  js  c++  java
  • bzoj2120: 数颜色

    2120: 数颜色

    Time Limit: 6 Sec  Memory Limit: 259 MB
    Submit: 3669  Solved: 1422
    [Submit][Status][Discuss]

    Description

    墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问。墨墨会像你发布如下指令: 1、 Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔。 2、 R P Col 把第P支画笔替换为颜色Col。为了满足墨墨的要求,你知道你需要干什么了吗?

    Input

    第1行两个整数N,M,分别代表初始画笔的数量以及墨墨会做的事情的个数。第2行N个整数,分别代表初始画笔排中第i支画笔的颜色。第3行到第2+M行,每行分别代表墨墨会做的一件事情,格式见题干部分。

    Output

    对于每一个Query的询问,你需要在对应的行中给出一个数字,代表第L支画笔到第R支画笔中共有几种不同颜色的画笔。

    Sample Input

    6 5
    1 2 3 4 5 5
    Q 1 4
    Q 2 6
    R 1 2
    Q 1 4
    Q 2 6

    Sample Output

    4
    4
    3
    4

    HINT

    对于100%的数据,N≤10000,M≤10000,修改操作不多于1000次,所有的输入数据中出现的所有整数均大于等于1且不超过10^6。

    题解

    我们记录pre[i]为与i相同颜色的上一个点在什么位置,一个显然的结论是,在查询区间[l,r]时对于一个点i只有pre[i]<l才会对答案有贡献(可以理解为只计算每种颜色第一次出现,如果后面再出现pre就会在[l,r])

    这样我们可以分块来做。对于每一块,我们将块内按照pre的值排序,然后在块内二分查找就可以轻松解决。修改操作因为不超过1000次,我们可以直接暴力重建(我一开始想用平衡树维护前驱后继然后重建然后写挂了

    分块大法坠吼了!

    {$S-}{$R-}{$I-}
    program j01;
    var pre,b,bel,c:array[0..10086]of longint;
        l,r:array[0..10000]of longint;
        head:array[0..1000086]of longint;
        size,n,m,x,y,i,cnt:longint;
        ch:char;
     
    procedure qsort(l,r:longint);
    var i,j,x,y:longint;
    begin
      i:=l;j:=r;x:=pre[(i+j)div 2];
      repeat
        while pre[i]<x do inc(i);
        while x<pre[j] do dec(j);
        if i<=j then
        begin
          y:=pre[i];pre[i]:=pre[j];pre[j]:=y;
          inc(i);dec(j);
        end;
      until i>j;
      if i<r then qsort(i,r);
      if l<j then qsort(l,j);
    end;
     
    procedure res(x:longint);
    var i:longint;
    begin
      for i:=l[x] to r[x] do pre[i]:=b[i];
      qsort(l[x],r[x]);
    end;
     
    procedure change(x,y:longint);
    var i,tmp:longint;
    begin
      c[x]:=y;
      for i:=1 to n do head[c[i]]:=0;
      for i:=1 to n do
      begin
        tmp:=b[i];
        b[i]:=head[c[i]];
        if b[i]<>tmp then res(bel[i]);
        head[c[i]]:=i;
      end;
    end;
     
    function find(p,x:longint):longint;
    var ll,rr,mid:longint;
    begin
      ll:=l[p];rr:=r[p];
      if pre[ll]>=x then exit(0);
      while ll<rr do
      begin
        mid:=(ll+rr)div 2;
        if pre[mid+1]<x then ll:=mid+1
          else rr:=mid;
      end;
      exit(ll+1-l[p]);
    end;
     
    function ask(x,y:longint):longint;
    var i,j,ans:longint;
    begin
      ans:=0;
      if bel[x]=bel[y] then
      begin
        for i:=x to y do if b[i]<x then inc(ans);
        exit(ans);
      end;
      for i:=x to r[bel[x]] do if b[i]<x then inc(ans);
      for i:=l[bel[y]] to y do if b[i]<x then inc(ans);
      for i:=bel[x]+1 to bel[y]-1 do ans:=ans+find(i,x);
      exit(ans);
    end;
     
    begin
      readln(n,m);
      for i:=1 to n do read(c[i]);
      size:=trunc(sqrt(n)+ln(2*n)/ln(2));
      for i:=1 to n do bel[i]:=(i-1)div size+1;
      cnt:=bel[n];
      for i:=1 to cnt do
      begin
        l[i]:=(i-1)*size+1;
        if i=cnt then r[i]:=n else r[i]:=i*size;
      end;
      fillchar(head,sizeof(head),0);
      for i:=1 to n do
      begin
        pre[i]:=head[c[i]];
        b[i]:=pre[i];
        head[c[i]]:=i;
      end;
      for i:=1 to cnt do qsort(l[i],r[i]);
      for i:=1 to m do
      begin
        read(ch);
        while not(ch in['Q','R']) do read(ch);
        readln(x,y);
        if ch='Q' then writeln(ask(x,y));
        if ch='R' then change(x,y);
      end;
    end.
  • 相关阅读:
    codeforces 980A Links and Pearls
    zoj 3640 Help Me Escape
    sgu 495 Kids and Prizes
    poj 3071 Football
    hdu 3853 LOOPS
    hdu 4035 Maze
    hdu 4405 Aeroplane chess
    poj 2096 Collecting Bugs
    scu 4444 Travel
    zoj 3870 Team Formation
  • 原文地址:https://www.cnblogs.com/oldjang/p/6194560.html
Copyright © 2011-2022 走看看