• 传送门
  • 题目大意
  • 思路
  • 参考代码
  • 总结

时光如梭,Codeforces 的题号还是三位数的时代已经过去;量子语言原来已经来临。

传送门
题目大意

给定一个长度为 n n n 的序列 a" role="presentation">aaa,有 m m m 个询问,每次询问给定一个区间 [l,r]" role="presentation">[l,r][l,r][l,r],如果这个区间里存在恰好只出现一次的数,输出这个数(如果有多个就任意输出一个),否则输出 0 0 0。

n,m≤5×105" role="presentation">n,m≤5×105n,m≤5×105n, m \le 5 \times 10^5, ai≤105 a i ≤ 10 5 a_i \le 10^5。

思路

还好题解是英文的看不懂啊哈哈哈。

首先题解中有三个类型:主席树,线段树,基于莫队的算法(Mo-based algorithm,出题人不是中国的,真神仙),然后我瞄了第一句话:如果所有询问右端点一样……然后我就自己想了……

不妨求出每个位置下一个与它相同的数所在的位置, O(n) O ( n ) O(n) 扫描即可,我们用一个从前往后的箭头表示这个东西。发现,如果区间内存在恰好只出现一次的数,那么这个数必须满足两个条件:指向它的箭头不存在或者起点在左端点外;它指向其它位置的箭头在右端点外(如果没有,不妨令这个箭头指向 n+1 n + 1 n + 1)。

假设一个区间内所有位置都不存在指向它的箭头,怎么判断这个区间是否有解呢?直接取这个区间内指向别的地方的位置的最大值,拿它和右端点比较即可,这个可以用线段树维护。现在有些位置有箭头指向它,换句话说,有些位置在求最大值时不能算进对答案的贡献中。

考虑把询问按左端点离线,从左到右处理。对于有箭头指向它的位置,我们让它的箭头指向的位置减去一个极大值,然后在区间中取最大值即可。当处理完一个左端点后,如果左端点指向了某个位置,我们就把那个位置指向的位置加上之前减去的那个极大值,这样就能正确统计答案了。用线段树维护,时间复杂度 O(nlogn) O ( n log ⁡ n ) O(n \log n)。

参考代码
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cassert>
#include <cctype>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <list>
#include <functional>
typedef long long LL;
typedef unsigned long long ULL;
using std::cin;
using std::cout;
using std::endl;
typedef int INT_PUT;
INT_PUT readIn()
{INT_PUT a = 0; bool positive = true;char ch = getchar();while (!(ch == '-' || std::isdigit(ch))) ch = getchar();if (ch == '-') { positive = false; ch = getchar(); }while (std::isdigit(ch)) { a = a * 10 - (ch - '0'); ch = getchar(); }return positive ? -a : a;
}
void printOut(INT_PUT x)
{char buffer[20]; int length = 0;if (x < 0) putchar('-'); else x = -x;do buffer[length++] = -(x % 10) + '0'; while (x /= 10);do putchar(buffer[--length]); while (length);putchar('\n');
}const int INF = (~(int(1) << (sizeof(int) * 8 - 1))) >> 1;
const int maxn = int(5e5) + 5;
int n, m;
int a[maxn];
struct Query
{int l, r;void read(){l = readIn();r = readIn();}
} querys[maxn];
std::vector<int> offline[maxn];int appear[maxn];
int next[maxn];
bool pointer[maxn];
class SegTree
{static inline int code(int l, int r){return (l + r) | (l != r);}struct Node{int maxVal;int maxIdx;Node() : maxVal(-INF - 1) {}bool operator<(const Node& b) const{return maxVal < b.maxVal;}} nodes[maxn * 2];int g_Pos, g_Val, g_L, g_R;void update(int l, int r){Node& t = nodes[code(l, r)];int mid = (l + r) >> 1;Node& lc = nodes[code(l, mid)];Node& rc = nodes[code(mid + 1, r)];if (lc.maxVal >= rc.maxVal)t = lc;elset = rc;}void add_(int l, int r){if (l == r){nodes[code(l, r)].maxVal += g_Val;return;}int mid = (l + r) >> 1;if (g_Pos <= mid) add_(l, mid);else add_(mid + 1, r);update(l, r);}Node query_(int l, int r){if (g_L <= l && r <= g_R){return nodes[code(l, r)];}int mid = (l + r) >> 1;Node ret;if (g_L <= mid)ret = std::max(ret, query_(l, mid));if (g_R > mid)ret = std::max(ret, query_(mid + 1, r));return ret;}public:void build(int l, int r){if (l == r){Node& t = nodes[code(l, r)];t.maxVal = -INF + next[l];t.maxIdx = a[l];return;}int mid = (l + r) >> 1;build(l, mid);build(mid + 1, r);update(l, r);}void add(int pos, int val){g_Pos = pos;g_Val = val;add_(1, n);}int query(int l, int r){g_L = l;g_R = r;Node t = query_(1, n);if (t.maxVal > r)return t.maxIdx;elsereturn 0;}
} st;int ans[maxn];void run()
{n = readIn();for (int i = 1; i <= n; i++)a[i] = readIn();m = readIn();for (int i = 1; i <= m; i++)querys[i].read();for (int i = 1; i <= m; i++)offline[querys[i].l].push_back(i);for (int i = n; i; i--){next[i] = appear[a[i]];if (!next[i]) next[i] = n + 1;appear[a[i]] = i;}for (int i = 1; i <= n; i++)if (next[i])pointer[next[i]] = true;st.build(1, n);for (int i = 1; i <= n; i++)if (!pointer[i])st.add(i, INF);for (int l = 1; l <= n; l++){for (int i = 0; i < offline[l].size(); i++){int idx = offline[l][i];int r = querys[idx].r;ans[idx] = st.query(l, r);}if (next[l] <= n)st.add(next[l], INF);}for (int i = 1; i <= m; i++)printOut(ans[i]);
}int main()
{run();return 0;
}
总结

要是一眼题解都不看就会做就好了。唉,谁叫我太弱了呢 QAQ。

CF 1000F One Occurrence相关推荐

  1. 26.CF1000F One Occurrence

    26.CF1000F One Occurrence 个人Limitの线段树题单题解主目录:Limitの线段树题单 题解目录_HeartFireY的博客-CSDN博客 给定序列,要求支持查询区间内只出现 ...

  2. 『参考』.net CF组件编程(4)——为自定义组件添加工具箱图标!

    前言: 在前三篇的文章中,和大家一起创建了一个用于TCP连接检测的小组件,如果你记不得了,可以通过以下链接去回顾一下: 『参考』.net CF组件编程(1)--基础之后 『参考』.net CF组件编程 ...

  3. OC对象 vs CF对象

    2019独角兽企业重金招聘Python工程师标准>>> OC对象 vs CF对象 在ARC场景下,对象所有权没有转换 使用__bridge关键字即可实现CF对象和OC对象之间的自由转 ...

  4. CF 990A. Commentary Boxes【数学/模拟】

    [链接]:CF [题意]:对于一个数n,每次加一的代价是a,每次减一的代价是b,求被m整除时的最小代价. [分析]:分情况讨论,自己多举几个栗子. [代码]: #include<cstdio&g ...

  5. 推荐算法——基于协同过滤CF

    https://www.toutiao.com/a6643326861214482957/ 2019-01-06 18:21:09 前边我们已经介绍了推荐算法里的基于内容的推荐算法CB,今天我们来介绍 ...

  6. 索引贴——移动开发(.Net CF 停止更新)

    这是关于本人博客的技术索引贴,希望能方便的让您阅读到相关技术文章--不断更新中.一整理才发现,好多啊,哈哈- 一..Net CF技巧:搜集.转载一些和CF开发相关的辅助文章,比较适合初学者.开发入门者 ...

  7. 解答:CF截图保存在哪

    为什么80%的码农都做不了架构师?>>>    大家玩CF(穿越火线)的时候遇到精彩的画面总希望截图保存下来,然而有些游戏玩家截图后却不知道CF截图保存在哪!这不得不说是个悲剧,但是 ...

  8. CF里面的资源载入问题

    前一段时间已经发现CF在载入资源的时候会怪怪的,但是这一段时间都不曾记起要对这个问题研究一下.最近又发现这个问题了,实在是恼火.俗话说择日不如撞日(粤语),唉,就今天啦.这个问题是在VS2k5里面调试 ...

  9. [CF.Skills]播放嵌入资源的声音文件

    [CF.Skills]播放嵌入资源的声音文件 摘要:本文阐述了在Windows Mobile中如何播放潜入资源的声音文件KeywordsPlaySound, Windows Mobile, Embed ...

最新文章

  1. 1命名规则 sentinel_Spring Cloud Alibaba 整合 Sentinel 流控
  2. ASP.NET 2.0 AJAX中Webservice调用方法示例
  3. IDEA报错解决:Error:(33, 35) java: -source 7 中不支持 lambda 表达式 (请使用 -source 8 或更高版本以启用 lambda 表达式)
  4. 后台ajax调用中字符串到jquery中的json对象和数组对象转换问题
  5. mongo的php查询,如何在php中查询mongo?
  6. python requests请求失败重试_Python Requests.post()请求失败时的retry设置
  7. ubuntu20.04安装讯飞输入法(失败经历)
  8. AS(Autonomous System)
  9. 打印1-400以内 能同时被5和9 整数的数将这些数放入一个列表中,再输出这个列表
  10. php rinit,PHP执行原理
  11. 【树莓派】Linux指令使用记录
  12. 梦幻星球社区APP源码 HYBBS的iApp社区源码
  13. “你需要TrustedInstaller提供的权限才能对此文件进行更改” 解决方案
  14. 将白色背景图片变透明
  15. 疫情期间再读三体(3)——黑暗森林到底怎么个黑法
  16. swiper滑动切换变换样式,实时显示当前索引
  17. 商城小程序通过交易组件实现直播带货
  18. 爬取偶像/私房小姐姐图片--爬虫基础篇
  19. excel空白处自动填充内容怎么操作,excel空白处填充0或上行方法
  20. 漫画:5G 到底是个什么玩意儿?

热门文章

  1. Chemical Science | Img2Mol+: accurate SMILES recognition from molecular graphical depictions
  2. Django:TypeError: __init__() missing 1 required positional argument: 'on_delete'
  3. mysql左联和右联_MySQL联合查询语法内联、左联、右联、全联
  4. CentOS 6 nagios安装与监控
  5. 腾讯(来自百度百科)
  6. C++ int类型数据除法向上取整
  7. 进医院就有活有死!这标题真吓人!
  8. 如何进行微信公众平台的后台开发
  9. 战团服务器文件,战团导入导出修改文件
  10. 移动电子商务金融特点