Luogu 3939 数颜色
随手点开一个题。

咦,这不是裸的动态开点线段树吗?写一个写一个……
Code:
#include <cstdio>
#include <cstring>
using namespace std; const int N = 3e5 + ; int n, qn, a[N]; inline void read(int &X) {
X = ;
char ch = ;
int op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} struct Node {
int lc, rc, sum;
}; namespace PSegT {
int root[N], nodeCnt;
Node s[N * ]; #define mid ((l + r) >> 1) void up(int p) {
if(p) s[p].sum = s[s[p].lc].sum + s[s[p].rc].sum;
} void ins(int &p, int l, int r, int x) {
if(!p) p = ++nodeCnt;
if(l == x && x == r) {
s[p].sum++;
return;
} if(x <= mid) ins(s[p].lc, l, mid, x);
else ins(s[p].rc, mid + , r, x);
up(p);
} void del(int &p, int l, int r, int x) {
if(l == x && x == r) {
s[p].sum--;
if(s[p].sum == ) p = ;
return;
}
if(x <= mid) del(s[p].lc, l, mid, x);
else del(s[p].rc, mid + , r, x);
up(p); if(!s[p].lc && !s[p].rc && !s[p].sum) p = ;
} int query(int p, int l, int r, int x, int y) {
if(!p) return ;
if(x <= l && y >= r) return s[p].sum; int res = ;
if(x <= mid) res += query(s[p].lc, l, mid, x, y);
if(y > mid) res += query(s[p].rc, mid + , r, x, y);
return res;
} } using namespace PSegT; inline void swap(int &x, int &y) {
int t = x;
x = y;
y = t;
} int main() {
read(n), read(qn);
nodeCnt = ;
for(int i = ; i <= n; i++) {
read(a[i]);
ins(root[a[i]], , n, i);
} for(int op; qn--; ) {
read(op);
if(op == ) {
int x, y, c;
read(x), read(y), read(c);
printf("%d\n", query(root[c], , n, x, y));
} else {
int x;
read(x);
del(root[a[x]], , n, x);
del(root[a[x + ]], , n, x + );
swap(a[x], a[x + ]);
ins(root[a[x]], , n, x);
ins(root[a[x + ]], , n, x + );
}
} return ;
}
写完题的我:
内存太小,差评,卡常数,差评……这sb题……
点开题解:

……大概说的就是我……

没事复杂度其实是一样的
警醒a!!!不要一上手就乱写数据结构……
Luogu 3939 数颜色的更多相关文章
- luogu 3939 数颜色 - STL(vector)
传送门 分析: 虽然颜色种类很多,但是所有颜色个数之和n是一定的,这时候就可以使用vector对每个颜色维护一个坐标集合,空间只占n个. 对于查询L,R:直接一行: upper_bound(col[c ...
- Luogu 1903 数颜色 | 分块
Luogu 1903 数颜色 | 分块 莫队不会啊-- 这道题直接分块也能卡过! 这道题的做法很有趣:对于每个位置i,记录它的颜色a[i]上一次出现的位置,记为pre[i]. 这样在查询一个区间[l, ...
- [luogu]P3939 数颜色[二分]
[luogu]P3939 数颜色 题目描述 小 C 的兔子不是雪白的,而是五彩缤纷的.每只兔子都有一种颜色,不同的兔子可能有 相同的颜色.小 C 把她标号从 1 到 n 的 n 只兔子排成长长的一排, ...
- luogu P3939 数颜色 |vector
题目描述 小 C 的兔子不是雪白的,而是五彩缤纷的.每只兔子都有一种颜色,不同的兔子可能有 相同的颜色.小 C 把她标号从 1 到 n 的 n 只兔子排成长长的一排,来给他们喂胡萝卜吃. 排列完成后, ...
- P3939 数颜色 线段树动态开点
P3939 数颜色 线段树动态开点 luogu P3939 水.直接对每种颜色开个权值线段树即可,注意动态开点. #include <cstdio> #include <algori ...
- BZOJ 2120: 数颜色
2120: 数颜色 Time Limit: 6 Sec Memory Limit: 259 MBSubmit: 3623 Solved: 1396[Submit][Status][Discuss] ...
- 【BZOJ-2453&2120】维护队列&数颜色 分块 + 带修莫队算法
2453: 维护队列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 653 Solved: 283[Submit][Status][Discuss] ...
- BZOJ 2120 数颜色(带修改的莫队)
2120: 数颜色 Time Limit: 6 Sec Memory Limit: 259 MB Submit: 3478 Solved: 1342 [Submit][Status][Discus ...
- BZOJ 2120: 数颜色 分块
2120: 数颜色 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php? ...
随机推荐
- IDEA+testng,输出没有test-output目录
参考:http://www.cnblogs.com/veitch-623/p/6192601.html 在Edit Configurations里 使用默认报告就行
- IE9 placeholder 不兼容的解决
坑爹的IE9-,真的是够够的了,不过公司不要求兼容这个玩意了,自己觉得兼容这个鬼还是挺有挑战性的,自己也碰到不少难题,一个个解决. css: .placeholderColor { color : # ...
- 线程存储(Thread Specific Data)
线程中特有的线程存储, Thread Specific Data .线程存储有什么用了?他是什么意思了? 大家都知道,在多线程程序中,所有线程共享程序中的变量.现在有一全局变量,所有线程都可以使用它, ...
- JavaScript创建对象的几种重要模式
一.工厂模式 1. 代码示例 function person(name, age) { var p = new object(); p.name = name; p.age = age; p.sayN ...
- linux环境下搭建redis
1. 官网下载安装包,然后解压,或者直接从github上pull下来. git clone https://github.com/antirez/redis.git 2. 确保linux环境上已安装g ...
- DIDAO.Common --- 项目中的常用类及其中函数
常用函数: CommonHelper.cs using System; using System.Collections.Generic; using System.IO; using System. ...
- WCF svcutil工具
通过SvcUtil.exe生成客户端代码和配置 WCF服务调用通过两种常用的方式:一种是借助代码生成工具SvcUtil.exe或者添加服务引用的方式,一种是通过ChannelFactory直接创建服务 ...
- 机器学习:线性回归法(Linear Regression)
# 注:使用线性回归算法的前提是,假设数据存在线性关系,如果最后求得的准确度R < 0,则说明很可能数据间不存在任何线性关系(也可能是算法中间出现错误),此时就要检查算法或者考虑使用其它算法: ...
- 【转】onclick事件与href='javascript:function()'的区别
href='javascript:function()'和onclick能起到同样的效果,一般来说,如果要调用脚本还是在onclick事件里面写代码,而不推荐在href='javascript:fun ...
- springmvc----demo3---rest风格---bai
input_stu_path.jsp: showinput_stu_path.jsp: