传送门

分析:

虽然颜色种类很多,但是所有颜色个数之和n是一定的,这时候就可以使用vector对每个颜色维护一个坐标集合,空间只占n个。

对于查询L,R:直接一行: upper_bound(col[c].begin(), col[c].end(), r) - lower_bound(col[c].begin(), col[c].end(), l)

对于修改:

  1. 两种颜色相同,不管。
  2. 两种颜色不同:由于刚开始向每种颜色集合中加入坐标时,坐标是升序加入的,假设交换的是X和X+1,那么X变成X+1后整个集合仍然满足单调性,X+1同理,这样就可以省去排序的复杂度。

code

#include<bits/stdc++.h>
using namespace std;
namespace IO {
template<typename T>
inline void read(T &x) {
T i = 0, f = 1;
char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar()) i = (i << 3) + (i << 1) + (ch - '0');
x = i * f;
}
template<typename T>
inline void wr(T x) {
if(x < 0) putchar('-'), x = -x;
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
}
}using namespace IO;
const int N = 3e5 + 50;
vector<int> col[N];
int a[N], n, m; int main(){
read(n), read(m);
for(int i = 1; i <= n; i++){
int x; read(x); a[i] = x;
col[x].push_back(i);
}
for(int i = 1; i <= m; i++){
int opt; read(opt);
if(opt == 1){
int l, r, c; read(l), read(r), read(c);
wr(upper_bound(col[c].begin(), col[c].end(), r) - lower_bound(col[c].begin(), col[c].end(), l));
putchar('\n');
}
else {
int x; read(x);
if(a[x] != a[x + 1]){
vector<int>::iterator it = lower_bound(col[a[x]].begin(), col[a[x]].end(), x); (*it) = x + 1;
// sort(it, col[a[x]].end());
it = lower_bound(col[a[x + 1]].begin(), col[a[x + 1]].end(), x + 1); (*it) = x;
// sort(col[a[x + 1]].begin(), it + 1);
swap(a[x], a[x + 1]);
}
}
}
return 0;
}

luogu 3939 数颜色 - STL(vector)的更多相关文章

  1. 【题解】数颜色 STL vector数组

    小 C 的兔子不是雪白的,而是五彩缤纷的. 题目 题目描述 小 C 的兔子不是雪白的,而是五彩缤纷的.每只兔子都有一种颜色,不同的兔子可能有 相同的颜色.小 C 把她标号从 1 到 n 的 n只兔子排 ...

  2. Luogu 3939 数颜色

    随手点开一个题. 咦,这不是裸的动态开点线段树吗?写一个写一个…… Code: #include <cstdio> #include <cstring> using names ...

  3. [luogu]P3939 数颜色[二分]

    [luogu]P3939 数颜色 题目描述 小 C 的兔子不是雪白的,而是五彩缤纷的.每只兔子都有一种颜色,不同的兔子可能有 相同的颜色.小 C 把她标号从 1 到 n 的 n 只兔子排成长长的一排, ...

  4. Luogu 1903 数颜色 | 分块

    Luogu 1903 数颜色 | 分块 莫队不会啊-- 这道题直接分块也能卡过! 这道题的做法很有趣:对于每个位置i,记录它的颜色a[i]上一次出现的位置,记为pre[i]. 这样在查询一个区间[l, ...

  5. luogu P3939 数颜色 |vector

    题目描述 小 C 的兔子不是雪白的,而是五彩缤纷的.每只兔子都有一种颜色,不同的兔子可能有 相同的颜色.小 C 把她标号从 1 到 n 的 n 只兔子排成长长的一排,来给他们喂胡萝卜吃. 排列完成后, ...

  6. 洛谷P3939 数颜色(二分 vector)

    题意 题目链接 Sol 直接拿vector维护每种颜色的出现位置,然后二分一下. #include<bits/stdc++.h> using namespace std; const in ...

  7. 【模拟8.03】数颜色(vector//主席树)

    才知道vector在插入值后是可以直接修改的... 那就很简单了 用vector的lowerbound这样的二分操作,提前储存每个颜色的位置 发现交换相对位置不变 关于vector的lowerboun ...

  8. 洛谷——P3939 数颜色(暴力vecotr+二分)

    P3939 数颜色 $vecotr$里二分就是好用,全是$STL$ 颜色数目比较少,可以对每一种颜色弄一个$vector$记录一下,查找$l,r$内颜色数为$x$的兔子数,直接在$G[x]$这个$ve ...

  9. 2019.8.3 NOIP模拟测试12 反思总结【P3938 斐波那契,P3939 数颜色,P3940 分组】

    [题解在下面] 早上5:50,Gekoo同学来到机房并表态:“打暴力,打暴力就对了,打出来我就赢了.” 我:深以为然. (这是个伏笔) 据说hzoi的人还差两次考试[现在是一次了]就要重新分配机房,不 ...

随机推荐

  1. POJ 2253-Frogger (Prim)

    题目链接:Frogger 题意:两仅仅青蛙,A和B,A想到B哪里去,可是A得弹跳有限制,所以不能直接到B,可是有其它的石头作为过渡点,能够通过他们到达B,问A到B的全部路径中.它弹跳最大的跨度的最小值 ...

  2. Hibernate之API初识及增删改查实现

    声明:关于hibernate的学习.非常大一部分东西都是概念性的. 大家最好手里都有一份学习资料,在我的博文中.我不会把书本上的概念一类的东西搬过来.那没有不论什么意义.关于hibernate的学习, ...

  3. 学习笔记(四):jQuery之动画效果

    1.show()显示效果 语法:show(speed,callback) Number/String,Function speend为动画执行时间,单位为毫秒.也可以为slow"," ...

  4. rhel5安装 oracle11

    readhat 安装11gr2文档 需要注意的地方:必须关掉的 1,防火墙:2,SElinux . root 用户运行  setup  命令可关防火墙与SElinux 修改网络配置文件,一定要重启此文 ...

  5. opencv cvPreCornerDetect

    关于OpenCv中cvPreCornerDetect 运行出错解决方法 http://m.blog.csdn.net/blog/wode0239 由于书本上示例的不全,相信大家在做的时候,肯定是无从下 ...

  6. 【例题 7-2 UVA - 11059】Maximum Product

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] C语言for循环练习题 [代码] /* 1.Shoud it use long long ? 2.Have you ever tes ...

  7. js进阶 14-1 jquery的ajax系列中的load方法的作用是什么

    js进阶 14-1 jquery的ajax系列中的load方法的作用是什么 一.总结 一句话总结:jQuery load()方法作用是从服务器加载数据,是一个简单但强大的AJAX方法. 1.load函 ...

  8. 编程一一C语言的问题,cpu中的专用寄存器

  9. 使用mingw制作dll文件

    使用mingw制作dll文件 安装mingw 准备math.c文件 //math.c #include<stdio.h> int add(int a,int b){ return a+b; ...

  10. Centos NFS 简单设置

    Server 端: NFS的安装配置:centos 5 :yum install nfs-utils portmapcentos 6 :yum install nfs-utils rpcbind vi ...