bzoj 2120: 数颜色
来源:互联网 发布:二手域名交易平台 编辑:程序博客网 时间:2024/05/21 06:17
莫队水过
每次修改之间的询问用莫队搞一下,还挺快……
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <map>using namespace std;#define MAXN 100003#define MAXQ 100003int n, a[MAXN], sqrtn;struct Qtype{ int num; char opt; int x, y;} qy[MAXQ];bool cmp(const Qtype &a, const Qtype &b){ int ax = a.x / sqrtn, bx = b.x / sqrtn; if (ax != bx) return ax < bx; return a.y < b.y;}map<int, int> lisan;int lct = 0;int ct[MAXN], ans[MAXN];void work(int ql, int qr){ int l, r, m; l = r = m = 0; memset(ct, 0, sizeof(ct)); for (int i = ql; i < qr; ++i) qy[i].num = i - ql; sort(qy + ql, qy + qr, cmp); for (int i = ql; i < qr; ++i) { while (l < qy[i].x) { if (--ct[a[l]] == 0) --m; ++l; } while (l > qy[i].x) { --l; if (++ct[a[l]] == 1) ++m; } while (r < qy[i].y) { if (++ct[a[r]] == 1) ++m; ++r; } while (r > qy[i].y) { --r; if (--ct[a[r]] == 0) --m; } ans[qy[i].num] = m; } for (int i = ql; i < qr; ++i) printf("%d\n", ans[i - ql]);}int main(){ int Q; scanf("%d%d", &n, &Q); sqrtn = sqrt(n); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); for (int i = 0; i < Q; ++i) { scanf(" %c%d%d", &qy[i].opt, &qy[i].x, &qy[i].y); --qy[i].x; } for (int i = 0; i < n; ++i) { map<int, int>::iterator it = lisan.find(a[i]); if (it == lisan.end()) { lisan[a[i]] = lct; a[i] = lct++; } else a[i] = it->second; } for (int i = 0; i < Q; ++i) if (qy[i].opt == 'R') { map<int, int>::iterator it = lisan.find(qy[i].y); if (it == lisan.end()) { lisan[qy[i].y] = lct; qy[i].y = lct++; } else qy[i].y = it->second; } int last = 0; for (int i = 0; i <= Q; ++i) if (i == Q) work(last, i); else if (qy[i].opt == 'R') { work(last, i); a[qy[i].x] = qy[i].y; last = i + 1; } return 0;}
0 0
- bzoj 2120: 数颜色
- bzoj 2120: 数颜色
- bzoj 2120: 数颜色
- BZOJ 2120 数颜色
- BZOJ 2120 数颜色
- BZOJ 2120: 数颜色
- BZOJ 2120 数颜色
- bzoj 2120 数颜色 题解
- BZOJ 2120 数颜色 暴力
- bzoj 2120: 数颜色(分块)
- 【BZOJ 2120】【国家集训队 2011】【数颜色】
- 【BZOJ 2120】 数颜色 (分块,暴力)
- BZOJ 2120 数颜色 分块+二分
- bzoj 2120 数颜色(可持久化莫队)
- 【bzoj 2120】维护队列【bzoj 2453】数颜色 双倍经验!!!
- BZOJ 2453: 维护队列&&BZOJ 2120 数颜色 分块
- bzoj 2120 数颜色 树状数组套平衡树
- 【BZOJ 2120】 数颜色 (乱搞分块,直指暴力)
- 如何让自己的Android程序、Service永不被系统kill
- 二维图形的几何变换
- UVa12696 - Cabin Baggage(水题)
- Linux USB驱动工作流程
- drupal install module
- bzoj 2120: 数颜色
- HDU 2087 剪花布条
- mysql导入tab分隔的文本文件
- 【HDU 3436】Queue-jumpers(Splay)
- JAVA中final
- SDUTOJ 1466--双向队列
- 布隆过滤器
- HDU 3336 Count the string
- 关于NHibernate实体和hbm.xml的那些错误