P9094 [PA2020] Mieszanie kolorów
显然可以直接差分。
但是我看题的时候看成了多次查询。
所以我就写了个线段树。
注意到一个油漆桶被多次覆盖一个颜色是没有贡献的,所以考虑线段树的每个节点维护 c[0/1][0/1][0/1]
分别表示
接着维护一个 tag
,可以使用 unordered_set
等结构,顺序存储每一个操作。
pushdown
的时候维护 c
即可。
但是这样是过不去的,会 MLE。考虑如果一个数被涂了第
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <set>
#include <vector>
#include <unordered_set>
using namespace std;
const int N = 8e5 + 5;
class SegmentTree
{
public:
struct Node
{
int c[2][2][1];
unordered_set<int> tag;
//set<int> used;
friend Node operator+(const Node& a, const Node& b)
{
if (a.c[0][0][0] == -1) return b;
if (b.c[0][0][0] == -1) return a;
Node c;
c.tag.clear();
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 1; k++) c.c[i][j][k] = b.c[i][j][k] + a.c[i][j][k];
}
}
return c;
}
}tr[N << 2];
void pushup(int u)
{
tr[u] = tr[u << 1] + tr[u << 1 | 1];
}
void pushdown(int u)
{
auto& rt = tr[u], & l = tr[u << 1], & r = tr[u << 1 | 1];
for (int i : rt.tag)
{
if (i == 0)
{
if (l.tag.count(i) == 0)
{
l.tag.insert(i);
l.c[1][0][0] += l.c[0][0][0];
//l.c[1][0][1] += l.c[0][0][1];
l.c[1][1][0] += l.c[0][1][0];
//l.c[1][1][1] += l.c[0][1][1];
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 1; k++) l.c[0][j][k] = 0;
}
}
if (r.tag.count(i) == 0)
{
r.tag.insert(i);
r.c[1][0][0] += r.c[0][0][0];
//r.c[1][0][1] += r.c[0][0][1];
r.c[1][1][0] += r.c[0][1][0];
//r.c[1][1][1] += r.c[0][1][1];
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 1; k++) r.c[0][j][k] = 0;
}
}
}
else if (i == 1)
{
if (l.tag.count(i) == 0)
{
l.tag.insert(i);
l.c[0][1][0] += l.c[0][0][0];
//l.c[0][1][1] += l.c[0][0][1];
l.c[1][1][0] += l.c[1][0][0];
//l.c[1][1][1] += l.c[1][0][1];
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 1; k++) l.c[j][0][k] = 0;
}
}
if (r.tag.count(i) == 0)
{
r.tag.insert(i);
r.c[0][1][0] += r.c[0][0][0];
//r.c[0][1][1] += r.c[0][0][1];
r.c[1][1][0] += r.c[1][0][0];
//r.c[1][1][1] += r.c[1][0][1];
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 1; k++) r.c[j][0][k] = 0;
}
}
}
else if (i == 2)
{
l.tag.insert(2);
r.tag.insert(2);
//printf("!\n");
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++) l.c[j][k][0] = 0;
}
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++) r.c[j][k][0] = 0;
}
}
}
rt.tag.clear();
}
void build(int u, int l, int r)
{
tr[u].tag.clear();
tr[u].c[0][0][0] = 1;
if (l == r) return;
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
void update(int u, int lx, int rx, int x, int l, int r)
{
if (tr[u].tag.count(x)) return;
if (l >= lx and r <= rx)
{
tr[u].tag.insert(x);
auto& l = tr[u];
if (x == 0)
{
l.c[1][0][0] += l.c[0][0][0];
//l.c[1][0][1] += l.c[0][0][1];
l.c[1][1][0] += l.c[0][1][0];
//l.c[1][1][1] += l.c[0][1][1];
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 1; k++) l.c[0][j][k] = 0;
}
}
else if (x == 1)
{
l.c[0][1][0] += l.c[0][0][0];
//l.c[0][1][1] += l.c[0][0][1];
l.c[1][1][0] += l.c[1][0][0];
//l.c[1][1][1] += l.c[1][0][1];
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 1; k++) l.c[j][0][k] = 0;
}
}
else
{
l.tag.insert(2);
//printf("!\n");
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++) l.c[j][k][0] = 0;
}
}
return;
}
pushdown(u);
int mid = l + r >> 1;
if (lx <= mid) update(u << 1, lx, rx, x, l, mid);
if (rx > mid) update(u << 1 | 1, lx, rx, x, mid + 1, r);
pushup(u);
}
Node query(int u, int l, int r, int nl, int nr)
{
if (nl >= l and nr <= r) return tr[u];
pushdown(u);
int mid = nl + nr >> 1;
Node res;
res.c[0][0][0] = -1;
if (l <= mid) res = query(u << 1, l, r, nl, mid);
if (r > mid) res = res + query(u << 1 | 1, l, r, mid + 1, nr);
return res;
}
}tr;
int n, m;
int main()
{
scanf("%d%d", &n, &m);
tr.build(1, 1, n);
while (m--)
{
int l, r, x;
scanf("%d%d%d", &l, &r, &x);
x--;
tr.update(1, l, r, x, 1, n);
}
printf("%d\n", tr.query(1, 1, n, 1, n).c[1][1][0]);
//tr.print(1, 1, n);
return 0;
}
分类:
题解
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现