結果
問題 | No.899 γatheree |
ユーザー | バイト |
提出日時 | 2019-10-10 22:11:18 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 63,261 bytes |
コンパイル時間 | 4,822 ms |
コンパイル使用メモリ | 250,928 KB |
実行使用メモリ | 72,696 KB |
最終ジャッジ日時 | 2024-11-22 00:32:25 |
合計ジャッジ時間 | 36,506 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 10 ms
12,880 KB |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | TLE | - |
testcase_07 | RE | - |
testcase_08 | WA | - |
testcase_09 | RE | - |
testcase_10 | WA | - |
testcase_11 | RE | - |
testcase_12 | WA | - |
testcase_13 | RE | - |
testcase_14 | RE | - |
testcase_15 | RE | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | RE | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
ソースコード
//#pragma GCC optimize ("-O3") #include <bits/stdc++.h> using namespace std; //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned) clock() + (unsigned) time(NULL)); }; } __initon; //衝突対策 #define ws ___ws struct T { int f, s, t; T() { f = -1, s = -1, t = -1; } T(int f, int s, int t) : f(f), s(s), t(t) {} bool operator<(const T &r) const { return f != r.f ? f < r.f : s != r.s ? s < r.s : t < r.t; //return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 大きい順 } bool operator>(const T &r) const { return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; //return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 小さい順 } bool operator==(const T &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T &r) const { return f != r.f || s != r.s || t != r.t; } int operator[](int i) { assert(i < 3); return i == 0 ? f : i == 1 ? s : t; } }; #define int long long #define ll long long #define double long double #define ull unsigned long long using dou = double; using itn = int; using str = string; using bo= bool; #define au auto using P = pair<ll, ll>; #define fi first #define se second #define vec vector #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == //マクロ省略系 コンテナ using vi = vector<int>; #define _overloadvvi(_1, _2, _3, _4, name, ...) name #define vvi0() vec<vi> #define vvi1(a) vec<vi> a #define vvi2(a, b) vec<vi> a(b) #define vvi3(a, b, c) vec<vi> a(b,vi(c)) #define vvi4(a, b, c, d) vec<vi> a(b,vi(c,d)) #define vvi(...) _overloadvvi(__VA_ARGS__,vvi4,vvi3,vvi2 ,vvi1,vvi0)(__VA_ARGS__) using vl = vector<ll>; #define _overloadvvl(_1, _2, _3, _4, name, ...) name #define vvl1(a) vec<vl> a #define vvl2(a, b) vec<vl> a(b) #define vvl3(a, b, c) vec<vl> a(b,vl(c)) #define vvl4(a, b, c, d) vec<vl> a(b,vl(c,d)) #define vvl(...) _overloadvvl(__VA_ARGS__,vvl4,vvl3,vvl2 ,vvl1)(__VA_ARGS__) using vb = vector<bool>; #define _overloadvvb(_1, _2, _3, _4, name, ...) name #define vvb1(a) vec<vb> a #define vvb2(a, b) vec<vb> a(b) #define vvb3(a, b, c) vec<vb> a(b,vb(c)) #define vvb4(a, b, c, d) vec<vb> a(b,vb(c,d)) #define vvb(...) _overloadvvb(__VA_ARGS__,vvb4,vvb3,vvb2 ,vvb1)(__VA_ARGS__) using vs = vector<string>; #define _overloadvvs(_1, _2, _3, _4, name, ...) name #define vvs1(a) vec<vs> a #define vvs2(a, b) vec<vs> a(b) #define vvs3(a, b, c) vec<vs> a(b,vs(c)) #define vvs4(a, b, c, d) vec<vs> a(b,vs(c,d)) #define vvs(...) _overloadvvs(__VA_ARGS__,vvs4,vvs3,vvs2 ,vvs1)(__VA_ARGS__) using vd = vector<double>; #define _overloadvvd(_1, _2, _3, _4, name, ...) name #define vvd1(a) vec<vd> a #define vvd2(a, b) vec<vd> a(b) #define vvd3(a, b, c) vec<vd> a(b,vd(c)) #define vvd4(a, b, c, d) vec<vd> a(b,vd(c,d)) #define vvd(...) _overloadvvd(__VA_ARGS__,vvd4,vvd3,vvd2 ,vvd1)(__VA_ARGS__) using vc=vector<char>; #define _overloadvvc(_1, _2, _3, _4, name, ...) name #define vvc1(a) vec<vc> a #define vvc2(a, b) vec<vc> a(b) #define vvc3(a, b, c) vec<vc> a(b,vc(c)) #define vvc4(a, b, c, d) vec<vc> a(b,vc(c,d)) #define vvc(...) _overloadvvc(__VA_ARGS__,vvc4,vvc3,vvc2 ,vvc1)(__VA_ARGS__) using vp = vector<P>; #define _overloadvvp(_1, _2, _3, _4, name, ...) name #define vvp1(a) vec<vp> a #define vvp2(a, b) vec<vp> a(b) #define vvp3(a, b, c) vec<vp> a(b,vp(c)) #define vvp4(a, b, c, d) vec<vp> a(b,vp(c,d)) using vt = vector<T>; #define _overloadvvt(_1, _2, _3, _4, name, ...) name #define vvt1(a) vec<vt> a #define vvt2(a, b) vec<vt> a(b) #define vvt3(a, b, c) vec<vt> a(b,vt(c)) #define vvt4(a, b, c, d) vec<vt> a(b,vt(c,d)) #define v3i(a, b, c, d) vector<vector<vi>> a(b, vector<vi>(c, vi(d))) #define v3d(a, b, c, d) vector<vector<vd>> a(b, vector<vd>(c, vd(d))) #define v3m(a, b, c, d) vector<vector<vm>> a(b, vector<vm>(c, vm(d))) #define _vvi vector<vi> #define _vvl vector<vl> #define _vvb vector<vb> #define _vvs vector<vs> #define _vvd vector<vd> #define _vvc vector<vc> #define _vvp vector<vp> #define PQ priority_queue<ll, vector<ll>, greater<ll> > #define tos to_string using mapi = map<int, int>; using mapd = map<dou, int>; using mapc = map<char, int>; using maps = map<str, int>; using seti = set<int>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<int>; #define bset bitset #define uset unordered_set #define mset multiset #define umap unordered_map #define umapi unordered_map<int,int> #define umapp unordered_map<P,int> #define mmap multimap //マクロ 繰り返し #define _overloadrep(_1, _2, _3, _4, name, ...) name # define _rep(i, n) for(int i = 0,_lim=n; i < _lim ; i++) #define repi(i, m, n) for(int i = m,_lim=n; i < _lim ; i++) #define repadd(i, m, n, ad) for(int i = m,_lim=n; i < _lim ; i+= ad) #define rep(...) _overloadrep(__VA_ARGS__,repadd,repi,_rep,)(__VA_ARGS__) #define _rer(i, n) for(int i = n; i >= 0 ; i--) #define reri(i, m, n) for(int i = m,_lim=n; i >= _lim ; i--) #define rerdec(i, m, n, dec) for(int i = m,_lim=n; i >= _lim ; i-=dec) #define rer(...) _overloadrep(__VA_ARGS__,rerdec,reri,_rer,)(__VA_ARGS__) #define fora(a, b) for(auto&& a : b) //マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const int inf = (int) 1e9 + 100; const ll linf = (ll) 1e18 + 100; const double eps = 1e-9; const double PI = 3.1415926535897932384626433832795029L; ll ma = numeric_limits<ll>::min(); ll mi = numeric_limits<ll>::max(); const int y4[] = {-1, 1, 0, 0}; const int x4[] = {0, 0, -1, 1}; const int y8[] = {0, 1, 0, -1, -1, 1, 1, -1}; const int x8[] = {1, 0, -1, 0, 1, -1, 1, -1}; //マクロ省略形 関数等 #define arsz(a) (sizeof(a)/sizeof(a[0])) #define sz(a) ((int)(a).size()) #define rs resize #define mp make_pair #define pb push_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() inline void sort(string &a) { sort(a.begin(), a.end()); } template<class T> inline void sort(vector<T> &a) { sort(a.begin(), a.end()); }; template<class T> inline void sort(vector<T> &a, int len) { sort(a.begin(), a.begin() + len); }; template<class T, class F> inline void sort(vector<T> &a, F f) { sort(a.begin(), a.end(), [&](T l, T r) { return f(l) < f(r); }); }; enum ___pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; inline void sort(vector<P> &a, ___pcomparator type) { switch (type) { case fisi: sort(all(a), [&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); break; case fisd: sort(all(a), [&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); break; case fdsi: sort(all(a), [&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); break; case fdsd: sort(all(a), [&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); break; case sifi: sort(all(a), [&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); break; case sifd: sort(all(a), [&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); break; case sdfi: sort(all(a), [&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); break; case sdfd: sort(all(a), [&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); break; } }; inline void sort(vector<T> &a, ___pcomparator type) { switch (type) { case fisi: sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); break; case fisd: sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); break; case fdsi: sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); break; case fdsd: sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); break; case sifi: sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); break; case sifd: sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); break; case sdfi: sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); break; case sdfd: sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); break; } }; template<class T> inline void rsort(vector<T> &a) { sort(a.begin(), a.end(), greater<T>()); }; template<class T> inline void rsort(vector<T> &a, int len) { sort(a.begin(), a.begin() + len, greater<T>()); }; template<class U, class F> inline void rsort(vector<U> &a, F f) { sort(a.begin(), a.end(), [&](U l, U r) { return f(l) > f(r); }); }; template<class U> inline void sortp(vector<U> &a, vector<U> &b) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n)c.eb(a[i], b[i]); sort(c); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second;; } }; //F = T<T> //例えばreturn p.fi + p.se; template<class U, class F> inline void sortp(vector<U> &a, vector<U> &b, F f) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n)c.eb(a[i], b[i]); sort(c, f); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; } }; template<class U, class F> inline void sortp(vector<U> &a, vector<U> &b, char type) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n)c.eb(a[i], b[i]); sort(c, type); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; } }; template<class U> inline void rsortp(vector<U> &a, vector<U> &b) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n)c.eb(a[i], b[i]); rsort(c); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; } }; template<class U, class F> inline void rsortp(vector<U> &a, vector<U> &b, F f) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n)c.eb(a[i], b[i]); rsort(c, f); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; } }; template<class U> inline void sortt(vector<U> &a, vector<U> &b, vector<U> &c) { vt r; int n = sz(a); assert(n == sz(b)); assert(n == sz(c)); rep(i, n)r.eb(a[i], b[i], c[i]); sort(r); rep(i, n) { a[i] = r[i].f; b[i] = r[i].s; c[i] = r[i].t; } }; template<class U, class F> inline void sortt(vector<U> &a, vector<U> &b, vector<U> &c, F f) { vt r; int n = sz(a); assert(n == sz(b)); assert(n == sz(c)); rep(i, n)r.eb(a[i], b[i], c[i]); sort(r, f); rep(i, n) { a[i] = r[i].f; b[i] = r[i].s; c[i] = r[i].t; } }; template<class U, class F> inline void rsortt(vector<U> &a, vector<U> &b, vector<U> &c, F f) { vt r; int n = sz(a); assert(n == sz(b)); assert(n == sz(c)); rep(i, n)r.eb(a[i], b[i], c[i]); rsort(r, f); rep(i, n) { a[i] = r[i].f; b[i] = r[i].s; c[i] = r[i].t; } }; template<class T> inline void sort2(vector<vector<T>> &a) { for (int i = 0, n = a.size(); i < n; i++)sort(a[i]); } template<class T> inline void rsort2(vector<vector<T>> &a) { for (int i = 0, n = a.size(); i < n; i++)rsort(a[i]); } template<typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) { rep(i, N)a[i] = v; } template<typename A, size_t N, size_t O, typename T> void fill(A (&a)[N][O], const T &v) { rep(i, N)rep(j, O)a[i][j] = v; } template<typename A, size_t N, size_t O, size_t P, typename T> void fill(A (&a)[N][O][P], const T &v) { rep(i, N)rep(j, O)rep(k, P)a[i][j][k] = v; } template<typename A, size_t N, size_t O, size_t P, size_t Q, typename T> void fill(A (&a)[N][O][P][Q], const T &v) { rep(i, N)rep(j, O)rep(k, P)rep(l, Q)a[i][j][k][l] = v; } template<typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, typename T> void fill(A (&a)[N][O][P][Q][R], const T &v) { rep(i, N)rep(j, O)rep(k, P)rep(l, Q)rep(m, R)a[i][j][k][l][m] = v; } template<typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S, typename T> void fill(A (&a)[N][O][P][Q][R][S], const T &v) { rep(i, N)rep(j, O)rep(k, P)rep(l, Q)rep(m, R)rep(n, S)a[i][j][k][l][m][n] = v; } template<typename V, typename T> void fill(V &xx, const T vall) { xx = vall; } template<typename V, typename T> void fill(vector<V> &vecc, const T vall) { for (auto &&vx: vecc) fill(vx, vall); } //@汎用便利関数 入力 template<typename T = int> T _in() { T x; cin >> x; return (x); } #define _overloadin(_1, _2, _3, _4, name, ...) name #define in0() _in() #define in1(a) cin>>a #define in2(a, b) cin>>a>>b #define in3(a, b, c) cin>>a>>b>>c #define in4(a, b, c, d) cin>>a>>b>>c>>d #define in(...) _overloadin(__VA_ARGS__,in4,in3,in2 ,in1,in0)(__VA_ARGS__) #define _overloaddin(_1, _2, _3, _4, name, ...) name #define din1(a) int a;cin>>a #define din2(a, b) int a,b;cin>>a>>b #define din3(a, b, c) int a,b,c;cin>>a>>b>>c #define din4(a, b, c, d) int a,b,c,d;cin>>a>>b>>c>>d #define din(...) _overloadin(__VA_ARGS__,din4,din3,din2 ,din1)(__VA_ARGS__) #define _overloaddind(_1, _2, _3, _4, name, ...) name #define din1d(a) int a;cin>>a;a-- #define din2d(a, b) int a,b;cin>>a>>b;a--,b-- #define din3d(a, b, c) int a,b,c;cin>>a>>b>>c;a--,b--,c-- #define din4d(a, b, c, d) int a,b,c,d;cin>>a>>b>>c>>d;;a--,b--,c--,d-- #define dind(...) _overloaddind(__VA_ARGS__,din4d,din3d,din2d ,din1d)(__VA_ARGS__) string sin() { return _in<string>(); } ll lin() { return _in<ll>(); } #define na(a, n) a.resize(n); rep(nai,n) cin >> a[nai]; #define nao(a, n) a.resize(n+1); rep(i,n) cin >> a[i+1]; #define nad(a, n) a.resize(n); rep(i,n){ cin >> a[i]; a[i]--;} #define na2(a, b, n) a.resize(n),b.resize(n);rep(i, n)cin >> a[i] >> b[i]; #define na2d(a, b, n) a.resize(n),b.resize(n);rep(i, n){cin >> a[i] >> b[i];a[i]--,b[i]--;} #define na3(a, b, c, n) a.resize(n),b.resize(n),c.resize(n); rep(i, n)cin >> a[i] >> b[i] >> c[i]; #define na3d(a, b, c, n) a.resize(n),b.resize(n),c.resize(n); rep(i, n){cin >> a[i] >> b[i] >> c[i];a[i]--,b[i]--,c[i]--;} #define nt(a, h, w) resize(a,h,w);rep(hi,h)rep(wi,w) cin >> a[hi][wi]; #define ntd(a, h, w) rs(a,h,w);rep(hi,h)rep(wi,w) cin >> a[hi][wi], a[hi][wi]--; #define ntp(a, h, w) fill(a,'#');rep(hi,1,h+1)rep(wi,1,w+1) cin >> a[hi][wi]; //デバッグ #define sp << " " << #define debugName(VariableName) # VariableName #define _deb1(x) cerr << debugName(x)<<" = "<<x << endl #define _deb2(x, y) cerr << debugName(x)<<" = "<<x<<", "<< debugName(y)<<" = "<<y<< endl #define _deb3(x, y, z) cerr << debugName(x)<<" = "<<x << ", " << debugName(y)<<" = "<<y <<", " debugName(z)<<" = "<<z <<endl #define _deb4(x, y, z, a) cerr << debugName(x)<<" = "<<x <<", " << debugName(y)<<" = "<<y <<", " << debugName(z)<<" = "<<z <<", " << debugName(a)<<" = "<<a<<endl #define _deb5(x, y, z, a, b) cerr << debugName(x)<<" = "<<x <<", " << debugName(y)<<" = "<<y <<", " << debugName(z)<<" = "<<z <<", " << debugName(a)<<" = "<<a<<", " << debugName(b)<<" = "<<b<<endl #define _overloadebug(_1, _2, _3, _4, _5, name, ...) name #define debug(...) _overloadebug(__VA_ARGS__,_deb5,_deb4,_deb3,_deb2,_deb1)(__VA_ARGS__) #define deb(...) _overloadebug(__VA_ARGS__,_deb5,_deb4,_deb3,_deb2,_deb1)(__VA_ARGS__) #define debugline(x) cerr << x << " " << "(L:" << __LINE__ << ")" << '\n' void ole() { #ifdef _DEBUG debugline("ole"); exit(0); #endif string a = "a"; rep(i, 30)a += a; rep(i, 1 << 17)cout << a << endl; cout << "OLE 出力長制限超過" << endl; exit(0); } void tle() { while (inf)cout << inf << endl; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll gcd(vi b) { ll res = b[0]; for (auto &&v :b)res = gcd(v, res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll rev(ll a) { ll res = 0; while (a) { res *= 10; res += a % 10; a /= 10; } return res; } template<class T> void rev(vector<T> &a) { reverse(all(a)); } void rev(string &a) { reverse(all(a)); } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else return (a + b - 1) / b; } ll sqrt(ll a) { if (a < 0) { debugline("sqrt"); deb(a); ole(); } ll res = (ll) std::sqrt(a); while (res * res < a)res++; return res; } double log(double e, double x) { return log(x) / log(e); } ll sig(ll t) { return (1 + t) * t / 2; } ll sig(ll s, ll t) { return (s + t) * (t - s + 1) / 2; } vi divisors(int v) { vi res; double lim = std::sqrt(v); for (int i = 1; i <= lim; ++i) { if (v % i == 0) { res.pb(i); if (i != v / i)res.pb(v / i); } } return res; } vb isPrime; vi primes; void setPrime() { int len = 4010101; isPrime.resize(4010101); fill(isPrime, true); isPrime[0] = isPrime[1] = false; for (int i = 2; i <= sqrt(len) + 5; ++i) { if (!isPrime[i])continue; for (int j = 2; i * j < len; ++j) { isPrime[i * j] = false; } } rep(i, len)if (isPrime[i])primes.pb(i); } vi factorization(int v) { int tv = v; vi res; if (isPrime.size() == 0)setPrime(); for (auto &&p :primes) { if (v % p == 0)res.push_back(p); while (v % p == 0) { v /= p; } if (v == 1 || p * p > tv)break; } if (v > 1)res.pb(v); return res; } inline bool inside(int h, int w, int H, int W) { return h >= 0 && w >= 0 && h < H && w < W; } inline bool inside(int v, int l, int r) { return l <= v && v < r; } #define ins inside ll u(ll a) { return a < 0 ? 0 : a; } template<class T> vector<T> u(const vector<T> &a) { vector<T> ret = a; fora(v, ret)v = u(v); return ret; } #define MIN(a) numeric_limits<a>::min() #define MAX(a) numeric_limits<a>::max() void yn(bool a) { if (a)cout << "yes" << endl; else cout << "no" << endl; } void Yn(bool a) { if (a)cout << "Yes" << endl; else cout << "No" << endl; } void YN(bool a) { if (a)cout << "YES" << endl; else cout << "NO" << endl; } void fyn(bool a) { if (a)cout << "yes" << endl; else cout << "no" << endl; exit(0); } void fYn(bool a) { if (a)cout << "Yes" << endl; else cout << "No" << endl; exit(0); } void fYN(bool a) { if (a)cout << "YES" << endl; else cout << "NO" << endl; exit(0); } void Possible(bool a) { if (a)cout << "Possible" << endl; else cout << "Impossible" << endl; exit(0); } void POSSIBLE(bool a) { if (a)cout << "POSSIBLE" << endl; else cout << "IMPOSSIBLE" << endl; exit(0); } template<class T, class U> set<T> &operator+=(set<T> &a, U v) { a.insert(v); return a; } template<class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.pb(v); return a; } template<class T> T sum(vector<T> &v, int s = 0, int t = inf) { T ret = 0; rep(i, s, min(sz(v), t))ret += v[i]; return ret; } void mod(int &a, int m) { a = (a % m + m) % m; } template<class F> inline int mgr(int ok, int ng, F f) { #define _mgrbody int mid = (ok + ng) / 2; if (f(mid))ok = mid; else ng = mid; if (ok < ng)while (ng - ok > 1) { _mgrbody } else while (ok - ng > 1) { _mgrbody } return ok; } template<class F> inline int mgr(int ok, int ng, int second, F f) { #define _mgrbody2 int mid = (ok + ng) / 2; if (f(mid, second))ok = mid; else ng = mid; if (ok < ng) while (ng - ok > 1) { _mgrbody2 } else while (ok - ng > 1) { _mgrbody2 } return ok; } template<typename T> ostream &operator<<(ostream &os, vector<T> &m) { for (auto &&v:m) os << v << " "; return os; } constexpr bool bget(ll m, int keta) { return (m >> keta) & 1; } int bget(ll m, int keta, int sinsuu) { m /= (ll) pow(sinsuu, keta); return m % sinsuu; } ll bit(int n) { return (1LL << (n)); } ll bit(int n, int sinsuu) { return (ll) pow(sinsuu, n); } int mask(int n) { return (1ll << n) - 1; } #define bcou __builtin_popcountll template<class T> vector<T> ruiv(vector<T> &a) { vector<T> ret(a.size() + 1); rep(i, a.size())ret[i + 1] = ret[i] + a[i]; return ret; } template<class T, class U> inline bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template<class U> inline bool chma(const U &b) { return chma(ma, b); } template<class T, class U> inline bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template<class U> inline bool chmi(const U &b) { return chmi(mi, b); } #define unique(v) v.erase( unique(v.begin(), v.end()), v.end() ); int max(vi &a) { int res = a[0]; fora(v, a) { res = max(res, v); } return res; } int min(vi &a) { int res = a[0]; fora(v, a) { res = min(res, v); } return res; } int N, K, H, W; vi A, B, C; //@formatter:off //よく使うクラス、構造体 struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees;//連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); }} bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes(){ vi cou(n); vi ret; ret.reserve(n); rep(i, n){ cou[root (i)]++; } rep(i, n){ if(cou[i])ret.push_back(cou[i]); } return ret; } //つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } vec<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1)ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r)continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; /*@formatter:off*/ #define forg(gi, ve) for (ll gi = 0,forglim = ve.size(), f, t, c; gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); ++gi) #define fort(gi, ve) for (ll gi = 0, f, t, c; gi < ve.size() && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); ++gi)if(t!=p) #define fore(gi, ve) for (ll gi = 0,forglim = ve.size(), f, t, c,ty, id; gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, id=ve[gi].id, ty = ve[gi].ty, true); ++gi) //typeが追加される #define forg2(gi, ve) for (ll gi = 0,forglim = ve.size(), f, t, c,ty; gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c,ty=ve[gi].ty, true); ++gi) #define fort2(gi, ve) for (ll gi = 0, f, t, c,ty; gi < ve.size() && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c,ty=ve[gi].ty, true); ++gi)if(t!=p) template<class T> struct edge { int f, t; T c; int id; int ty; edge(int f, int t, T c = 1, int ty = -1,int id = -1) : f(f), t(t), c(c), id(id), ty(ty) {} bool operator<(const edge &b) const { return c < b.c; } bool operator>(const edge &b) const { return c > b.c; }}; template<class T> ostream &operator<<(ostream &os, edge<T> &e) { os << e.f << " " << e.t << " " << e.c; return os;} template<typename T> class graph {protected: vector<bool> usedv;public : vector<vector<edge<T>>> g; vector<edge<T>> edges; int n; graph(int n) : n(n) { g.resize(n), usedv.resize(n); } void clear() { g.clear(), edges.clear(); } void resize(int n) { this->n = n; g.resize(n); usedv.resize(n); } int size() { return g.size(); } vector<edge<T> > &operator[](int i) { return g[i]; } virtual void add(int f, int t, T c, int ty ,int id) = 0; virtual bool used(edge<T> &e) = 0; virtual bool used(int id) = 0; virtual void del(edge<T> &e) = 0; virtual void del(int id) = 0; virtual void set_edges() = 0;}; template<typename T =ll> class digraph : public graph<T> { public: using graph<T>::g; using graph<T>::n; using graph<T>::edges; using graph<T>::usedv; int eid = 0; digraph(int n) : graph<T>(n) {} void add(int f, int t, T c = 1, int ty = -1,int id = -1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("digraph add"); deb(f, t, c, ty,id); ole(); } if (id == -1)id = eid++; g[f].emplace_back(f, t, c, ty,id); edges.emplace_back(f, t, c, ty,id); } void ing(int n,int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t; cin >> f >> t; f -= minus; t -= minus; add(f, t); } } void ingc(int n,int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t, c; cin >> f >> t >> c; f -= minus; t -= minus; add(f, t,c); } } void ingct(int n,int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t, c,ty; cin >> f >> t >> c>>ty; f -= minus; t -= minus; ty -= minus; add(f, t,c,ty); } } void ingtc(int n,int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t, c,ty; cin >> f >> t >> ty>>c; f -= minus; t -= minus; ty -= minus; add(f, t,c,ty); } } bool used(edge<T> &e) { return usedv[e.id]; } bool used(int id) { return usedv[id]; } void del(edge<T> &e) { usedv[e.id] = 1; } void del(int id) { usedv[id] = 1; } void set_edges() { if (sz(edges))return; rep(i, n)fora(e, g[i])edges.push_back(e); } }; template<class T=int> class undigraph : public graph<T> { public: using graph<T>::g; using graph<T>::n; using graph<T>::edges; using graph<T>::usedv; int eid = 0; undigraph(int n) : graph<T>(n) {} // f < t void add(int f, int t, T c = 1, int ty = -1, int id = -1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("undigraph add"); deb(f, t, c, ty, id); ole(); } if (id == -1)id = eid++; g[f].emplace_back(f, t, c, ty, id); g[t].emplace_back(t, f, c, ty, id); edges.emplace_back(f, t, c, ty, id);// edges.emplace_back(t, f, c, ty, id); } void add(edge<T> &e) { int f = e.f, t = e.t, ty = e.ty; T c = e.c; add(f, t, c, ty); } void ing(int n,int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t; cin >> f >> t; f -= minus; t -= minus; add(f, t); } } /*@formatter:on*/ void ingc(int n, int m, int minus = 1) { rep(i, m) { this->resize(n); int f, t, c; cin >> f >> t >> c; f -= minus; t -= minus; add(f, t, c); } } /*@formatter:off*/ void ingct(int n,int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t, c, ty; cin >> f >> t >> c >> ty; f -= minus; t -= minus; ty -= minus; add(f, t, c, ty); } } void ingtc(int n,int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t, c, ty; cin >> f >> t >> ty >> c; f -= minus; t -= minus; ty -= minus; add(f, t, c, ty); } } bool used(edge<T> &e) { return usedv[e.id]; } bool used(int id) { return usedv[id]; } void del(edge<T> &e) { usedv[e.id] = 1; } void del(int id) { usedv[id] = 1; } void set_edges() { if (sz(edges))return; rep(i, n)fora(e, g[i])edges.push_back(e); } }; template<class T> vector<T> dijkstra(const graph<T> &g, int s, int init_value = -1) { if (!(0 <= s && s < g.n)) { debugline("dijkstra"); deb(s, g.n); ole(); } T initValue = MAX(T); vector<T> dis(g.n, initValue); priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q; dis[s] = 0; q.emplace(0, s); while (q.size()) { T nowc = q.top().fi; int i = q.top().se; q.pop(); if (dis[i] != nowc)continue; for (auto &&e : g.g[i]) { int to = e.t; T c = nowc + e.c; if (dis[to] > c) { dis[to] = c; q.emplace(dis[to], to); } } } /*基本、たどり着かないなら-1*/ for (auto &&d :dis) if (d == initValue)d = init_value; return dis;} /*@formatter:on*/ template<class T> vector<vector<T>> dijkstra_all(const graph<T> &g, int init_value = -1) { vector<vector<T>> dis(g.n); rep(i, g.n) { dis[i] = dijkstra(g, i, init_value); } return dis; } /*@formatter:off*/ //ret vector(dis,count); 最短経路とその通りを数える template<class T> auto dijkstra_cou(const graph<T> &g, int s, int init_value = -1) { if (!(0 <= s && s < g.n)) { debugline("dijkstra"); deb(s, g.n); ole(); } T initValue = MAX(T); vector<T> dis(g.n, initValue); vi cou(g.n); cou[0] = 1; priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q; dis[s] = 0; q.emplace(0, s); while (q.size()) { T nowc = q.top().fi; int i = q.top().se; q.pop(); if (dis[i] != nowc)continue; for (auto &&e : g.g[i]) { int to = e.t; T c = nowc + e.c; if (dis[to] > c) { dis[to] = c; cou[to] = cou[e.f]; q.emplace(dis[to], to); } else if (dis[to] == c) { cou[to] += cou[e.f]; } } } /*基本、たどり着かないなら-1*/ for (auto &&d :dis) if (d == initValue)d = init_value; return vtop(dis, cou);} //コストを無限に減らせる := -linf //たどり着けない := linf template<class T> vector<T> bell(graph<T> &g, int s) { if (g.n >= 1e4) { cout << "bell size too big" << endl; exit(0); } vector<T> res(g.n, linf); res[s] = 0; vb can(g.n); /*頂点から行けない頂点を持つ、辺を消しておく */ fix([&](auto ds, int p, int i) -> void { if (can[i])return; can[i] = true; forg(gi, g[i])if (t != p)ds(i, t); })(-1, 0); vector<edge<T>> es; fora(e, g.edges) { if (can[e.f])es += e; } rep(i, g.n) { bool upd = false; fora(e, es) { if (res[e.f] != linf && res[e.t] > res[e.f] + e.c) { upd = true; res[e.t] = res[e.f] + e.c; } } if (!upd)break; } rep(i, g.n * 2) { bool upd = 0; fora(e, g.edges) { if (res[e.f] != linf && res[e.t] != -linf && res[e.t] > res[e.f] + e.c) { upd = 1; res[e.t] = -linf; } } if (!upd)break; } return res;} //コストを無限に増やせる := linf //たどり着けない := -linf template<class T> vector<T> bell_far(graph<T> &g, int s) { if (g.n >= 1e4) { cout << "bell_far size too big" << endl; exit(0); } vector<T> res(g.n, linf); res[s] = 0; vb can(g.n); /*頂点から行けない頂点を持つ、辺を消しておく*/ fix([&](auto ds, int p, int i) -> void { if (can[i])return; can[i] = true; forg(gi, g[i])if (t != p)ds(i, t); })(-1, 0); vector<edge<T>> es; fora(e, g.edges) { if (can[e.f])es += e; } rep(i, g.n) { bool upd = false; fora(e, es) { if (res[e.f] != linf && res[e.t] > res[e.f] - e.c) {/*-c*/ upd = true; res[e.t] = res[e.f] - e.c;/*-c*/ } } if (!upd)break; } rep(i, g.n * 2) { bool upd = 0; fora(e, g.edges) { if (res[e.f] != linf && res[e.t] != -linf && res[e.t] > res[e.f] - e.c) {/*-c*/ upd = 1; res[e.t] = -linf; } } if (!upd)break; } rep(i, g.n)res[i] *= -1; return res;} template<class T> vector<vector<T>> warshall(const graph<T> &g, int init_value = -1) { int n = g.n; vector<vector<T> > dis(n, vector<T>(n, linf)); rep(i, n)fora(e, g.g[i])chmi(dis[e.f][e.t], e.c); rep(i, n)dis[i][i] = 0; rep(k, n)rep(i, n)rep(j, n)chmi(dis[i][j], dis[i][k] + dis[k][j]); /*基本、たどり着かないなら-1*/ rep(i, n)rep(j, n) if (dis[i][j] == linf)dis[i][j] = init_value; return dis;} template<class T> class MinOp { public: T operator()(T a, T b) { return min(a, b); }}; template<typename OpFunc> struct SparseTable { OpFunc op; signed size; vector<signed> lg; vector<vector<pair<signed, signed>>> table; void init(const vector<pair<signed, signed>> &array, OpFunc opfunc) { signed n = array.size(); op = opfunc; lg.assign(n + 1, 0); for (signed i = 1; i <= n; i++) { lg[i] = 31 - __builtin_clz(i); } table.assign(lg[n] + 1, array); for (signed i = 1; i <= lg[n]; i++) { for (signed j = 0; j < n; j++) { if (j + (1 << (i - 1)) < n) { table[i][j] = op(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]); } else { table[i][j] = table[i - 1][j]; }}} } pair<signed, signed> query(signed l, signed r) { assert(l < r); return op(table[lg[r - l]][l], table[lg[r - l]][r - (1 << lg[r - l])]); }}; struct PMORMQ { vector<signed> a; SparseTable<MinOp<pair<signed, signed> > > sparse_table; vector<vector<vector<signed> > > lookup_table; vector<signed> block_type; signed block_size, n_block; void init(const vector<signed> &array) { a = array; signed n = a.size(); block_size = std::max(1, (31 - __builtin_clz(n)) / 2); while (n % block_size != 0) { a.push_back(a.back() + 1); n++; } n_block = n / block_size; vector<pair<signed, signed> > b(n_block, make_pair(INT_MAX, INT_MAX)); for (signed i = 0; i < n; i++) { b[i / block_size] = min(b[i / block_size], make_pair(a[i], i)); } sparse_table.init(b, MinOp<pair<signed, signed> >()); block_type.assign(n_block, 0); for (signed i = 0; i < n_block; i++) { signed cur = 0; for (signed j = 0; j < block_size - 1; j++) { signed ind = i * block_size + j; if (a[ind] < a[ind + 1]) { cur |= 1 << j; } } block_type[i] = cur; } lookup_table.assign(1 << (block_size - 1), vector<vector<signed> >(block_size, vector<signed>(block_size + 1))); for (signed i = 0; i < (1 << (block_size - 1)); i++) { for (signed j = 0; j < block_size; j++) { signed res = 0; signed cur = 0; signed pos = j; for (signed k = j + 1; k <= block_size; k++) { lookup_table[i][j][k] = pos; if (i & (1 << (k - 1))) { cur++; } else { cur--; } if (res > cur) { pos = k; res = cur; } } } } } signed query(signed l, signed r) { assert(l < r); signed lb = l / block_size; signed rb = r / block_size; if (lb == rb) { return lb * block_size + lookup_table[block_type[lb]][l % block_size][r % block_size]; } signed pl = lb * block_size + lookup_table[block_type[lb]][l % block_size][block_size]; signed pr = rb * block_size + lookup_table[block_type[rb]][0][r % block_size]; signed pos = pl; if (r % block_size > 0 && a[pl] > a[pr]) { pos = pr; } if (lb + 1 == rb) { return pos; } signed spv = sparse_table.query(lb + 1, rb).second; if (a[pos] > a[spv]) { return spv; } return pos; }}; template<class T=int> class tree : public undigraph<T> { /*@formatter:on*/ PMORMQ rmq; int cnt; vector<signed> id, in; bool never = true; bool never_hld = true; void dfs(int x, int p, int d, int dis = 0) { id[cnt] = x; par_[x] = p; dep_.push_back(d); disv[x] = dis; in[x] = cnt++; forg(gi, g[x]) { if (t == p) { continue; } dfs(t, x, d + 1, dis + c); id[cnt] = x; dep_.push_back(d); cnt++; } } void precalc() { never = false; cnt = 0; dep_.clear(); disv.assign(n, 0); in.assign(n, -1); id.assign(2 * n - 1, -1); par_.assign(n, -1); dfs(root, -1, 0); rmq.init(dep_); #ifdef _DEBUG cerr << "---tree---" << endl; rep(i, n) { if (!(i == root || sz(g[i]) > 1))continue; cerr << i << " -> "; vi ts; forg(gi, g[i]) { if (t != par_[i])ts.push_back(t); } rep(i, sz(ts) - 1)cerr << ts[i] << ", "; cerr << ts.back() << endl; } cerr << endl; #endif } int pos; void hld_build() { never_hld = false; if (never)precalc(); pos = 0; vid.assign(n, -1); head.assign(n, 0); sub.assign(n, 1); hvy.assign(n, -1); hdep.assign(n, 0); inv.assign(n, 0); type.assign(n, 0); build(); #ifdef _DEBUG cerr << "---hld_index---" << endl; vi inds; rep(i, n) if (sz(g[i]))inds.push_back(i); rep(i, sz(inds)) { str s = tos(bel(inds[i])); cerr << std::right << std::setw(sz(s) + (i ? 1 : 0)) << inds[i]; } cerr << endl; rep(i, sz(inds)) { cerr << bel(inds[i]) << " "; } cerr << endl << endl; cerr << "---hld_edge_index---" << endl; fora(e, edges) { if (e.f <= e.t) cerr << e.f << "-" << e.t << " " << bel(e) << endl; } cerr << endl << endl; #endif } void build(vector<int> rs = vector<int>(1, 0)) { int c = 0; for (int r:rs) { dfs(r); bfs(r, c++); } } void dfs(int rt) { stack<P> st; hdep[rt] = 0; st.emplace(rt, 0); while (!st.empty()) { int v = st.top().first; int &i = st.top().second; if (i < (int) g[v].size()) { int u = g[v][i++].t; if (u == par_[v]) continue; hdep[u] = hdep[v] + 1; st.emplace(u, 0); } else { st.pop(); int res = 0; forg(gi, g[v]) { int u = t; if (u == par_[v]) continue; sub[v] += sub[u]; if (res < sub[u]) res = sub[u], hvy[v] = u; } } } } void bfs(int r, int c) { int &k = pos; queue<int> q({r}); while (!q.empty()) { int h = q.front(); q.pop(); for (int i = h; i != -1; i = hvy[i]) { type[i] = c; vid[i] = k++; inv[vid[i]] = i; head[i] = h; forg(gi, g[i])if (t != par_[i] && t != hvy[i]) q.push(t); } } } vi vid; public: /*@formatter:on*/ using undigraph<T>::g; using undigraph<T>::n; using undigraph<T>::edges; using undigraph<T>::usedv; vector<int> disv; private: vector<signed> dep_, par_; public: vector<int> head, sub, hvy, inv, type, hdep/*おそらくグループ内のdep*/;/*vid := bel()*/ int root; int par(int v) { if (never)precalc(); return par_[v]; } int dep(int v) { if (never)precalc(); return dep_[v]; } tree(int n_, int root = 0) : undigraph<T>(n_), root(root) { n = n_; } int lca(int a, int b) { if (never)precalc(); int x = in[a]; int y = in[b]; if (x > y) { swap(x, y); } int pos = rmq.query(x, y + 1); return id[pos]; } int dis(int a, int b) { if (never)precalc(); int x = in[a]; int y = in[b]; if (x > y) { swap(x, y); } int pos = rmq.query(x, y + 1); int p = id[pos]; return disv[a] + disv[b] - disv[p] * 2; } /*O(N) hldを使わず木を普通にたどる*/ void for_each_l(int u, int v, function<void(int)> fnode) { int r = lca(u, v); while (u != r) { fnode(u); u = par_[u]; } while (v != r) { fnode(v); v = par_[v]; } fnode(r); } void for_each_edge_l/*O(N) 頂点に対しての処理順が可換*/(int u, int v, function<void(edge<int> &)> fedge) { int r = lca(u, v); auto sub = [&](int u, int r) { while (u != r) { forg(gi, g[u]) { if (t == par_[u]) { fedge(g[u][gi]); u = par_[u]; break; } } } }; sub(u, r); sub(v, r); } /*Fは半開 (u,v)は木の頂点* / /*中ではhldの頂点を見るため、seg木のupdateはhldのindexで行なう*/ void for_each_(int u, int v, const function<void(int, int)> &f) { if (never_hld)hld_build(); while (1) { if (vid[u] > vid[v]) swap(u, v); int l = max(vid[head[v]], vid[u]); int r = vid[v] + 1; f(l, r); if (head[u] != head[v]) v = par_[head[v]]; else break; } } void for_each_edge/*[l,r) O(log(N)) 辺を頂点として扱っている 上と同じ感じで使える*/(int u, int v, const function<void(int, int)> &f) { if (never_hld)hld_build(); while (1) { if (vid[u] > vid[v]) swap(u, v); if (head[u] != head[v]) { int l = vid[head[v]]; int r = vid[v] + 1; f(l, r); v = par_[head[v]]; } else { if (u != v) { int l = vid[u] + 1; int r = vid[v] + 1; f(l, r); } break; } } } int bel(int v) { /*hld内での頂点番号を返す*/ if (never_hld)hld_build(); return vid[v]; } int bel(int f, int t) { /*辺のクエリを扱うときどの頂点に持たせればいいか(vidを返すのでそのままupd出来る)*/ if (never_hld)hld_build(); return hdep[f] > hdep[t] ? vid[f] : vid[t]; } int bel(edge<T> &e) { /*辺のクエリを扱うときどの頂点に持たせればいいか(vidを返すのでそのままupd出来る)*/ if (never_hld)hld_build(); return hdep[e.f] > hdep[e.t] ? vid[e.f] : vid[e.t]; } template<class ... U> int operator()(U ... args) { return bel(args...); } };; //辺によりメモリを大量消費ためedgesを消している //頂点10^6でメモリを190MB(制限の8割)使う template<class T=int> class grid_k6 : public undigraph<T> { public: using undigraph<T>::g; using undigraph<T>::n; using undigraph<T>::edges; using undigraph<T>::usedv; int H, W; int eid = 0; void add(int f, int t, T c = 1, int ty = -1, int id = -1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("grid_k6 add"); deb(f, t, c, ty, id); ole(); } g[f].emplace_back(f, t, c, ty, eid++); g[t].emplace_back(t, f, c, ty, eid++); } int getid(int h, int w) { if (!ins(h, w, H, W))return -1; return W * h + w; } P get2(int id) { return mp(id / W, id % W); } P operator()(int id) { return get2(id); } int operator()(int h, int w) { return getid(h, w); } grid_k6(int H, int W) : H(H), W(W), undigraph<T>(H * W) { rep(h, H) { rep(w, W) { int f = getid(h, w); if (w + 1 < W) add(f, getid(h, w + 1)); if (h + 1 < H)add(f, getid(h + 1, w)); } } } grid_k6(vector<vector<char>> ba, char wall = '#') : H(sz(ba)), W(sz(ba[0])), undigraph<T>(sz(ba) * sz(ba[0])) { rep(h, H) { rep(w, W) { if (ba[h][w] == wall)con; int f = getid(h, w); if (w + 1 < W && ba[h][w + 1] != wall) { add(f, getid(h, w + 1)); } if (h + 1 < H && ba[h + 1][w] != wall) { add(f, getid(h + 1, w)); } } } } void add(int fh, int fw, int th, int tw) { add(getid(fh, fw), getid(th, tw)); } void set_edges() { rep(i, n)fora(e, g[i])if (e.f < e.t)edges.push_back(e); } }; //左上から右下に移動できる template<class T=int> class digrid_k6 : public digraph<T> { public: using digraph<T>::g; using digraph<T>::n; using digraph<T>::edges; using digraph<T>::usedv; int H, W; int eid = 0; void add(int f, int t, T c = 1, int ty = -1, int id = -1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("digrid_k6 add"); deb(f, t, c, ty, id); ole(); } g[f].emplace_back(f, t, c, ty, eid++); } int getid(int h, int w) { if (!ins(h, w, H, W))return -1; return W * h + w; } P get2(int id) { return mp(id / W, id % W); } P operator()(int id) { return get2(id); } int operator()(int h, int w) { return getid(h, w); } digrid_k6(int H, int W) : H(H), W(W), digraph<T>(H * W) { rep(h, H) { rep(w, W) { int f = getid(h, w); if (w + 1 < W) add(f, getid(h, w + 1)); if (h + 1 < H)add(f, getid(h + 1, w)); } } } digrid_k6(vector<vector<char>> ba, char wall = '#') : H(sz(ba)), W(sz(ba[0])), digraph<T>(sz(ba) * sz(ba[0])) { rep(h, H) { rep(w, W) { if (ba[h][w] == wall)con; int f = getid(h, w); if (w + 1 < W && ba[h][w + 1] != wall) { add(f, getid(h, w + 1)); } if (h + 1 < H && ba[h + 1][w] != wall) { add(f, getid(h + 1, w)); } } } } void add(int fh, int fw, int th, int tw) { add(getid(fh, fw), getid(th, tw)); } void set_edges() { rep(i, n)fora(e, g[i])edges.push_back(e); } }; template<class T> bool nibu(const graph<T> &g) { int size = 0; rep(i, g.n)size += sz(g.g[i]); if (size == 0)return true; unionfind uf(g.n * 2); rep(i, g.n)fora(e, g.g[i])uf.unite(e.f, e.t + g.n), uf.unite(e.f + g.n, e.t); rep(i, g.n)if (uf.same(i, i + g.n))return 0; return 1; } //二部グラフを色分けした際の頂点数を返す template<class T> vp nibug(graph<T> &g) { vp cg; if (!nibu(g)) { debugline("nibu"); ole(); } int n = g.size(); vb was(n); queue<P> q; rep(i, n) { if (was[i])continue; q.push(mp(i, 1)); was[i] = 1; int red = 0; int coun = 0; while (q.size()) { int now = q.front().fi; int col = q.front().se; red += col; coun++; q.pop(); forg(gi, g[now]) { if (was[t])continue; q.push(mp(t, col ^ 1)); was[t] = 1; } } cg.push_back(mp(red, coun - red)); } return cg; } template<class T> ostream &operator<<(ostream &os, digraph<T> &g) { os << endl << g.n << " " << sz(g.edges) << endl; fore(gi, g.edges) { os << f << " " << t << " " << c << endl; } return os; } template<class T> ostream &operator<<(ostream &os, undigraph<T> &g) { os << endl << g.n << " " << sz(g.edges) << endl; fore(gi, g.edges) { if (f < t)os << f << " " << t << " " << c << endl; } return os; } //閉路がなければtrue bool topo(vi &res, digraph<int> &g) { int n = g.g.size(); vi nyu(n); rep(i, n)for (auto &&e :g[i])nyu[e.t]++; queue<int> st; rep(i, n)if (nyu[i] == 0)st.push(i); while (st.size()) { int v = st.front(); st.pop(); res.push_back(v); fora(e, g[v]) if (--nyu[e.t] == 0)st.push(e.t); } return res.size() == n; } //辞書順最小トポロジカルソート bool topos(vi &res, digraph<int> &g) { int n = g.g.size(); vi nyu(n); rep(i, n)for (auto &&e :g[i])nyu[e.t]++; /*小さい順*/ priority_queue<int, vector<int>, greater<int> > q; rep(i, n)if (nyu[i] == 0)q.push(i); while (q.size()) { int i = q.top(); q.pop(); res.push_back(i); fora(e, g[i])if (--nyu[e.t] == 0)q.push(e.t); } return res.size() == n; } //閉路がある時linfを返す template<class T> int longest_path(digraph<T> &g) { vi top; if (!topo(top, g)) { return linf; } int n = sz(top); vi dp(n, 0); for (auto &&i : top) { forg(gi, g[i]) { chma(dp[t], dp[i] + 1); }} return max(dp); } template<class T> vi longest_path_v(digraph<T> &g) { vi top; if (!topo(top, g)) { return vi(); } int n = sz(top); vi dp(n, 0); vi pre(n, -1); for (auto &&i : top) { forg(gi, g[i]) { if (chma(dp[t], dp[i] + 1)) { pre[t] = i; }}} int s = std::max_element(dp.begin(), dp.end()) - dp.begin(); vi path; while (s != -1) { path.push_back(s); s = pre[s]; } std::reverse(path.begin(), path.end()); return path; } //連結グラフが与えられる 閉路があるか template<class T> bool close(undigraph<T> &g) { int n = 0; int e = 0; rep(i, g.n) { if (sz(g[i]))n++; forg(gi, g[i]) { e++; } } return (e >> 1) >= n; } template<class T> bool close(undigraph<T> &g, int v) { unionfind uf(g.n); rep(i, g.n) { forg(gi, g[i]) { if (f < t)break; if (f == t && f == v)return true; if (uf.same(f, v) && uf.same(t, v))return true; uf.unite(f, t); } } return false; } template<class T> bool close(digraph<T> &g) { vi res; return topo(res, g); } template<class T> vi indegree(graph<T> &g) { vi ret(g.size()); rep(i, g.size()) { forg(gi, g[i]) { ret[t]++; }} return ret; } template<class T> vi outdegree(graph<T> &g) { vi ret(g.size()); rep(i, g.size()) { ret[i] = g[i].size(); } return ret; } template<class T> digraph<T> rev(digraph<T> &g) { digraph<T> r(g.n); rep(i, g.n) { forg(gi, g[i]) { r.add(t, f, c); }} return r; } //橋を列挙する (取り除くと連結でなくなる辺) template<class T> vp bridge(graph<T> &G) { static bool was; vp brid; vi articulation; vi ord(G.n), low(G.n); vb vis(G.n); function<void(int, int, int)> dfs = [&](int v, int p, int k) { vis[v] = true; ord[v] = k++; low[v] = ord[v]; bool isArticulation = false; int ct = 0; for (int i = 0; i < G[v].size(); i++) { if (!vis[G[v][i].t]) { ct++; dfs(G[v][i].t, v, k); low[v] = min(low[v], low[G[v][i].t]); if (~p && ord[v] <= low[G[v][i].t]) isArticulation = true; if (ord[v] < low[G[v][i].t]) brid.push_back(make_pair(min(v, G[v][i].t), max(v, G[v][i].t))); } else if (G[v][i].t != p) { low[v] = min(low[v], ord[G[v][i].t]); } } if (p == -1 && ct > 1) isArticulation = true; if (isArticulation) articulation.push_back(v); }; int k = 0; rep(i, G.n) { if (!vis[i]) dfs(i, -1, k); } sort(brid.begin(), brid.end()); return brid; } //間接点を列挙する (取り除くと連結でなくなる点) template<class T> vi articulation(undigraph<T> &G) { static bool was; vp bridge; vi arti; vi ord(G.n), low(G.n); vb vis(G.n); function<void(int, int, int)> dfs = [&](int v, int p, int k) { vis[v] = true; ord[v] = k++; low[v] = ord[v]; bool isArticulation = false; int ct = 0; for (int i = 0; i < G[v].size(); i++) { if (!vis[G[v][i].t]) { ct++; dfs(G[v][i].t, v, k); low[v] = min(low[v], low[G[v][i].t]); if (~p && ord[v] <= low[G[v][i].t]) isArticulation = true; if (ord[v] < low[G[v][i].t]) bridge.push_back(make_pair(min(v, G[v][i].t), max(v, G[v][i].t))); } else if (G[v][i].t != p) { low[v] = min(low[v], ord[G[v][i].t]); } } if (p == -1 && ct > 1) isArticulation = true; if (isArticulation) arti.push_back(v); }; int k = 0; rep(i, G.n) { if (!vis[i]) dfs(i, -1, k); } sort(arti.begin(), arti.end()); return arti; } #define kansetu articulation P farthest(undigraph<> &E, int cur, int pre, int d, vi &D) { D[cur] = d; P r = {d, cur}; forg(gi, E[cur]) if (t != pre) { P v = farthest(E, t, cur, d + 1, D); r = max(r, v); } return r; } //dagでなければ-1を返す int diameter(digraph<> &g) { vi per; if (!topo(per, g))return -1; int n = g.n; vi dp(n, 1); fora(v, per) { forg(gi, g[v]) { chma(dp[t], dp[f] + 1); }} return max(dp); } vi diameter(undigraph<> &E) { /* diameter,center*/vi D[3]; D[0].resize(E.size()); D[1].resize(E.size()); auto v1 = farthest(E, 0, 0, 0, D[0]); auto v2 = farthest(E, v1.second, v1.second, 0, D[0]); farthest(E, v2.second, v2.second, 0, D[1]); int i; rep(i, D[0].size()) D[2].push_back(max(D[0][i], D[1][i])); return D[2]; } //i d vp diameter_p(undigraph<> &E) { /* diameter,center*/vector<int> D[3]; D[0].resize(E.size()); D[1].resize(E.size()); auto v1 = farthest(E, 0, 0, 0, D[0]); auto v2 = farthest(E, v1.second, v1.second, 0, D[0]); farthest(E, v2.second, v2.second, 0, D[1]); int i; vp res(E.size()); rep(i, D[0].size()) { if (D[0][i] > D[1][i])res[i] = mp(D[0][i], v1.second); else res[i] = mp(D[1][i], v2.second); } return res; } /*閉路が1つしかない場合、その閉路に含まれる頂点を1としたvectorを返す*/; //template<class T> vi get_close1(digraph<T> &g) { int n = g.n; queue<int> q; vi d = outdegree(g); vi res(n, 1); rep(i, n) { if (d[i] == 0) { q += i; res[i] = 0; } } auto rg = rev(g); while (q.size()) { auto now = q.front(); q.pop(); forg(gi, rg[now]) { if (--d[t] == 0) { q += t; res[t] = 0; } } } return res;}; //閉路パスを一つ返す //vi close_path(digraph<> &g) { int n = g.n; vi state(n); vi path; rep(i, n) if (!state[i]) { if (fix([&](auto dfs, int v) -> bool { if (state[v]) { if (state[v] == 1) { path.erase(path.begin(), find(path.begin(), path.end(), v)); return true; } return false; } path.push_back(v); state[v] = 1; forg(gi, g[v]) { if (dfs(t))return true; } state[v] = -1; path.pop_back(); return false; })(i)) { return path; } } return vi();} vi close_path_min(digraph<> &g) { int n = g.n; vvi(dis, n); rep(i, n)dis[i] = dijkstra(g, i, linf); int mind = linf; int f = 0, t = 0; rep(i, n) { rep(j, n) { if (i == j)continue; if (chmi(mind, dis[i][j] + dis[j][i])) { f = i; t = j; } } } vi path; auto add = [&](int f, int t) { int now = f; while (now != t) { rep(i, n) { if (dis[now][i] == 1 && dis[f][i] + dis[i][t] == dis[f][t]) { path.push_back(i); now = i; break; } } } }; add(f, t); add(t, f); return path; } template<class T> int krus(undigraph<T> &g) { int res = 0; unionfind uf(g.n); if (sz(g.edges) == 0)g.set_edges(); int i = 0; auto E = g.edges; sort(E); fora(e, E) { if (uf.unite(e.f, e.t)) { res += e.c; }} return res; } //idは 00 11 22のようにedgesに持たれている template<class T> vi krus_id(undigraph<T> &g) { unionfind uf(g.n); if (sz(g.edges) == 0)g.set_edges(); int i = 0; auto E = g.edges; sort(E); vi res; fora(e, E) { if (uf.unite(e.f, e.t)) { res.push_back(e.id); }} return res; } template<class T> vector<edge<T>> krus_ed(undigraph<T> &g) { unionfind uf(g.n); if (sz(g.edges) == 0)g.set_edges(); int i = 0; auto E = g.edges; sort(E); vector<edge<T>> res; fora(e, E) { if (uf.unite(e.f, e.t)) { res.push_back(e); }} return res; } template<class T> tree<T> krus_tr(undigraph<T> &g) { tree<T> res(g.n); unionfind uf(g.n); if (sz(g.edges) == 0)g.set_edges(); int i = 0; auto E = g.edges; sort(E); fora(e, E) { if (uf.unite(e.f, e.t)) { res.add(e.f, e.t); }} return res; } template<class T> vector<vector<edge<T>>> type_list(digraph<T> &g) { vector<vector<edge<T>>> res; rep(i, g.n) { forg2(gi, g[i]) { res[ty].push_back(g[i][gi]); }} return res; } template<class T> vector<vector<edge<T>>> type_list(undigraph<T> &g, int types = -1) { int tn = types; if (types == -1)tn = g.n; rep(i, g.n) { forg2(gi, g[i]) { chma(tn, ty); }} vector<vector<edge<T>>> res(tn + 1); vi was(g.n); rep(i, g.n) { forg2(gi, g[i]) { if (f < t)res[ty].push_back(g[i][gi]); else if (f == t && !was[f]) { res[ty].push_back(g[i][gi]); was[f] = 1; } } } return res; } /*頂点数がkの木を一つ返す サイズが0の木が帰ったら終了*/ //短い版 tree<> g(2 * k5); void ds(int i, int p, vi &l, vi &r, vi &ind) { forg(gi, g[i]) { chmi(l[i], ind[t]); chma(r[i], ind[t]); ds(t, i, l, r, ind); } } template<typename T> struct segusum {/*@formatter:off*/ int n, height; T fvv(T a, T b) {return a+b;} T fvl(int len, T x) {return len*x;} T ve = 0; //演算における単位元 T le = MIN(T);//作用素に対しての単位元 vector<T> dat; vector<T> laz; vi len; segusum(vector<T> a) { n = 1; int n_ = a.size(); height = 0; while (n <= n_) n <<= 1, height++; dat.assign(n << 1, ve); laz.assign(n << 1, le); len.assign(n << 1, 0); int l = 1, v = n; rep(i, 1, n << 1) { if ((l << 1) == i) { l <<= 1; v >>= 1; } len[i] = v; } for (int i = 0; i < n_; i++) dat[n + i] = a[i]; for (int i = n - 1; i; i--) dat[i] = fvv(dat[(i << 1) | 0], dat[(i << 1) | 1]); } //おそらく1-indexed T reflect(int k) { return laz[k] == le ? dat[k] : fvl(len[k], laz[k]); } void eval(int k) { if (laz[k] == le) return; laz[(k << 1)] = laz[k]; laz[(k << 1) | 1] = laz[k]; dat[k] = reflect(k); laz[k] = le; } void thrust(int k) {for (int i = height; i; i--) eval(k >> i);} void recalc(int k) { while (k >>= 1)dat[k] = fvv(reflect((k << 1) | 0), reflect((k << 1) | 1)); } void update(int a, int b, T x) { thrust(a += n); thrust(b += n - 1); for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) laz[l] = x, l++; if (r & 1) --r, laz[r] = x; } recalc(a); recalc(b); } void update(int a,T x){update(a,a+1,x);} // void set_val(int a, T x) { thrust(a += n); dat[a] = x; laz[a] = le; recalc(a); } T get(int a, int b) { thrust(a += n); thrust(b += n - 1); T vl = ve, vr = ve; for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) vl = fvv(vl, reflect(l++)); if (r & 1) vr = fvv(reflect(--r), vr); } return fvv(vl, vr); } T get(int b){return get(0,b);} T operator[](int a){return get(a,a+1);} void debu() { rep(i, min(n, 10ll)) { cerr << get(i, i + 1) << " "; } cerr << "" << endl; }/*@formatter:on*/ }; template<class T> tree<T> get_bfs_tree(tree<T> &g, vi &ind, vi &l, vi &r) { if (sz(ind)) { cerr << "ind must be empty" << endl; exit(0); } int N = sz(g); ind.resize(N); l.resize(N, inf); r.resize(N, -1); tree<T> h(N); queue<P> q; q.emplace(-1, 0); int c = 0; while (sz(q)) { int p = q.front().first; int i = q.front().second; q.pop(); ind[i] = c; if (~p)chmi(l[ind[p]], c); if (~p)chma(r[ind[p]], c); c++; forg(gi, g[i]) { if (t != p)q.emplace(i, t); } } fora(e, g.edges) { if (e.f < e.t) { h.add(ind[e.f], ind[e.t], e.c); } } rep(i, N) { if (l[i] == inf)l[i] = -1; } return h; } signed main() { cin >> N; tree<> dg(2 * k5); dg.ing(N, N - 1, 0); vi ind, l, r; auto g = get_bfs_tree(dg, ind, l, r); na(A, N); vi B(N); rep(i, N) { B[ind[i]] = A[i]; } deb(l, r); deb(B); din(Q); segusum<int> s(B); rep(i, Q) { din(x); x = ind[x]; int sum = 0; auto calc = [&](int lp, int rp) { if (lp == -1)return; sum += s.get(lp, rp + 1); s.update(lp, rp + 1, 0); }; auto tan = [&](int p) { calc(p, p); }; calc(l[x], r[x]);//1 calc(l[l[x]], r[r[x]]);//2 int par = g.par(x); tan(x); if (par != -1) { tan(par); calc(l[par], r[par]); int pp = g.par(par); tan(pp); } s.update(x, sum); cout << sum << endl; } return 0; }