[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 [题目大意] 给出一颜色序列,每次可以修改一个位置的颜色或者询问一个区间不同颜色 ...
随机推荐
- bzoj 1914: [Usaco2010 OPen]Triangle Counting 数三角形【叉积+极角排序+瞎搞】
参考:https://blog.csdn.net/u012288458/article/details/50830498 有点神啊 正难则反,考虑计算不符合要求的三角形.具体方法是枚举每个点,把这个点 ...
- Kafka详解与总结(七)-Kafka producer拦截器(interceptor)
1. 拦截器原理 Producer拦截器(interceptor)是在Kafka 0.10版本被引入的,主要用于实现clients端的定制化控制逻辑. 对于producer而言,interceptor ...
- [ZJOI2008]杀蚂蚁
题意翻译 注意在(0,0)已经有蚂蚁的时候是不会生成新蚂蚁的 还有如果有蚂蚁扛着蛋糕,但是不在某个炮的范围内,炮仍然会打最近的蚂蚁 题目描述 最近,佳佳迷上了一款好玩的小游戏:antbuster. 游 ...
- 51nod 1018 排序
1018 排序 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出N个整数,对着N个整数进行排序 Input 第1行:整数的数量N(1 <= N ...
- ACM_寒冰王座(完全背包)
寒冰王座 Time Limit: 2000/1000ms (Java/Others) Problem Description: 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,只有一张钞票) ...
- Http协议对格式、请求头、方法
######### #概览 ######### 超文本传输协议(Http: Hyper Text Transfer Protocol) :用于发送WWW方式的数据.采用TCP/IP协议,是一个无状态协 ...
- hbase本地调试环境搭建
1,前言 想要深入的了解hbase,看hbase源码是必须的.以下描述了搭建hbase本地调试环境的经历 2,安装步骤 2.1,启动hbase 1,安装java和IDE IntelliJ,下载源码等. ...
- 15 C#中的条件执行,if else
在这一节的练习中,给大家介绍C#编程中的一个重要部分,条件执行.也就是If else语句.我们现实生活中的很多复杂的推理都可以用这个语法实现. If else语句的常规的样子,如下面所示. if (测 ...
- Spring.Net学习笔记(1)-容器的使用
一.下载地址: http://www.springframework.net/download.html 二.相关程序集 Spring.Net容器定义在程序集Spring.Core.dll中,它依赖于 ...
- Python 开发初识
从今天开始记录自己的python开发之路,用博客记录自己的学习经历,以及学习小结,小的项目模块,努力充实,做最好的自己!!!