結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー akun0716akun0716
提出日時 2020-07-17 22:09:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 2,881 bytes
コンパイル時間 932 ms
コンパイル使用メモリ 86,080 KB
実行使用メモリ 13,076 KB
最終ジャッジ日時 2023-08-20 01:26:24
合計ジャッジ時間 3,978 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
11,332 KB
testcase_01 AC 5 ms
11,260 KB
testcase_02 AC 5 ms
11,356 KB
testcase_03 AC 81 ms
12,648 KB
testcase_04 AC 90 ms
12,636 KB
testcase_05 AC 78 ms
12,608 KB
testcase_06 AC 72 ms
12,376 KB
testcase_07 AC 88 ms
12,644 KB
testcase_08 AC 9 ms
11,332 KB
testcase_09 AC 38 ms
12,084 KB
testcase_10 AC 64 ms
13,052 KB
testcase_11 AC 6 ms
11,336 KB
testcase_12 AC 95 ms
12,896 KB
testcase_13 AC 89 ms
12,920 KB
testcase_14 AC 95 ms
13,076 KB
testcase_15 AC 5 ms
11,280 KB
testcase_16 AC 6 ms
11,340 KB
testcase_17 AC 5 ms
11,256 KB
testcase_18 AC 6 ms
11,340 KB
testcase_19 AC 6 ms
11,288 KB
testcase_20 AC 5 ms
11,276 KB
testcase_21 AC 6 ms
11,348 KB
testcase_22 AC 6 ms
11,476 KB
testcase_23 AC 12 ms
11,348 KB
testcase_24 AC 34 ms
11,864 KB
testcase_25 AC 69 ms
12,340 KB
testcase_26 AC 18 ms
11,596 KB
testcase_27 AC 39 ms
11,808 KB
testcase_28 AC 51 ms
12,120 KB
testcase_29 AC 71 ms
12,324 KB
testcase_30 AC 86 ms
12,604 KB
testcase_31 AC 23 ms
11,596 KB
testcase_32 AC 17 ms
11,592 KB
testcase_33 AC 58 ms
12,640 KB
testcase_34 AC 6 ms
11,320 KB
testcase_35 AC 6 ms
11,340 KB
testcase_36 AC 6 ms
11,284 KB
testcase_37 AC 6 ms
11,328 KB
testcase_38 AC 5 ms
11,344 KB
testcase_39 AC 5 ms
11,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
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; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12 
//priority_queue<int,vector<int>, greater<int> > q2;

struct segtree {
public:
	const int SIZE = 1 << 18;
	VI seg, lazy;
	//seg:区間の合計値 lazy:区間に対して、加える値でまだ遅延しているもの

	segtree() :seg(SIZE * 2), lazy(SIZE * 2) {}
	void lazy_evaluate(int k, int l, int r) {
		if (lazy[k] != 0) {
			seg[k] += lazy[k];
			//区間[l,r)にすべて同じ値を追加することになっていて、
			//segには合計値が入っているので、加える値を足す
			if (r - l > 1) {
				lazy[k * 2 + 1] += lazy[k];
				lazy[k * 2 + 2] += lazy[k];
			}
			lazy[k] = 0;
		}
	}

	void update(int a, int b, int k, int l, int r, int x) {
		lazy_evaluate(k, l, r);
		if (r <= a || b <= l)return;
		if (a <= l && r <= b) {
			lazy[k] += x;
			lazy_evaluate(k, l, r);
		}
		else {
			update(a, b, k * 2 + 1, l, (l + r) / 2, x);
			update(a, b, k * 2 + 2, (l + r) / 2, r, x);
			seg[k] = seg[k * 2 + 1] + seg[k * 2 + 2];
		}
	}
	int query(int a, int b, int k, int l, int r) {
		lazy_evaluate(k, l, r);
		if (r <= a || b <= l)return 0;
		if (a <= l && r <= b)return seg[k];
		int x = query(a, b, k * 2 + 1, l, (l + r) / 2);
		int y = query(a, b, k * 2 + 2, (l + r) / 2, r);
		return x + y;
	}

	void update(int a, int b, int x) { update(a, b, 0, 0, SIZE, x); }
	//update(a,b,x) := [a,b)を全てxを加える
	int query(int a, int b) { return query(a, b, 0, 0, SIZE); }
	//query(a,b) := [a,b)に対する合計値を求める

};

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N; cin >> N;
	VI pos(N + 1), B(N);
	REP(i, N) {
		int a; cin >> a;
		pos[a] = i;
	}
	REP(i, N)cin >> B[i];
	int ans = 0;
	segtree seg;
	REP(i, N) {
		ans += pos[B[i]] - i;
		seg.update(0, pos[B[i]], 1);
		ans += seg.query(pos[B[i]], pos[B[i]] + 1);
	}


	cout << ans << endl;

	return 0;
}

0