题面

Bzoj

洛谷

题解

考虑对操作离线后分块处理询问操作(莫队算法),将询问操作按照编号分块后左端点第一关键字,右端点第二关键字排序(分块大小为$n^{\frac 23}$),对于每一个询问操作,记下当前最后一个修改操作。

之后就是莫队的板子了。

#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using std::min; using std::max;
using std::swap; using std::sort;
typedef long long ll;
using std::set; template<typename T>
void read(T &x) {
int flag = 1; x = 0; char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') flag = -flag; ch = getchar(); }
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag;
} const int N = 2e6 + 10, M = 5e4 + 10;
int n, m, siz, col[M], l, r, tot[N], ans[M], ret, now, bel[M];
struct Ques { int x, y, z, pre; } q[M]; int cntqu;
struct Modi { int pos, val; } o[M]; int cntmo;
inline bool cmp (const Ques &a, const Ques &b) {
if(bel[a.z] != bel[b.z]) return bel[a.z] < bel[b.z];
if(a.x != b.x) return a.x < b.x;
if(a.y != b.y) return a.y < b.y;
return a.pre < b.pre;
} inline void del (int c) { if(--tot[c] == 0) --ret; }
inline void add (int c) { if(++tot[c] == 1) ++ret; }
inline void doit (int now, int k) {
if(o[now].pos >= q[k].x && o[now].pos <= q[k].y)
del(col[o[now].pos]), add(o[now].val);
swap(col[o[now].pos], o[now].val);
//将操作记忆化,当处理替换时,相当于换过去,撤销时,相当于换回来。
} int main () {
read(n), read(m), siz = pow(n, 0.66666666);
for(int i = 1; i <= n; ++i)
read(col[i]), bel[i] = (i - 1) / siz + 1;
char opt;
while(m--) {
scanf("\n%c", &opt);
if(opt == 'Q') {
read(q[++cntqu].x), read(q[cntqu].y);
q[cntqu].z = cntqu, q[cntqu].pre = cntmo;
} else read(o[++cntmo].pos), read(o[cntmo].val);
}
sort(&q[1], &q[cntqu + 1], cmp), l = 1;
for(int i = 1; i <= cntqu; ++i) {
while(l < q[i].x) del(col[l++]);
while(l > q[i].x) add(col[--l]);
while(r < q[i].y) add(col[++r]);
while(r > q[i].y) del(col[r--]);
while(now < q[i].pre) doit(++now, i);
while(now > q[i].pre) doit(now--, i);
ans[q[i].z] = ret;
}
for(int i = 1; i <= cntqu; ++i)
printf("%d\n", ans[i]);
return 0;
}

Bzoj2120/洛谷P1903 数颜色(莫队)的更多相关文章

  1. [bzoj2120] [洛谷P1903] 数颜色

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

  2. 洛谷P1903 数颜色 [国家集训队] 莫队

    正解:带修莫队 解题报告: 可以理解为引入时间参数,然后就是有了仨参数,关于这个修改同样的是,如果时间是相同的,不用搞,如果时间不相同做一下时光倒流/时光推移就成嘛 但是肯定既然这样的话,按照原来的s ...

  3. BZOJ2120/洛谷P1903 [国家集训队] 数颜色 [带修改莫队]

    BZOJ传送门:洛谷传送门 数颜色 题目描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R ...

  4. bzoj2120 数颜色 莫队 带修改

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

  5. bzoj2120: 数颜色 [莫队][分块]

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

  6. BZOJ2120 数颜色 莫队 带修莫队

    原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ2120.html 题目传送门 - BZOJ2120 题意 给定一个长度为 $n$ 的序列 $a$ ,有 ...

  7. [BZOJ2120]数颜色(莫队算法)

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

  8. bzoj 3236: 洛谷 P4396: [AHOI2013]作业 (莫队, 分块)

    题目传送门:洛谷P4396. 题意简述: 给定一个长度为\(n\)的数列.有\(m\)次询问,每次询问区间\([l,r]\)中数值在\([a,b]\)之间的数的个数,和数值在\([a,b]\)之间的不 ...

  9. 洛谷 P4396 (离散化+莫队+树状数组)

    ### 洛谷P4396  题目链接 ### 题目大意: 有 n 个整数组成的数组,m 次询问,每次询问中有四个参数 l ,r,a,b .问你在[l,r] 的区间内的所有数中,值属于[a,b] 的数的个 ...

随机推荐

  1. [译] man 7 pthreads

    NAME      pthreads - POSIX threads   DESCRIPTION      POSIX.1 指定了一组叫做POSIX线程或Pthreads的编程接口(函数,头文件).单 ...

  2. 遍历hashmap

    转]Java中HashMap遍历的两种方式原文地址: http://www.javaweb.cc/language/java/032291.shtml 第一种: Map map = new HashM ...

  3. LintCode 406: Minimum Size

    LintCode 406: Minimum Size 题目描述 给定一个由 n 个整数组成的数组和一个正整数 s ,请找出该数组中满足其和 ≥ s 的最小长度子数组.如果无解,则返回 -1. 样例 给 ...

  4. FastDFS图片服务器java后台的简单调用

    工具类: package com.liveyc.common.fdfs; import org.apache.commons.io.FilenameUtils; import org.csource. ...

  5. HDU 1999 不可摸数 (模拟)

    题目链接 Problem Description s(n)是正整数n的真因子之和,即小于n且整除n的因子和.例如s(12)=1+2+3+4+6=16.如果任何数m,s(m)都不等于n,则称n为不可摸数 ...

  6. Test plan

    Options for Test Strategy: 1. Regular test: all the planned test cases will be executed 2. Extented ...

  7. 使用JSON语法创建JS对象(重要)

    JS对象的键值可以加单引号或者不加或者加双引号 JSON语法提供了一种更简单的方式来创建对象,可以避免书写函数,也可避免用new关键字,可以直接创建一个JS对象,使用一个花括号,然后将每个属性写成&q ...

  8. ubuntu sougou输入法

    1, 打开搜狗输入法Linux版的官网http://pinyin.sogou.com/linux/?r=pinyin,并下载你需要的版本,这里选择64位版. 2,在Ubuntu14.01下可以直接点击 ...

  9. c# 使用httpclient

    using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using ...

  10. 144.Binary Tree Preorder Traversal---二叉树先序、中序非递归遍历

    题目链接 题目大意:返回二叉树的先序遍历list.中序见94,后序见145. 法一:普通递归遍历,只是这里多了一个list数组,所以分成了两个函数.代码如下(耗时1ms): public List&l ...