[BZOJ2120] 数颜色 && [bzoj2453] 维护队列(莫队 || 分块)
只有第一个,第二个权限题。
分块,然而wa,没看出来错在哪里,有时间再看。
#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm> const int N = , M = ; int n, m, S, C;
int a[N], st[N], ed[N], belong[N], pre[N], last[M], c[N]; inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} inline int min(int x, int y)
{
return x < y ? x : y;
} inline int find(int x, int y, int z)
{
int mid, l = x;
while(x < y)
{
mid = (x + y) >> ;
if(c[mid] >= z) y = mid;
else x = mid + ;
}
return x - l;
} inline void reset(int x)
{
int i;
for(i = st[x]; i <= ed[x]; i++) c[i] = pre[i];
std::sort(c + st[x], c + ed[x] + );
} inline void init()
{
int i, j;
n = read();
m = read();
S = sqrt(n);
for(i = ; i <= n; i++)
{
a[i] = read();
pre[i] = last[a[i]];
last[a[i]] = i;
}
for(i = ; i <= n; i += S)
st[++C] = i, ed[C] = min(i + S - , n);
for(i = ; i <= C; i++)
{
reset(i);
for(j = st[i]; j <= ed[i]; j++) belong[j] = i;
}
} inline int query(int x, int y)
{
int i, l = belong[x], r = belong[y], ans = ;
if(l == r)
{
for(i = x; i <= y; i++) if(c[i] < x) ans++;
return ans;
//return std::lower_bound(c + x, c + y + 1, x) - c - x;
}
//ans += std::lower_bound(c + x, c + ed[l] + 1, x) - c - x;
for(i = x; i <= ed[l]; i++) if(c[i] < x) ans++;
for(i = l + ; i <= r - ; i++) ans += find(st[i], ed[i] + , x);
//ans += std::lower_bound(c + st[r], c + y + 1, x) - c - st[r];
for(i = st[r]; i <= y; i++) if(c[i] < x) ans++;
return ans;
} inline void update(int x, int d)
{
int i, t;
for(i = ; i <= n; i++) last[a[i]] = ;
a[x] = d;
for(i = ; i <= n; i++)
{
t = pre[i];
pre[i] = last[a[i]];
if(t ^ pre[i]) reset(belong[i]);
last[a[i]] = i;
}
} inline void work()
{
int i, x, y;
char ch[];
for(i = ; i <= m; i++)
{
scanf("%s", ch);
x = read();
y = read();
if(ch[] == 'Q') printf("%d\n", query(x, y));
else update(x, y);
}
} int main()
{
init();
work();
return ;
}
莫队
比分块不知道高到哪里去了。
带修改之后真是。。。。
#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm> const int N = , M = 1e6 + ;
int n, Q, S, cnt1, cnt2, cur, l = , r, now;
int a[N], t[N], belong[N], ans[N], c[M]; struct ovo
{
int l, r, tim, id;
ovo(int l = , int r = , int tim = , int id = ) : l(l), r(r), tim(tim), id(id) {}
}q[N];
struct qwq
{
int p, v, last;
qwq(int p = , int v = , int last = ) : p(p), v(v), last(last) {}
}cq[N]; inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} inline bool cmp(ovo x, ovo y)
{
return belong[x.l] == belong[y.l] ? (belong[x.r] == belong[y.r] ? x.tim < y.tim : belong[x.r] < belong[y.r]) : belong[x.l] < belong[y.l];
} inline void add(int x)
{
now += (++c[x]) == ;
} inline void del(int x)
{
now -= (--c[x]) == ;
} inline void update(int x, int d)
{
if(l <= x && x <= r) add(d), del(a[x]);
a[x] = d;
} int main()
{
int i, x, y;
char ch[];
n = read();
Q = read();
S = sqrt(n);
for(i = ; i <= n; i++) a[i] = t[i] = read(), belong[i] = i / S + ;
for(i = ; i <= Q; i++)
{
scanf("%s", ch);
x = read();
y = read();
if(ch[] == 'Q') q[++cnt1] = ovo(x, y, cnt2, cnt1);
else cq[++cnt2] = qwq(x, y, t[x]), t[x] = y;
}
std::sort(q + , q + cnt1 + , cmp);
for(i = ; i <= cnt1; i++)
{
while(cur < q[i].tim) cur++, update(cq[cur].p, cq[cur].v);
while(cur > q[i].tim) update(cq[cur].p, cq[cur].last), cur--;
while(l < q[i].l) del(a[l]), l++;
while(l > q[i].l) l--, add(a[l]);
while(r < q[i].r) r++, add(a[r]);
while(r > q[i].r) del(a[r]), r--;
ans[q[i].id] = now;
}
for(i = ; i <= cnt1; i++) printf("%d\n", ans[i]);
return ;
}
[BZOJ2120] 数颜色 && [bzoj2453] 维护队列(莫队 || 分块)的更多相关文章
- Bzoj 2120: 数颜色 && 2453: 维护队列 莫队,分块,bitset
2120: 数颜色 Time Limit: 6 Sec Memory Limit: 259 MBSubmit: 2645 Solved: 1039[Submit][Status][Discuss] ...
- bzoj2120: 数颜色 &&bzoj2453: 维护队列
题目大意: 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少.当然,A有时候会依据个人喜好 ...
- BZOJ2120 数颜色 【带修莫队】
BZOJ2120 数颜色 Description 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到 ...
- BZOJ2120 数颜色 【带修改莫队】
2120: 数颜色 Time Limit: 6 Sec Memory Limit: 259 MB Submit: 6579 Solved: 2625 [Submit][Status][Discus ...
- BZOJ2120 数颜色(带修改莫队)
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
- 【BZOJ2120】数颜色(带修莫队)
点此看题面 大致题意:告诉你\(n\)只蜡笔的颜色,有两种操作:第一种操作将第\(x\)只蜡笔颜色改成\(y\),第二种操作询问区间\([l,r]\)内有多少种颜色的蜡笔. 考虑普通莫队 这题目第一眼 ...
- 「洛谷1903」「BZOJ2120」「国家集训队」数颜色【带修莫队,树套树】
题目链接 [BZOJ传送门] [洛谷传送门] 题目大意 单点修改,区间查询有多少种数字. 解法1--树套树 可以直接暴力树套树,我比较懒,不想写. 稍微口胡一下,可以直接来一个树状数组套主席树,也就是 ...
- BZOJ 2120 数颜色 【带修改莫队】
任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2120 2120: 数颜色 Time Limit: 6 Sec Memory Limit: ...
- BZOJ 2120 数颜色(带修改莫队)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2120 [题目大意] 给出一颜色序列,每次可以修改一个位置的颜色或者询问一个区间不同颜色 ...
随机推荐
- deepin 安装 idea
1.su root 2.sudo apt install idea 3.sudo vi /etc/hosts 最后一行添加 0.0.0.0 account.jetbrains.com 4.注册码 N7 ...
- centos 安装sysbench
安装sysbench 下载并且解压 shell> wget https://github.com/akopytov/sysbench/archive/1.0.zip -O "sysbe ...
- 如何保证access_token长期有效--微信公众平台开发
http://blog.csdn.net/qq_33556185/article/details/52758781 import javax.servlet.ServletContext; impor ...
- 【转】HIVE UDF UDAF UDTF 区别 使用
原博文出自于:http://blog.csdn.net/longzilong216/article/details/23921235(暂时) 感谢! 自己写代码时候的利用到的模板 UDF步骤: 1 ...
- [ CodeForces 438 D ] The Child and Sequence
\(\\\) \(Description\) 维护长为 \(N\) 的数列,\(M\)次操作,支持单点修改,区间取模,查询区间和. \(N,M\le 10^5\) \(\\\) \(Solution\ ...
- (1)麻省理工:计算机科学和 Python 编程导论
本门课用的语言是python2.7,我的主要学习语言是C++11,所以不是特殊说明,则认为和C++中的是一样的(不管是语法还是表达式),当然,也有我不懂而错认为与C++一样的东西~ Week1 第一讲 ...
- Android RecyclerView 滑动时图片加载的优化
RecyclerView 滑动时的优化处理 在滑动时停止加载图片,在滑动停止时开始加载图片,这里用了Glide.pause 和Glide.resume.这里为了避免重复设置增加开销,设置了一个标志变量 ...
- 7z.exe 命令行压缩文件排除文件(exclude filenames) 手记
命令行使用格式:Usage: 7z <command> [<switches>...] <archive_name> [<file_names>...] ...
- (转)Hibernate框架基础——多对多关联关系映射
http://blog.csdn.net/yerenyuan_pku/article/details/52756536 多对多关联关系映射 多对多的实体关系模型也是很常见的,比如学生和课程的关系.一个 ...
- Redis系列(五)--主从复制
单机环境存在的问题: 1.机器故障,直接凉凉 2.容量瓶颈 3.QPS瓶颈 主从复制 对于可拓展平台来说,复制(replication)是必不可少的.replication可以让其他服务器slave拥 ...