有编号为1~T的T种颜色和一块长为L的色板,每块色板只有一个颜色,最初均为颜色1,有O次操作:
- C x y z,将区间[x,y]的色板涂成颜色z。
- P x y,询问区间[x,y]有多少种不同的颜色。
范围:1<=L<=1e5, 1<=T<=30, 1<=O<=1e5。
分析:线段树维护区间内有哪些颜色,因为颜色种数少,可以用状压或者bitset。
#include <bits/stdc++.h>
using llong = long long;
template<class Info, class Tag>
struct LazySegmentTree {
int n;
std::vector<Info> info;
std::vector<Tag> tag;
LazySegmentTree():n(0) {}
LazySegmentTree(int _n, Info v=Info()) {
init(_n, v);
}
template<class T>
LazySegmentTree(std::vector<T> v) {
init(v);
}
void init(int _n, Info v=Info()) {
init(std::vector<Info>(_n, v));
}
template<class T>
void init(std::vector<T> v) {
n = v.size();
info.assign(4 << std::__lg(n), Info());
tag.assign(4 << std::__lg(n), Tag());
std::function<void(int,int,int)> build = [&](int x, int l, int r) {
if (l + 1 == r) {
info[x] = v[l];
return;
}
int m = (l + r) / 2;
build(2*x+1, l, m);
build(2*x+2, m, r);
pushup(x);
};
build(0, 0, n);
}
void pushup(int x) {
info[x] = info[2*x+1] + info[2*x+2];
}
void apply(int x, const Tag &t) {
info[x].apply(t);
tag[x].apply(t);
}
void pushdown(int x) {
apply(2*x+1, tag[x]);
apply(2*x+2, tag[x]);
tag[x] = Tag();
}
void assign(int x, int l, int r, int p, const Info &v) {
if (l + 1 == r) {
info[x] = v;
return;
}
int m = (l + r) / 2;
pushdown(x);
if (p < m) {
assign(2*x+1, l, m, p, v);
} else {
assign(2*x+2, m, r, p, v);
}
pushup(x);
}
void assign(int p, const Info &v) {
assign(0, 0, n, p, v);
}
void add(int x, int l, int r, int p, const Info &v) {
if (l + 1 == r) {
info[x] += v;
return;
}
int m = (l + r) / 2;
pushdown(x);
if (p < m) {
add(2*x+1, l, m, p, v);
} else {
add(2*x+2, m, r, p, v);
}
pushup(x);
}
void add(int p, const Info &v) {
add(0, 0, n, p, v);
}
Info rangeQuery(int x, int l, int r, int L, int R) {
if (R <= l || r <= L) {
return Info();
}
if (L <= l && r <= R) {
return info[x];
}
int m = (l + r) / 2;
pushdown(x);
return rangeQuery(2*x+1, l, m, L, R) + rangeQuery(2*x+2, m, r, L, R);
}
Info rangeQuery(int L, int R) {
return rangeQuery(0, 0, n, L, R);
}
void rangeApply(int x, int l, int r, int L, int R, const Tag &t) {
if (R <= l || r <= L) {
return;
}
if (L <= l && r <= R) {
apply(x, t);
return;
}
int m = (l + r) / 2;
pushdown(x);
rangeApply(2*x+1, l, m, L, R, t);
rangeApply(2*x+2, m, r, L, R, t);
pushup(x);
}
void rangeApply(int L, int R, const Tag &t) {
return rangeApply(0, 0, n, L, R, t);
}
template<class F>
int findFirst(int x, int l, int r, int L, int R, F && pred) {
if (R <= l || r <= L) {
return -1;
}
if (L <= l && r <= R && !pred(info[x])) {
return -1;
}
if (l + 1 == r) {
return l;
}
int m = (l + r) / 2;
pushdown(x);
int res = findFirst(2*x+1, l, m, L, R, pred);
if (res == -1) {
res = findFirst(2*x+2, m, r, L, R, pred);
}
return res;
}
template<class F>
int findFirst(int L, int R, F &&pred) {
return findFirst(0, 0, n, L, R, pred);
}
template<class F>
int findLast(int x, int l, int r, int L, int R, F &&pred) {
if (R <= l || r <= L) {
return -1;
}
if (L <= l && r <= R && !pred(info[x])) {
return -1;
}
if (l + 1 == r) {
return l;
}
int m = (l + r) / 2;
pushdown(x);
int res = findLast(2*x+2, m, r, L, R, pred);
if (res == -1) {
res = findLast(2*x+1, l, m, L, R, pred);
}
return res;
}
template<class F>
int findLast(int L, int R, F &&pred) {
return findLast(0, 0, n, L, R, pred);
}
void print(int x, int l, int r) {
std::cerr << x << "(" << l << "," << r << ") " << info[x] << " " << tag[x] << "\n";
if (l + 1 < r) {
int m = (l + r) / 2;
print(2*x+1, l, m);
print(2*x+2, m, r);
}
}
void print() {
std::cerr << "-----------------------------\n";
print(0, 0, n);
}
};
struct Tag {
int c;
Tag(int C=0):c(C) {}
void apply(Tag t) {
if (t.c) {
c = t.c;
}
}
friend std::ostream& operator<<(std::ostream &out, Tag &tag) {
out << "tag:(" << tag.c << ")";
return out;
}
};
struct Info {
std::bitset<32> color;
Info() {}
void apply(Tag t) {
if (t.c) {
color.reset();
color.set(t.c);
}
}
friend Info operator+(const Info &a, const Info &b) {
Info ans;
ans.color = a.color | b.color;
return ans;
}
friend std::ostream& operator<<(std::ostream &out, Info &info) {
out << "info:(" << info.color.to_string() << ")";
return out;
}
};
void solve() {
int L, T, O;
std::cin >> L >> T >> O;
std::vector<Info> A(L);
for (int i = 0; i < L; i++) {
A[i].color.set(1);
}
LazySegmentTree<Info,Tag> tr(A);
for (int i = 0; i < O; i++) {
std::string op;
int x, y, z;
std::cin >> op >> x >> y;
if (x > y) std::swap(x, y);
if (op == "C") {
std::cin >> z;
tr.rangeApply(x-1, y, Tag(z));
} else if (op == "P") {
std::cout << tr.rangeQuery(x-1, y).color.count() << "\n";
}
}
}
int main() {
std::cin.tie(0)->sync_with_stdio(0);
int t = 1;
while (t--) solve();
return 0;
}
标签:Info,std,info,return,游戏,int,void,色板,lgP1558
From: https://www.cnblogs.com/chenfy27/p/18301497