답안 #115793

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
115793 2019-06-09T06:55:16 Z choikiwon 허수아비 (JOI14_scarecrows) C++17
100 / 100
896 ms 82044 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MN = 200010;

int N;
pii P[MN];
int A[MN];
vector<int> X, Y;
unordered_map<int, int> dx, dy;

void add(int x, int v, vector<pii> &stk) {
    while(!stk.empty() && stk.back().first > v) stk.pop_back();
    stk.push_back(pii(v, x));
}

struct BIT {
    vector<vector<pii> > tree;
    void init() {
        tree = vector<vector<pii> >(4 * N);
    }
    void upd(int x, int v, int l, int r, int n) {
        if(x < l || r < x) return;
        add(v, x, tree[n]);
        if(l == r) return;
        int m = (l + r)>>1;
        upd(x, v, l, m, 2*n);
        upd(x, v, m + 1, r, 2*n + 1);
    }
    int quer(int a, int b, int &v, int l, int r, int n) {
        if(b < l || r < a) return 0;
        if(a <= l && r <= b) {
            int s = 0, e = (int)tree[n].size() - 1, p = (int)tree[n].size();
            while(s <= e) {
                int m = (s + e)>>1;

                if(tree[n][m].second < v) {
                    p = m;
                    e = m - 1;
                }
                else s = m + 1;
            }
            if(p != tree[n].size()) v = tree[n].back().second;
            return (int)tree[n].size() - p;
        }
        int m = (l + r)>>1;
        int L = quer(a, b, v, l, m, 2*n);
        int R = quer(a, b, v, m + 1, r, 2*n + 1);
        return L + R;
    }
} bit;

int main() {
    scanf("%d", &N);

    for(int i = 0; i < N; i++) {
        scanf("%d %d", &P[i].first, &P[i].second);
        X.push_back(P[i].first);
        Y.push_back(P[i].second);
    }

    sort(X.begin(), X.end());
    sort(Y.begin(), Y.end());
    X.resize(unique(X.begin(), X.end()) - X.begin());
    Y.resize(unique(Y.begin(), Y.end()) - Y.begin());
    for(int i = 0; i < N; i++) {
        dx[X[i]] = i;
        dy[Y[i]] = i;
    }
    for(int i = 0; i < N; i++) {
        P[i].first = dx[P[i].first];
        P[i].second = dy[P[i].second];
        A[ P[i].first ] = P[i].second;
    }

    ll ans = 0;
    bit.init();
    for(int i = N - 1; i >= 0; i--) {
        int t = N;
        ans += bit.quer(A[i], N - 1, t, 0, N - 1, 1);
        bit.upd(A[i], i, 0, N - 1, 1);
    }
    printf("%lld", ans);
}

Compilation message

scarecrows.cpp: In member function 'int BIT::quer(int, int, int&, int, int, int)':
scarecrows.cpp:46:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             if(p != tree[n].size()) v = tree[n].back().second;
                ~~^~~~~~~~~~~~~~~~~
scarecrows.cpp: In function 'int main()':
scarecrows.cpp:57:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &N);
     ~~~~~^~~~~~~~~~
scarecrows.cpp:60:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &P[i].first, &P[i].second);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 3 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 2304 KB Output is correct
2 Correct 11 ms 1664 KB Output is correct
3 Correct 10 ms 1920 KB Output is correct
4 Correct 8 ms 1664 KB Output is correct
5 Correct 10 ms 1664 KB Output is correct
6 Correct 10 ms 1664 KB Output is correct
7 Correct 11 ms 1664 KB Output is correct
8 Correct 10 ms 2304 KB Output is correct
9 Correct 10 ms 1792 KB Output is correct
10 Correct 10 ms 1792 KB Output is correct
11 Correct 11 ms 1664 KB Output is correct
12 Correct 9 ms 1664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 398 ms 81260 KB Output is correct
2 Correct 896 ms 53736 KB Output is correct
3 Correct 604 ms 66144 KB Output is correct
4 Correct 484 ms 51604 KB Output is correct
5 Correct 642 ms 52168 KB Output is correct
6 Correct 728 ms 53052 KB Output is correct
7 Correct 775 ms 53504 KB Output is correct
8 Correct 777 ms 53448 KB Output is correct
9 Correct 588 ms 82044 KB Output is correct
10 Correct 641 ms 60532 KB Output is correct
11 Correct 738 ms 55212 KB Output is correct
12 Correct 803 ms 53964 KB Output is correct
13 Correct 818 ms 53660 KB Output is correct
14 Correct 572 ms 81476 KB Output is correct
15 Correct 742 ms 55364 KB Output is correct
16 Correct 795 ms 53704 KB Output is correct
17 Correct 467 ms 37780 KB Output is correct
18 Correct 717 ms 53580 KB Output is correct
19 Correct 674 ms 53588 KB Output is correct
20 Correct 700 ms 53704 KB Output is correct