結果

問題 No.1282 Display Elements
ユーザー renjyaku_intrenjyaku_int
提出日時 2020-11-11 22:52:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 3,699 bytes
コンパイル時間 2,449 ms
コンパイル使用メモリ 207,300 KB
実行使用メモリ 8,700 KB
最終ジャッジ日時 2023-09-30 00:57:25
合計ジャッジ時間 4,111 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 3 ms
4,384 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 70 ms
8,108 KB
testcase_16 AC 29 ms
5,792 KB
testcase_17 AC 58 ms
7,480 KB
testcase_18 AC 36 ms
6,144 KB
testcase_19 AC 37 ms
8,700 KB
testcase_20 AC 37 ms
8,568 KB
testcase_21 AC 77 ms
8,496 KB
testcase_22 AC 49 ms
8,504 KB
testcase_23 AC 77 ms
8,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#include <tourist>
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;

typedef pair<ll, ll> p;
const int INF = 1e9;
const double eps = 1e-7;
const ll LINF = ll(1e18);
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const long double pi = 4 * atan(1);
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v)          \
    cout << #v << ":";    \
    for (auto x : v)      \
    {                     \
        cout << x << ' '; \
    }                     \
    cout << endl;
template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
//cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
//g++ yarudake.cpp -std=c++17 -I .
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
template <typename T>
struct Compress
{
    vector<T> xs;

    Compress(const vector<T> &vs) : xs(vs)
    {
        sort(xs.begin(), xs.end());
        xs.erase(unique(xs.begin(), xs.end()), xs.end());
    }

    int compress(const T &x)
    {
        return lower_bound(xs.begin(), xs.end(), x) - xs.begin();
    }

    T decompress(int i)
    {
        return xs[i];
    }
};
//1-indexだから0にaddすると死んじゃうよ
struct fenwick_tree
{
    typedef int T;
    T n;
    vector<T> bit;
    T b = 1;
    // 各要素の初期値は 0
    fenwick_tree(T num) : bit(num + 1, 0) { n = num; }

    // a_i += w
    void add(T i, T w)
    {
        for (T x = i; x <= n; x += x & -x)
        {
            bit[x] += w;
        }
    }
    ll get(T i)
    {
        ll s = 0;
        while (i > 0)
        {
            s += bit[i];
            i -= i & -i;
        }
        return s;
    }
    // [1, i] の和を計算.
    T sum(T i)
    {
        T ret = 0;
        for (T x = i; x > 0; x -= x & -x)
        {
            ret += bit[x];
        }
        return ret;
    }
    // [left+1, right] の和を計算.
    T sum(T left, T right)
    {
        return sum(right) - sum(left);
    }
    int get_index(ll x)
    { //something like lower_bound(all(sum), a). O(logn)
        x--;
        while (b * 2 <= n)
            b *= 2;
        int r = b;
        int res = 0;
        while (r > 0)
        {
            if ((r | res) <= n)
            {
                int rv = bit[r | res];
                if (x >= rv)
                {
                    x -= rv;
                    res += r;
                }
            }
            r >>= 1;
        }
        return res + 1;
    }
};
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, m;
    cin >> n;
    vector<ll> a(n), b(n);
    rep(i, n) cin >> a[i];
    rep(i, n) cin >> b[i];
    vector<ll> all;
    rep(i, n) all.push_back(a[i]);
    rep(i, n) all.push_back(b[i]);
    Compress<ll> c(all);
    sort(ALL(a));
    rep(i, n)
    {
        a[i] = c.compress(a[i]);
        a[i]++;
    }
    rep(i, n)
    {
        b[i] = c.compress(b[i]);
        b[i]++;
    }
    ll ans=0;
    fenwick_tree ft(200001);
    rep(i,n){
        ft.add(b[i], 1);
        ans += ft.sum(a[i]-1);
        //cout<<ans<<"\n";
    }
    cout<<ans<<"\n";
}
0