[CF538F]A Heap of Heaps && 持久化线段树
来源:互联网 发布:剑三军爷男神捏脸数据 编辑:程序博客网 时间:2024/05/29 08:45
直接枚举k 每个有儿子的节点和他的儿子区间的起点 容易发现这样枚举是调和级数
然后需要一个东西查询某段区间比x小的数 上主席树即可
#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<queue>#define SF scanf#define PF printfusing namespace std;typedef long long LL;const int MAXN = 200000;int root[MAXN+10], A[MAXN+10], B[MAXN+10], n;struct Node {int ls, rs, sum;} ;struct Seg_Tree {Node tree[MAXN*100+10];int ncnt;void NewNode(int &i) {tree[++ncnt] = tree[i];i = ncnt;}void ins(int &i, int L, int R, int val) {NewNode(i);tree[i].sum++;if(L == R) return ;int mid = (L+R) >> 1;if(val <= mid) ins(tree[i].ls, L, mid, val);else ins(tree[i].rs, mid+1, R, val);}int query(int i, int L, int R, int l, int r) {if(l > R || r < L) return 0;if(l <= L && R <= r) return tree[i].sum;return query(tree[i].ls, L, (L+R)>>1, l, r) + query(tree[i].rs, ((L+R)>>1)+1, R, l, r);}void build(int &i, int L, int R) {NewNode(i);if(L == R) return ;int mid = (L+R) >> 1;build(tree[i].ls, L, mid);build(tree[i].rs, mid+1, R);}} seg;int query(int L, int R, int x) {if(R > n) R = n;return seg.query(root[R], 1, n, 1, x-1) - seg.query(root[L-1], 1, n, 1, x-1);}int main() {SF("%d", &n);seg.build(root[0], 1, n);for(int i = 1; i <= n; i++) SF("%d", &A[i]), B[i] = A[i];sort(B+1, B+1+n);int Len = unique(B+1, B+1+n) - (B+1);for(int i = 1; i <= n; i++) A[i] = lower_bound(B+1, B+1+Len, A[i]) - B;for(int i = 1; i <= n; i++) {root[i] = root[i-1];seg.ins(root[i], 1, n, A[i]);}for(int k = 1; k < n; k++) {LL ans = 0;for(int i = 1, j = 1; j <= n; i++, j += k)ans += query(j+1, j+k, A[i]);cout << ans << ' ';}puts("");return 0;}/*51 21 32 42 5*/
0 0
- [CF538F]A Heap of Heaps && 持久化线段树
- [CF538F]A Heap of Heaps
- Codeforces 538F A Heap of Heaps 离线+树状数组+离散化
- 51nod 1533 一堆的堆 & Codeforces538F A Heap of Heaps(树状数组||主席树)
- Codeforces538F A Heap of Heaps【分块+差分求前缀和】
- bzoj 3489: A simple rmq problem 可持久化线段树套可持久化线段树
- 可持久化线段树
- 可持久化线段树
- 可持久化线段树
- 可持久化线段树
- 可持久化线段树
- 可持久化线段树
- 可持久化线段树
- 【CF768G】The Winds of Winter 可持久化线段树 DFS序
- 2588: Spoj 10628. Count on a tree[可持久化线段树+倍增lca]
- BZOJ 2588 Count on a tree (COT) 可持久化线段树
- BZOJ 3218 a + b Problem 可持久化线段树+最小割
- 【BZOJ2588】【Spoj 10628.】 Count on a tree 可持久化线段树+lca
- Android FTM Porting笔记
- 第5题
- Qt学习
- IOS 如何选择delegate、notification、KVO?
- 模板方法和接口参数的联合使用
- [CF538F]A Heap of Heaps && 持久化线段树
- 第四章第5题
- 第4章 第7题
- 2.1 各种标签
- CACTI :Required modules for PHP Weathermap 0.97c were not present. Not running
- 编程之美2.17—数组循环移位(旋转数组)
- Linux串口驱动分析初始化
- getBaseContext(),getApplication(),getApplicationContext()
- 市赛回顾