题意:给定一个线段数组,含有不同颜色的线段若干,要求出每个线段附近最近不同颜色的线段的距离

思路:很显然的模拟题,对于每条线段我们可以发现一个性质,对于每一个线条来说,线条的段点和线条的端点是相同的。我们将线段之间的距离分为两种情况,一种是距离为0的情况,一种是距离不为0的情况。于是想到线段树和multiset神奇的数据结构存储数据。

于是模拟一下就行了,注意枚举删去本身颜色影响时,枚举的是颜色,防止多次重复删除添加颜色退化时间复杂度。multiset的特殊删除方式也需要注意(二分迭代器删除)

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<bitset>
#include<cmath>
#include<array>
#include<atomic>
#include<sstream>
#include<stack>
#include<iomanip>
//#include<bits/stdc++.h>//#define int ll
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(0);
#define pb push_back
#define endl '\n'
#define x first
#define y second
#define Endl endl
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,b,a) for(int i=b;i>=a;i--)
#define si(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define ss(x) scanf("%s", x);
#define YES {puts("YES");return;}
#define NO {puts("NO"); return;}
#define all(x) x.begin(),x.end()using namespace std;typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<char, int> PCI;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
const int N = 1000010, M = 2 * N, B = N, MOD = 998244353;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;//int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
int dx[8] = { 1,2,2,1,-1,-2,-2,-1 }, dy[8] = { 2,1,-1,-2,-2,-1,1,2 };
int n, m, k;
struct Segment {int l, r, c;
}seg[N];
struct Node {int l, r;ll sum;int add;
}tr[N*4];
map<int, int> mapp;
int idx;
vector<int> col[N];
int ans[N];ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lowbit(ll x) { return x & -x; }
ll qmi(ll a, ll b, ll MOD) {ll res = 1;while (b) {if (b & 1) res = res * a % MOD;a = a * a % MOD;b >>= 1;}return res;
}inline void init() {}int get(int x)
{if (mapp.count(x))return mapp[x];else return mapp[x] = ++idx;
}void push_down(Node& p, Node& l, Node& r)
{if (p.add){l.sum += p.add * (l.r - l.l + 1);l.add += p.add;r.sum += p.add * (r.r - r.l + 1);r.add += p.add;p.add = 0;}
}void push_down(int u)
{push_down(tr[u], tr[u << 1], tr[u << 1 | 1]);
}void push_up(int u)
{tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum;
}void build(int l, int r, int u = 1)
{tr[u] = { l,r };if (l == r) {tr[u].sum = tr[u].add = 0;return;}int mid = tr[u].l + tr[u].r >> 1;build(l, mid, u << 1); build(mid + 1, r, u << 1 | 1);push_up(u);
}void modify(int l, int r, int c, int u = 1)
{if (tr[u].l >= l && tr[u].r <= r){tr[u].sum += (tr[u].r - tr[u].l + 1) * c;tr[u].add += c;return;}else {push_down(u);int mid = tr[u].l + tr[u].r >> 1;if (r <= mid)modify(l, r, c, u << 1);else if (l > mid)modify(l, r, c, u << 1 | 1);else {modify(l, mid, c, u << 1);modify(mid + 1, r, c, u << 1 | 1);}push_up(u);}
}ll query(int l, int r, int u = 1) {if (tr[u].l >= l && tr[u].r <= r)return tr[u].sum;else {push_down(u);int mid = tr[u].l + tr[u].r >> 1;ll res = 0;if (r <= mid)return res = query(l, r, u << 1);else if (l > mid) return res = query(l, r, u << 1 | 1);else {ll left = query(l, mid, u << 1);ll right = query(mid + 1, r, u << 1 | 1);return res = left + right;}}
}void slove()
{cin >> n;multiset<int> le, ri;vector<int> ves;idx = 0;mapp.clear();for (int i = 1; i <= n; i++) col[i].clear();pre(i, 1, n){scanf("%d%d%d", &seg[i].l, &seg[i].r, &seg[i].c);ves.push_back(seg[i].l); ves.push_back(seg[i].r);le.insert(seg[i].l); ri.insert(seg[i].r);col[seg[i].c].push_back(i);}sort(all(ves));ves.erase(unique(all(ves)), ves.end());for (int i = 0; i < ves.size(); i++)get(ves[i]);build(1, idx);for (int i = 1; i <= n; i++)modify(mapp[seg[i].l], mapp[seg[i].r], 1);for (int i = 1; i <= n; i++) ans[i] = INF;for (int color = 1; color <= n; color++) {for (int& i : col[color]) {auto [l, r, c] = seg[i];modify(mapp[l], mapp[r], -1);le.erase(le.lower_bound(l)), ri.erase(ri.lower_bound(r));}for (int& i : col[color]){auto [l, r, c] = seg[i];int res = query(mapp[l], mapp[r]);if (res) { ans[i] = 0; continue; }else {auto it = ri.lower_bound(l);if (it != ri.begin()) {it = prev(it);ans[i] = min(ans[i], l - *it);}it = le.upper_bound(r);if(it!=le.end())ans[i] = min(ans[i], *it - r);}}for (int& i : col[color]) {auto [l, r, c] = seg[i];modify(mapp[l], mapp[r], 1);le.insert(l), ri.insert(r);}}for (int i = 1; i <= n; i++)printf("%d ", ans[i]);puts("");
}signed main()
{//IOS;int _ = 1;si(_);init();while (_--){slove();}return 0;
}
/*
8
abccdbad*/

F. Multi-Colored Segments相关推荐

  1. 新冠患者样本单细胞测序文献汇总

    科研工作者的信仰就是将真相大白于天下 NGS系列文章包括NGS基础.转录组分析 (Nature重磅综述|关于RNA-seq你想知道的全在这).ChIP-seq分析 (ChIP-seq基本分析流程).单 ...

  2. Check It Again:论文整理

    蕴含简介 文本蕴含:文本间的推理关系,又称为文本蕴含关系,作为一种基本的文本间语义联系,广泛存在于自然语言文本中.简单的来说文本蕴含关系描述的是两个文本之间的推理关系,其中一个文本作为前提,另一个文本 ...

  3. html5图片墙,超炫酷。

    html5图片墙,可以跟着鼠标滚动. 一个css文件 两个JavaScript文件 一个html5文件 图片可以自己设置,放在images里 css代码 /* hardware accelatator ...

  4. 整理了 65 个 Matplotlib 案例,这能不收藏?

    作者|周萝卜 来源|萝卜大杂烩 Matplotlib 作为 Python 家族当中最为著名的画图工具,基本的操作还是要掌握的,今天就来分享一波 文章很长,高低要忍一下,如果忍不了,那就收藏吧,总会用到 ...

  5. mSystems:干旱对土壤微生物组的影响

    文章目录 干旱对土壤微生物组的影响 摘要 重要性 主要结果 图1. 采样地图 图2. 平均土壤相对湿度与alpha多样性 图3. 未加权的UniFrac PCoA图 表1. 最佳环境因子组合 图4. ...

  6. uploadify初体验

    js调用 $("#fileInput1").uploadify({'uploader':'images/uploadify.swf','script':'uploadfile.as ...

  7. ZOJ 1610 Count the Colors

    段树:延迟标志+暴力更新 我记得刚学段树做的时候这个话题WA一个版本.....如今,每分钟获得.... Count the Colors Time Limit: 2 Seconds      Memo ...

  8. S3C6410 硬件加速功能

    转自:http://blog.chinaunix.net/uid-22028566-id-2983719.html 三星的微处理器S3C6410,基于ARM11核心,主要针对的就是低功耗超便携产品,如 ...

  9. java实现附件预览(openoffice+swftools+flexpaper)

    先附上本人参考的文章,基于的 flexpaper版本 为 1.5,本人由于使用的是 2.1.9 ,故之后说明: 已经支持加载中文文件名 代码下载 1.概述 主要原理 1.通过第三方工具openoffi ...

  10. Pathview包:整合表达谱数据可视化KEGG通路

    Pathview是一个用于整合表达谱数据并用于可视化KEGG通路的一个R包,其会先下载KEGG官网上的通路图,然后整合输入数据对通路图进行再次渲染,从而对KEGG通路图进行一定程度上的个性化处理,并且 ...

最新文章

  1. KindEditor自动过滤首行缩进和全角空格的解决方法
  2. 一步一步学习C#(一)
  3. traceback异常打印
  4. QLibrary 动态加载外部库文件
  5. java类同步,Java同步工具類(一)
  6. 免费直播丨企服 9 大标杆规模化获客模型解读,制胜企服 3.0 时代
  7. csdn编辑器模板2
  8. 聊一聊声明式接口调用与Nacos的结合使用
  9. 为什么不可以使用哈曼顿距离_K-means真的不能使用曼哈顿距离吗?
  10. 当下,所有工程师都要具备技术变现思考和能力【赢在CSDN】
  11. ECharts 经常会修改到的一些样式配置
  12. win7关机快捷键_win7电脑键盘关机的操作方法
  13. SAP License:SAP不便解决的问题之七——权限问题
  14. XX项目技术架构模板
  15. No serializer found for class
  16. c语言usb串口通信程序,C语言在RS232串行接口通信中的实现
  17. oracle对同义词修改数据,对 Oracle Database 中的同义词的操作 - BizTalk Server | Microsoft Docs...
  18. 收盘涨幅大于1.5%小于5%的选股公式
  19. python你TM太皮了——区区30行代码就能记录键盘的一举一动
  20. Seaweed-FS综合使用测试

热门文章

  1. el转义html标签,vue 中el-table转义后台字符
  2. 叠氮-三聚乙二醇-琥珀酰亚胺1245718-89-1Azido-PEG3-NHS ester
  3. python变量赋值方式_python中变量的命令规制及变量的赋值方式
  4. ai描边工具怎么打开_AI的工具栏怎么用快捷键打开?
  5. Springboot快速开发-书本信息管理系统(项目源码)
  6. 一汽启明的PDM解决方案_三木_新浪博客
  7. 剑网三重置版服务器维护,《剑网3:指尖江湖》新人须知,端游重制版是道伤疤,请勿论...
  8. Vue + Spring Boot 项目实战(二十一):缓存的应用
  9. Creator新手引导 | 限制只能点击一个按钮 | 文字打字机效果
  10. JAVA程序设计:灯泡开关(LeetCode:319)