HDU 4747 Mex(线段树)(2013 ACM/ICPC Asia Regional Hangzhou Online)
Consider a sequence of non-negative integers {ai}, we define mex(L,R) as the least non-negative integer which is not appeared in the continuous subsequence from aL to aR, inclusive. Now we want to calculate the sum of mex(L,R) for all 1 <= L <= R <= n.
For each test case, the first line contains one integer n, denoting the length of sequence.
The next line contains n non-integers separated by space, denoting the sequence.
(1 <= n <= 200000, 0 <= ai <= 10^9)
The input ends with n = 0.
然后只有一个0在位置3
那么就可以确定mex[1,1]和mex[1,2]为0了,mex[1,i],i≥3都至少为1,但还不知道他们会不会大于1,所以s[1] = n - 2
然后只有一个1在位置7
那么就可以确定mex[1,i],3≤i<7,都为1(都只有0没有1),而i≥7的mex[1,i]都至少为2(他们都含有0和1),所以s[1] = n - 7
s[i]就是从i开始的mex[i,x]在第 p 阶段还没有确定值只知道有s[i]个mex[i,x]至少大于p
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL; #define ll x * 2
#define rr x * 2 + 1 const int MAXN = ; LL tree[MAXN * ];
int maxt[MAXN * ], mint[MAXN * ];
int a[MAXN], n; int head[MAXN], lcnt;
int pos[MAXN], next[MAXN]; void init() {
memset(head, , (n + ) * sizeof(int));
lcnt = ;
} void add_link(int x, int i) {
pos[lcnt] = i; next[lcnt] = head[x]; head[x] = lcnt++;
} void build(int x, int left, int right) {
if(left == right) tree[x] = maxt[x] = mint[x] = n - left + ;
else {
int mid = (left + right) >> ;
if(left <= mid) build(ll, left, mid);
if(mid < right) build(rr, mid + , right);
tree[x] = tree[ll] + tree[rr];
maxt[x] = max(maxt[ll], maxt[rr]);
mint[x] = min(mint[ll], mint[rr]);
}
} void update(int x, int left, int right, int a, int b, int val) {
if(a <= left && right <= b && mint[x] >= val) {
tree[x] = LL(val) * (right - left + );
maxt[x] = mint[x] = val;
}
else {
if(right == left) return ;
int mid = (left + right) >> ;
if(maxt[x] == mint[x]) {
maxt[ll] = mint[ll] = maxt[x];
tree[ll] = LL(mid - left + ) * maxt[x];
maxt[rr] = mint[rr] = maxt[x];
tree[rr] = LL(right - (mid + ) + ) * maxt[x];
}
if(a <= mid && maxt[ll] > val) update(ll, left, mid, a, b, val);
if(mid < b && maxt[rr] > val) update(rr, mid + , right, a, b, val);
tree[x] = tree[ll] + tree[rr];
maxt[x] = max(maxt[ll], maxt[rr]);
mint[x] = min(mint[ll], mint[rr]);
}
} LL solve() {
LL ret = ;
build(, , n);
for(int i = ; i <= n && tree[]; ++i) {
int last = ;
for(int p = head[i]; p; p = next[p]) {
update(, , n, last + , pos[p], n - pos[p] + );
last = pos[p];
}
update(, , n, last + , n, );
ret += tree[];
}
return ret;
} int main() {
while(scanf("%d", &n) != EOF && n) {
for(int i = ; i <= n; ++i) scanf("%d", &a[i]);
init();
for(int i = n; i > ; --i) if(a[i] <= n) add_link(a[i], i);
cout<<solve()<<endl;
}
}
HDU 4747 Mex(线段树)(2013 ACM/ICPC Asia Regional Hangzhou Online)的更多相关文章
- hdu 4747 Mex (2013 ACM/ICPC Asia Regional Hangzhou Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 思路: 比赛打得太菜了,不想写....线段树莽一下 实现代码: #include<iost ...
- HDU4747——2013 ACM/ICPC Asia Regional Hangzhou Online
啦啦啦. 这是杭州网赛的一个题目,当时没做出来,当然这个想法确实比较难想到. 题目质量很高,这个题目也很特别,以前都没做过类似的题目.让我又一次体验了线段树的强大力量. 题目的意思是给你n个数a1-a ...
- [2013 ACM/ICPC Asia Regional Hangzhou Online J/1010]hdu 4747 Mex (线段树)
题意: + ;];;;], seg[rt << | ]);)) * fa.setv;) * fa.setv;;], seg[rt << | ], r - l + );;, ...
- HDU 4758——Walk Through Squares——2013 ACM/ICPC Asia Regional Nanjing Online
与其说这是一次重温AC自动机+dp,倒不如说这是个坑,而且把队友给深坑了. 这个题目都没A得出来,我只觉得我以前的AC自动机的题目都白刷了——深坑啊. 题目的意思是给你两个串,每个串只含有R或者D,要 ...
- hdu 4751 Divide Groups bfs (2013 ACM/ICPC Asia Regional Nanjing Online 1004)
SDUST的训练赛 当时死磕这个水题3个小时,也无心去搞其他的 按照题意,转换成无向图,预处理去掉单向的边,然后判断剩下的图能否构成两个无向完全图(ps一个完全图也行或是一个完全图+一个孤点) 代码是 ...
- HDU 4745 Two Rabbits(最长回文子序列)(2013 ACM/ICPC Asia Regional Hangzhou Online)
Description Long long ago, there lived two rabbits Tom and Jerry in the forest. On a sunny afternoon ...
- HDU 4744 Starloop System(最小费用最大流)(2013 ACM/ICPC Asia Regional Hangzhou Online)
Description At the end of the 200013 th year of the Galaxy era, the war between Carbon-based lives a ...
- 2013 ACM/ICPC Asia Regional Hangzhou Online hdu4739 Zhuge Liang's Mines
Zhuge Liang's Mines Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU4745——Two Rabbits——2013 ACM/ICPC Asia Regional Hangzhou Online
这个题目虽然在比赛的时候苦思无果,但是赛后再做就真的是个水题,赤果果的水题. 题目的意思是给n个数构成的环,两只兔子从任一点开始分别顺逆时针跳,每次可以调到任意一个数(最多不会跳过一圈). 求最多能跳 ...
随机推荐
- 整理关于 VS Code 一些小技巧:系列一
官方介绍 VisualStudioCode是一个轻量级且功能强大的源代码编辑器,它运行在桌面上,支持Windows.MacOS和Linux系统.它提供了对JavaScript.TypeScript和N ...
- CSS 中定位方式有几种,说明他们的意义
1.static 默认定位方式 显示为没有设置定位时的位置 2.fixed(固定定位) 他所相对固定的对象是可视窗口,与其他无关.以浏览器窗口作为参考进行定位 3.relative(相对定位) 元 ...
- 转载:/etc/security/limits.conf 控制文件描述符,进程数,栈大小
原文地址:http://ilikedo.iteye.com/blog/1554822 linux下安装Oracle 一般都会修改/etc/security/limits.conf这个文件,但是这里面的 ...
- TortoiseSVN SendRpt.exe not found解决方案
重启了Explorer.exe即可.这里也补充下简单的重启Explorer.exe的方法:打开任务管理器,找到“Windows资源管理器”,右键--重新启动. 或者,右键--结束任务,然后点击 文件- ...
- D - 湫湫系列故事——减肥记II
虽然制定了减肥食谱,但是湫湫显然克制不住吃货的本能,根本没有按照食谱行动! 于是,结果显而易见… 但是没有什么能难倒高智商美女湫湫的,她决定另寻对策——吃没关系,咱吃进去再运动运动消耗掉不就好了? 湫 ...
- hdu_2588_GCD
The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the ...
- 来自一枚初生牛犊不怕虎的小菜鸟的Mock.js使用,不足之处欢迎读者的指出 谢谢
本文章写的是基于require的mock.js的几种常用生成随机数据和ajax模拟前后端的交互信息 <script src="./app/libs/require.js"&g ...
- java.lang.UnsupportedOperationException: seccomp unavailable: CONFIG_SECCOMP not compiled into kernel, CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER are needed
错误描述: ElasticSearch集群启动错误,错误的原因是:因为Centos6不支持SecComp,而ES默认bootstrap.system_call_filter为true进行检测,所以导致 ...
- Go web表单验证
开发Web的一个原则就是,不能信任用户输入的任何信息,所以验证和过滤用户的输入信息就变得非常重要 必填字段 if len(r.Form["username"][0])==0{ // ...
- 【Leetcode】647. Palindromic Substrings
Description Given a string, your task is to count how many palindromic substrings in this string. Th ...