結果

問題 No.2821 A[i] ← 2A[j] - A[i]
ユーザー Aging1986Aging1986
提出日時 2024-07-26 22:25:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 359 ms / 1,500 ms
コード長 6,603 bytes
コンパイル時間 2,655 ms
コンパイル使用メモリ 217,152 KB
実行使用メモリ 28,032 KB
最終ジャッジ日時 2024-07-26 22:25:39
合計ジャッジ時間 7,611 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 292 ms
26,232 KB
testcase_02 AC 359 ms
28,032 KB
testcase_03 AC 353 ms
26,532 KB
testcase_04 AC 188 ms
16,852 KB
testcase_05 AC 201 ms
17,112 KB
testcase_06 AC 111 ms
10,660 KB
testcase_07 AC 108 ms
10,652 KB
testcase_08 AC 182 ms
15,188 KB
testcase_09 AC 49 ms
7,032 KB
testcase_10 AC 52 ms
7,036 KB
testcase_11 AC 52 ms
6,940 KB
testcase_12 AC 48 ms
7,032 KB
testcase_13 AC 56 ms
7,036 KB
testcase_14 AC 67 ms
9,140 KB
testcase_15 AC 84 ms
8,912 KB
testcase_16 AC 43 ms
6,944 KB
testcase_17 AC 68 ms
8,840 KB
testcase_18 AC 75 ms
8,976 KB
testcase_19 AC 8 ms
6,940 KB
testcase_20 AC 9 ms
6,940 KB
testcase_21 AC 4 ms
6,944 KB
testcase_22 AC 6 ms
6,944 KB
testcase_23 AC 8 ms
6,940 KB
testcase_24 AC 3 ms
6,940 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 3 ms
6,944 KB
testcase_27 AC 3 ms
6,940 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 3 ms
6,944 KB
testcase_30 AC 2 ms
6,944 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 3 ms
6,944 KB
testcase_33 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<pdd> vpdd;
typedef vector<vd> vvd;
#define yn(ans) printf("%s\n", (ans)?"Yes":"No");
#define YN(ans) printf("%s\n", (ans)?"YES":"NO");
    template<class T> bool chmax(T &a, T b) {
	if (a >= b) return false;
	a = b; return true;
}
template<class T> bool chmin(T &a, T b) {
	if (a <= b) return false;
	a = b; return true;
}
#define FOR(i, s, e, t) for ((i) = (s); (i) < (e); (i) += (t)) 
#define REP(i, e) for (int i = 0; i < (e); ++i) 
#define REP1(i, s, e) for (int i = (s); i < (e); ++i)
#define RREP(i, e) for (int i = (e); i >= 0; --i)
#define RREP1(i, e, s) for (int i = (e); i >= (s); --i)
#define all(v) v.begin(), v.end()
#define pb push_back
#define qb pop_back
#define pf push_front
#define qf pop_front
#define maxe max_element
#define mine min_element
ll inf = 1e18;
#define DEBUG printf("%d\n", __LINE__); fflush(stdout);
template<class T> void print(vector<T> &v, bool withSize = false) {
	if (withSize) cout << v.size() << endl;
	REP(i, v.size()) cout << v[i] << " "; 
	cout << endl;
}
mt19937_64 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count());

int __FAST_IO__ = []() {
	std::ios::sync_with_stdio(0);
	std::cin.tie(0);
	std::cout.tie(0);
	return 0;
}();

// non-lazy version, op for merge node
template <class S, S (*op)(S, S), S (*e)()> struct segtree {
  public:
    segtree() : segtree(0) {}
    explicit segtree(int n) : segtree(std::vector<S>(n, e())) {}
    explicit segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = 0;
        while ((1 << log) < _n) ++log;
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) const {
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    S prod(int l, int r) const {
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;

        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() const { return d[1]; }

    template <bool (*f)(S)> int max_right(int l) const {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) const {
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*f)(S)> int min_left(int r) const {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) const {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

pll op(pll a, pll b) {
    return {__gcd(a.first, b.first), a.second + b.second};
}

pll e() {
    return {0, 0};
}

template <typename T> struct Discrete {
	Discrete() {}
	Discrete(vector<T> &v): p(v) {init();}
	void add(T t) {
		p.pb(t);
	}
	
	void init() {
		sort(all(p));
		p.resize(unique(all(p)) - p.begin());
	}
	
	int size() {return p.size();}
	
	int id(T t) {
		return lower_bound(all(p), t) - p.begin();
	}
	
	T operator[](int id) {return p[id];}
 
    	vector<T> &get() {return p;}
	vector<T> p;
};


#define TESTS int t; cin >> t; while (t--)
#define TEST 
int main() {
    TEST {
    	int N;
    	cin >> N;
    	vll v(N);
    	REP(i, N) cin >> v[i];
    	
    	Discrete<ll> d;
    	{
    	    multiset<ll> s;
    	    REP(i, N) {
    	        auto it = s.lower_bound(v[i]);
    	        if (it != s.end()) {
    	            d.add(*it - v[i]);
    	        }
    	        if (it != s.begin()) {
    	            d.add(v[i] - *prev(it));
    	        }
    	        s.insert(v[i]);
    	    }
    	    d.init();
    	}
    	
    	{
    	    int M = d.size();
    	    multiset<ll> s;
    	    segtree<pll, op, e> seg(M);
    	    
    	    auto add = [&](ll df) {
    	        int id = d.id(df);
    	        pll p = seg.get(id);
    	        p.first = d[id], ++p.second;
    	        seg.set(id, p);
    	    };
    	    
    	    auto remove = [&](ll df) {
    	        int id = d.id(df);
    	        pll p = seg.get(id);
    	        if (--p.second = 0) p.first = 0;
    	        seg.set(id, p);
    	    };
    	    
    	    REP(i, N) {
    	        if (i > 0) {
    	            auto it = s.lower_bound(v[i]);
    	            if (it != s.end()) {
    	                if (it != s.begin()) {
    	                    remove(*it - *prev(it));
    	                } 
    	                add(*it - v[i]);
    	            } else {
    	                add(v[i] - *prev(it));
    	            }
    	        }
    	        s.insert(v[i]);
    	        printf("%lld\n", seg.all_prod().first);
    	    }
    	}
    }
    
    return 0;
}


0