銆愭ā鏉裤€戝姩鎬佷富甯爲

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆愭ā鏉裤€戝姩鎬佷富甯爲相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/_0_s' title='%s'>%s   str   span   turn   odi   sum   nod   hup   main   

濡傞锛岃繖鏄竴涓ā鏉裤€傘€傘€?/p>

  1 #include <algorithm>
  2 #include <iostream>
  3 #include <cstring>
  4 #include <cstdio>
  5 #include <cctype>
  6 
  7 inline void read(int & x)
  8 {
  9     x = 0;
 10     int k = 1;
 11     char c = getchar();
 12     while (!isdigit(c))
 13         if (c == 鈥?/span>-鈥?/span>) c = getchar(), k = -1;
 14         else c = getchar();
 15     while (isdigit(c))
 16         x = (x << 1) + (x << 3) + (c ^ 48),
 17         c = getchar();
 18     x *= k;
 19 }
 20 
 21 struct Node
 22 {
 23     int l, r, k, q;
 24 }q[101010];
 25 
 26 const int MAXN = 1010100 << 6;
 27 int n, m, tot = 0, id = 0;
 28 int a[MAXN], b[MAXN], ls[MAXN], rs[MAXN], sum[MAXN]; 
 29 int T[MAXN], S[MAXN], use[MAXN];
 30 
 31 char opt[11];
 32 
 33 inline int lowbit(int x)
 34 {
 35     return x & (-x);
 36 }
 37 
 38 inline int Lsh(int x)
 39 {
 40     return std::lower_bound(a + 1, a + tot + 1, x) - a;
 41 }
 42 
 43 inline void Pushup(int u)
 44 {
 45     sum[u] = sum[ls[u]] + sum[rs[u]];
 46 }
 47 
 48 int Build(int l, int r)
 49 {
 50     int rt = ++id;
 51     sum[rt] = 0;
 52     if (l != r)
 53     {
 54         int mid = l + r >> 1;
 55         ls[rt] = Build(l, mid);
 56         rs[rt] = Build(mid + 1, r);
 57     }
 58     return rt;
 59 }
 60 
 61 int Add(int pre, int l, int r, int x, int cm)
 62 {
 63     int rt = ++id;
 64     ls[rt] = ls[pre],
 65     rs[rt] = rs[pre],
 66     sum[rt] = sum[pre] + cm;
 67     if (l < r)
 68     {
 69         int mid = l + r >> 1;
 70         if (x <= mid)
 71             ls[rt] = Add(ls[pre], l, mid, x, cm);
 72         else 
 73             rs[rt] = Add(rs[pre], mid + 1, r, x, cm);
 74     }
 75     return rt;
 76 }
 77 
 78 inline void Modify(int u, int pos, int x)
 79 {
 80     while (u <= n)
 81     {
 82         S[u] = Add(S[u], 1, tot, pos, x);
 83         u += lowbit(u);
 84     }
 85 }
 86 
 87 inline int Sum(int u)
 88 {
 89     int ans = 0;
 90     while (u > 0)
 91     {
 92         ans += sum[ls[use[u]]];
 93         u -= lowbit(u);
 94     }
 95     return ans;
 96 }
 97 
 98 int Query(int u, int v, int ur, int vr, int l, int r, int k)
 99 {
100     if (l >= r) return l;
101     int mid = l + r >> 1;
102     int df = Sum(v) - Sum(u) + sum[ls[vr]] - sum[ls[ur]];
103     if (k <= df)
104     {
105         for (int i = u; i; i -= lowbit(i))
106             use[i] = ls[use[i]];
107         for (int i = v; i; i -= lowbit(i))
108             use[i] = ls[use[i]];
109         return
110             Query(u, v, ls[ur], ls[vr], l, mid, k);
111     }
112     else
113     {
114         for (int i = u; i; i -= lowbit(i))
115             use[i] = rs[use[i]];
116         for (int i = v; i; i -= lowbit(i))
117             use[i] = rs[use[i]];
118         return
119             Query(u, v, rs[ur], rs[vr], mid + 1, r, k - df);
120     }
121 }
122 
123 signed main()
124 {
125     read(n), read(m);
126     for (int i = 1; i <= n; ++i)
127         read(b[i]), a[++tot] = b[i];
128     for (int i = 1; i <= m; ++i)
129     {
130         scanf("%s", opt);
131         if (opt[0] == 鈥?/span>Q鈥?/span>)
132             read(q[i].l), read(q[i].r), read(q[i].k), q[i].q = 1;
133         else 
134             read(q[i].l), read(q[i].r), q[i].q = 0,
135             a[++tot] = q[i].r;
136     }
137     std::sort(a + 1, a + tot + 1);
138     int cur = std::unique(a + 1, a + tot + 1) - a - 1;
139     tot = cur;
140     T[0] = Build(1, tot);
141     for (int i = 1; i <= n; ++i)
142         T[i] = Add(T[i - 1], 1, tot, Lsh(b[i]), 1);
143     for (int i = 1; i <= n; ++i)
144         S[i] = T[0];
145     for (int i = 1; i <= m; ++i)
146         if (q[i].q)
147         {
148             for (int j = q[i].l - 1; j; j -= lowbit(j))
149                 use[j] = S[j];
150             for (int j = q[i].r; j; j -= lowbit(j))
151                 use[j] = S[j];
152             printf("%d
", a[Query(q[i].l - 1, q[i].r, T[q[i].l - 1], T[q[i].r], 1, tot, q[i].k)]);
153         }
154         else
155         {
156             Modify(q[i].l, Lsh(b[q[i].l]), -1);
157             Modify(q[i].l, Lsh(q[i].r), 1);
158             b[q[i].l] = q[i].r;
159         }
160     return 0;
161 }

 

以上是关于銆愭ā鏉裤€戝姩鎬佷富甯爲的主要内容,如果未能解决你的问题,请参考以下文章

銆愭ā鏉裤€戝嚫鍖呭悜鍐呮帹杩涙眰涓嶄弗鏍肩殑鍗婂钩闈氦鈥斺€攑oj3384

銆怢eetCode銆戝姩鎬佽鍒掞紙涓嬬瘒鍏?9棰橈級

銆愩€恏enuacm2016绾ф殤鏈熻缁冦€戝姩鎬佽鍒掍笓棰?D銆慦riting Code

鏄撻敊鏄撴贩娣嗙煡璇嗙偣璁茶В绯诲垪锛堜節锛夆€斿缓绔嬪姩鎬佽鍒掓ā鍨嬬殑鍩烘湰姝ラ

銆愭瘡鏃ヤ笌浜т笟鍚岄銆戝箍鐢?G鍟嗙敤锛涘浘鍍忚瘑鍒妧鏈師鐞嗭紱5G鑺墖绔炰簤娼紱鍏ㄧ悆鍗婂浣撹涓氭牸灞€锛涚嚎鎬ц皟鑺傚櫒鍩烘湰姒傚康