省选模拟(12.08) T1 逐梦逐梦逐梦

来源:互联网 发布:美萍进销存软件破解版 编辑:程序博客网 时间:2024/06/15 12:45

逐梦逐梦逐梦

题目背景:

12.08 省选模拟T1

分析:主席树

 

这个题,除了细节太多,代码太难写,实现起来比较烦以外······其他还是很简单的。需要支持区间前k大求和,以及区间查找第k个偶数或者奇数,那么我们需要维护的就是当前权值区间的所有数的个数,奇数的个数,偶数的个数,以及所有数的和,因为是区间上的查询,所以外面套主席树就可以了,对于一次询问,先求当前区间的前k大之和,以及前k个中有多少个奇数,多少个偶数,如果和为偶数则就是答案,否则需要查找下一个奇数,以及下一个偶数,减去和中的最后一个奇数,然后加上下一个偶数,或者减去和中的最后一个偶数,加上下一个奇数,两者中的较大值,即为答案,复杂度大常数O(nlogn)

  

Source:

/*created by scarlyw*/#include <cstdio>#include <string>#include <algorithm>#include <cstring>#include <iostream>#include <cmath>#include <cctype>#include <vector>#include <set>#include <queue>inline char read() {static const int IN_LEN = 1024 * 1024;static char buf[IN_LEN], *s, *t;if (s == t) {t = (s = buf) + fread(buf, 1, IN_LEN, stdin);if (s == t) return -1;}return *s++;}///*template<class T>inline void R(T &x) {static char c;static bool iosig;for (c = read(), iosig = false; !isdigit(c); c = read()) {if (c == -1) return ;if (c == '-') iosig = true;}for (x = 0; isdigit(c); c = read()) x = ((x << 2) + x << 1) + (c ^ '0');if (iosig) x = -x;}//*/const int OUT_LEN = 1024 * 1024;char obuf[OUT_LEN], *oh = obuf;inline void write_char(char c) {if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;*oh++ = c;}template<class T>inline void W(T x) {static int buf[30], cnt;if (x == 0) write_char('0');else {if (x < 0) write_char('-'), x = -x;for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;while (cnt) write_char(buf[cnt--]);}}inline void flush() {fwrite(obuf, 1, oh - obuf, stdout);}/*template<class T>inline void R(T &x) {static char c;static bool iosig;for (c = getchar(), iosig = false; !isdigit(c); c = getchar())if (c == '-') iosig = true;for (x = 0; isdigit(c); c = getchar()) x = ((x << 2) + x << 1) + (c ^ '0');if (iosig) x = -x;}//*/const int MAXN = 300000 + 10;struct node {int left, right;long long tot_s;int tot_c, cnt[2];} tree[MAXN * 20];struct data {int num, ori;inline bool operator < (const data &a) const {return num > a.num;}} a[MAXN];int cnt;int rk[MAXN], root[MAXN], b[MAXN];inline void insert(int &cur, int l, int r, int x) {tree[++cnt] = tree[cur], cur = cnt, tree[cur].tot_s += (long long)rk[x];tree[cur].tot_c++, tree[cur].cnt[rk[x] & 1]++;if (l == r) return ;int mid = l + r >> 1;if (x <= mid) insert(tree[cur].left, l, mid, x);else insert(tree[cur].right, mid + 1, r, x);}int c[2];long long ans;inline void query_k_sum(int l, int r, int ql, int qr, int k) {if (tree[qr].tot_c - tree[ql].tot_c == k) {c[0] += tree[qr].cnt[0] - tree[ql].cnt[0];c[1] += tree[qr].cnt[1] - tree[ql].cnt[1];ans += tree[qr].tot_s - tree[ql].tot_s;return ;}if (k == 0) return ;int mid = l + r >> 1, ll = tree[ql].left, rl = tree[qr].left;int temp = tree[rl].tot_c - tree[ll].tot_c;if (temp <= k) {c[0] += tree[rl].cnt[0] - tree[ll].cnt[0];c[1] += tree[rl].cnt[1] - tree[ll].cnt[1];ans += tree[rl].tot_s - tree[ll].tot_s;query_k_sum(mid + 1, r, tree[ql].right, tree[qr].right, k - temp);} else query_k_sum(l, mid, tree[ql].left, tree[qr].left, k);}inline int query_kth(int l, int r, int ql, int qr, int k, int type) {if (l == r) return rk[l];int mid = l + r >> 1, ll = tree[ql].left, rl = tree[qr].left;int temp = tree[rl].cnt[type] - tree[ll].cnt[type];if (temp >= k) {return query_kth(l, mid, tree[ql].left, tree[qr].left, k, type);} else return query_kth(mid + 1, r, tree[ql].right, tree[qr].right, k - temp, type);}int n, q, l, r, k;inline void solve_tree() {R(n);for (int i = 1; i <= n; ++i) R(a[i].num), a[i].ori = i;std::sort(a + 1, a + n + 1);for (int i = 1; i <= n; ++i) b[a[i].ori] = i, rk[i] = a[i].num;for (int i = 1; i <= n; ++i) root[i] = root[i - 1], insert(root[i], 1, n, b[i]);}inline void solve_query() {R(q);while (q--) {R(l), R(r), R(k), ans = c[0] = c[1] = 0;if (k > r - l + 1 || (k & 1)) {W(-1), write_char('\n');continue ;}query_k_sum(1, n, root[l - 1], root[r], k);if ((c[0] & 1) || (c[1] & 1)) {int sum[2];long long ret = -1;sum[0] = tree[root[r]].cnt[0] - tree[root[l - 1]].cnt[0];sum[1] = tree[root[r]].cnt[1] - tree[root[l - 1]].cnt[1];if (sum[0] != c[0]) {int x1 = query_kth(1, n, root[l - 1], root[r], c[1], 1);int x2 = query_kth(1, n, root[l - 1], root[r], c[0] + 1, 0);ret = std::max(ret, ans - x1 + x2);}if (sum[1] != c[1]) {int x1 = query_kth(1, n, root[l - 1], root[r], c[0], 0);int x2 = query_kth(1, n, root[l - 1], root[r], c[1] + 1, 1);ret = std::max(ret, ans - x1 + x2);}W(ret), write_char('\n');} else W(ans), write_char('\n');}}int main() {freopen("a.in", "r", stdin);freopen("a.out", "w", stdout);solve_tree();solve_query();flush();return 0;}