---
id: 537
title: "数颜色题解"
slug: count-colors
lang: zh
date: 2019-01-27T15:09:59
date_gmt: 2019-01-27T07:09:59
modified: 2019-01-27T15:09:59
status: publish
password_protected: false
category: { name: Olympiad in Informatics, slug: oi }
tags: [OI, 线段树, 主席树]
original_url: https://kalorona.com/oi/数颜色题解/
archived: 2026-11-27
editor: classic
media: []
archive_notes:
  - "Body headings were <h1> in the original; demoted to level 2 so the page title is the only h1."
comments: []
---

## 题意

给定一个长度为n的序列A[]，你需要回答q个询问。每次给定两个端点，询问区间内不同颜色的个数。n, q < 1e5。

## 主要思路

可以考虑建立主席树，以位置做版本下标，以颜色离散化后的值做下标，记录该颜色前缀个数，然后版本信息相减即可。
