結果
問題 | No.235 めぐるはめぐる (5) |
ユーザー | anta |
提出日時 | 2015-06-27 15:27:27 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 755 ms / 10,000 ms |
コード長 | 9,114 bytes |
コンパイル時間 | 1,043 ms |
コンパイル使用メモリ | 97,072 KB |
実行使用メモリ | 14,308 KB |
最終ジャッジ日時 | 2024-07-07 20:00:50 |
合計ジャッジ時間 | 4,229 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 755 ms
14,308 KB |
testcase_01 | AC | 361 ms
14,208 KB |
testcase_02 | AC | 679 ms
14,120 KB |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:319:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 319 | scanf("%d", &S[i]); | ~~~~~^~~~~~~~~~~~~ main.cpp:323:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 323 | scanf("%d", &C[i]); | ~~~~~^~~~~~~~~~~~~ main.cpp:336:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 336 | scanf("%d%d", &A, &B), -- A, -- B; | ~~~~~^~~~~~~~~~~~~~~~ main.cpp:344:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 344 | scanf("%d", &Q); | ~~~~~^~~~~~~~~~ main.cpp:348:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 348 | scanf("%d", &ty); | ~~~~~^~~~~~~~~~~ main.cpp:352:38: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 352 | scanf("%d%d%d", &X, &Y, &Z), -- X, -- Y; | ~~~~~^~~~~~~~~~~~~~~~~~~~~~ main.cpp:362:38: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 362 | scanf("%d%d", &X, &Y), -- X, -- Y; | ~~~~~^~~~~~~~~~~~~~~~
ソースコード
#include <string> #include <vector> #include <algorithm> #include <numeric> #include <set> #include <map> #include <queue> #include <iostream> #include <sstream> #include <cstdio> #include <cmath> #include <ctime> #include <cstring> #include <cctype> #include <cassert> #include <limits> #include <functional> #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i)) #define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i)) #if defined(_MSC_VER) || __cplusplus > 199711L #define aut(r,v) auto r = (v) #else #define aut(r,v) __typeof(v) r = (v) #endif #define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it) #define all(o) (o).begin(), (o).end() #define pb(x) push_back(x) #define mp(x,y) make_pair((x),(y)) #define mset(m,v) memset(m,v,sizeof(m)) #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii; typedef long long ll; template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; } template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; } template<int MOD> struct ModInt { static const int Mod = MOD; unsigned x; ModInt(): x(0) { } ModInt(signed sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; } ModInt(signed long long sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; } int get() const { return (int)x; } ModInt &operator+=(ModInt that) { if((x += that.x) >= MOD) x -= MOD; return *this; } ModInt &operator-=(ModInt that) { if((x += MOD - that.x) >= MOD) x -= MOD; return *this; } ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; } ModInt &operator/=(ModInt that) { return *this *= that.inverse(); } ModInt operator+(ModInt that) const { return ModInt(*this) += that; } ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } ModInt inverse() const { signed a = x, b = MOD, u = 1, v = 0; while(b) { signed t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } if(u < 0) u += Mod; ModInt res; res.x = (unsigned)u; return res; } bool operator==(ModInt that) const { return x == that.x; } bool operator!=(ModInt that) const { return x != that.x; } ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; } }; typedef ModInt<1000000007> mint; vector<mint> coefs, coefsum; struct Val { mint val; mint coef; Val() { } }; struct Sum { mint sum, coefsum; Sum() { } Sum(const Val &val): sum(val.val), coefsum(val.coef) { } Sum &operator+=(const Sum &that) { sum += that.sum; coefsum += that.coefsum; return *this; } Sum operator+(const Sum &that) const { return Sum(*this) += that; } }; struct Add { mint add; Add() { } Add &operator+=(const Add &that) { add += that.add; return *this; } void addToVal(Val &val) const { val.val += add * val.coef; } void addToSum(Sum &sum) const { sum.sum += add * sum.coefsum; } operator bool() const { return add.x != 0; } }; struct Node { private: Node *parent, *left, *right; Val val; Sum sum; Add add; bool rev; public: Node(): parent(NULL), left(NULL), right(NULL), val(), sum(Val()), add(), rev(false) { } private: bool is_root() const { return !parent || (parent->left != this && parent->right != this); } typedef unsigned long long ull; void update() { ull sum_t = val.val.x; sum.coefsum = val.coef; if(left) { sum_t += left->sum.sum.x; sum_t += (ull)left->add.add.x * left->sum.coefsum.x; sum.coefsum += left->sum.coefsum; } if(right) { sum_t += right->sum.sum.x; sum_t += (ull)right->add.add.x * right->sum.coefsum.x; sum.coefsum += right->sum.coefsum; } sum.sum.x = sum_t % mint::Mod; } void propagate() { if(rev) { if(left) left->rev = !left->rev; if(right) right->rev = !right->rev; swap(left, right); rev = false; } if(add) { val.val.x = (val.val.x + (ull)add.add.x * val.coef.x) % mint::Mod; sum.sum.x = (sum.sum.x + (ull)add.add.x * sum.coefsum.x) % mint::Mod; if(left) left->add += add; if(right) right->add += add; add.add.x = 0; } } public: bool debugCheckUpdated() const { Node tmp = *this; tmp.update(); return memcmp(this, &tmp, sizeof(Node)) == 0; } bool debugCheckPropagated() const { return !rev && !add; } private: void rotateR() { Node *q = parent, *r = q->parent; if(q->left = right) right->parent = q; right = q; q->parent = this; if(parent = r) { if(r->left == q) r->left = this; else if(r->right == q) r->right = this; } } void rotateL() { Node *q = parent, *r = q->parent; if(q->right = left) left->parent = q; left = q; q->parent = this; if(parent = r) { if(r->left == q) r->left = this; else if(r->right == q) r->right = this; } } static void global_topdown(Node *a) { Node *r = a, *q = a->parent; while(q != NULL) { Node *p = q; q = p->parent; p->parent = r; r = p; } while(r != a) { Node *c = r->parent; r->parent = q; q = r; r->propagate(); r = c; } a->propagate(); } static void splay(Node *a, bool nolastupdate = false) { if(a->is_root()) return; while(1) { Node *p = a->parent; bool plr = p->right == a; if(p->is_root()) { if(!plr) a->rotateR(), a->right->update(); else a->rotateL(), a->left->update(); break; }else { Node *g = p->parent; bool glr = g->right == p; bool groot = g->is_root(); if(plr == glr) { if(!plr) p->rotateR(), a->rotateR(), p->right->update(); else p->rotateL(), a->rotateL(), p->left->update(); p->update(); }else { if(!plr) a->rotateR(), a->rotateL(); else a->rotateL(), a->rotateR(); a->left->update(); a->right->update(); } if(groot) break; } } if(!nolastupdate) a->update(); } public: void setVal(const Val &val_) { assert(is_root()); val = val_; update(); } void addAdd(const Add &add_) { assert(is_root()); add += add_; } Val getVal() const { assert(is_root()); Val t = val; add.addToVal(t); return t; } Sum getSum() const { assert(is_root() && debugCheckUpdated()); Sum t = sum; add.addToSum(t); return t; } private: static Node *pathHead(Node *a) { assert(a->is_root() && a->debugCheckPropagated()); Node *h = a; while(1) { Node *c = h->left; if(!c) break; c->propagate(); h = c; } splay(h); return h; } static void splitPath(Node *a) { assert(a->is_root() && a->debugCheckPropagated()); Node *r = a->right; if(r != NULL) { a->right = NULL; a->update(); } } public: static void expose(Node *a) { global_topdown(a); Node *rp = NULL; for(Node *p = a; p != NULL; p = p->parent) { splay(p, true); p->right = rp; p->update(); rp = p; } splay(a); } static void exposePath(Node *a, Node *b) { evert(a); a->propagate(); expose(b); assert(a == b || a->parent != NULL); splitPath(b); } static void evert(Node *x) { expose(x); splitPath(x); assert(x->debugCheckPropagated()); x->rev = true; } static void connect(Node *x, Node *y) { evert(x); x->parent = y; } }; bool naivegetpath(int i, int p, int t, const vector<vi> &g, vi &path) { bool r = false; if(i == t) { r = true; }else { each(j, g[i]) if(*j != p) r = r || naivegetpath(*j, i, t, g, path); } if(r) path.push_back(i); return r; } int main() { int N; while(1) { if(!~scanf("%d", &N)) break; // N=rand()%10+1; vector<int> S(N), C(N); rep(i, N) { scanf("%d", &S[i]); // S[i]=rand()%100; } rep(i, N) { scanf("%d", &C[i]); // C[i]=rand()%100; } vector<Node> nodes(N); rep(i, N) { Val val; val.val = S[i]; val.coef = C[i]; nodes[i].setVal(val); } // vector<vi> g(N); rep(i, N-1) { int A, B; scanf("%d%d", &A, &B), -- A, -- B; // A=i+1,B=rand()%(i+1); Node::connect(&nodes[A], &nodes[B]); // g[A].push_back(B); // g[B].push_back(A); } // vector<mint> naiveval(all(S)); int Q; scanf("%d", &Q); // Q=rand()%100+1; rep(ii, Q) { int ty; scanf("%d", &ty); // ty=rand()%2; if(ty == 0) { int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z), -- X, -- Y; // X=rand()%N,Y=rand()%N,Z=rand()%100; Add add; add.add = Z; Node::exposePath(&nodes[X], &nodes[Y]); nodes[Y].addAdd(add); // vi naivepath; naivegetpath(X, -1, Y, g, naivepath); // each(j, naivepath) naiveval[*j] += mint(Z) * C[*j]; }else { int X, Y; scanf("%d%d", &X, &Y), -- X, -- Y; // X=rand()%N,Y=rand()%N; Node::exposePath(&nodes[X], &nodes[Y]); mint ans = nodes[Y].getSum().sum; printf("%d\n", ans.get()); // vi naivepath; naivegetpath(X, -1, Y, g, naivepath); // mint naivesum; // each(j, naivepath) naivesum += naiveval[*j]; // if(ans.get() != naivesum.get()) // cerr << ans.get() << " != " << naivesum.get() << endl; } } // rep(i, N) nodes[i].debugCheckUpdated(); } return 0; }