其实这两题都是基础的线段树,但对于我这个线段树的初学者来说,总结一下还是很有用的;

poj3468显然是线段树区间求和,区间更改的问题,而poj2528是对区间染色,问有多少种颜色的问题;

线段树的建立和求和附代码,还是比较简单的;

这里想说的是区间修改,用到了了lazy思想:打标记;

拿poj2528举例,比如对区间[l,r]染色,我们只要在线段树中,被[l,r]覆盖的最大子区间[p,q]上标记被染成了什么颜色即可,不需要再往下遍历[p,q]的左右孩子;当下次修改影响到了区间[p,q]时(区间有交集),说明[p,q]一定不会全都维持原来的颜色。我们将标记向下传递给左右孩子(同时自身标记清除),不断传递下去,直至某个区间完全被要修改区间覆盖,,再给这个区间打上新的标记。这样可保证时间复杂度为O(logn);

总之lazy思想的精髓就是,能不往下访问就不访问,要更改的时候再将子节点更改,从而减少时间复杂度;

 var lazy,tree:array[..] of int64;
l,n,m,j,x,a,b,i:longint;
ans:int64;
c:char;
procedure pushdown(l,r,i:longint);
var m:longint;
begin
m:=(l+r) div ;
if lazy[i]= then exit;
lazy[i*]:=lazy[i*]+lazy[i];
lazy[i*+]:=lazy[i*+]+lazy[i];
tree[i*]:=tree[i*]+lazy[i]*(m-l+);
tree[i*+]:=tree[i*+]+lazy[i]*(r-m);
lazy[i]:=;
end; procedure build(i,l,r:longint);
var m:longint;
begin
if l=r then read(tree[i])
else begin
m:=(l+r) div ;
build(i*,l,m);
build(*i+,m+,r);
tree[i]:=tree[*i]+tree[*i+];
end;
end; function find(i,l,r,l1,r1:longint):int64;
var m:longint;
t:int64;
begin
if (l>=l1) and (r<=r1) then exit(tree[i])
else begin
pushdown(l,r,i);
m:=(l+r) div ;
t:=;
if l1<=m then t:=t+find(*i,l,m,l1,r1);
if r1>m then t:=t+find(*i+,m+,r,l1,r1);
exit(t);
end;
end; procedure work(i,l,r,l1,r1,x:longint);
var m:longint;
begin
if (l1<=l) and (r<=r1) then
begin
lazy[i]:=lazy[i]+x;
tree[i]:=tree[i]+(r-l+)*x;
end
else begin
pushdown(l,r,i);
m:=(l+r) div ;
if (l1<=m) then work(i*,l,m,l1,r1,x);
if (r1>m) then work(i*+,m+,r,l1,r1,x);
tree[i]:=tree[i*]+tree[i*+];
end;
end; begin
readln(n,m);
build(,,n);
readln;
fillchar(lazy,sizeof(lazy),);
for i:= to m do
begin
read(c);
if c='Q' then
begin
read(a,b);
ans:=find(,,n,a,b);
writeln(ans);
end
else if c='C' then
begin
read(a,b,j);
work(,,n,a,b,j);
end;
readln;
end;
end.

poj3468

而poj2528还要复杂一点,简单的建立线段树会爆空间,这需要我们把出现的区间离散化,减小空间复杂度。

 var tree:array[..] of integer;
    x,y:array[..] of longint;
    a:array[..] of longint;         //表示离散化乎的标号对应的区间
    f:array[..] of boolean;
    ff:array[..] of boolean;
    i,j,k,n,t,s:longint;
procedure sort(l,r: longint);
  var i,j,x,y: longint;
  begin
    i:=l;
    j:=r;
    x:=a[(l+r) div ];
    repeat
      while a[i]<x do inc(i);
      while x<a[j] do dec(j);
      if not(i>j) then
      begin
        y:=a[i];
        a[i]:=a[j];
        a[j]:=y;
        inc(i);
        j:=j-;
      end;
    until i>j;
    if l<j then sort(l,j);
    if i<r then sort(i,r);
  end;
procedure putdown(i,p,q:longint);         //传递标记
  begin
    if p<>q then
    begin
      tree[i*]:=tree[i];
      tree[i*+]:=tree[i];
      tree[i]:=;
    end;
  end;
procedure build(i,p,q,l,r,x:longint);
  var m:longint;
  begin
    if (a[p]>=l) and (r>=a[q]) then tree[i]:=x
    else begin
      if tree[i]<> then putdown(i,p,q);
      m:=(p+q) div ;
      if l<=a[m] then
      begin
        build(i*,p,m,l,r,x);
      end;
      if r>a[m] then
      begin
        build(i*+,m+,q,l,r,x);
      end;
    end;
  end;
procedure dfs(i,p,q:longint);              //统计多少可见海报
  var m:longint;
  begin
    if (tree[i]>) and not ff[tree[i]] then
    begin
      s:=s+;
      ff[tree[i]]:=true;
    end
    else if (tree[i]=) and (p<>q) then
    begin
      m:=(p+q) div ;
      dfs(i*,p,m);
      dfs(i*+,m+,q);
    end;
  end;
begin
  readln(t);
  for i:= to t do
  begin
    k:=;
    fillchar(f,sizeof(f),false);
    readln(n);                        
    for j:= to n do 
    begin
      readln(x[j],y[j]);
      if not f[x[j]] then                        //离散化
      begin
        k:=k+;
        a[k]:=x[j];
        f[x[j]]:=true;
      end;
      if not f[y[j]] then
      begin
        k:=k+;
        a[k]:=y[j];
        f[y[j]]:=true;
      end;
    end;
    sort(,k);
    fillchar(tree,sizeof(tree),);
    for j:= to n do                     
      build(,,k,x[j],y[j],j);
    s:=;
    fillchar(ff,sizeof(ff),false);
    dfs(,,k);
    writeln(s);
  end;
end.

poj2528

poj3468,poj2528的更多相关文章

  1. 【poj3468】 A Simple Problem with Integers

    http://poj.org/problem?id=3468 (题目链接) 题意 给出一个序列,要求维护区间修改与区间求和操作. Solution 多年以前学习的树状数组区间修改又忘记了→_→. 其实 ...

  2. poj2528(线段树+离散化)

    题目链接:https://vjudge.net/problem/POJ-2528 题意:在区间[1,1e7]内染色,依次染n(<=1e4)中颜色,给出每种颜色染色的范围,可重叠,求最终有多少种颜 ...

  3. poj3468 线段树的懒惰标记

    题目链接:poj3468 题意:给定一段数组,有两种操作,一种是给某段区间加c,另一种是查询一段区间的和 思路:暴力的方法是每次都给这段区间的点加c,查询也遍历一遍区间,复杂度是n*n,肯定过不去,另 ...

  4. poj3468 A Simple Problem with Integers(线段树区间更新)

    https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...

  5. poj-2528线段树练习

    title: poj-2528线段树练习 date: 2018-10-13 13:45:09 tags: acm 刷题 categories: ACM-线段树 概述 这道题坑了我好久啊啊啊啊,,,, ...

  6. 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和

    poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...

  7. 线段树---poj2528 Mayor’s posters【成段替换|离散化】

    poj2528 Mayor's posters 题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报 思路:这题数据范围很大,直接搞超时+超内存,需要离散化: 离散化简单的来说就是只取我们需要 ...

  8. POJ2528 Mayor's posters —— 线段树染色 + 离散化

    题目链接:https://vjudge.net/problem/POJ-2528 The citizens of Bytetown, AB, could not stand that the cand ...

  9. POJ3468 A Simple Problem with Integers —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...

随机推荐

  1. 屌丝IT男

    偶尔翻到豆瓣里一篇对中国屌丝的批评,突然想到当年美国那个垮掉的一代,吸毒,淫乱,绝望的生存,而如今我们苦逼的80后自诩为屌丝的时候,也不想想每一个堕落的时代还是有牛逼的人存在,中国的大学,绝大部分在逃 ...

  2. R语言学习笔记 之 可视化地研究参议员相似性

    基于相似性聚类 很多时候,我们想了解一群人中的一个成员与其他成员之间有多么相似.例如,假设我们是一家品牌营销公司,刚刚完成了一份有潜力新品牌的研究调查问卷.在这份调查问卷中,我们向一群人展示了新品牌的 ...

  3. c++ 获取本地ip地址

    最终版本:采用指针传参数,不使用别名形式. #include <unistd.h> #include <netdb.h> //gethostbyname #include &l ...

  4. select()2

    只要接触过c/c++网路编程人都可能会知道select io 模式,网络书籍都说 fd_set {]} 有所限制,因为数组的长度只有64,那么超过64你就不能放,要么你就是用多线程分别实用select ...

  5. [C/CPP系列知识] C++中extern “C” name mangling -- Name Mangling and extern “C” in C++

    http://www.geeksforgeeks.org/extern-c-in-c/ C++函数重载(function overloading),但是C++编译器是如何区分不同的函数的呢?----是 ...

  6. 【DDD-Apwork框架】事件总线和事件聚合器

    第一步:事件总线和事件聚合器 [1]事件总线 IEventBus IUnitOfWork.cs using System; using System.Collections.Generic; usin ...

  7. jquery 实现层级下拉框联动效果 代码

    <select name="fCareId" id="fCareId"> <option selected="selected&qu ...

  8. hbase操作的问题

    写了一个java程序,需要向hbase中写入大量的数据,但是这个程序执行一半就报错, 问题是,此时已经写入了很多数据. 查看jps,发现hmaster进程崩溃了. 基于以上信息,发现是在程序中,链接h ...

  9. hadoop+hbase

    hadoop的配置见下面这篇文章 http://www.powerxing.com/install-hadoop-2-4-1-single-node/ Hadoop安装教程_单机/伪分布式配置_Had ...

  10. POJ1416Shredding Company

    http://poj.org/problem?id=1416 题意 : 要为碎纸机公司开发一种新的碎纸机,这种碎纸机要具有3个特性 :一是粉碎机以一个目标数 t 作为输入,并且粉碎的纸上写有一个数字n ...