BZOJ 3339/3585 Rmq Problem/mex 莫队算法
2016-03-13 15:29
513 查看
连线段树都不愿意打了。。明明线段树要短
至于区间转移
如果是新增一个数字,和当前维护的mex值比较,如果相等,即已经把最小缺失的数字补上了,因此暴力查找下一个数字。另外大于n的值不会影响答案,所以忽略即可。其实想想写个树状数组应该也可?带两个log伤不起。。
如果删除一个数字,而且比当前维护的mex值小,更新答案。
至于区间转移
如果是新增一个数字,和当前维护的mex值比较,如果相等,即已经把最小缺失的数字补上了,因此暴力查找下一个数字。另外大于n的值不会影响答案,所以忽略即可。其实想想写个树状数组应该也可?带两个log伤不起。。
如果删除一个数字,而且比当前维护的mex值小,更新答案。
#include <cmath> #include <cstdio> #include <algorithm> using namespace std; const int N = 200005; int block , a , c , x , t ; struct Query { int l, r, i; } q ; bool operator< (const Query &a, const Query &b) { return block[a.l] == block[b.l] ? a.r < b.r : a.l < b.l; } int read() { int s = 0, f = 1; char ch = getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();} return s * f; } int now = 0; void add(int x) { if (x > n) return; ++t[x]; if (x == now || !x) while(t[now]) ++now; } void del(int x) { if (x > n) return; --t[x]; if (x < now && !t[x]) now = x; } void transform(int &l, int &r, int &ans, int ql, int qr) { while (r < qr) add(c[++r]); while (r > qr) del(c[r--]); while (l < ql) del(c[l++]); while (l > ql) add(c[--l]); ans = now; } int main() { int n, i, l = 1, r = 0, m, sz; sz = (int) sqrt(n = read()); m = read(); for (i = 1; i <= n; ++i) c[i] = read(), block[i] = (i - 1) / sz + 1; for (i = 1; i <= m; ++i) q[i].l = read(), q[i].r = read(), q[i].i = i; sort(q + 1, q + m + 1); for (i = 1; i <= m; ++i) transform(l, r, x[q[i].i], q[i].l, q[i].r); for (i = 1; i <= m; ++i) printf("%u\n", x[i]); return 0; }
相关文章推荐
- js数据层中间件单页运用简单构思和应用案例
- 递归与算法分析(一)递归总论
- 第一次上机实验-2
- [转载]java 中finally关键字的使用
- 新建应用程序集注意事项
- hdoj 最小长方形 1859 (水)
- 第2周项目2—本月有几天?
- DynamicDataDisplay 不压缩横坐标和图例的修改
- leetcode:41. First Missing Positive (Java)
- 3月11日作业重点及点评
- win7下ftp及svn服务器创建
- Python Adaboost 实现MNIST 分类
- 波动数列
- CocoaPods安装及使用
- 图的邻接矩阵存储表示 深度优先和广度优先遍历
- Jetty反向代理
- 单元测试
- 读书这件小事
- 进程描述与控制
- 求一个整数是否可以被3整除