結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー ゆにぽけゆにぽけ
提出日時 2023-10-09 11:37:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 5,885 bytes
コンパイル時間 1,476 ms
コンパイル使用メモリ 129,176 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-26 18:25:55
合計ジャッジ時間 4,768 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 53 ms
6,940 KB
testcase_04 AC 61 ms
6,940 KB
testcase_05 AC 53 ms
6,944 KB
testcase_06 AC 48 ms
6,940 KB
testcase_07 AC 61 ms
6,944 KB
testcase_08 AC 4 ms
6,944 KB
testcase_09 AC 32 ms
6,944 KB
testcase_10 AC 55 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 62 ms
6,944 KB
testcase_13 AC 62 ms
6,940 KB
testcase_14 AC 62 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 6 ms
6,940 KB
testcase_24 AC 21 ms
6,940 KB
testcase_25 AC 45 ms
6,940 KB
testcase_26 AC 12 ms
6,944 KB
testcase_27 AC 26 ms
6,944 KB
testcase_28 AC 36 ms
6,944 KB
testcase_29 AC 49 ms
6,944 KB
testcase_30 AC 60 ms
6,940 KB
testcase_31 AC 14 ms
6,940 KB
testcase_32 AC 10 ms
6,944 KB
testcase_33 AC 48 ms
6,944 KB
testcase_34 AC 2 ms
6,944 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 1 ms
6,940 KB
testcase_37 AC 2 ms
6,940 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cassert>
#include<cmath>
#include<ctime>
#include<iomanip>
#include<numeric>
#include<stack>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<bitset>
#include<random>
using namespace std;
template<int m> struct modint
{
	private:
	unsigned int value;
	static constexpr int mod() {return m;}

	public:
	constexpr modint(const long long x = 0) noexcept
	{
		long long y = x;
		if(y < 0 || y >= mod())
		{
			y %= mod();
			if(y < 0) y += mod();
		}
		value = (unsigned int)y;
	}
	constexpr unsigned int val() noexcept {return value;}
	constexpr modint &operator+=(const modint &other) noexcept
	{
		value += other.value;
		if(value >= mod()) value -= mod();
		return *this;
	}
	constexpr modint &operator-=(const modint &other) noexcept
	{
		unsigned int x = value;
		if(x < other.value) x += mod();
		x -= other.value;
		value = x;
		return *this;
	}
	constexpr modint &operator*=(const modint &other) noexcept
	{
		unsigned long long x = value;
		x *= other.value;
		value = (unsigned int) (x % mod());
		return *this;
	}
	constexpr modint &operator/=(const modint &other) noexcept
	{
		return *this *= other.inverse();
	}
	constexpr modint inverse() const noexcept
	{
		assert(value);
		long long a = value,b = mod(),x = 1,y = 0;
		while(b)
		{
			long long q = a/b;
			a -= q*b; swap(a,b);
			x -= q*y; swap(x,y);
		}
		return modint(x);
	}
	constexpr modint power(long long N) const noexcept
	{
		assert(N >= 0);
		modint p = *this,ret = 1;
		while(N)
		{
			if(N & 1) ret *= p;
			p *= p;
			N >>= 1;
		}
		return ret;
	}
	constexpr modint operator+() {return *this;}
	constexpr modint operator-() {return modint() - *this;}
	constexpr modint &operator++(int) noexcept {return *this += 1;}
	constexpr modint &operator--(int) noexcept {return *this -= 1;}
	friend modint operator+(const modint& lhs, const modint& rhs) {return modint(lhs) += rhs;}
	friend modint operator-(const modint& lhs, const modint& rhs) {return modint(lhs) -= rhs;}
	friend modint operator*(const modint& lhs, const modint& rhs) {return modint(lhs) *= rhs;}
	friend modint operator/(const modint& lhs, const modint& rhs) {return modint(lhs) /= rhs;}
	friend ostream &operator<<(ostream &os,const modint &x) {return os << x.value;}
};
//using mint = modint<998244353>;
using mint = modint<1000000007>;
template<class T,T (*op)(T,T),T e(),class F,T (*mapping)(F,T),F (*composition)(F,F),F id()> struct LazySegmentTree
{
    private:
    vector<T> node;
    vector<F> lazy;
    int siz,log;

    void map_and_comp(F f,int u)
    {
        node[u] = mapping(f,node[u]);
        if(u < siz) lazy[u] = composition(f,lazy[u]);
    }

    void propagate(int u)
    {
        assert(0 <= u && u < siz);

        map_and_comp(lazy[u],2*u);
        map_and_comp(lazy[u],2*u+1);

        lazy[u] = id();
    }

    void recalculate(int u) {node[u] = op(node[2*u],node[2*u+1]);}

    public:
    LazySegmentTree(int n)
    {
        siz = 1;
        log = 0;
        while(siz < n) siz <<= 1,log++;

        node.assign(2*siz,e());
        lazy.assign(siz,id());
    }

    void fill(T x,int l = 0,int r = -1) {if(r < 0){r = siz;} for(int i = l;i < r;i++) replace(i,x);}

    void clear() {fill(e());}

    void replace(int pos,T x) {func_update(pos,x,[](T u,T v){return u = v;});}

    void add(int pos,T x) {func_update(pos,x,[](T u,T v){return u + v;});}

    void func_update(int pos,T x,T (*func)(T,T))
    {
        assert(0 <= pos && pos < siz);
        pos += siz;

        for(int i = log;i >= 1;i--) propagate(pos >> i);

        node[pos] = func(node[pos],x);

        for(int i = 1;i <= log;i++) recalculate(pos >> i);
    }

    T operator[](int pos)
    {
        assert(0 <= pos && pos < siz);

        pos += siz;
        for(int i = log;i >= 1;i--) propagate(pos >> i);
        return node[pos];
    }

    void apply(int pos,F f)
    {
        assert(0 <= pos && pos < siz);
        pos += siz;

        for(int i = log;i >= 1;i--) propagate(pos >> i);

        node[pos] = mapping(f,node[pos]);

        for(int i = 1;i <= log;i++) recalculate(pos >> i);
    }

    void apply(int l,int r,F f)
    {
        assert(0 <= l && l <= r && r <= siz);
        if(l == r) return;

        l += siz;
        r += siz;

        int l0 = l/(l & -l);
        int r0 = r/(r & -r);

        for(int i = log;i >= 1;i--) propagate(l0 >> i);
        for(int i = log;i >= 1;i--) propagate((r0-1) >> i);

        while(l < r)
        {
            if(l & 1) map_and_comp(f,l++);
            if(r & 1) map_and_comp(f,--r);

            l >>= 1;
            r >>= 1;
        }

        for(int i = 1;i <= log;i++) recalculate(l0 >> i);
        for(int i = 1;i <= log;i++) recalculate((r0-1) >> i);
    }

    T prod() {return node[1];}

    T prod(int l,int r)
    {
        assert(0 <= l && l <= r && r <= siz);

        l += siz;
        r += siz;

        int l0 = l/(l & -l);
        int r0 = r/(r & -r);

        for(int i = log;i >= 1;i--) propagate(l0 >> i);
        for(int i = log;i >= 1;i--) propagate((r0-1) >> i);

        T Lval = e(),Rval = e();
        while(l < r)
        {
            if(l & 1) Lval = op(Lval,node[l++]);
            if(r & 1) Rval = op(node[--r],Rval);

            l >>= 1;
            r >>= 1;
        }

        return op(Lval,Rval);
    }
};
int N,pos[1 << 17];
int op(int a,int b) {return a+b;}
int e() {return 0;}
void solve()
{
	cin >> N;
	for(int i = 0;i < N;i++)
	{
		int a;
		cin >> a;
		a--;
		pos[a] = i;
	}
	LazySegmentTree<int,op,e,int,op,op,e> seg(N);
	long long ans = 0;
	for(int i = 0;i < N;i++)
	{
		int b;
		cin >> b;
		b--;
		int j = pos[b];
		seg.apply(j+1,N,-1);
		j += seg[j];
		ans += j;
	}
	cout << ans << endl;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	//cin >> tt;
	while(tt--) solve();
}
0