luogu 3939 数颜色 - STL(vector)
分析:
虽然颜色种类很多,但是所有颜色个数之和n是一定的,这时候就可以使用vector对每个颜色维护一个坐标集合,空间只占n个。
对于查询L,R:直接一行: upper_bound(col[c].begin(), col[c].end(), r) - lower_bound(col[c].begin(), col[c].end(), l)
对于修改:
- 两种颜色相同,不管。
- 两种颜色不同:由于刚开始向每种颜色集合中加入坐标时,坐标是升序加入的,假设交换的是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)的更多相关文章
- 【题解】数颜色 STL vector数组
小 C 的兔子不是雪白的,而是五彩缤纷的. 题目 题目描述 小 C 的兔子不是雪白的,而是五彩缤纷的.每只兔子都有一种颜色,不同的兔子可能有 相同的颜色.小 C 把她标号从 1 到 n 的 n只兔子排 ...
- Luogu 3939 数颜色
随手点开一个题. 咦,这不是裸的动态开点线段树吗?写一个写一个…… Code: #include <cstdio> #include <cstring> using names ...
- [luogu]P3939 数颜色[二分]
[luogu]P3939 数颜色 题目描述 小 C 的兔子不是雪白的,而是五彩缤纷的.每只兔子都有一种颜色,不同的兔子可能有 相同的颜色.小 C 把她标号从 1 到 n 的 n 只兔子排成长长的一排, ...
- Luogu 1903 数颜色 | 分块
Luogu 1903 数颜色 | 分块 莫队不会啊-- 这道题直接分块也能卡过! 这道题的做法很有趣:对于每个位置i,记录它的颜色a[i]上一次出现的位置,记为pre[i]. 这样在查询一个区间[l, ...
- luogu P3939 数颜色 |vector
题目描述 小 C 的兔子不是雪白的,而是五彩缤纷的.每只兔子都有一种颜色,不同的兔子可能有 相同的颜色.小 C 把她标号从 1 到 n 的 n 只兔子排成长长的一排,来给他们喂胡萝卜吃. 排列完成后, ...
- 洛谷P3939 数颜色(二分 vector)
题意 题目链接 Sol 直接拿vector维护每种颜色的出现位置,然后二分一下. #include<bits/stdc++.h> using namespace std; const in ...
- 【模拟8.03】数颜色(vector//主席树)
才知道vector在插入值后是可以直接修改的... 那就很简单了 用vector的lowerbound这样的二分操作,提前储存每个颜色的位置 发现交换相对位置不变 关于vector的lowerboun ...
- 洛谷——P3939 数颜色(暴力vecotr+二分)
P3939 数颜色 $vecotr$里二分就是好用,全是$STL$ 颜色数目比较少,可以对每一种颜色弄一个$vector$记录一下,查找$l,r$内颜色数为$x$的兔子数,直接在$G[x]$这个$ve ...
- 2019.8.3 NOIP模拟测试12 反思总结【P3938 斐波那契,P3939 数颜色,P3940 分组】
[题解在下面] 早上5:50,Gekoo同学来到机房并表态:“打暴力,打暴力就对了,打出来我就赢了.” 我:深以为然. (这是个伏笔) 据说hzoi的人还差两次考试[现在是一次了]就要重新分配机房,不 ...
随机推荐
- [Javascript AST] 3. Continue: Write ESLint rule
The rule we want to write is show warning if user using console method: // valid foo.console() conso ...
- HDU 5389 Zero Escape (MUT#8 dp优化)
[题目链接]:pid=5389">click here~~ [题目大意]: 题意: 给出n个人的id,有两个门,每一个门有一个标号,我们记作a和b,如今我们要将n个人分成两组,进入两个 ...
- 22. Spring Boot 动态数据源(多数据源自动切换)
转自:https://blog.csdn.net/catoop/article/details/50575038
- 1.3 Quick Start中 Step 2: Start the server官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Step 2: Start the server Step : 启动服务 Kafka ...
- 软件——机器学习与Python,Python3的输出与输入
输出 用print()在括号中加上字符串,就可以向屏幕上输出指定的文字.比如输出'hello, world',用代码实现如下: >>> print('hello, world') p ...
- UIViewController所有API的学习。
<欢迎大家加入iOS开发学习交流群:QQ529560119> /* UIViewController is a generic controller base class tha ...
- Could not find action or result: There is no Action mapped for namespace [/] and action name [GetG
Could not find action or result: /car/GetGpsDataAction There is no Action mapped for namespace [/] ...
- JS学习笔记 - 面向对象
类.对象类:模子对象:产品(成品) 蛋糕(对象) 模子(类) Array 类 arr 对象 Array.push(); 错 arr.push(); 对 new arr(); 错 原型prototype ...
- Oracle数据库(二)
指令来练习 1.password,修改密码输入旧命令,在输入新的命令 2.查询当前用户 show user: 2.查询用户下的所有对象,使用tab表,tab是每一个用户都有的 select *from ...
- 每日技术总结:setInterval,setTimeout,文本溢出,小程序,wepy
前言: 项目背景:vue,电商,商品详情页 1.倒计时,倒计到0秒时停止 data () { return { n: 10 } }, created () { let int = setInterva ...