码迷,mamicode.com
首页 > 其他好文 > 详细

Educational Codeforces Round 87 (Rated for Div. 2) D

时间:2020-05-19 18:38:23      阅读:60      评论:0      收藏:0      [点我收藏+]

标签:fat   puts   ati   操作   define   double   i++   tac   stdin   

D - Multiset

题目大意:给一个长度为n的数组a,有q次操作,每次输入一个数k,如果k是正数则插入a数组中,负数就删除 下标为-k的数,下标从1-n;

思路:用树状数组维护每个数前面有几个数,即是第几大,插入一个数add操作,删除用二分查找,找到那个数。

code

#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <stack>
#include <vector>
#define FT(a, b) memset(a, b, sizeof(a))
#define FAT(a) memset(a, 0, sizeof(a))
using namespace std;
typedef long long ll;
const int M = 1e6 + 100;
const int INF = 0x3f3f3f3f;
const int mod = 998244353;
const double PI = 3.1415926535;
int c[M], n, q;
int lowbit(int x) { return x & -x; }

void add(int x, int y)
{
    for (int i = x; i <= n; i += lowbit(i))
        c[i] += y;
}
ll sum(int x)
{
    ll ans = 0;
    for (int i = x; i > 0; i -= lowbit(i))
        ans += c[i];
    return ans;
}
int find(int x)
{
    int l = 1, r = n;
    while (l < r)
    {
        int mid = l + r >> 1;
        if (sum(mid) >= x)
            r = mid;
        else
            l = mid + 1;
    }
    return l;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("D:\\code\\c++\\in.txt", "r", stdin);
#endif
    scanf("%d%d", &n, &q);
    FAT(c);
    for (int i = 1; i <= n; i++)
    {
        int a;
        scanf("%d", &a);
        add(a, 1);
    }
    while (q--)
    {
        int x;
        scanf("%d", &x);
        if (x < 0)
            add(find(-x), -1);
        else
            add(x, 1);
    }
    int x = find(1);
    if (c[x])
        printf("%d\n", x);
    else
        puts("0");
    return 0;
}

 

Educational Codeforces Round 87 (Rated for Div. 2) D

标签:fat   puts   ati   操作   define   double   i++   tac   stdin   

原文地址:https://www.cnblogs.com/ignorance/p/12918366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!