树状数组&pair&离散化

题目:https://vjudge.z180.cn/contest/428881#problem/G

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int N = 1e5 + 9;
int a[N], b[N], x[N], y[N], cnt;
int sum[3 * N];
typedef pair<int, int> P;
P p[3 * N];   //pair 数组
int n, m;
int lowbit(int x) { return x & -x; }   //树状数组模板
void add(int pos, int data) {
    for (int i = pos; i <= cnt; i += lowbit(i)) sum[i] += data;
}
int query(int pos) {
    int s = 0;
    for (int i = pos; i > 0; i -= lowbit(i)) s += sum[i];
    return s;
}
int main(int argc, char const *argv[]) {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++) {
        scanf("%d%d", &a[i], &b[i]);
        x[a[i]]--;
        y[a[i]] += b[i];
        p[++cnt] = P(x[a[i]], y[a[i]]);
    }
    memset(x, 0, sizeof x);  //还要再用
    memset(y, 0, sizeof y);
    sort(p + 1, p + 1 + cnt);
    for (int i = 1; i <= m; i++) {   
             //找到位置,给他减一,P(0,0)最大
        int v = lower_bound(p + 1, p + 1 + cnt, P(x[a[i]], y[a[i]])) - p;
        add(v, -1);
        x[a[i]]--;
        y[a[i]] += b[i];  //改变再找到,然后加一
        v = lower_bound(p + 1, p + 1 + cnt, P(x[a[i]], y[a[i]])) - p;
        add(v, 1);
        v = lower_bound(p + 1, p + 1 + cnt, P(x[1], y[1])) - p;
        printf("%d\n", query(v - 1) + 1);   //找到位置输出前面的
    }
    return 0;
}
上一篇:[ABC143D] Triangles


下一篇:call,apply和bind方法的实现