[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 [题目大意] 给出一颜色序列,每次可以修改一个位置的颜色或者询问一个区间不同颜色 ...
随机推荐
- [转载]android常用的API接口调用
原文地址:android常用的API接口调用作者:宋耀 显示网页: Uri uri = Uri.parse("http://www.google.com"); In ...
- bzoj 2101: [Usaco2010 Dec]Treasure Chest 藏宝箱【区间dp】
就是区间dp啦f[i][j]表示以i开头的长为j+1的一段的答案,转移是f[i][j]=s[i+l]-s[i-1]+min(f[i][j-1],f[i+1][j-1]),初始是f[i][1]=a[i] ...
- 使用frp工具实现内网的穿透以及配置多个ssh和web服务
frp简介 FRP 项目地址 https://github.com/fatedier/frp/blob/master/README_zh.md frp 是一个可用于内网穿透的高性能的反向代理应用,支持 ...
- php 编译时 报错 configure: error: libXpm.(a|so) not found.
编译环境 centos7 php 5.4.26 $ yum install libXpm-devel 显示已安装 百度得知 ubuntu虚拟机安装lamp遇到的问题 configure: error: ...
- 数学 Codeforces Round #219 (Div. 2) B. Making Sequences is Fun
题目传送门 /* 数学:这题一直WA在13组上,看了数据才知道是计算cost时超long long了 另外不足一个区间的直接计算个数就可以了 */ #include <cstdio> #i ...
- 297 Serialize and Deserialize Binary Tree 二叉树的序列化与反序列化
序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据.请设计一个算法来实现二叉树 ...
- 13 继续C#中的方法,带返回值的方法介绍
在这一个练习中,我们要使用带返回值的方法.如果一个方法带返回值,那么它的形式是这样的. 定义一个带返回值的C#方法 static 返回类型 方法名字 (参数类型 参数1的名字,参数类型 参数2的名字) ...
- Objective-C设计模式——适配器Adapter(接口适配)
适配器模式 适配器模式通俗来讲,其实就是对客户端添加新的类但却不修改客户端和新的类的接口.此时我们需要自己来实现适配,在适配器模式中有Target对象,即客户端所需要的接口对象,Adaptee对象,即 ...
- ES6十大常用特性
. Default Parameters(默认参数) in ES6 2. Arrow Functions (箭头函数)in ES6 3. Block-Scoped Constructs ...
- Lazarus 日志工具 MultiLog
MultiLog是一种同时以灵活性和低开销为目标的日志系统.顾名思义,它可用于将日志实例到多个目标,如文本文件.可视控件或其他应用程序.添加新的日志目标使用两个方法就可以实现了,其中一个方法是可选的. ...