---
id: hld
title: "Heavy-Light Decomposition"
author: Benjamin Qi
prerequisites: 
 - Gold - Euler Tour Technique
 - Platinum - Range Update Range Query
description: Path and subtree updates and queries.
frequency: 1
---

import { Problem } from "../models";

export const metadata = {
  problems: {
    sample: [
      new Problem("CSES", "Company Queries II", "1688", "Intro|Easy", false, ["LCA"], "Pure implementation; see Benq's library code, it has a function for LCA. Though this problem can be solved with binary lifting as well, you should do it with HLD to get practice."),
    ],
    general: [
      new Problem("Gold", "Milk Visits", "970", "Easy", false, ["HLD"], "This has a linear time solution, but it's easier to make modifications to the HLD template."),
      new Problem("Gold", "Cow Land", "921", "Easy", false, ["PURS", "HLD"], ""),
      new Problem("Old Gold", "Grass Planting", "102", "Intro|Easy", false, ["HLD", "PURS"], ""),
      new Problem("Plat", "Disrupt", "842", "Easy", false, ["HLD"], ""),
      new Problem("HR", "Subtrees & Paths", "https://www.hackerrank.com/challenges/subtrees-and-paths", "Intro|Easy", false, ["HLD", "RURQ"], "See adamant's blog."),
      new Problem("YS","Vertex Set Path Composite","vertex_set_path_composite", "Intro|Normal", false, ["HLD", "SegTree"], "Function order matters! Maintain two segment trees, one for going up and the other for going down the tree."),
      new Problem("CF", "Tree Queries", "contest/1254/problem/D", "Hard", false, ["HLD"], "maybe hard to see why this applies here, gives $O(N\\log N)$ while most people solved it with some factor of $\\sqrt N$"),
      new Problem("ojuz", "JOI - Synchronization", "JOI13_synchronization", "Hard", false, ["HLD"], "$O(N\\log N)$ :D"),
      new Problem("ojuz", "JOI - Cats or Dogs", "JOI18_catdog", "Very Hard", false, ["HLD"], ""),
    ],
  }
};

<problems-list problems={metadata.problems.sample} />

## Tutorial

<resources>
  <resource source="cp-algo" title="HLD" url="https://cp-algorithms.com/graph/hld.html" starred>For an alternate implementation, see below</resource>
  <resource source="anudeep2011" title="HLD" url="https://blog.anudeep2011.com/heavy-light-decomposition/">explains what HLD is (but incomplete & overly complicated code)</resource>
</resources>

## Implementations

<resources>
  <resource source="CF" title="AI-Cash - HLD Implementation" url="blog/entry/22072" starred />
  <resource source="CF" title="adamant - Easiest HLD with subtree queries" url="blog/entry/53170" starred>not complete</resource>
  <resource source="Benq" title="Complete HLD Implementation" url="https://github.com/bqi343/USACO/blob/master/Implementations/content/graphs%20(12)/Trees%20(10)/HLD%20(10.3).h" starred>complete implementation following the above two articles with minor modifications</resource>
</resources>

## Problems

<problems-list problems={metadata.problems.general} />