[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 [题目大意] 给出一颜色序列,每次可以修改一个位置的颜色或者询问一个区间不同颜色 ...
随机推荐
- oracle创建默认表空间---重要
当oracle创建数据库后,sys创建用户时还要有默认表空间.不创建默认表空间在导如项目时会有些数据表导入不成功! 由于时间仓促以截屏为例 之后会在刚刚那个空文件生成一个文件 ----------- ...
- mysql机制总结
Innodb和myisam最大的不同就是 innodb支持事物 采用了行锁 myisam 采用了表锁 默认就使用了表锁 表锁:速度快 并发小 发生锁冲突高 开销小 行锁:速度慢 并发高 发生锁冲突低 ...
- CSS实现居中的方式
在介绍居中方式之前,简单介绍一下行内元素和块级元素. 行内元素 和其他元素都在同一行 高,行高及外边距和内边距部分可以改变 宽度只与内容有关 行内元素只能容纳文本或者其他行内元素 常用内联元素:a,i ...
- laravel学习一
Laravel 是一款简洁,优雅的一款框架,可以说是入门TP后的第二款可以选择的框架. 目录部分: app -> 自己写的代码 http -> Controller -> 控制器 b ...
- 如何在Eclipse或者Myeclipse中使用tomcat(配置tomcat,发布web项目)?(图文详解)(很实用)
前期博客 Eclipse里的Java EE视图在哪里?MyEclipse里的Java EE视图在哪里?MyEclipse里的MyEclipse Java Enterprise视图在哪里?(图文详解) ...
- ajax无限循环
// 猜你喜欢的无限加载 (function(){ var content = document.getElementsByClassName("content")[0]; var ...
- Elasticsearch--建议器
目录 可用的建议器类型 term建议器 term建议器的配置选项 phrase建议器 completion建议器 在考虑性能的情况下,允许用户的拼写错误,以及构建一个自动完成功能 可用的建议器类型 t ...
- IBatis的分页研究
IBatis的分页研究 博客分类: Ibatis学习 摘自: http://cpu.iteye.com/blog/311395 yangtingkun Oracle分页查询语句 ibaits. ...
- Ubuntu-Python2.7安装 scipy,numpy,matplotlib
sudo apt-get install python-scipy sudo apt-get install python-numpy sudo apt-get install python-matp ...
- C++ 程序的编译
一.编译器都具备集成开发环境(Integrated Developed Environment,IDE) 二.程序源文件命名约定: C++ 的后缀一般是 .cpp .cc .C .cpp .cxx 三 ...