結果

問題 No.2595 Parsing Challenge
ユーザー ecotteaecottea
提出日時 2024-05-04 15:48:10
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 14,735 bytes
コンパイル時間 10,888 ms
コンパイル使用メモリ 432,344 KB
実行使用メモリ 310,952 KB
最終ジャッジ日時 2024-11-26 04:12:30
合計ジャッジ時間 85,491 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 47 TLE * 8
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#ifndef HIDDEN_IN_VS //
//
#define _CRT_SECURE_NO_WARNINGS
//
#include <bits/stdc++.h>
using namespace std;
//
using ll = long long; using ull = unsigned long long; // -2^63 2^63 = 9 * 10^18int -2^31 2^31 = 2 * 10^9
using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>;
using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vvvvl = vector<vvvl>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;
using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;
//
const double PI = acos(-1);
int DX[4] = {1, 0, -1, 0}; // 4
int DY[4] = {0, 1, 0, -1};
int INF = 1001001001; ll INFL = 4004004003094073385LL; // (int)INFL = INF, (int)(-INFL) = -INF;
//
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;
//
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 n-1
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s t
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s t
#define repe(v, a) for(const auto& v : (a)) // a
#define repea(v, a) for(auto& v : (a)) // a
#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d
#define repis(i, set) for(int i = lsb(set), bset##i = set; i >= 0; bset##i -= 1 << i, i = lsb(bset##i)) // set
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} //
#define EXIT(a) {cout << (a) << endl; exit(0);} //
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) //
//
template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // true
    
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // true
    
template <class T> inline T getb(T set, int i) { return (set >> i) & T(1); }
template <class T> inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // mod
//
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }
#endif //
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#ifdef _MSC_VER
#include "localACL.hpp"
#endif
//using mint = modint1000000007;
using mint = modint998244353;
//using mint = modint; // mint::set_mod(m);
namespace atcoder {
inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>;
#endif
#ifdef _MSC_VER // Visual Studio
#include "local.hpp"
#else // gcc
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }
template <size_t N> inline int lsb(const bitset<N>& b) { return b._Find_first(); }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(v)
#define dump_list(v)
#define dump_mat(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) { vc MLE(1<<30); EXIT(MLE.back()); } } // RE MLE
#endif
//static top tree
/*
* Static_top_tree<S, F, get_val, get_fnc, add_edge, merge, add_vtx, comp, act>(Graph g, int r) : O(n (log n)^2)
* r g
*
* set(int s) : O((log n)^2)
* s
*
* S get() : O(1)
*
*
*
*
* S get_val(int s) :
* s
*
* F get_fnc(int s) :
* s 1
* : ( s )
*
* S add_edge(S x, int s) :
* s x p
* p'→s p' '
*
* S merge(S x, S y) :
* 2 x, y
*
* F add_vtx(S x, int s) :
* s' s' x
* s s : ( s )
*
* F comp(F f, F g) :
* f o g
*
* S act(F f, S x) :
* f(x)
*/
template <class S, class F, S(*get_val)(int), F(*get_fnc)(int), S(*add_edge)(const S&, int), S(*merge)(const S&, const S&), F(*add_vtx)(const S&, int
    , int), F(*comp)(const F&, const F&), S(*act)(const F&, const S&)>
class Static_top_tree {
// : https://atcoder.jp/contests/abc351/editorial/9868
struct Node {
// tp :
// A:act, C:comp, V:add_vtx, M:merge, E:add_edge, f:get_fnc, x:get_val
char tp = '?';
// id : heavy path light child
// 使
int id = -1;
// [l..r) : heavy path, light child
int l = -1, r = -1;
// pp : lp[rp] : []
// 1 lp 使
Node* pp = nullptr, * lp = nullptr, * rp = nullptr;
// f :
F f;
// x :
S x;
};
// root :
Node* root;
// st[s] : s
vector<Node*> st;
public:
// r g
Static_top_tree(const Graph& g, int rt) {
// verify : https://atcoder.jp/contests/abc351/tasks/abc351_g
int n = sz(g);
// j_max[s] : s
vi j_max(n, -1);
// 調
function<int(int, int)> dfs_wgt = [&](int s, int p) {
int ws = 0; int wt_max = -INF;
rep(j, sz(g[s])) {
auto t = g[s][j];
if (t == p) continue;
int wt = dfs_wgt(t, s);
ws += wt + 1;
if (chmax(wt_max, wt)) j_max[s] = j;
}
return ws;
};
dfs_wgt(rt, -1);
// hp[s] : s heavy path
vvi hp(n);
// lc[s] : s light child
vvi lc(n);
// HL
function<void(int, int, int)> dfs_hld = [&](int s, int p, int r) {
hp[r].push_back(s);
if (j_max[s] != -1) {
int t = g[s][j_max[s]];
dfs_hld(t, s, r);
}
rep(j, sz(g[s])) {
int t = g[s][j];
if (t == p || j == j_max[s]) continue;
lc[s].push_back(t);
dfs_hld(t, s, t);
}
};
dfs_hld(rt, -1, rt);
root = new Node{ 'A', rt, 0, sz(hp[rt]) };
st.resize(n);
//
function<void(Node*)> dfs_btree = [&](Node* p) {
if (p->tp == 'A' || p->tp == 'C') {
if (p->r - p->l > 1) {
int m = (p->l + p->r) / 2;
p->lp = new Node{ 'C', p->id, p->l, m, p };
dfs_btree(p->lp);
p->rp = new Node{ p->tp, p->id, m, p->r, p };
dfs_btree(p->rp);
if (p->tp == 'A') p->x = act(p->lp->f, p->rp->x);
else p->f = comp(p->lp->f, p->rp->f);
}
else {
p->id = hp[p->id][p->l]; // 使
st[p->id] = p;
int r = sz(lc[p->id]);
if (r > 0) {
p->tp = 'V';
p->lp = new Node{ 'M', p->id, 0, r, p };
dfs_btree(p->lp);
p->f = add_vtx(p->lp->x, p->id, p->lp->id);
}
else {
if (p->tp == 'A') {
p->tp = 'x';
p->x = get_val(p->id);
}
else {
p->tp = 'f';
p->f = get_fnc(p->id);
}
}
}
}
else if (p->tp == 'M') {
if (p->r - p->l > 1) {
int m = (p->l + p->r) / 2;
p->lp = new Node{ 'M', p->id, p->l, m, p };
dfs_btree(p->lp);
p->rp = new Node{ 'M', p->id, m, p->r, p };
dfs_btree(p->rp);
p->x = merge(p->lp->x, p->rp->x);
}
else {
p->id = lc[p->id][p->l]; // 使
int r = sz(hp[p->id]);
p->tp = 'E';
p->lp = new Node{ 'A', p->id, 0, r, p };
dfs_btree(p->lp);
p->x = add_edge(p->lp->x, p->id);
}
}
};
dfs_btree(root);
}
//
S get() {
// verify : https://atcoder.jp/contests/abc351/tasks/abc351_g
return root->x;
}
/*
using S = mint;
struct F {
mint a, b;
};
S get_val(int v) {
return a[v];
}
F get_fnc(int v) {
return { 1, a[v] };
}
S add_edge(const S& x, int i) {
return x;
}
S merge(const S& x, const S& y) {
return x * y;
}
F add_vtx(const S& x, int v) {
return { x, a[v] };
}
F comp(const F& f, const F& g) {
return { f.a * g.a, f.a * g.b + f.b };
}
S act(const F& f, const S& x) {
return f.a * x + f.b;
}
Static_top_tree<S, F, get_val, get_fnc, add_edge, merge, add_vtx, comp, act> G(g, 0);
*/
};
#include <boost/multiprecision/cpp_int.hpp>
using Bint = boost::multiprecision::cpp_int;
string to_string(boost::multiprecision::cpp_int x, int B = 10) {
using Bint = boost::multiprecision::cpp_int;
if (x == 0) return "0";
bool neg = false;
if (x < 0) {
x *= -1;
neg = true;
}
// powB[k] : B^(2^k)
vector<Bint> powB{ B };
while (powB.back() < x) powB.push_back(powB.back() * powB.back());
powB.pop_back();
int K = sz(powB), n = 1 << K;
dump(K); dump(powB);
// a :
vector<Bint> a(2 * n);
a[1] = x;
repi(i, 1, n - 1) {
int k = K - 1 - msb(i);
a[2 * i + 0] = a[i] / powB[k];
a[2 * i + 1] = a[i] % powB[k];
}
dumpel(a);
string s; bool l0 = true;
if (neg) s += '-';
repi(i, n, 2 * n - 1) {
if (l0 && a[i] == 0) continue;
l0 = false;
s += a[i].str();
}
dump(s);
return s;
}
string s; int i;
vi l, r; vc op; vector<Bint> v; int pt = 0;
int number2() {
int sgn = 1;
while (s[i] == '-') {
i++;
sgn *= -1;
}
string num;
while (isdigit(s[i])) {
num += s[i];
i++;
}
l.push_back(-1);
r.push_back(-1);
op.push_back('n');
v.emplace_back(sgn * Bint(move(num))); // TLE
return pt++;
}
int expression2();
int factor2() {
int x = -1;
if (s[i] == '(') {
i++;
x = expression2();
i++;
}
else {
x = number2();
}
return x;
}
int term2() {
int x = factor2();
while (1) {
if (s[i] == '*') {
i++;
int y = factor2();
l.push_back(x);
r.push_back(y);
op.push_back('*');
v.emplace_back(0);
x = pt++;
}
else break;
}
return x;
}
int expression2() {
int x = term2();
while (1) {
if (s[i] == '+') {
i++;
int y = term2();
l.push_back(x);
r.push_back(y);
op.push_back('+');
v.emplace_back(0);
x = pt++;
}
else if (s[i] == '-') {
i++;
int y = term2();
l.push_back(x);
r.push_back(y);
op.push_back('-');
v.emplace_back(0);
x = pt++;
}
else break;
}
return x;
}
vc lr;
using S = Bint;
struct F {
Bint a, b;
};
S get_val(int s) {
return v[s];
}
F get_fnc(int s) {
return { -INFL, -INFL };
}
S add_edge(const S& x, int i) {
return x;
}
S merge(const S& x, const S& y) {
return -INFL;
}
F add_vtx(const S& x, int s, int t) {
// '-'
// static top tree
if (op[s] == '+') return { 1, x };
if (op[s] == '-') {
if (lr[t] == 'L') return { -1, x };
if (lr[t] == 'R') return { 1, -x };
}
if (op[s] == '*') return { x, 0 };
return { -INFL, -INFL };
}
F comp(const F& f, const F& g) {
return { f.a * g.a, f.a * g.b + f.b };
}
S act(const F& f, const S& x) {
return f.a * x + f.b;
}
int main() {
// input_from_file("input.txt");
// output_to_file("output.txt");
// Bint x("-123456789"); to_string(x, 10); exit(0);
int n;
cin >> n >> s;
s.push_back('$');
//
int rt = expression2();
dump(l); dump(r); dump(op); dump(v); dump(rt); dump(pt);
// static top tree
Graph g(pt); lr.assign(pt, '?');
rep(s, pt) {
if (l[s] != -1) {
lr[l[s]] = 'L';
g[s].push_back(l[s]);
g[l[s]].push_back(s);
}
if (r[s] != -1) {
lr[r[s]] = 'R';
g[s].push_back(r[s]);
g[r[s]].push_back(s);
}
}
dumpel(g); dump(lr);
Static_top_tree<S, F, get_val, get_fnc, add_edge, merge, add_vtx, comp, act> G(g, rt);
cout << to_string(G.get()) << endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0