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. 基于Python的信用评分卡模型分析(一)

    信用风险计量体系包括主体评级模型和债项评级两部分.主体评级和债项评级均有一系列评级模型组成,其中主体评级模型可用“四张卡”来表示,分别是A卡.B卡.C卡和F卡:债项评级模型通常按照主体的融资用途,分为 ...

  2. python将response中的cookies加入到header

    url = “http://abad.com”header = { "user-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64 ...

  3. CocoaPods pod install的时候报错:invalid byte sequence in UTF-8 (ArgumentError)解决办法

    CocoaPods pod install的时候报错:invalid byte sequence in UTF-8 (ArgumentError)解决办法: 基本可以确定是Podfile中的内容编码有 ...

  4. Nginx是如何配置为 Web 服务器的【转载】

    详解 Nginx是如何配置为 Web 服务器的 林涛 发表于:2016-11-29 23:23 分类:WebServer 标签:Nginx,web,web服务器 521次 抽象来说,将 Nginx 配 ...

  5. Android:java.lang.OutOfMemoryError:GC overhead limit exceeded

    Android编译:java.lang.OutOfMemoryError:GC overhead limit exceeded 百度好多什么JVM啊之类的东西,新手简单粗暴的办法: 1.在的Model ...

  6. LeetCode题解:(19) Remove Nth Node From End of List

    题目说明 Given a linked list, remove the nth node from the end of list and return its head. For example, ...

  7. cxVerticalGrid

    cxVerticalGrid can't get values procedure TForm1.Button1Click(Sender: TObject); var i: Integer; lvNa ...

  8. Cocos2d入门及第一次运行时遇到的问题

    先通过github下载cocos2d.これ:https://github.com/ZhouWeikuan/cocos2d 进入上面的网址后,如果不会用git或者svn的朋友就在页面的右下角点那个“Do ...

  9. 《统计学习方法》P179页10.22前向后向算法公式推导

  10. Tomcat 启动流程