HYSBZ 1901 Dynamic Rankings tree Array set Chairman tree

Source: Internet
Author: User

Zoj above this problem memory limit is too strict, bare tree Bush tree Chairman tree pointers, Bzoj above this put the relatively loose, can pass.

is to use a tree-like array to maintain the N-tree, and then use the prefix and properties to solve the K-large.

#include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set># Include <vector> #include <string> #include <queue> #include <deque> #include <bitset># Include <list> #include <cstdlib> #include <climits> #include <cmath> #include <ctime># Include <algorithm> #include <stack> #include <sstream> #include <numeric> #include <fstream > #include <functional> using namespace std; #define MP make_pair#define PB push_backtypedef Long long ll;typedef unsigned long long ull;typedef vector<int> vi;t Ypedef pair<int, int> pii;typedef pair<double, double> pdd;const int INF = int_max/3;const double eps = 1e-8  Const LL Linf = 1e17;const double dinf = 1e60;const int MAXN = 5e4 + 10;const int maxm = MAXN * 67;const int maxq = 1e4 + 10;struct Query {char cmd;int L, R, K; Query (char cmd = 0, int l = 0, int r = 0, int k = 0): cmd (cmd), L (L), R (R), K (k) {}};int SUMV[MAXM], LC[MAXM], RC[MAXM], CNT, C[maxn];int N, M, NUM[MAXN + MAXQ], numcnt, A[MAXN]; Query ask[maxq];int lowbit (int x) {return x & (-X);} void init () {cnt = NUMCNT = 0;memset (c, 0, sizeof (c));} void build (int rt, int l, int r) {Sumv[rt] = 0;if (L = = r) Return;int mid = (L + R) >> 1;lc[rt] = cnt++; Rc[rt] = cnt ++;build (Lc[rt], L, mid); build (Rc[rt], mid + 1, r);} int update (int prt, int pos, int Val) {int NRT = cnt++, ret = NRT;SUMV[NRT] = sumv[prt] + val;int L = 0, r = Numcnt-1;wh Ile (L < R) {int mid = (L + R) >> 1;if (pos <= mid) {LC[NRT] = cnt++; RC[NRT] = Rc[prt];sumv[lc[nrt]] = sumv[lc[ PRT]] + VAL;NRT = LC[NRT]; PRT = Lc[prt];r = mid;} else {RC[NRT] = cnt++; LC[NRT] = Lc[prt];sumv[rc[nrt]] = Sumv[rc[prt]] + VAL;NRT = RC[NRT]; prt = Rc[prt];l = mid + 1;}} return ret;} void Gao (int tid, int pos, int Val) {while (tid <= n) {int ret = update (C[tid], POS, Val); C[tid] = Ret;tid + = Lowbit (TID);}} int LRT[MAXN], rrt[maxn];int query (int ql, int qr, int k) {int lcnt = 0,rcnt = 0;ql--;while (QL > 0) {lrt[lcnt++] = c[ql];ql-= Lowbit (QL);} while (QR > 0) {rrt[rcnt++] = c[qr];qr-= Lowbit (QR);} int L = 0, r = Numcnt-1;while (L < r) {int mid = (L + r) >> 1, lsum = 0, rsum = 0;for (int i = 0; i < lcnt; i+ +) Lsum + = sumv[lc[lrt[i]]];for (int i = 0; i < rcnt; i++) Rsum + = Sumv[lc[rrt[i]]];if (rsum-lsum >= k) {r = mid;for (int i = 0; i < lcnt; i++) Lrt[i] = lc[lrt[i]];for (int i = 0; i < rcnt; i++) rrt[i] = Lc[rrt[i]];} else {L = mid + 1; k-= (rsum-lsum); for (int i = 0; i < lcnt; i++) Lrt[i] = rc[lrt[i]];for (int i = 0; i < rcnt; i++ ) Rrt[i] = Rc[rrt[i]];}} return l;} int getid (int Val) {return lower_bound (num, num + numcnt, Val)-num; int main () {init (); scanf ("%d%d", &n, &m); for (int i = 1; I <= n; i++) {scanf ("%d", &a[i]); num[numcnt++] = a[ I];} Char Cmd[5];int L, R, k;for (int i = 1; I <= m; i++) {scanf ("%s", cmd), if (cmd[0] = = ' Q ') {scanf ("%d%d%d", &l, &r , &k); Ask[i] = Query (cmd[0], L, R, K);} else {SCANF ("%d%d", &l, &k); Ask[i] = Query (cmd[0], L, 0, K); num[numcnt++] = k;}} Sort (num, num + numcnt), numcnt = unique (num, num + numcnt)-num;build (0, 0, numcnt-1); for (int i = 1; I <= n; i++) {g AO (i, GetID (A[i]), 1);} for (int i = 1; I <= m; i++) {if (Ask[i].cmd = = ' Q ') printf ("%d\n", Num[query (ASK[I].L, ASK[I].R, ASK[I].K)]); else {int P Rev = GetID (A[ASK[I].L]), now = GetID (ASK[I].K); A[ASK[I].L] = Ask[i].k;gao (ASK[I].L, Prev,-1); Gao (ASK[I].L, now, 1);}} return 0;}

  

HYSBZ 1901 Dynamic Rankings tree Array set Chairman tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.