【题目大意】

求有多少区间只包含1个出现次数为1的数。

$1\leq n \leq 5*10^5, 0 \leq a_i \leq 10^9$

【题解】

考虑枚举右端点,设这个数上一次出现位置为pre[i],那么就是$[pre[i]+1,i]$区间加1,$[pre[pre[i]]+1, pre[i]]$区间减1,和统计区间中1的个数。

注意到数不会减到负的,那么1只可能是最小值或次小值,那么线段树直接维护即可。

复杂度$O(nlogn)$。

考场交错代码导致爆0...QAQ

# include <vector>
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull;
typedef long double ld; const int M = 5e5 + , N = 1e5 + , inf = 1e9; inline int getint() {
int x = ; char ch = getchar();
while(!isdigit(ch)) ch = getchar();
while(isdigit(ch)) x = (x<<) + (x<<) + ch - '', ch = getchar();
return x;
} int n, m, a[M], pre[M], lst[M];
vector<int> ps; struct pa {
int x, tx, y, ty;
pa() {}
pa(int x, int tx, int y, int ty) : x(x), tx(tx), y(y), ty(ty) {}
inline friend pa operator + (pa a, pa b) {
pa c;
if(a.x < b.x) {
c.x = a.x, c.tx = a.tx;
c.y = min(a.y, b.x); c.ty = ;
if(a.y == c.y) c.ty += a.ty;
if(b.x == c.y) c.ty += b.tx;
} else if(a.x > b.x) {
c.x = b.x, c.tx = b.tx;
c.y = min(a.x, b.y); c.ty = ;
if(a.x == c.y) c.ty += a.tx;
if(b.y == c.y) c.ty += b.ty;
} else {
c.x = a.x, c.tx = a.tx + b.tx;
c.y = min(a.y, b.y); c.ty = ;
if(a.y == c.y) c.ty += a.ty;
if(c.y == b.y) c.ty += b.ty;
}
return c;
}
}; struct SMT {
pa w[M << ]; int tag[M << ];
# define ls (x<<)
# define rs (x<<|)
inline void up(int x) {
w[x] = w[ls] + w[rs];
} inline void pushtag(int x, int t) {
w[x].x += t;
if(w[x].y != inf) w[x].y += t;
tag[x] += t;
} inline void down(int x) {
if(!tag[x]) return ;
pushtag(ls, tag[x]);
pushtag(rs, tag[x]);
tag[x] = ;
} inline void build(int x, int l, int r) {
if(l == r) {
w[x].x = , w[x].tx = ;
w[x].y = inf, w[x].ty = ;
return ;
}
int mid = l+r >> ;
build(ls, l, mid);
build(rs, mid+, r);
up(x);
} inline void edt(int x, int l, int r, int L, int R, int d) {
if(L <= l && r <= R) {
pushtag(x, d);
return ;
}
down(x);
int mid = l+r>>;
if(L <= mid) edt(ls, l, mid, L, R, d);
if(R > mid) edt(rs, mid+, r, L, R, d);
up(x);
} inline pa sum(int x, int l, int r, int L, int R) {
if(L <= l && r <= R) return w[x];
down(x);
int mid = l+r>>;
if(R <= mid) return sum(ls, l, mid, L, R);
else if(L > mid) return sum(rs, mid+, r, L, R);
else return sum(ls, l, mid, L, mid) + sum(rs, mid+, r, mid+, R);
} inline void debug(int x, int l, int r) {
printf("x = %d,l = %d,r = %d,mi = %d,semi = %d,minum = %d,seminum = %d,tag = %d\n", x, l, r, w[x].x, w[x].y, w[x].tx, w[x].ty, tag[x]);
if(l == r) return ;
int mid = l+r>>;
debug(ls, l, mid); debug(rs, mid+, r);
}
}T; int main() {
freopen("a1.in", "r", stdin);
// freopen("a.out", "w", stdout);
n = getint();
for (int i=; i<=n; ++i) {
a[i] = getint();
ps.push_back(a[i]);
}
sort(ps.begin(), ps.end());
ps.erase(unique(ps.begin(), ps.end()), ps.end());
m = ps.size();
for (int i=; i<=n; ++i) a[i] = lower_bound(ps.begin(), ps.end(), a[i]) - ps.begin() + ;
for (int i=; i<=n; ++i) pre[i] = lst[a[i]], lst[a[i]] = i;
ll ans = ; pa t; T.build(, , n);
for (int i=; i<=n; ++i) {
// T.debug(1, 1, n); cout << endl;
T.edt(, , n, pre[i] + , i, );
if(pre[i]) T.edt(, , n, pre[pre[i]] + , pre[i], -);
t = T.sum(, , n, , i);
if(t.x == ) ans += t.tx;
if(t.y == ) ans += t.ty;
// 区间加1,区间减1,查询区间内1的出现次数
}
cout << ans;
return ;
}

省队集训 Day4 a的更多相关文章

  1. FJ省队集训DAY4 T3

    #include<cstdio> #include<iostream> #include<cmath> #include<cstring> #inclu ...

  2. FJ省队集训DAY4 T2

    XXX #include<cstdio> #include<iostream> #include<cmath> #include<cstring> #i ...

  3. FJ省队集训DAY4 T1

    直接上题解 #include<cstdio> #include<iostream> #include<cmath> #include<cstring> ...

  4. HN2018省队集训

    HN2018省队集训 Day1 今天的题目来自于雅礼的高二学长\(dy0607\). 压缩包下载 密码: 27n7 流水账 震惊!穿着该校校服竟然在四大名校畅通无阻?霸主地位已定? \(7:10\)从 ...

  5. JS省队集训记

    不知不觉省队集训已经结束,离noi也越来越近了呢 论考前实战训练的重要性,让我随便总结一下这几天的考试 Day 1 T1 唉,感觉跟xj测试很像啊?meet in middle,不过这种题不多测是什么 ...

  6. Loj #6069. 「2017 山东一轮集训 Day4」塔

    Loj #6069. 「2017 山东一轮集训 Day4」塔 题目描述 现在有一条 $ [1, l] $ 的数轴,要在上面造 $ n $ 座塔,每座塔的坐标要两两不同,且为整点. 塔有编号,且每座塔都 ...

  7. Loj 6068. 「2017 山东一轮集训 Day4」棋盘

    Loj 6068. 「2017 山东一轮集训 Day4」棋盘 题目描述 给定一个 $ n \times n $ 的棋盘,棋盘上每个位置要么为空要么为障碍.定义棋盘上两个位置 $ (x, y),(u, ...

  8. loj6068. 「2017 山东一轮集训 Day4」棋盘 二分图,网络流

    loj6068. 「2017 山东一轮集训 Day4」棋盘 链接 https://loj.ac/problem/6068 思路 上来没头绪,后来套算法,套了个网络流 经典二分图 左边横,右边列 先重新 ...

  9. [2018HN省队集训D9T1] circle

    [2018HN省队集训D9T1] circle 题意 给定一个 \(n\) 个点的竞赛图并在其中钦定了 \(k\) 个点, 数据保证删去钦定的 \(k\) 个点后这个图没有环. 问在不删去钦定的这 \ ...

随机推荐

  1. lintcode-18-带重复元素的子集

    带重复元素的子集 给定一个可能具有重复数字的列表,返回其所有可能的子集 注意事项 子集中的每个元素都是非降序的 两个子集间的顺序是无关紧要的 解集中不能包含重复子集 样例 如果 S = [1,2,2] ...

  2. iOS开发SDWebImage源码解析之SDWebImageManager的注解

    最近看了两篇博客,写得很不错,关于SDWebImage源码解析之SDWebImageManager的注解: 1.http://www.jianshu.com/p/6ae6f99b6c4c 2.http ...

  3. OSG数学基础:坐标系统

    坐标系是一个精确定位对象位置的框架,所有的图形变换都是基于一定的坐标系进行的. 三维坐标系总体上可以分为两大类:左手坐标系和右手坐标系. 常用的坐标系:世界坐标系.物体坐标系和摄像机坐标系. 世界坐标 ...

  4. YaoLingJump开发者日志(九)V1.1.1版本完成

    跳跃吧瑶玲下载连接 百度网盘下载 介绍   Android版本升级到8.0,发现原来的图标不显示了:   百度了一下解决方案,用了该博客提供的第二种方法,修改roundIcon的内容.   可爱的图标 ...

  5. 用c++读取文件夹中的所有文件名

    //头文件,注意要加stdafx.h和io.h等 #include "stdafx.h" #include <io.h> #include <vector> ...

  6. 自学网络 arp_ignore/arp_announce

    1)机器上有好几个IP地址,如何让网卡只接收自己IP地址的数据包: 如何只接收自己网卡的数据包 http://www.cnblogs.com/honpey/p/8447819.html 相关的配置ar ...

  7. lucene 学习之基础篇

    一.什么是全文索引 全文检索首先将要查询的目标文档中的词提取出来,组册索引(类似书的目录),通过查询索引达到搜索目标文档的目的,这种先建立索引,再对索引进行搜索的过程就叫全文索引. 从图可以看出做全文 ...

  8. MySQL中的条件语句

    判断学生表中成绩是否小于60,将小于60的学生成绩列为不及格 学生表(student) 字段:姓名(name),学号(主键)(num),性别(sex),成绩(score) select *,if(sc ...

  9. 解决编写 xml 没有代码提示

    有时候在编写 struts.xml 会没有代码提示,一般是因为没有联网导致的,或者之前配置过 dtd 文件 url,但是文件路径之后被修改了. 解决方案有: 让电脑联网 修改 dtd 的本地路径以及 ...

  10. 第29天:js-数组添加删除、数组和字符串相互转换

    一.添加数组var arr=[1,3,5];arr.push(7,9);//添加7和9到数组arr后面,得到[1,3,5,7,9]1.push();可向数组末尾添加一个或多个元素,并返回新的长度.2. ...