Description

给定一个长度为 \(n\) 的序列,有 \(m\) 次询问,每次询问一段区间,求区间中有多少个数出现次数超过 \(1\) 次

Limitation

\(n,~m~\leq~2~\times~10^6\)

Solution

好像和大众做法不大一样?

考虑对于每个位置,我们记一个 pre 和一个 post,代表他前面最后一个出现这个数的位置和他后面第一个出现这个数的位置。

考虑一个位置 \(x\) 对一个 \([l,~r]\) 的查询产生贡献当且仅当 \(pre_x~\geq~l\) 并且 \(post_x~>~r\)。

我们发现这个东西是可以树状数组维护的。

考虑统计一段区间内 pre 大于等于某个数的个数显然可以对 pre 开权值树状数组,然后按照左端点不升序排序,左端点左移的时候将对应位置的 pre 加入树状数组,一次查询的答案即为 \(BIT_{r}~-~BIT_{l - 1}\)。

现在考虑加入 \(post_x ~>~r\) 的限制。

我们发现当且仅当满足该条件的位置才对答案有贡献,于是我们只需要保证树状数组里只存有满足该条件的位置的 pre,就可以统计答案了。考虑按照右端点不升序排序,一开始先将最后一次出现的数字的 pre 加入到树状数组中,每次移动右端点的时候,将 post 为右端点所在位置的位置的 pre 加入树状数组即可(即将 \(post_x~=~R\) 的 \(x\) 的对应 \(per_x\) 加入树状数组)。由于大于右端点的部分的 pre 对答案同样没有贡献了,所以移动右端点的时候别忘了把右端点对应的 pre 在树状数组上删除。

Code

#include <cstdio>
#include <vector>
#include <algorithm>
#ifdef ONLINE_JUDGE
#define freopen(a, b, c)
#endif typedef long long int ll; namespace IPT {
const int L = 1000000;
char buf[L], *front=buf, *end=buf;
char GetChar() {
if (front == end) {
end = buf + fread(front = buf, 1, L, stdin);
if (front == end) return -1;
}
return *(front++);
}
} template <typename T>
inline void qr(T &x) {
char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
if (lst == '-') x = -x;
} namespace OPT {
char buf[120];
} template <typename T>
inline void qw(T x, const char aft, const bool pt) {
if (x < 0) {x = -x, putchar('-');}
int top=0;
do {OPT::buf[++top] = static_cast<char>(x % 10 + '0');} while (x /= 10);
while (top) putchar(OPT::buf[top--]);
if (pt) putchar(aft);
} const int maxn = 2000010; int n, c, m, dn;
int MU[maxn], oc[maxn], pos[maxn], pre[maxn], tree[maxn];
std::vector<int> vc[maxn]; struct Ask {
int l, r, id, ans; inline bool operator<(const Ask &_others) const {
return this->r > _others.r;
}
};
Ask ask[maxn];
bool cmp(const Ask&, const Ask&); int lowbit(int);
void update(int, int);
int query(int); int main() {
freopen("1.in", "r", stdin);
qr(n); qr(c); qr(m); dn = n + 1;
for (int i = 1; i <= n; ++i) {
qr(MU[i]); pre[i] = oc[MU[i]]; oc[MU[i]] = i;
}
for (int i = 1; i <= c; ++i) oc[i] = dn;
for (int i = n; i; --i) {
vc[pos[i] = oc[MU[i]]].push_back(i);
oc[MU[i]] = i;
}
for (int i = 1; i <= m; ++i) {
qr(ask[i].l); qr(ask[i].r); ask[i].id = i;
}
std::sort(ask + 1, ask + 1 + m);
for (auto i : vc[dn]) update(pre[i], 1);
for (int i = 1, R = n; i <= m; ++i) {
int l = ask[i].l, r = ask[i].r;
while (R > r) {
update(pre[R], -1);
for (auto j : vc[R]) update(pre[j], 1);
--R;
}
ask[i].ans = query(r) - query(l - 1);
}
std::sort(ask + 1, ask + 1 + m, cmp);
for (int i = 1; i <= m; ++i) {
qw(ask[i].ans, '\n', true);
}
return 0;
} inline bool cmp(const Ask &_a, const Ask &_b) {
return _a.id < _b.id;
} inline int lowbit(int x) {return x & -x;} void update(int x, int v) {
if (x == 0) return;
while (x <= dn) {
tree[x] += v;
x += lowbit(x);
}
} int query(int x) {
int _ret = 0;
while (x) {
_ret += tree[x];
x -= lowbit(x);
}
return _ret;
}

【树状数组】【P4113】[HEOI2012]采花的更多相关文章

  1. 洛谷P4113 [HEOI2012]采花

    题目描述 萧薰儿是古国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花. 花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一排的,以便于 ...

  2. P4113 [HEOI2012]采花 (莫队TLE)

    思路 update 11.2 树状数组AC 本题莫队过不去,会TLE ----------------------- 但也是个不错的莫队练手题 ------------------------ 毕竟C ...

  3. P4113 [HEOI2012]采花

    题目描述 萧薰儿是古国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花. 花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一排的,以便于 ...

  4. 【luogu P4113 [HEOI2012]采花】 假题解

    题目链接:https://www.luogu.org/problemnew/show/P4113 为什么要卡莫队!为什么加强的这么毒瘤! 莫队可以拿100分剩下三个点没治了 // luogu-judg ...

  5. LUOGU P4113 [HEOI2012]采花

    传送门 解题思路 莫队题卡莫队...莫队只能拿到100分,满分200.正解主席树??发个莫队100分代码. 代码 #include<iostream> #include<cstdio ...

  6. BZOJ_2743_[HEOI2012]采花_离线+树状数组

    BZOJ_2743_[HEOI2012]采花_离线+树状数组 Description 萧芸斓是Z国的公主,平时的一大爱好是采花.今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花 .花园足够大 ...

  7. BZOJ 2743: [HEOI2012]采花 离线树状数组

    2743: [HEOI2012]采花 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2743 Description 萧芸斓是Z国的公主, ...

  8. 【BZOJ2743】[HEOI2012]采花 离线+树状数组

    [BZOJ2743][HEOI2012]采花 Description 萧芸斓是Z国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了n朵花, ...

  9. 【bzoj2743】[HEOI2012]采花 树状数组

    题目描述 萧芸斓是Z国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一排的,以便于公 ...

随机推荐

  1. openstack系列文章(一)

    学习openstack的系列文章-虚拟化 虚拟化 KVM CPU 虚拟化 KVM 内存虚拟化 全虚拟化 I/O 设备 半虚拟化 I/O 设备 I/O PCI PCIe 设备直接分配 SR-IOV 在 ...

  2. Spring Bean注册解析(二)

           在上文Spring Bean注册解析(一)中,我们讲解了Spring在注册Bean之前进行了哪些前期工作,以及Spring是如何存储注册的Bean的,并且详细介绍了Spring是如何解析 ...

  3. LIFI热火下的VLC基本链路、标准及发展问题

    和白炽及荧光灯相比,白光发光二极管(LED)具有寿命长.光效高.功耗低.无辐射.安全性好.可靠性高等特点,被称为"绿色照明"并得到迅猛发展.白光LED在未来市场极具竞争力.世界范围 ...

  4. “Hello World!”团队第五周第六次会议

    “Hello World!”团队第五周第六次会议   博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八.checkout& ...

  5. C++:类中的赋值函数

    先来看一个例子: #include<iostream> #include<string> using namespace std; class Student{ public: ...

  6. Linux环境下Web环境搭建——Nginx

    1.安装依赖 yum -y install make gcc gcc-c++ ncurses-devel  ##编译环境 yum -y install zlib zlib-devel openssl ...

  7. Sprint2

    进展:主要进行了在安卓手机端进行APP开发的资料及有关学习的视频的查找等.了解也学习了这些资料还有技术.第一个任务完成了一半. 燃尽图:

  8. 关于datatable的数据绑定问题

    最近做项目掉在数据绑定这个小坑里了,最后发现问题其实很简单,只是官方的文档描述可能不太清除导致的吧.首先贴上官网地址:http://www.datatables.club/ 关于这个插件的简单使用就不 ...

  9. 【转】Verilog HDL常用建模方式——《Verilog与数字ASIC设计基础》读书笔记(四)

    Verilog HDL常用建模方式——<Verilog与数字ASIC设计基础>读书笔记(四) Verilog HDL的基本功能之一是描述可综合的硬件逻辑电路.所谓综合(Synthesis) ...

  10. 配置docker的私有仓库

    1:安装docker-registry包 yum install -y docker-distribution   2:启动docker-distribution,默认监听于TCP/5000端口 sy ...