【BZOJ3809】Gty的二逼妹子序列【莫队算法】【分块】
来源:互联网 发布:linux切换目录命令 编辑:程序博客网 时间:2024/05/09 03:49
【题目链接】
一开始写了个莫队+树状数组,T飞了。
需要对权值分块,然后分块查询。
/* Telekinetic Forest Guard */#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int maxn = 100005, maxm = 1000005;int n, m, num[maxn], belong[maxn], pre[maxn], last[maxn], size, ans[maxm], vis[maxn], blo[maxn];struct _que {int id, l, r, a, b;bool operator < (const _que &x) const {return belong[l] != belong[x.l] ? belong[l] < belong[x.l] : r < x.r;}} que[maxm];inline int iread() {int f = 1, x = 0; char ch = getchar();for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';return f * x;}inline int query(int a, int b) {int res = 0, L = belong[a], R = belong[b];for(int i = L + 1; i < R; i++) res += blo[i];if(L == R) {for(int i = a; i <= b; i++) if(vis[i]) res++;} else {for(int i = a; i <= last[a]; i++) if(vis[i]) res++;for(int i = pre[b]; i <= b; i++) if(vis[i]) res++;}return res;}inline void add(int x) {x = num[x];if(!vis[x]) blo[belong[x]]++;vis[x]++;}inline void del(int x) {x = num[x];vis[x]--;if(!vis[x]) blo[belong[x]]--;}int main() {n = iread(); m = iread(); size = (int)sqrt(n / 2);for(int i = 1; i <= n; i++) num[i] = iread(), belong[i] = (i - 1) / size + 1, pre[i] = (belong[i] - 1) * size + 1, last[i] = belong[i] * size;for(int i = 1; i <= m; i++) que[i] = (_que){i, iread(), iread(), iread(), iread()};sort(que + 1, que + 1 + m);int l = 1, r = 0;for(int i = 1; i <= m; i++) {for(; que[i].l < l; add(--l));for(; r < que[i].r; add(++r));for(; l < que[i].l; del(l++));for(; que[i].r < r; del(r--));ans[que[i].id] = query(que[i].a, que[i].b);}for(int i = 1; i <= m; i++) printf("%d\n", ans[i]);return 0;}
0 0
- 【BZOJ3809】Gty的二逼妹子序列【莫队算法】【分块】
- [BZOJ3809]Gty的二逼妹子序列(莫队+分块)
- 【bzoj3809】【GTY的二逼妹子序列】【莫队+分块】
- 【bzoj3809】Gty的二逼妹子序列 莫队+分块
- 【BZOJ3809】Gty的二逼妹子序列 (莫队+分块)
- bzoj3809 Gty的二逼妹子序列(莫队+分块)
- 【BZOJ3809】Gty的二逼妹子序列 莫队 分块
- bzoj3809: Gty的二逼妹子序列
- BZOJ3809: Gty的二逼妹子序列
- [bzoj3809]Gty的二逼妹子序列
- 【bzoj3809】Gty的二逼妹子序列
- bzoj3809 Gty的二逼妹子序列
- BZOJ3809: Gty的二逼妹子序列
- BZOJ3809 Gty的二逼妹子序列
- bzoj3809: Gty的二逼妹子序列
- BZOJ3809[Gty的二逼妹子序列]
- BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块
- BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块
- 297. Serialize and Deserialize Binary Tree 序列化及反序列化
- Frequent Pattern 挖掘之二(FP Growth算法)
- 电路设计中电阻的选择及其作用
- js学习二函数
- 访问页面异常:nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
- 【BZOJ3809】Gty的二逼妹子序列【莫队算法】【分块】
- ButterKnife框架原理和使用
- 神奇的树
- 就辣不行了
- hdu 4310 Hero
- 优酷去广告最新的关于如何屏蔽优酷广告的方法
- C++ string 类中substr的使用方法
- MySQL 5.7及以上解压缩版本配置安装
- postgresql在Ubuntu【虚拟机】上新建数据库 外部可访问(亲测)