[POJ1354D]Multiset(权值线段树)

Posted hfcdyp

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[POJ1354D]Multiset(权值线段树)相关的知识,希望对你有一定的参考价值。

【原题】

Note that the memory limit is unusual.

You are given a multiset consisting of nn integers. You have to process queries of two types:

  • add integer kk into the multiset;
  • find the kk-th order statistics in the multiset and remove it.

kk-th order statistics in the multiset is the kk-th element in the sorted list of all elements of the multiset. For example, if the multiset contains elements 11, 44, 22, 11, 44, 55, 77, and k=3k=3, then you have to find the 33-rd element in [1,1,2,4,4,5,7][1,1,2,4,4,5,7], which is 22. If you try to delete an element which occurs multiple times in the multiset, only one occurence is removed.

After processing all queries, print any number belonging to the multiset, or say that it is empty.

 

Input

The first line contains two integers nn and qq (1n,q1061≤n,q≤106) — the number of elements in the initial multiset and the number of queries, respectively.

The second line contains nn integers a1a1, a2a2, ..., anan (1a1a2?ann1≤a1≤a2≤?≤an≤n) — the elements of the multiset.

The third line contains qq integers k1k1, k2k2, ..., kqkq, each representing a query:

  • if 1kin1≤ki≤n, then the ii-th query is "insert kiki into the multiset";
  • if ki<0ki<0, then the ii-th query is "remove the |ki||ki|-th order statistics from the multiset". For this query, it is guaranteed that |ki||ki| is not greater than the size of the multiset.
 
Output

If the multiset is empty after all queries, print 00.

Otherwise, print any integer that belongs to the resulting multiset.

 

Examples

input

5 5
1 2 3 4 5
-1 -1 -1 -1 -1

output

0

input

5 4
1 2 3 4 5
-5 -1 -3 -1

output

3

input

6 2
1 1 1 2 3 4
5 6

output

6

Note

In the first example, all elements of the multiset are deleted.

In the second example, the elements 55, 11, 44, 22 are deleted (they are listed in chronological order of their removal).

In the third example, 66 is not the only answer.

 1  
 2 #include <algorithm>
 3 #include <cmath>
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <list>
 7 #include <map>
 8 #include <iostream>
 9 #include <queue>
10 #include <set>
11 #include <stack>
12 #include <string>
13 #include <vector>
14 #include <iomanip>
15 #define LL long long
16 #define inf 0x3f3f3f3f
17 #define INF 0x3f3f3f3f3f3f
18 #define PI 3.1415926535898
19 #define F first
20 #define S second
21 #define lson  rt << 1
22 #define rson  rt << 1 | 1
23 using namespace std;
24 
25 const int maxn = 1e6 + 7;
26 const int maxm = 1e5 + 7;
27 int n, m, dig;
28 int a[maxn];
29 
30 int tree[maxn * 4];
31 
32 LL read()
33 {
34     LL x = 0, f = 1;char ch = getchar();
35     while (ch < 0 || ch>9) { if (ch == -)f = -1;ch = getchar(); }
36     while (ch >= 0 && ch <= 9) { x = x * 10 + ch - 0;ch = getchar(); }
37     return x * f;
38 }
39 
40 void add(int n, int index, int L, int R, int rt)
41 {
42     if (L == R)
43     {
44         tree[rt] += n;
45         return;
46     }
47     int mid = (L + R) / 2;
48     if (index <= mid) add(n, index, L, mid, lson);
49     else add(n, index, mid + 1, R, rson);
50     tree[rt] = tree[lson] + tree[rson];
51 }
52 
53 int kth(int l, int r, int rt, int k)
54 {
55     if (l == r) return l;
56     else
57     {
58         int mid = (l + r) / 2, s1 = tree[lson], s2 = tree[rson];
59         if (k <= s1) return kth(l, mid, lson, k);
60         else return kth(mid + 1, r, rson, k - s1);
61     }
62 }
63 
64 int main()
65 {
66     ios::sync_with_stdio(false);
67     cin.tie(0);
68     n = read(), m = read();
69     for (int i = 1; i <= n; i++)
70     {
71         dig = read();
72         add(1, dig, 1, n, 1);
73     }
74     int num;
75     for (int i = 1; i <= m; i++)
76     {
77         num = read();
78         if (num > 0)
79         {
80             add(1, num, 1, n, 1);
81         }
82         else
83         {
84             num = -num;
85             if (tree[1] < num) continue;
86             int l = 1, r = n;
87             int tmp = kth(1, n, 1, num);
88             add(-1, tmp, 1, n, 1);
89         }
90     }
91     if (tree[1] == 0) cout << "0" << endl;
92     else cout << kth(1, n, 1, 1) << endl;
93 
94 }

 

以上是关于[POJ1354D]Multiset(权值线段树)的主要内容,如果未能解决你的问题,请参考以下文章

题解 CF1354D Multiset

Multiset (权值线段树模版)

EC R 87 div2 D. Multiset 线段树 树状数组 二分

EC R 87 div2 D. Multiset 线段树 树状数组 二分

《算法竞赛进阶指南》0x43线段树 扫描线算法 POJ2482

POJ 3237 /// 树链剖分 线段树区间修改(*-1)