P4113 [HEOI2012]采花 (莫队TLE)
思路
update 11.2 树状数组AC
本题莫队过不去,会TLE
但也是个不错的莫队练手题
毕竟Chen_Zhe还给了100分莫队分 (还会给你小对勾)
莫队&&树状数组代码
// luogu-judger-enable-o2
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 2e6 + 7;
int n, m, a[maxn], sum[maxn << 1];
struct edge {
int size,las1,las2;
} zz[maxn];
int ans[maxn];
struct node {
int s, t,id;
bool operator < (const node b ) const {
return t < b.t;
}
} b[maxn];
int read() {
int x = 0, f = 1; char s = getchar();
for (; s < '0' || s > '9'; s = getchar()) if (s == '-') f = -1;
for (; s >= '0' && s <= '9'; s = getchar()) x = x * 10 + s - '0';
return x * f;
}
int lowbit(int x) {
return x & -x;
}
void add(int x, int dat) {
for (; x <= n; x += lowbit(x))
sum[x] += dat;
}
int query(int x) {
int ans = 0;
for (; x >= 1; x -= lowbit(x))
ans += sum[x];
return ans;
}
int main() {
n = read();
m = read();
m=read();
for (int i = 1; i <= n; ++i)
a[i] = read();
for (int i = 1; i <= m; ++i)
b[i].s = read(), b[i].t = read(), b[i].id = i;
sort(b + 1, b + 1 + m);
int js = 1;
for (int i = 1; i <= n; ++i) {
if(!zz[a[i]].size) {
zz[a[i]].las1=i;
} else {
if(zz[a[i]].size>=2)
add(zz[a[i]].las2,-1);
add(zz[a[i]].las1,1);
swap(zz[a[i]].las1,zz[a[i]].las2);
zz[a[i]].las1=i;
}
++zz[a[i]].size;
if(b[js].t==i) {
int end_ans=query(b[js].t);
for(; b[js].t==i; ++js) {
ans[b[js].id]=end_ans-query(b[js].s-1);
}
}
}
for (int i = 1; i <= m; ++i) {
printf("%d\n", ans[i]);
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 2000007
using namespace std;
inline int read()
{
int x=0,f=1;char s=getchar();
while('0'>s||s>'9') {
if(s=='-') f=-1;
s=getchar();
}
while('0'<=s&&s<='9') {
x=x*10+s-'0';
s=getchar();
}
return x*f;
}
int n,m,k,now;
int a[maxn];
int belong[maxn];
int vis[maxn];
int ans[maxn];
struct node{
int l,r,id;
}q[maxn];
inline bool cmp(const node &a,const node &b)
{
return belong[a.l]==belong[b.l] ? a.r<b.r : belong[a.l]<belong[b.l];
}
inline void Add(int x)
{
vis[x]==1 ? ++now : now;
++vis[x];
}
inline void Delet(int x)
{
vis[x]==2 ? --now : now;
--vis[x];
}
int main()
{
n=read();
int meiyongdebianliang_qidaotixingfanweidezuoye__233zhemechangdebianliangming=read();
m=read();
k=sqrt(n);
for(int i=1;i<=n;++i)
a[i]=read();
for(int i=1;i<=n;++i)
belong[i]=(i-1)/k+1;
for(int i=1;i<=m;++i)
{
q[i].l=read();
q[i].r=read();
q[i].id=i;
}
sort(q+1,q+1+m,cmp);
int l=1,r=0;
for(int i=1;i<=m;++i)
{
while(l > q[i].l) Add(a[--l]);
while(l < q[i].l) Delet(a[l++]);
while(r < q[i].r) Add(a[++r]);
while(r > q[i].r) Delet(a[r--]);
ans[q[i].id]=now;
}
for(int i=1;i<=m;++i)
printf("%d\n",ans[i]);
return 0;
}
P4113 [HEOI2012]采花 (莫队TLE)的更多相关文章
- 【luogu P4113 [HEOI2012]采花】 假题解
题目链接:https://www.luogu.org/problemnew/show/P4113 为什么要卡莫队!为什么加强的这么毒瘤! 莫队可以拿100分剩下三个点没治了 // luogu-judg ...
- 洛谷 P2056 采花 - 莫队算法
萧芸斓是 Z国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了 n 朵花,花有 c 种颜色(用整数 1-c 表示) ,且花是排成一排的,以 ...
- P4113 [HEOI2012]采花
题目描述 萧薰儿是古国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花. 花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一排的,以便于 ...
- 洛谷P4113 [HEOI2012]采花
题目描述 萧薰儿是古国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花. 花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一排的,以便于 ...
- LUOGU P4113 [HEOI2012]采花
传送门 解题思路 莫队题卡莫队...莫队只能拿到100分,满分200.正解主席树??发个莫队100分代码. 代码 #include<iostream> #include<cstdio ...
- BZOJ 2743: [HEOI2012]采花
2743: [HEOI2012]采花 Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 2056 Solved: 1059[Submit][Status ...
- cogs:1619. [HEOI2012]采花/luogu P2056
1619. [HEOI2012]采花 ★★☆ 输入文件:1flower.in 输出文件:1flower.out 简单对比时间限制:5 s 内存限制:128 MB [题目描述] 萧薰儿是 ...
- BZOJ_2743_[HEOI2012]采花_离线+树状数组
BZOJ_2743_[HEOI2012]采花_离线+树状数组 Description 萧芸斓是Z国的公主,平时的一大爱好是采花.今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花 .花园足够大 ...
- BZOJ 2743: [HEOI2012]采花 离线树状数组
2743: [HEOI2012]采花 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2743 Description 萧芸斓是Z国的公主, ...
随机推荐
- git push 文件过大时出错,fatal: The remote end hung up unexpectedly
可以修改配置文件: 1 使用命令:git config http.postBuffer = 524288000 2修改git文件夹中的config文件,加入如下一段: [http] postBuffe ...
- QEMU IO事件处理框架
Qemu IO事件处理框架 qemu是基于事件驱动的,在基于KVM的qemu模型中,每一个VCPU对应一个qemu线程,且qemu主线程负责各种事件的监听,这里有一个小的IO监听框架,本节对此进行介绍 ...
- 借用HTML5 插入视频。音频
HTML5 规定了一种通过 video 元素来包含视频的标准方法. 插入视频 <video width="320" height="240" contro ...
- Mint linux中调整屏幕亮度的方法
/********************************************************************* * Author : Samson * Date ...
- filter push down
filter push down filter push down :先filter再做join 如果SQL里有where条件,那么数据库引擎会先filter再做join 但是MySQL5.6之前还不 ...
- Python3学习之路~2.4 字典操作
字典一种key - value 的数据类型,使用就像我们上学用的字典,通过笔划.字母来查对应页的详细内容. 定义字典(dictionary) info = { 'stu1101': "Amy ...
- eigen 笔记2
4. Block operations 1) Using block operations Block of size(p, q), starting at (i, j) dynamic-size b ...
- 软件包管理:rpm命令管理-校验和文件提取
校验主要用于判断文件是否做了更改 修改标志: 会用-V,会看输出结果即可. 当有误操作,比如删了某一个文件,只需知道他属于哪一个rpm包,可用提取找回覆盖就行.并不把整个rpm包安装,而是提取其中的某 ...
- js模拟电梯操作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vue项目打包后css背景图路径不对的问题
问题描述: 自己在自学vue做项目的过程中,遇到一个有关背景图片路径的问题,就是css代码中背景图片是根据相对路径来写的,如下图: 当使用npm run dev命令本地访问的时候,背景图片是正常显示的 ...