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国的公主, ...
随机推荐
- 编码问题:python写入文件
方法一:(推荐) line1 = "我爱中国111" line2 = u"我爱祖国222" with open('1.txt','w',encoding='ut ...
- 2018最新php笔试题及答案(持续更新)
php中include和require的区别 在 PHP 中,您可以在服务器执行 PHP 文件之前在该文件中插入一个文件的内容.include 和 require 语句用于在执行流中插入写在其他文件中 ...
- 关于找不到类org/apache/commons/lang/xwork/StringUtils的问题
在替换最新版的 struts2包的解决过程中.遇到 找不到这两个包org/apache/commons/lang/xwork/StringUtils.org/apache/commons/lang/x ...
- Saving Princess claire_(hdu 4308 bfs模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Jav ...
- AngularJS 表达式 对象和数组
AngularJS 使用 表达式 把数据绑定到 HTML. AngularJS 表达式 AngularJS 表达式写在双大括号内:{{ expression }}. AngularJS 表达式把数据绑 ...
- 从Maven仓库中导出jar包
从Maven仓库中导出jar包:进入工程pom.xml 所在的目录下,输入以下命令:mvn dependency:copy-dependencies -DoutputDirectory=lib更简单的 ...
- java多态性方法的重写Overriding和重载Overloading详解
java多态性方法的重写Overriding和重载Overloading详解 方法的重写Overriding和重载Overloading是Java多态性的不同表现.重写Overriding是父类与子类 ...
- Java之构造器和构造方法的使用和意义
我总是要把构造器和方法混淆,后来发现, 方法,实际上,是需要用于执行java代码的,而构造器, 构造器,,,是一个类的实例!!(我的理解,构造器是一个对象) 为什么呢? 类的实例,我们需要用类来创建对 ...
- H2O.ai初步使用
1.官网下载最新稳定版,https://www.h2o.ai/download/ ,如果点击下载无反应,请使用ie浏览器 2.解压h2o-3.18.0.10.zip到目录h2o-3.18.0.10 3 ...
- MDX导航结构层次:《Microsoft SQL Server 2008 MDX Step by Step》学习笔记九
<Microsoft SQL Server 2008 MDX Step by Step>学习笔记九:导航结构层次 SQL Server 2008中SQL应用系列及BI笔记系列--目录索 ...