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国的公主, ...
随机推荐
- Charles 使用(拦截与修改)
一.http 拦截(Breakpoints) 选取需要拦截的地址,鼠标右击选择Breakpoints 二.拦截结果与修改 可以在请求与返回结果中修改内容 1.所拦截的数据包 2.所拦截的请求 3.所拦 ...
- Ball---hdu5821(排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5821 题意:有n个盒子,每个盒子又一个值 a[i] 如果 a[i] 大于 0 说明盒子里面有 1 个颜 ...
- 2.搭建cassandra时遇到没有公网网卡的问题
阿里云服务器有两种网络,一种是经典网络,一种是专用网络,经典网络是公网网卡的,但是专用网络是没有公网网卡的. 如图: 经典网络,公网ip是139.129.31.108: 专用网络,公网ip是 问题: ...
- windows环境下为php打开ssh2扩展
安装步骤 1. 下载 php extension ssh2下载地址 http://windows.php.net/downloads/pecl/releases/ssh2/0.12/ 根据自己PHP的 ...
- 如何调用另一个python文件中的代码
模块的搜索路径 模块的搜索路径都放在了sys.path列表中,如果缺省的sys.path中没有含有自己的模块或包的路径,可以动态的加入(sys.path.apend)即可.下面是sys.path在Wi ...
- Ubuntu16.04 安装 “宋体,微软雅黑,Consolas雅黑混合版编程字体” 等 Windows 7 下的字体
Windows平台下,“宋体”.“微软雅黑”.“Courier New(编程字体)”用的比较多,看的也习惯了.那如何在 Ubuntu下也安装这些字体呢? 操作步骤如下: 第一步:从 Windows 7 ...
- Bootstrap学习笔记-响应式布局原理
响应式布局的原理就是利用css3中@media媒体来实现的 <html> <head> <meta charset="utf-8"> <t ...
- docker:搭建lamp应用
(原文http://blog.csdn.net/smallfish1983/article/details/40108999?utm_source=tuicool) LAMP 指的 Linux(操作系 ...
- Verilog篇(四)时序模型
时序模型:仿真器的时间推进模型,它反映了推进仿真时间和调度事件的方式. 1)门级时序模型:适用于分析所有的连续赋值语句,过程连续赋值语句,门级原语,用户自定义原语. 特点:任意时刻,任意输入变化都将重 ...
- transform 和 transition
transform的属性包括:rotate() / skew() / scale() / translate() /matrix() 其中 rotate() 旋转度数,0-360 skew() 元素 ...