线段树的每个点表示当前点的前驱,即这个颜色上一次出现的位置,这个玩意multiset随便写写就完了。

重要的是怎么查询答案,无解显然先判掉。

线段树上二分就可以了

#include <bits/stdc++.h>

using namespace std;
int read() {
int x = 0;
char c = getchar();
while (c < 48) c = getchar();
while (c > 47) x = x * 10 + (c - 48), c = getchar();
return x;
} int min(int x, int y) { return x < y ? x : y; }
int max(int x, int y) { return x > y ? x : y; } int n, k, q;
const int maxn = 3e5 + 53;
const int maxm = 1e7; multiset<int> st[maxn];
int ans[maxn]; struct node {
int x, t, id, type;
bool operator<(const node& other) const {
if (t != other.t) return t < other.t;
return type < other.type;
}
} t[maxn << 2]; int R;
int rt, cnt = 0;
int ls[maxm], rs[maxm], mn[maxm];
multiset<int> ms[maxm];
void build(int& p, int l, int r) {
p = ++cnt;
if (l == r) {
for (int i = 1; i <= k; i++) ms[p].insert(0);
return;
}
int mid = l + r >> 1;
build(rs[p], mid + 1, r);
} void modify(int& p, int l, int r, const int& x, const int& inc, const int& del) {
if (!p) p = ++cnt;
if (l == r) {
if (inc >= 0) ms[p].insert(inc);
if (del >= 0) ms[p].erase(ms[p].find(del));
mn[p] = ((ms[p].size()) ? *ms[p].begin() : R);
return;
}
int mid = l + r >> 1;
if (x <= mid)
modify(ls[p], l, mid, x, inc, del);
else
modify(rs[p], mid + 1, r, x, inc, del);
mn[p] = min(mn[ls[p]], mn[rs[p]]);
} int qry(int x) {
int l = 1, r = R;
int p = rt, chk = x << 1, tmn = R;
while (l < r) {
int mid = l + r >> 1, d = min(tmn, mn[rs[p]]);
if (mid < x || d + mid < chk || d < 1)
l = mid + 1, p = rs[p];
else
tmn = d, r = mid, p = ls[p];
}
return l - x;
} int main() {
// freopen("testdata.in", "r", stdin);
n = read(), k = read(), q = read();
int cnt = 0;
for (int i = 1; i <= n; i++) {
int x = read(), id = read(), a = read(), b = read();
R = max(R, x);
t[++cnt] = { x, a, id, 0 }, t[++cnt] = { x, b + 1, id, 1 };
}
for (int i = 1; i <= q; i++) {
int x = read(), time = read();
t[++cnt] = { x, time, i, 2 };
} R = R << 1 | 1, build(rt, 1, R), mn[0] = R;
for (int i = 1; i <= k; i++) st[i].insert(0), st[i].insert(R); sort(t + 1, t + cnt + 1);
int tot = 0;
for (int i = 1; i <= cnt; i++) {
int x = t[i].x, id = t[i].id;
if (t[i].type == 0) {
auto it = st[id].lower_bound(x);
auto it2 = it;
it2--;
modify(rt, 1, R, x, *it2, -1);
modify(rt, 1, R, *it, x, *it2);
if (st[id].size() == 2) ++tot;
st[id].insert(x);
}
if (t[i].type == 1) {
st[id].erase(st[id].find(x));
auto it = st[id].lower_bound(x);
auto it2 = it;
it2--;
modify(rt, 1, R, x, -1, *it2);
modify(rt, 1, R, *it, *it2, x);
if (st[id].size() == 2) --tot;
}
if (t[i].type == 2) {
if (tot == k)
ans[id] = qry(x);
else
ans[id] = -1;
}
}
for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
return 0;
}

最新文章

  1. C#中的匿名方法
  2. [Node.js] 使用TypeScript编写Node项目
  3. Goldengate进程的拆分与合并
  4. Message Forwarding
  5. web开发基础(同步更新中)
  6. Unity 用C#脚本读取JSON文件数据
  7. ubuntu下tcpdump使用
  8. C#版-百度网盘API的实现(二)
  9. CodeForces 22C System Administrator
  10. (简单) POJ 2406 Power Strings,扩展KMP。
  11. java 基础知识九 类与对象
  12. PAT1028. List Sorting (25)---strcmp
  13. 项目Beta冲刺Day5
  14. POM文件分析记
  15. bootstrap全局样式二
  16. iOS-获取当前时间的年、月、日、时、分、秒
  17. 嵌入式开发之hi3519---PCIE DMA
  18. oracle a:=100 和 b=:c 区别
  19. 安装Redis的PHP扩展
  20. 19.Mysql优化数据库对象

热门文章

  1. String字符串性能优化的几种方案
  2. Gradle | Gradle项目无法导入依赖包
  3. 请注意安全!你的mongodb已经被黑了!互联网安全生产大过天!
  4. pyinstaller 还原python代码的方法
  5. MYSQL 常用语句与函数命令
  6. JSP&amp;Servlet学习笔记----第3章
  7. JAVA String对象和字符串常量的关系解析
  8. 《Android Studio实战 快速、高效地构建Android应用》--五、备忘录实验(1/2)
  9. Airtest自动化测试工具介绍
  10. JFrame的BorderLayout