传送门

只有第一个,第二个权限题。

分块,然而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] 维护队列(莫队 || 分块)的更多相关文章

  1. Bzoj 2120: 数颜色 && 2453: 维护队列 莫队,分块,bitset

    2120: 数颜色 Time Limit: 6 Sec  Memory Limit: 259 MBSubmit: 2645  Solved: 1039[Submit][Status][Discuss] ...

  2. bzoj2120: 数颜色 &&bzoj2453: 维护队列

    题目大意: 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少.当然,A有时候会依据个人喜好 ...

  3. BZOJ2120 数颜色 【带修莫队】

    BZOJ2120 数颜色 Description 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到 ...

  4. BZOJ2120 数颜色 【带修改莫队】

    2120: 数颜色 Time Limit: 6 Sec  Memory Limit: 259 MB Submit: 6579  Solved: 2625 [Submit][Status][Discus ...

  5. BZOJ2120 数颜色(带修改莫队)

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  6. 【BZOJ2120】数颜色(带修莫队)

    点此看题面 大致题意:告诉你\(n\)只蜡笔的颜色,有两种操作:第一种操作将第\(x\)只蜡笔颜色改成\(y\),第二种操作询问区间\([l,r]\)内有多少种颜色的蜡笔. 考虑普通莫队 这题目第一眼 ...

  7. 「洛谷1903」「BZOJ2120」「国家集训队」数颜色【带修莫队,树套树】

    题目链接 [BZOJ传送门] [洛谷传送门] 题目大意 单点修改,区间查询有多少种数字. 解法1--树套树 可以直接暴力树套树,我比较懒,不想写. 稍微口胡一下,可以直接来一个树状数组套主席树,也就是 ...

  8. BZOJ 2120 数颜色 【带修改莫队】

    任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2120 2120: 数颜色 Time Limit: 6 Sec  Memory Limit: ...

  9. BZOJ 2120 数颜色(带修改莫队)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2120 [题目大意] 给出一颜色序列,每次可以修改一个位置的颜色或者询问一个区间不同颜色 ...

随机推荐

  1. 运行Tomcat 遇到的问题以及解决总结

    本文持续更新…… 情况一:在eclipse中启动tomcat提示 1. Server Tomcat v8.5 Server at localhost failed to start . 解决方案 方法 ...

  2. 服务器出现 nginx 502 Bad Gateway

    发生原因1.PHP FastCGI进程数不够用 当网站并发访问巨大时,php fastcgi的进程数不有一定的保障,因为cgi是单线程多进程工作的,也就是说cgi需要处理完一个页面后再继续下一个页面. ...

  3. ----堆栈 STL 函数库 ----有待补充

    #include<cstdio> #include<string> #include<vector> #include<iostream> using ...

  4. Python之列表生成式、生成器

    列表生成式 ——可以快速生成list,可以通过一个list推导出另一个list,而代码却十分简洁: >>> [x * x for x in range(1, 11)] [1, 4, ...

  5. [JOI2014] 小笼包

    题面 : https://www.ioi-jp.org/joi/2013/2014-yo/2014-yo-t6/2014-yo-t6.html 题解 dp + 康托展开 一看这题不知道怎么处理 只能枚 ...

  6. mysqli 进一步分析

    1. 一.mysql与mysqli的概念相关: 1.mysql与mysqli都是php方面的函数集,与mysql数据库关联不大. 2.在php5版本之前,一般是用php的mysql函数去驱动mysql ...

  7. kafka_2.11-0.8.2.2.tgz的3节点集群的下载、安装和配置(图文详解)

    kafka_2.10-0.8.1.1.tgz的1或3节点集群的下载.安装和配置(图文详细教程)绝对干货 一.安装前准备 1.1 示例机器 二. JDK7 安装 1.1 下载地址 下载地址: http: ...

  8. 什么是2MSL以及TIME_WAIT的作用

    TIME_WAIT主要是用来解决以下几个问题: 1)上面解释为什么主动关闭方需要进入TIME_WAIT状态中提到的: 主动关闭方需要进入TIME_WAIT以便能够重发丢掉的被动关闭方FIN包的ACK. ...

  9. Java多线程——线程之间的协作

    Java多线程——线程之间的协作 摘要:本文主要学习多线程之间是如何协作的,以及如何使用wait()方法与notify()/notifyAll()方法. 部分内容来自以下博客: https://www ...

  10. gdb如何保存和读取断点

    刚开始在linux下学编程使用gdb的同学可能会发现,每次用gdb设置断点调试程序,但下次打开的时候所有断点都没有了,很不方便.下面介绍保存和读取断点的方法. 1. 保存断点 先用info b 查看一 ...