Pular para o conteúdo principal
ProdutosFerramentas de desktopTASTECADVisualização de desenhosTASTEFILESGerenciamento de arquivosTASTEFTPTransferência de arquivosTASTESSHEspaço de trabalho remotoTASTEZIPCompactação e extraçãoTASTEWINMGerenciamento de arquivosFerramentas para desenvolvedoresTASTECODEAmbiente de desenvolvimento integrado para RustTASTESTUDIOAmbiente integrado de desenvolvimento e testesTASTEETLIntegração de dados (ETL)ServidoresTASTEREMOTEServidor de área de trabalho remotaTASTEGEOServidor geoespacialTASTEMAILEspaço de trabalho de e-mailInvestimentosTASTESTOCKAplicativo desktop de negociação automática de açõesComeçarDownloadsDocumentaçãoComprarTodos os produtos
Marketplace
Notícias
TecnologiaNotíciasNotícias de desenvolvimento de hojeDocumentaçãoLinguagens de programaçãoRustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDartFrameworks de front-end webNext.jsReactVue.js · NuxtSvelteKitAngularFrameworks de servidorSpring BootDjangoFastAPIASP.NET CoreNestJS · ExpressFrameworks de apps e desktopTauriFlutterReact NativeElectronAlgoritmosFundamentos de algoritmosAlgoritmos essenciaisAlgoritmos avançadosAlgoritmos práticos
ComunidadeProjetosRegistros de desenvolvimentoPromptsProblemas de ferramentas de IAVitrineGuia da comunidade
ColaboreTransferência técnicaConstrução de siteConstrução de app de desktopPipeline de lançamento e pagamentoParceiro de vendasContato
ServiçosÁrea de trabalho remotaPacotes de instalação sob medida
TASTEDEV
Produtos

Ferramentas de desktop

TASTECADDesenhos complexos, abertos com levezaTASTEFILESSó os arquivos de que você precisa, o mais rápido possívelTASTEFTPO caminho mais organizado para seus arquivosTASTESSHEntre você e o servidor, uma conexão mais sólidaTASTEZIPCompacte mais rápido, extraia com mais segurançaTASTEWINMUm gerenciador de arquivos de dois painéis feito para o teclado

Ferramentas para desenvolvedores

TASTECODEEdite, compile, depure e teste apps Rust em uma única janelaTASTESTUDIODa edição aos testes remotos e às correções com IA aprovadasTASTEETLMova dados de olho nos valores

Servidores

TASTEREMOTEUma área de trabalho remota rodando no seu próprio servidorTASTEGEOUm servidor geoespacial compatível com o GeoServer, reconstruído em RustTASTEMAILRetome o controle do seu e-mail

Investimentos

TASTESTOCKNegociação automática de ações pelas regras que você define: quando e o quê

Começar

DownloadsVersões mais recentes de cada produtoDocsComo usar cada produtoComprarAssinaturas e licenças perpétuasTodos os produtosTodos os produtos de relance
Todos os produtos
MarketplaceNotícias
Tecnologia

Notícias

Notícias de desenvolvimento de hojeAnúncios oficiais de linguagens, frameworks, GitHub e IADocumentaçãoGuias de linguagens, frameworks e algoritmos

Linguagens de programação

RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDart

Frameworks

Frameworks de front-end webNext.jsReactVue.js · NuxtSvelteKitAngular
Frameworks de servidorSpring BootDjangoFastAPIASP.NET CoreNestJS · Express
Frameworks de apps e desktopTauriFlutterReact NativeElectron

Algoritmos

Fundamentos de algoritmosAnálise de complexidade · Ordenação · Busca · Estruturas de dados básicas · Árvores e heapsAlgoritmos essenciaisBusca em grafos · Caminho mínimo e árvore geradora mínima · Programação dinâmica · Algoritmos gulosos · Divisão e conquista · BacktrackingAlgoritmos avançadosAlgoritmos de strings · Matemática e teoria dos números · Manipulação de bits · Árvores de segmentos · Union-FindAlgoritmos práticosEstratégia para entrevistas de código · Cache LRU · Limitação de taxa · Hashing consistente
Toda a documentação de desenvolvimento
Comunidade
ProjetosDescubra criadores e trabalhosRegistros de desenvolvimentoDecisões e aprendizadosPromptsInstruções reutilizáveisProblemas de ferramentas de IAProblemas e contornos de Claude, Codex, GeminiVitrineResultados concluídosGuia da comunidadeComo construímos em aberto
Colabore
Transferência técnicaApps nativos em Rust e eguiConstrução de site10 idiomas, contas, documentação e downloadsConstrução de app de desktopRust e egui, sem WebViewPipeline de lançamento e pagamentoAssinatura, MSI, deb, rpm, AppImage, hashesParceiro de vendasRecomende e ganhe 30%ContatoDesenvolvimento, parcerias e investimento
Serviços
Área de trabalho remotaAcesse seu computador pelo navegadorPacotes de instalação sob medidaSó o necessário, configurações pré-carregadas
Comprar
PT
한국어EnglishDeutschEspañolFrançaisItalianoPortuguês (Brasil)日本語简体中文繁體中文
Entrar
Entrar
TASTEDEV

De pequenas ferramentas a um desenvolvimento melhor.

support@tastedev.netApoiar TASTEDEV
ProdutosLinha de produtosComprarDocsDownloadsMarketplaceSuporte
ComunidadeProjetosRegistros de desenvolvimentoPromptsProblemas de ferramentas de IAVitrine
PolíticasSobre nósPolítica de privacidadeTermos de usoTermos de assinaturaReembolsoDiretrizes da comunidade
ContaEntrarCriar conta
Visitantes
Hoje
Ontem
Visualizações totais
Empresa
GXSOFT
Representante
CHOI YOUNG GU
Número de registro comercial
853-44-00368
Endereço
9F V24, 351 Gyeongin-ro, Michuhol-gu, Incheon, Republic of Korea
Telefone
+82-10-4404-1523
E-mail
whitecalvin@tastedev.net
Responsável pela privacidade
CHOI YOUNG GU

© 2026 TASTEDEVAs informações dos produtos se baseiam em fontes de desenvolvimento verificadas.Lançado · em melhoria

Documentação de Árvores de segmentos
Árvores de segmentos
ConceptsHow It WorksImplementationComplexityPractice ProblemsReal-World Use
Discussões

Guia de Árvores de segmentos · 1/6

Concepts

Por enquanto, este capítulo está disponível apenas em inglês.

A segment tree is a data structure that answers questions about contiguous ranges of an array, such as "what is the sum of elements 3 through 7?" or "what is the minimum in this range?", and keeps those answers fast even while the array keeps changing. This chapter explains why you need one, what it looks like, and the vocabulary used in the rest of the guide.

The problem: queries and updates mixed together

Suppose an array of length n receives a long stream of two kinds of requests:

  • Point update: change the value of a[i].
  • Range query: return the sum (or minimum, or maximum) of a[l] through a[r-1].

The simplest approach keeps the plain array and loops over the range for every query. Updates cost O(1), but each query costs O(n).

a = [5, 3, 7, 9, 6, 4, 1, 2]

def update(i, value):
    a[i] = value          # O(1)

def query(l, r):
    return sum(a[l:r])    # O(n): adds every element in the range

Prefix sums versus a segment tree

If the values never change, prefix sums are hard to beat. Store in prefix[i] the sum of the first i elements, and any range sum becomes a single subtraction. The catch is that changing one element invalidates every prefix sum after it, so an update costs O(n).

from itertools import accumulate

a = [5, 3, 7, 9, 6, 4, 1, 2]
prefix = [0, *accumulate(a)]   # [0, 5, 8, 15, 24, 30, 34, 35, 37]

def query(l, r):
    return prefix[r] - prefix[l]   # O(1)

def update(i, value):
    diff = value - a[i]
    a[i] = value
    for k in range(i + 1, len(prefix)):   # O(n): fix every later prefix
        prefix[k] += diff

print(query(2, 6))   # 26
ApproachPoint updateRange queryGood fit
Plain arrayO(1)O(n)Queries are rare
Prefix sumsO(n)O(1)Values never change
Segment treeO(log n)O(log n)Updates and queries interleave

Instead of making one operation extremely fast and the other slow, a segment tree makes both operations `O(log n)`. With hundreds of thousands of requests, that is the difference between seconds and hours.

What the tree looks like

A segment tree is a binary tree in which every node is responsible for one range of the array and stores a of that range (its sum, minimum, and so on).

Dúvidas sobre este capítulo?
Próximo capítuloHow It Works
summary
  • The root covers the whole range [0, n).
  • A node covering [lo, hi) splits at the midpoint mid: the left child covers [lo, mid) and the right child covers [mid, hi).
  • A leaf covers a range of length 1, that is, a single element.
  • An internal node stores the combination of its two children.

Because each split halves the range, the tree has height about log2 n. Any query range can be covered by a handful of disjoint nodes (at most two per level), and changing one element only requires recomputing the single path from that leaf to the root. That is why both operations run in O(log n).

What the combine operation must satisfy

The operation stored in the tree must be associative, and the implementation is cleanest when it also has an identity element. In algebra this structure is called a monoid.

import math

# (combine function, identity)
SUM = (lambda x, y: x + y, 0)
MIN = (min, math.inf)
MAX = (max, -math.inf)
GCD = (math.gcd, 0)
XOR = (lambda x, y: x ^ y, 0)

op, identity = MIN
print(op(op(4, 9), 2) == op(4, op(9, 2)))   # True: associativity
print(op(identity, 7))                      # 7: the identity changes nothing

A question like "what is the median of this range?" does not fit: the parent's answer cannot be computed from the two children's summaries alone, so a basic segment tree cannot answer it.

Vocabulary

  • Node and segment: a cell of the tree and the slice of the array it covers.
  • Combine (merge): the operation that turns two child values into the parent value.
  • Identity: the neutral value returned for nodes outside the query, such as 0 for sums and infinity for minimums.
  • Point update and range query: the two operations a basic segment tree supports.
  • Range update: a request such as "add 3 to every element in this range", handled with lazy propagation.
  • Fenwick tree (binary indexed tree, BIT): a shorter, lighter alternative specialized for invertible operations such as sums.

When to use it

  • Array values change often, and range sums, minimums, or maximums are queried in between.
  • You repeatedly count "how many elements seen so far are smaller than x" over sorted coordinates.
  • A sweep line opens and closes intervals and needs to maintain aggregate state as it moves.

If the values never change, prefix sums or a sparse table are simpler and faster.

Summary

A segment tree stores a summary for every range obtained by repeatedly halving the array, which makes both point updates and range queries O(log n). As long as the combine operation is associative and has an identity, the same structure answers sums, minimums, maximums, GCDs, and more. The next chapter builds a tree by hand and traces queries and updates through it.

0 comentários

Principais comentáriosMais recentes primeiro

Fazer login · Faça login para deixar um comentário.