[csu/coj 1080]划分树求区间前k大数和

题意:从某个区间内最多选择k个数,使得和最大

思路:首先题目给定的数有负数,如果区间前k大出现负数,那么负数不选和更大,于是对于所有最优选择,负数不会出现,所以用0取代负数,问题便转化为区间的前k大数和。

划分树:

[1  6  3  8  5  4  7  2]

[6  8  5  7][1  3  4  2]

[8  7][6  5][3  4][1  2]

[8][7][6][5][4][3][2][1]

把快排的结果从上至下依次放入线段树,就构成了划分树,划分的意思就是选定一个数,把原序列分成两块,使得左边整体大于右边,而一个块内的数在原序列的相对位置不发生变化。划分树的过程基本就是初始化,建树,和查找。初始化只要把原序列导入划分树的根就行了,建树过程依赖排好序的原序列来得到用来“划分”的数,查找过程也很简单,不需要像线段树那样将询问分解,在树上查找答案的时候是左右子树二选一。

划分树一般用来求区间第k大数,对于划分树为什么可以求区间k大和,见代码。

  1 #pragma comment(linker, "/STACK:10240000,10240000")
  2
  3 #include <iostream>
  4 #include <cstdio>
  5 #include <algorithm>
  6 #include <cstdlib>
  7 #include <cstring>
  8 #include <map>
  9 #include <queue>
 10 #include <deque>
 11 #include <cmath>
 12 #include <vector>
 13 #include <ctime>
 14 #include <cctype>
 15 #include <set>
 16 #include <bitset>
 17 #include <functional>
 18 #include <numeric>
 19 #include <stdexcept>
 20 #include <utility>
 21
 22 using namespace std;
 23
 24 #define mem0(a) memset(a, 0, sizeof(a))
 25 #define mem_1(a) memset(a, -1, sizeof(a))
 26 #define lson l, m, rt << 1
 27 #define rson m + 1, r, rt << 1 | 1
 28 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
 29 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
 30 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
 31 #define rep_down1(a, b) for (int a = b; a > 0; a--)
 32 #define all(a) (a).begin(), (a).end()
 33 #define lowbit(x) ((x) & (-(x)))
 34 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
 35 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
 36 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
 37 #define pchr(a) putchar(a)
 38 #define pstr(a) printf("%s", a)
 39 #define sstr(a) scanf("%s", a)
 40 #define sint(a) scanf("%d", &a)
 41 #define sint2(a, b) scanf("%d%d", &a, &b)
 42 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
 43 #define pint(a) printf("%d\n", a)
 44 #define test_print1(a) cout << "var1 = " << a << endl
 45 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
 46 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
 47 #define mp(a, b) make_pair(a, b)
 48 #define pb(a) push_back(a)
 49
 50 typedef unsigned int uint;
 51 typedef long long LL;
 52 typedef pair<int, int> pii;
 53 typedef vector<int> vi;
 54
 55 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
 56 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
 57 const int maxn = 1e4 + 17;
 58 const int md = 1e9 + 7;
 59 const int inf = 1e9 + 7;
 60 const LL inf_L = 1e18 + 7;
 61 const double pi = acos(-1.0);
 62 const double eps = 1e-6;
 63
 64 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
 65 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
 66 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
 67 template<class T>T condition(bool f, T a, T b){return f?a:b;}
 68 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
 69 int make_id(int x, int y, int n) { return x * n + y; }
 70
 71 /// 划分树求区间前k大和
 72 /// 关于为什么可以用前缀和来求前k大的和:对于一个询问区间,如果前k大在左子树,
 73 /// 那么这k个数会按原顺序依次进入左子树,进入左子树后它们之间不会有其它不是前k大的数,
 74 /// 所以对应到下一层也是连续的一段,于是可用前缀和来得到,对于前k大跨区间的情况同理。
 75 /// 区间范围为:1 ~ n
 76 struct PartitionTree {
 77     int val[20][maxn], sum[20][maxn], cnt[20][maxn];//划分后的结果,前缀和,进入左子树的个数
 78     void init(int a[], int l, int r) {
 79         mem0(val);
 80         mem0(sum);
 81         mem0(cnt);
 82         for (int i = l; i <= r; i ++) sum[0][i] = sum[0][i - 1] + (val[0][i] = a[i]);
 83     }
 84     /// 向下更新val和sum数组,同时维护cnt数组的值
 85     void build(int b[], int l, int r, int dep) {
 86         if (l == r) return ;
 87         int m = (l + r) >> 1;
 88         /// c记录大于中位数的数的个数,cc记录进入左子树的等于中位数的数的个数,由于相等情况的存在,这个信息必不可少。
 89         int lc = 0, rc = 0, lt = (r - l + 2) >> 1, c = 0, cc = 0;
 90         for (int i = l; i <= r; i ++) c += val[dep][i] > b[m];
 91         for (int i = l; i <= r; i ++) {
 92             cnt[dep][i] = cnt[dep][i - 1];
 93             if (lc < lt && (val[dep][i] > b[m] || val[dep][i] == b[m] && cc < lt - c)) {
 94                 val[dep + 1][l + lc ++] = val[dep][i];
 95                 cnt[dep][i] ++;
 96                 if (val[dep][i] == b[m]) cc ++;
 97             }
 98             else {
 99                 val[dep + 1][m + 1 + rc ++] = val[dep][i];
100             }
101         }
102
103         for (int i = l; i <= r; i ++) sum[dep + 1][i] = sum[dep + 1][i - 1] + val[dep + 1][i];
104         build(b, l, m, dep + 1);
105         build(b, m + 1, r, dep + 1);
106     }
107     int query_ksum(int L, int R, int k, int l, int r, int dep) {
108         if (k == 0) return 0;
109         if (l == r) return val[dep][l];
110         int m = (l + r) >> 1, c = cnt[dep][R] - cnt[dep][L - 1];
111         if (c >= k) {
112             int x = cnt[dep][L - 1] - cnt[dep][l - 1], y = cnt[dep][R] - cnt[dep][l - 1];
113             return query_ksum(l + x, l + y - 1, k, l, m, dep + 1);
114         }
115         else {
116             int x0 = cnt[dep][L - 1] - cnt[dep][l - 1], x = L - l - cnt[dep][L - 1] + cnt[dep][l - 1], y = R - l + 1 - cnt[dep][R] + cnt[dep][l - 1];
117             return sum[dep + 1][l + x0 - 1 + c] - sum[dep + 1][l + x0 - 1] + query_ksum(m + 1 + x, m + 1 + y - 1, k - c, m + 1, r, dep + 1);
118         }
119     }
120
121 };
122 PartitionTree pt;
123 pii node[maxn];
124 int a[maxn], b[maxn], p[maxn];
125
126 bool cmp(int i, int j) {
127     return i > j;
128 }
129 int main() {
130     //freopen("in.txt", "r", stdin);
131     int n, m;
132     while (cin >> n) {
133         rep_up0(i, n) {
134             sint2(node[i].first, node[i].second);
135             max_update(node[i].second, 0);
136         }
137         sort(node, node + n);
138         rep_up0(i, n) {
139             b[i] = a[i] = node[i].second;
140             p[i] = node[i].first;
141         }
142         sort(b, b + n, cmp);
143         pt.init(a - 1, 1, n);
144         pt.build(b - 1, 1, n, 0);
145         cin >> m;
146         rep_up0(i, m) {
147             int l, r, k;
148             sint3(l, r, k);
149             l = lower_bound(p, p + n, l) - p + 1;
150             r = upper_bound(p, p + n, r) - p;
151             min_update(k, r - l + 1);
152             printf("%d\n", pt.query_ksum(l, r, k, 1, n, 0));
153         }
154         cout << endl;
155     }
156     return 0;
157 }

时间: 2024-10-03 22:15:41

[csu/coj 1080]划分树求区间前k大数和的相关文章

poj 2401 划分树 求区间第k大的数

题目:http://poj.org/problem?id=2104 划分树待我好好理解下再写个教程吧,觉得网上的内容一般,,, 模板题: 贴代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define CLR(a) memset(a,0,sizeof(a)) const int MAXN = 1000

HDOJ题目4417 Super Mario(划分树求区间比k小的个数+二分)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3313    Accepted Submission(s): 1548 Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping a

hdu 2665 可持久化线段树求区间第K大值(函数式线段树||主席树)

http://acm.hdu.edu.cn/showproblem.php?pid=2665 Problem Description Give you a sequence and ask you the kth big number of a inteval. Input The first line is the number of the test cases. For each test case, the first line contain two integer n and m (

poj 2104主席树求区间第k小

POJ - 2104 题意:求区间第k小 思路:无修改主席树 AC代码: #include "iostream" #include "iomanip" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set&

主席树|求区间第k小模板

主席树 学了主席树,用来求区间上的第k小 写一下自己整理后的模板 求区间第k小 #include<bits/stdc++.h> using namespace std; //求区间第k小 const int maxn = 500010; struct node{ int v,lc,rc; }T[maxn * 21]; int n,m; int root[maxn]; int e; void insert(int pre,int cur,int pos,int l,int r){ if(l ==

HDU 3473-Minimum Sum(划分树-求区间sigma最小值)

Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3710    Accepted Submission(s): 852 Problem Description You are given N positive integers, denoted as x0, x1 ... xN-1. Then give you

POJ 2761 Feed the dogs(树状数组求区间第K大)

题目链接: 戳我 题目大意:Jiajia要为宠物狗,宠物狗按成一排站好(1 < i <= n),第 i 只狗的喜欢程度是 a[i], 之后他会先喂某个区间内第k个 即 n 个数, m个询问,接着是 n个数 接下来 m 行,每行是 l r k即 l 到 r 这个区间第 k 小的数,每个询问输出一个答案,即 a[i] 求区间第k大有很多算法, 详见此博客 [数据结构练习] 求区间第K大数的几种方法 我用的树状数组解法,来自 树状数组从前往后求和,用来解第k大(或小)的数 poj 2985 The

2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)

原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q questions. The i-th question is whether P remains balanced after pai and pbi  swapped. Note that questions ar

poj2104 求区间第k大 可持久化线段树

poj2104 求区间第k大  可持久化线段树 #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) using namespace std; typedef