9 Push 10 Push 20 Pop 2 Pop 10 Push 5 Push 2 Pop 10 Pop 11 Pop 19 3 Push 2 Push 5 Pop 2
No Element! 10 5 2 No Element! 2
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010;
const int inf = -0x7fffffff;
char str[10];
__int64 xis[N];
int cnt, x;
__int64 ans;
struct node
{
__int64 val;
int x;
int l, r;
}tree[N << 2];
struct ope
{
bool flag;
__int64 val;
}can[N];
int Binsearch(__int64 val)
{
int l = 1, r = cnt, mid;
while (l <= r)
{
mid = (l + r) >> 1;
if (xis[mid] == val)
{
break;
}
else if (xis[mid] > val)
{
r = mid - 1;
}
else
{
l = mid + 1;
}
}
return mid;
}
void build(int p, int l, int r)
{
tree[p].l = l;
tree[p].r = r;
tree[p].val = inf;
if (l == r)
{
tree[p].x = l;
return;
}
int mid = (l + r) >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
}
void insert(int p, int pos, __int64 val)
{
if (tree[p].l == tree[p].r)
{
tree[p].val = val;
return;
}
int mid = (tree[p].l + tree[p].r) >> 1;
if (pos <= mid)
{
insert(p << 1, pos, val);
}
else
{
insert(p << 1 | 1, pos, val);
}
if (tree[p << 1].val > tree[p << 1 | 1].val)
{
tree[p].val = tree[p << 1].val;
tree[p].x = tree[p << 1].x;
}
else
{
tree[p].val = tree[p << 1 | 1].val;
tree[p].x = tree[p << 1 | 1].x;
}
}
void query(int p, int l, int r)
{
if (tree[p].l >= l && r >= tree[p].r)
{
if (ans < tree[p].val)
{
ans = tree[p].val;
x = tree[p].x;
}
return;
}
int mid = (tree[p].l + tree[p].r) >> 1;
if (r <= mid)
{
query(p << 1, l, r);
}
else if (l > mid)
{
query(p << 1 | 1, l, r);
}
else
{
query(p << 1, l, mid);
query(p << 1 | 1, mid + 1, r);
}
}
void del(int p, int pos)
{
if (tree[p].l == tree[p].r)
{
tree[p].val = inf;
return;
}
int mid = (tree[p].l + tree[p].r) >> 1;
if (pos <= mid)
{
del(p << 1, pos);
}
else
{
del(p << 1 | 1, pos);
}
if (tree[p << 1].val > tree[p << 1 | 1].val)
{
tree[p].val = tree[p << 1].val;
tree[p].x = tree[p << 1].x;
}
else
{
tree[p].val = tree[p << 1 | 1].val;
tree[p].x = tree[p << 1 | 1].x;
}
}
int main()
{
int n;
while (~scanf("%d", &n))
{
cnt = 0;
__int64 val;
for (int i = 0; i < n; ++i)
{
scanf("%s%I64d", str, &val);
if (!strcmp(str, "Push"))
{
can[i].flag = 1;
}
else
{
can[i].flag = 0;
}
can[i].val = val;
xis[++cnt] = val;
}
sort (xis + 1, xis + 1 + cnt);
cnt = unique(xis + 1, xis + 1 + cnt) - xis - 1;
build(1, 1, cnt);
for (int i = 0; i < n; ++i)
{
int pos = Binsearch(can[i].val);
if (can[i].flag)
{
insert(1, pos, can[i].val);
}
else
{
ans = inf;
query(1, 1, pos);
if (ans == inf)
{
printf("No Element!\n");
}
else
{
printf("%I64d\n", ans);
del(1, x);
}
}
}
printf("\n");
}
return 0;
}#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
multiset<int>st;
multiset<int> :: iterator it;
char str[10];
int main()
{
int n, m;
while (~scanf("%d", &n))
{
st.clear();
for (int i = 0; i < n; i++)
{
scanf("%s%d", str, &m);
if (!strcmp(str, "Push"))
{
st.insert(m);
}
else
{
it = st.upper_bound(m);
if (it == st.begin())
{
printf("No Element!\n");
}
else
{
it--;
printf("%d\n", *it);
st.erase(it);
}
}
}
printf("\n");
}
return 0;
}原文:http://blog.csdn.net/guard_mine/article/details/41986791