結果
| 問題 |
No.511 落ちゲー 〜手作業のぬくもり〜
|
| コンテスト | |
| ユーザー |
はむこ
|
| 提出日時 | 2016-08-30 10:23:00 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2,430 ms / 4,000 ms |
| コード長 | 12,122 bytes |
| コンパイル時間 | 2,462 ms |
| コンパイル使用メモリ | 178,640 KB |
| 実行使用メモリ | 17,844 KB |
| 最終ジャッジ日時 | 2024-11-24 12:39:12 |
| 合計ジャッジ時間 | 18,181 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 32 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#ifdef _WIN32
#define scanfll(x) scanf("%I64d", x)
#define printfll(x) printf("%I64d", x)
#else
#define scanfll(x) scanf("%lld", x)
#define printfll(x) printf("%lld", x)
#endif
#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)
#define repi(i,a,b) for(long long i = (long long)(a); i < (long long)(b); i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mt make_tuple
#define mp make_pair
template<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
template<class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
using ll = long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vll>; using vld = vector<ld>;
using vi = vector<int>; using vvi = vector<vi>;
vll conv(vi& v) { vll r(v.size()); rep(i, v.size()) r[i] = v[i]; return r; }
using P = pair<ll, ll>;
template <typename T, typename U> ostream &operator<<(ostream &o, const pair<T, U> &v) { o << "(" << v.first << ", " << v.second << ")"; return o; }
template<size_t...> struct seq{}; template<size_t N, size_t... Is> struct gen_seq : gen_seq<N-1, N-1, Is...>{}; template<size_t... Is> struct gen_seq<0, Is...> : seq<Is...>{};
template<class Ch, class Tr, class Tuple, size_t... Is>
void print_tuple(basic_ostream<Ch,Tr>& os, Tuple const& t, seq<Is...>){ using s = int[]; (void)s{0, (void(os << (Is == 0? "" : ", ") << get<Is>(t)), 0)...}; }
template<class Ch, class Tr, class... Args>
auto operator<<(basic_ostream<Ch, Tr>& os, tuple<Args...> const& t) -> basic_ostream<Ch, Tr>& { os << "("; print_tuple(os, t, gen_seq<sizeof...(Args)>()); return os << ")"; }
ostream &operator<<(ostream &o, const vvll &v) { rep(i, v.size()) { rep(j, v[i].size()) o << v[i][j] << " "; cout << endl; } return o; }
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { o << '['; rep(i, v.size()) o << v[i] << (i != v.size()-1 ? ", " : ""); o << "]"; return o; }
template <typename T> ostream &operator<<(ostream &o, const set<T> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]"; return o; }
template <typename T, typename U> ostream &operator<<(ostream &o, const map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]"; return o; }
template <typename T, typename U> ostream &operator<<(ostream &o, const unordered_map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it; o << "]"; return o; }
void printbits(ll mask, ll n) { rep(i, n) { cout << !!(mask & (1ll << i)); } cout << endl; }
#define ldout fixed << setprecision(40)
// 半開区間!!!なので注意
struct Pool {
int pos;
char mem[20000000]; // 20MB
Pool(){ free(); }
template<class T>
T *fetch(size_t n = 1) {
T *res = (T*)(mem + pos);
pos += sizeof(T)*n;
return res;
}
void free(){ pos = 0; }
}; Pool pool;
template<class T>
class AssosiativeOperator {
public:
AssosiativeOperator(void) { }
T T0; // 単位元
virtual T op(T a, T b) = 0; // 結合二項演算
// 以下は、範囲更新クエリのために必要。点更新しかしないなら、そもそも呼ばれないので、適当な値を返してよい。
T L0; // 単位操作
virtual T op_lazy(T a, T b) = 0; // 遅延更新クエリの結合二項演算。元の値がaで、今新しく来たの値がb。
virtual T resolve_lazy(T d, T l, int nl, int nr) = 0; // 頂点(量d)が、その子ら[nl, nr)の全遅延更新クエリlを解消した時の、頂点の量
};
template<class T>
class AssosiativeOperatorSum : public AssosiativeOperator<T> {
public:
AssosiativeOperatorSum(void) { AssosiativeOperator<T>::T0 = 0; AssosiativeOperator<T>::L0 = 0; }
virtual T op(T a, T b) { return a + b; } // Range Sum
virtual T op_lazy(T a, T b) { return a + b; } // Range Add
virtual T resolve_lazy(T d, T l, int nl, int nr) { return d + l * (nr - nl); } // 子の数だけ総和が増える
};
template<class T>
class SegmentTree {
public:
// dat, lazyのデータ構造
// 0123456789ABCDEF // インターフェースの添字
// ################
// 1--------------- // datの添字, 0は使わない!!
// 2-------3-------
// 4---5---6---7---
// 8-9-A-B-C-D-E-F-
// GHIJKLMNOPQRSTUV
// v<<1, v<<1|1は子どもたちを表している
// lazy[v]: vとその子供たち[nl, nr)の全員はquery(lazy[v])を遅延しているという意味
T *dat, *lazy;
AssosiativeOperator<T>* op;
int n = 1; // 確保しているサイズ!
int bits = 0; // n == 1 << bits
const size_t size_; // 確保しているサイズではない!!
int ql, qr;
bool enable_range_update_flag = false;
bool enable_point_update_with_op_flag = false;
SegmentTree(int n_, AssosiativeOperator<T>* op) : size_(n_) {
this->op = op;
while(n < n_) { n <<= 1; bits++; }
dat = pool.fetch<T>(n+n);
lazy = pool.fetch<T>(n+n);
init();
}
void init(void) {
fill_n(dat, n*4, this->op->T0);
}
void enableRangeUpdate(bool flag) { enable_range_update_flag = flag; }
void enablePointUpdateWithOp(bool flag) { enable_point_update_with_op_flag = flag; }
// 親→子
// これが呼ばれると、必ずvが正しい状態になる!
// 親のlazyを解消して子に押し付ける
// 子がいなければ押し付けない
void inline pushdown(int v, int nl, int nr){
assert(enable_range_update_flag);
dat[v] = op->resolve_lazy(dat[v], lazy[v], nl, nr);
if(v < n){ // 子がいれば
lazy[v<<1] = op->op_lazy(lazy[v<<1], lazy[v]);
lazy[v<<1|1] = op->op_lazy(lazy[v<<1|1], lazy[v]);
}
lazy[v] = op->L0;
}
// 子→親
void inline pullup(int v){
assert(enable_range_update_flag);
dat[v] = op->op(dat[v<<1], dat[v<<1|1]);
}
// 点更新
void update(int v, const T &x){
v += n;
if (enable_point_update_with_op_flag)
dat[v] = op->op(dat[v], x);
else
dat[v] = x;
while (v){
v = v >> 1;
dat[v] = op->op(dat[v<<1], dat[v<<1|1]);
}
}
// 範囲更新
// 範囲番号nの区間[nl, nr)にop(x)を演算する
void update(int n, int nl, int nr, const T &x){
// この関数は、[ql, qr)より上のノードとその子の全てにHITする
assert(enable_range_update_flag);
pushdown(n, nl, nr);
if(nr <= ql || qr <= nl) return;
if(ql <= nl && nr <= qr) {
// 一回の区間更新に付き最大3回、した区間が小さい順にHitする。
lazy[n] = op->op_lazy(lazy[n], x);
pushdown(n, nl, nr);
return;
}
int m = (nl + nr) / 2;
update(n<<1, nl, m, x); // 子供1
update(n<<1|1, m, nr, x); // 子供2
pullup(n);
}
// [l, r)にop(x)の演算を行う。
void update(int l, int r, const T &x){
ql = l; qr = r;
return update(1, 0, n, x);
}
// 範囲クエリ
// 範囲番号nの区間[nl, nr)にop(x)を演算結果を返す
T query(int n, int nl, int nr){
// この関数は、[ql, qr)より上のノードとその子の全てにHITする
if (enable_range_update_flag) pushdown(n, nl, nr);
if(nr <= ql || qr <= nl) return op->T0;
if(ql <= nl && nr <= qr) return dat[n]; // 一回の区間更新に付き最大3回、した区間が小さい順にHitする。
if (enable_range_update_flag) pullup(n);
int m = (nl + nr) / 2;
return op->op(query(n<<1, nl, m), query(n<<1|1, m, nr));
}
// [l, r)の演算結果を出力
T query(int l, int r){
ql = l; qr = r;
return query(1, 0, n);
}
// lの値を出力
T query(int l){
return query(l, l + 1);
}
void print(void) {
rep(i, size()) { cout << query(i) << " "; } cout << endl;
}
void printAll(void) {
for (int i = 1; i < n * 2; i++) {
cout << dat[i];
int j = i, count = 0; while (j) { j /= 2; count++; }
for (int j = 0; j < (1 << (bits - count + 1)); j++) cout << "\t";
if (__builtin_popcount(i+1) == 1) cout << endl;
}
cout << endl;
}
void printLazy(void) {
for (int i = 1; i < n * 2; i++) {
cout << lazy[i];
int j = i, count = 0; while (j) { j /= 2; count++; }
for (int j = 0; j < (1 << (bits - count) + 1); j++) cout << "\t";
if (__builtin_popcount(i+1) == 1) cout << endl;
}
cout << endl;
}
size_t size() const { return size_; }
};
// 並列二分探索
//
// Incrementalな時変データ構造に対して、
// test(t, q)==trueであるのはいつか?というクエリをQ個処理する
// ただしtest(t, q0)はtに対して単調であることを前提とする
//
// O((qnum qtime + stime + tmax nexttime) log tmax)
// qnum: クエリサイズ
// qtime: クエリ応答時間
// stime: データ構造の一回構築時間
// tmax: 時間幅最大
template <class query>
class TimeDependentCharacteristicFunction {
public:
TimeDependentCharacteristicFunction(void) {};
virtual bool test(query& q) = 0;
virtual void next(void) = 0;
virtual void init(void) = 0;
};
template <class query>
class BinarySearchParallel {
public:
ll tmax;
TimeDependentCharacteristicFunction<query>* f;
vector<query> qs;
vector<ll> fail, pass;
BinarySearchParallel(ll t, TimeDependentCharacteristicFunction<query>* tdcf) : tmax(t), f(tdcf) { }
void addQuery(query& q) { qs.push_back(q); }
void solve(void) {
fail.assign(qs.size(), -1); pass.assign(qs.size(), tmax);
rep(stage, (ll)(log(tmax) / log(2) + 2)) {
vector<vi> check(tmax);
rep(i, qs.size())
check[(fail[i] + pass[i]) / 2].push_back(i);
f->init();
rep(i, tmax) {
f->next();
for (int id : check[i]) if (pass[id] - fail[id] > 1)
(f->test(qs[id]) ? pass : fail)[id] = i;
}
}
}
};
// ここで定義されるデータ構造がO(log tmax)回構築される。
//
// init: データ構造と時間を初期化する
// O(stime)
//
// next: 時間を進めて、データ構造を1つ進める
// O(nexttime)
//
// test: 現在時刻tにおけるデータ構造を用いて、クエリに応答する
struct Operator {
ll a, b, x;
};
template <class query>
class TD_LazySegementTree : public TimeDependentCharacteristicFunction<query> {
public:
ll t;
ll n;
ll h;
vector<Operator> op;
SegmentTree<ll> s;
TD_LazySegementTree(ll n, ll h, vector<Operator> op) : n(n), h(h), op(op), s(n, new AssosiativeOperatorSum<ll>()) {
s.enableRangeUpdate(true);
this->init();
}
bool test(query& q) {
return s.query(q, q+1) >= h;
}
void next(void) {
s.update(op[t].x, op[t].x + op[t].a, op[t].b);
t++;
}
void init(void) {
s.init();
t = 0;
}
};
int main() {
cin.tie(0); ios::sync_with_stdio(false);
ll n, w, h; cin >> n >> w >> h;
vector<Operator> ops(n);
rep(i, n) {
cin >> ops[i].a >> ops[i].b >> ops[i].x;
ops[i].x--;
}
BinarySearchParallel<ll> bsp(n, new TD_LazySegementTree<ll>(w, h, ops));
rep(wi, w)
bsp.addQuery(wi);
bsp.solve();
ll ret = 0;
for (auto x : bsp.pass)
ret += (x % 2 == 0 ? +1 : -1);
if (ret > 0)
cout << "A" << endl;
else if (ret < 0)
cout << "B" << endl;
else
cout << "DRAW" << endl;
return 0;
}
はむこ