結果
| 問題 |
No.650 行列木クエリ
|
| コンテスト | |
| ユーザー |
beet
|
| 提出日時 | 2019-10-25 20:38:22 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 603 ms / 2,000 ms |
| コード長 | 14,897 bytes |
| コンパイル時間 | 3,406 ms |
| コンパイル使用メモリ | 233,400 KB |
| 最終ジャッジ日時 | 2025-01-08 01:01:03 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 10 |
コンパイルメッセージ
In file included from /usr/include/c++/13/functional:59,
from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:53,
from main.cpp:2:
In copy constructor ‘std::function<_Res(_ArgTypes ...)>::function(const std::function<_Res(_ArgTypes ...)>&) [with _Res = std::pair<SquareMatrix<Mint<int>, 2>, SquareMatrix<Mint<int>, 2> >; _ArgTypes = {std::pair<SquareMatrix<Mint<int, 1000000007>, 2>, SquareMatrix<Mint<int, 1000000007>, 2> >, std::pair<SquareMatrix<Mint<int, 1000000007>, 2>, SquareMatrix<Mint<int, 1000000007>, 2> >}]’,
inlined from ‘Path<Np, LIM>::Path(F, G, H, S, T, E) [with Np = NodeBase<std::pair<SquareMatrix<Mint<int>, 2>, SquareMatrix<Mint<int>, 2> >, std::pair<SquareMatrix<Mint<int>, 2>, SquareMatrix<Mint<int>, 2> > >; long unsigned int LIM = 1000000]’ at main.cpp:176:13,
inlined from ‘int YUKI_650()’ at main.cpp:646:29:
/usr/include/c++/13/bits/std_function.h:391:17: warning: ‘<anonymous>’ may be used uninitialized [-Wmaybe-uninitialized]
391 | __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
| ~~~~^~~~~~~~~~
/usr/include/c++/13/bits/std_function.h: In function ‘int YUKI_650()’:
/usr/include/c++/13/bits/std_function.h:267:7: note: by argument 2 of type ‘const std::_Any_data&’ to ‘static bool std::_Function_handler<_Res(_ArgTypes ...), _Functor>::_M_manager(std::_Any_data&, const std::_Any_data&, std::_Manager_operation) [with _Res = std::pair<SquareMatrix<Mint<int>, 2>, SquareMatrix<Mint<int>, 2> >; _Functor = YUKI_650()::<lambda(SM2, SM2)>; _ArgTypes = {std::pair<SquareMatrix<Mint<int, 1000000007>, 2>, SquareMatrix<Mint<int, 1000000007>, 2> >, std::pair<SquareMatrix<Mint<int, 1000000007>, 2>, SquareMatrix<Mint<int, 1000000007>, 2> >}]’ declared here
267 | _M_manager(_Any_data& __dest, const _Any_data& __source,
| ^~~~~~~~~~
main.cpp:646:29: note: ‘<anonymous>’ declared here
646 | LCT lct(f,g,g,flip,ti2,ei2);
|
ソースコード
#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
#endif
//BEGIN CUT HERE
template<typename Node, size_t LIM>
struct LinkCutTreeBase{
static array<Node, LIM> pool;
size_t ptr;
LinkCutTreeBase():ptr(0){}
inline Node* create(){
return &pool[ptr++];
}
inline Node* create(Node v){
return &(pool[ptr++]=v);
}
virtual void toggle(Node *t) = 0;
virtual void eval(Node *t) = 0;
virtual void pushup(Node *t) = 0;
void rotR(Node *t){
Node *x=t->p,*y=x->p;
x->sz-=t->sz;
t->sz+=x->sz;
if((x->l=t->r)) t->r->p=x,x->sz+=x->l->sz;
t->r=x;x->p=t;
pushup(x);pushup(t);
if((t->p=y)){
if(y->l==x) y->l=t;
if(y->r==x) y->r=t;
pushup(y);
}
}
void rotL(Node *t){
Node *x=t->p,*y=x->p;
x->sz-=t->sz;
t->sz+=x->sz;
if((x->r=t->l)) t->l->p=x,x->sz+=x->r->sz;
t->l=x;x->p=t;
pushup(x);pushup(t);
if((t->p=y)){
if(y->l==x) y->l=t;
if(y->r==x) y->r=t;
pushup(y);
}
}
bool is_root(Node *t){
return !t->p||(t->p->l!=t&&t->p->r!=t);
}
void splay(Node *t){
eval(t);
while(!is_root(t)){
Node *q=t->p;
if(is_root(q)){
eval(q);eval(t);
if(q->l==t) rotR(t);
else rotL(t);
}else{
auto *r=q->p;
eval(r);eval(q);eval(t);
if(r->l==q){
if(q->l==t) rotR(q),rotR(t);
else rotL(t),rotR(t);
}else{
if(q->r==t) rotL(q),rotL(t);
else rotR(t),rotL(t);
}
}
}
}
Node* expose(Node *t){
Node *rp=nullptr;
for(Node *c=t;c;c=c->p){
splay(c);
c->r=rp;
pushup(c);
rp=c;
}
splay(t);
return rp;
}
void link(Node *par,Node *c){
expose(c);
expose(par);
c->p=par;
par->r=c;
par->sz+=c->sz;
pushup(par);
}
void cut(Node *c){
expose(c);
Node *par=c->l;
c->l=nullptr;
pushup(c);
par->p=nullptr;
c->sz-=par->sz;
}
void evert(Node *t){
expose(t);
toggle(t);
eval(t);
}
Node *root(Node *t){
expose(t);
while(t->l) t=t->l;
splay(t);
return t;
}
bool is_connected(Node *a,Node *b){
return root(a)==root(b);
}
Node *lca(Node *a,Node *b){
expose(a);
return expose(b);
}
};
template<typename Node, size_t LIM>
array<Node, LIM> LinkCutTreeBase<Node, LIM>::pool;
//END CUT HERE
#ifndef call_from_test
template<typename Tp,typename Ep>
struct NodeBase{
using T = Tp;
using E = Ep;
NodeBase *l,*r,*p;
size_t sz;// tree size (only for root)
int idx;
bool rev;
T val,dat;
E laz;
NodeBase():sz(1){}
NodeBase(int idx,T val,E laz):
sz(1),idx(idx),rev(0),val(val),dat(val),laz(laz){
l=r=p=nullptr;}
};
template<typename Np, size_t LIM>
struct Path : LinkCutTreeBase<Np, LIM>{
using super = LinkCutTreeBase<Np, LIM>;
using Node = Np;
using T = typename Node::T;
using E = typename Node::E;
using F = function<T(T, T)>;
using G = function<T(T, E)>;
using H = function<E(E, E)>;
using S = function<T(T)>;
F f;
G g;
H h;
S flip;
T ti;
E ei;
Path(F f,G g,H h,T ti,E ei):
super(),f(f),g(g),h(h),ti(ti),ei(ei){
flip=[](T a){return a;};
}
Path(F f,G g,H h,S flip,T ti,E ei):
super(),f(f),g(g),h(h),flip(flip),ti(ti),ei(ei){}
void propagate(Node *t,E v){
t->laz=h(t->laz,v);
t->val=g(t->val,v);
t->dat=g(t->dat,v);
}
void toggle(Node *t){
swap(t->l,t->r);
t->dat=flip(t->dat);
t->rev^=1;
}
void eval(Node *t){
if(t->laz!=ei){
if(t->l) propagate(t->l,t->laz);
if(t->r) propagate(t->r,t->laz);
t->laz=ei;
}
if(t->rev){
if(t->l) toggle(t->l);
if(t->r) toggle(t->r);
t->rev=false;
}
}
void pushup(Node *t){
t->dat=t->val;
if(t->l) t->dat=f(t->l->dat,t->dat);
if(t->r) t->dat=f(t->dat,t->r->dat);
}
using super::expose;
T query(Node *t){
expose(t);
return t->dat;
}
void update(Node *t,E v){
expose(t);
propagate(t,v);
eval(t);
}
};
#define call_from_test
#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
#endif
//BEGIN CUT HERE
struct FastIO{
FastIO(){
cin.tie(0);
ios::sync_with_stdio(0);
}
}fastio_beet;
//END CUT HERE
#ifndef call_from_test
signed main(){
return 0;
}
#endif
#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
#endif
//BEGIN CUT HERE
template<typename T,T MOD = 1000000007>
struct Mint{
static constexpr T mod = MOD;
T v;
Mint():v(0){}
Mint(signed v):v(v){}
Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}
Mint pow(long long k){
Mint res(1),tmp(v);
while(k){
if(k&1) res*=tmp;
tmp*=tmp;
k>>=1;
}
return res;
}
static Mint add_identity(){return Mint(0);}
static Mint mul_identity(){return Mint(1);}
Mint inv(){return pow(MOD-2);}
Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
Mint& operator/=(Mint a){return (*this)*=a.inv();}
Mint operator+(Mint a) const{return Mint(v)+=a;};
Mint operator-(Mint a) const{return Mint(v)-=a;};
Mint operator*(Mint a) const{return Mint(v)*=a;};
Mint operator/(Mint a) const{return Mint(v)/=a;};
Mint operator-() const{return v?Mint(MOD-v):Mint(v);}
bool operator==(const Mint a)const{return v==a.v;}
bool operator!=(const Mint a)const{return v!=a.v;}
bool operator <(const Mint a)const{return v <a.v;}
static Mint comb(long long n,int k){
Mint num(1),dom(1);
for(int i=0;i<k;i++){
num*=Mint(n-i);
dom*=Mint(i+1);
}
return num/dom;
}
};
template<typename T,T MOD> constexpr T Mint<T, MOD>::mod;
template<typename T,T MOD>
ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed ABC127_E(){
cin.tie(0);
ios::sync_with_stdio(0);
int h,w,k;
cin>>h>>w>>k;
using M = Mint<int>;
M ans{0};
for(int d=1;d<h;d++)
ans+=M(d)*M(h-d)*M(w)*M(w);
for(int d=1;d<w;d++)
ans+=M(d)*M(w-d)*M(h)*M(h);
ans*=M::comb(h*w-2,k-2);
cout<<ans<<endl;
return 0;
}
/*
verified on 2019/06/12
https://atcoder.jp/contests/abc127/tasks/abc127_e
*/
signed main(){
//ABC127_E();
return 0;
}
#endif
#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
#endif
//BEGIN CUT HERE
template<typename R, size_t N>
struct SquareMatrix{
typedef array<R, N> arr;
typedef array<arr, N> mat;
mat dat;
SquareMatrix(){
for(size_t i=0;i<N;i++)
for(size_t j=0;j<N;j++)
dat[i][j]=R::add_identity();
}
bool operator==(const SquareMatrix& a) const{
return dat==a.dat;
}
size_t size() const{return N;};
arr& operator[](size_t k){return dat[k];};
const arr& operator[](size_t k) const {return dat[k];};
static SquareMatrix add_identity(){return SquareMatrix();}
static SquareMatrix mul_identity(){
SquareMatrix res;
for(size_t i=0;i<N;i++) res[i][i]=R::mul_identity();
return res;
}
SquareMatrix operator*(const SquareMatrix &B) const{
SquareMatrix res;
for(size_t i=0;i<N;i++)
for(size_t j=0;j<N;j++)
for(size_t k=0;k<N;k++)
res[i][j]=res[i][j]+(dat[i][k]*B[k][j]);
return res;
}
SquareMatrix operator+(const SquareMatrix &B) const{
SquareMatrix res;
for(size_t i=0;i<N;i++)
for(size_t j=0;j<N;j++)
res[i][j]=dat[i][j]+B[i][j];
return res;
}
SquareMatrix pow(long long n) const{
SquareMatrix a=*this,res=mul_identity();
while(n){
if(n&1) res=res*a;
a=a*a;
n>>=1;
}
return res;
}
};
//END CUT HERE
#ifndef call_from_test
template<typename T,T MOD = 1000000007>
struct Mint{
T v;
Mint():v(0){}
Mint(signed v):v(v){}
Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}
Mint pow(long long k){
Mint res(1),tmp(v);
while(k){
if(k&1) res*=tmp;
tmp*=tmp;
k>>=1;
}
return res;
}
static Mint add_identity(){return Mint(0);}
static Mint mul_identity(){return Mint(1);}
Mint inv(){return pow(MOD-2);}
Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
Mint& operator/=(Mint a){return (*this)*=a.inv();}
Mint operator+(Mint a) const{return Mint(v)+=a;};
Mint operator-(Mint a) const{return Mint(v)-=a;};
Mint operator*(Mint a) const{return Mint(v)*=a;};
Mint operator/(Mint a) const{return Mint(v)/=a;};
Mint operator-() const{return v?Mint(MOD-v):Mint(v);}
bool operator==(const Mint a)const{return v==a.v;}
bool operator!=(const Mint a)const{return v!=a.v;}
bool operator <(const Mint a)const{return v <a.v;}
};
struct FastIO{
FastIO(){
cin.tie(0);
ios::sync_with_stdio(0);
}
}fastio_beet;
template<typename T>
vector<T> compress(vector<T> v){
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
return v;
}
template<typename T>
map<T, Int> dict(const vector<T> &v){
map<T, Int> res;
for(Int i=0;i<(Int)v.size();i++)
res[v[i]]=i;
return res;
}
template <typename T, typename F>
struct SegmentTree{
Int n;
F f;
T ti;
vector<T> dat;
SegmentTree(){};
SegmentTree(F f,T ti):f(f),ti(ti){}
void init(Int n_){
n=1;
while(n<n_) n<<=1;
dat.assign(n<<1,ti);
}
void build(const vector<T> &v){
Int n_=v.size();
init(n_);
for(Int i=0;i<n_;i++) dat[n+i]=v[i];
for(Int i=n-1;i;i--)
dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
}
void set_val(Int k,T x){
dat[k+=n]=x;
while(k>>=1)
dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
}
T query(Int a,Int b){
T vl=ti,vr=ti;
for(Int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
if(l&1) vl=f(vl,dat[l++]);
if(r&1) vr=f(dat[--r],vr);
}
return f(vl,vr);
}
};
//INSERT ABOVE HERE
signed YAHOO2019_FINAL_D(){
using M = Mint<Int>;
using MM = SquareMatrix<M, 2>;
Int n,q;
cin>>n>>q;
vector<Int> ts(q),ls(q),rs(q),ps(q);
for(Int i=0;i<q;i++){
cin>>ts[i];
if(ts[i]==1) cin>>ps[i];
if(ts[i]==2) cin>>ls[i]>>rs[i];
}
vector<Int> vs;
for(Int i=0;i<q;i++){
if(ts[i]==1){
vs.emplace_back(ps[i]);
vs.emplace_back(ps[i]+1);
vs.emplace_back(ps[i]+2);
vs.emplace_back(ps[i]+3);
}
if(ts[i]==2){
vs.emplace_back(ls[i]);
vs.emplace_back(ls[i]+1);
vs.emplace_back(rs[i]);
vs.emplace_back(rs[i]+1);
}
}
vs.emplace_back(0);
vs.emplace_back(n+1);
vs=compress(vs);
auto ms=dict(vs);
MM A;
A[0][0]=M(1);A[0][1]=M(1);
A[1][0]=M(1);A[1][1]=M(0);
vector<MM> vt(vs.size()-1,A);
for(Int i=0;i+1<(Int)vs.size();i++){
vt[i]=A.pow(vs[i+1]-vs[i]);
}
MM I=MM::mul_identity();
auto f=[&](MM a,MM b){return b*a;};
SegmentTree<MM, decltype(f)> seg(f,I);
seg.build(vt);
vector<Int> used(vs.size(),0);
for(Int i=0;i<q;i++){
if(ts[i]==1){
Int k=ms[ps[i]];
used[k]^=1;
MM B;
for(Int j=k;j<=k+2;j++){
if(used[j]){
B[0][0]=M(0);B[0][1]=M(0);
B[1][0]=M(1);B[1][1]=M(0);
}else{
B[0][0]=M(!used[j-1]);B[0][1]=M(!used[j-2]);
B[1][0]=M(1);B[1][1]=M(0);
}
seg.set_val(j,B);
}
}
if(ts[i]==2){
Int l=ms[ls[i]],r=ms[rs[i]];
if(used[l]){
cout<<0<<"\n";
continue;
}
auto B=seg.query(l+1,r+1);
cout<<B[0][0].v<<"\n";
}
}
cout<<flush;
return 0;
}
/*
verified on 2019/09/17
https://atcoder.jp/contests/yahoo-procon2019-final/tasks/yahoo_procon2019_final_d
*/
signed DDCC2019_FINAL_D(){
string s;
cin>>s;
struct M{
uint32_t v;
M(){*this=add_identity();}
M(uint32_t v):v(v){}
M operator+(const M &a)const{return M(v+a.v);}
M operator*(const M &a)const{return M(v*a.v);}
static M add_identity(){return M(0);}
static M mul_identity(){return M(1);}
};
using SM = SquareMatrix<M, 6>;
auto f=[](SM a,SM b){return a*b;};
SM ti=SM::mul_identity();
SegmentTree<SM, decltype(f)> seg(f,ti);
vector<SM> vt(s.size(),ti);
for(int i=0;i<(int)s.size();i++){
if(s[i]=='D') vt[i][0][1]=1;
if(s[i]=='I') vt[i][1][2]=1;
if(s[i]=='S') vt[i][2][3]=1;
if(s[i]=='C') vt[i][3][4]=1;
if(s[i]=='O') vt[i][4][5]=1;
}
seg.build(vt);
int q;
cin>>q;
for(int i=0;i<q;i++){
int l,r;
cin>>l>>r;
l--;
cout<<seg.query(l,r)[0][5].v<<"\n";
}
cout<<flush;
return 0;
}
/*
verified on 2019/09/17
https://atcoder.jp/contests/ddcc2019-final/tasks/ddcc2019_final_d
*/
signed main(){
//YAHOO2019_FINAL_D();
//DDCC2019_FINAL_D();
return 0;
}
#endif
#undef call_from_test
signed YUKI_650(){
using M = Mint<int>;
using SM = SquareMatrix<M, 2>;
using SM2 = pair<SM, SM>;
using Node = NodeBase<SM2, SM2>;
constexpr size_t LIM = 1e6;
using LCT = Path<Node, LIM>;
auto f=[](SM2 x,SM2 y){return SM2(x.first*y.first,y.second*x.second);};
auto g=[](SM2 x,SM2 y){(void)x;return y;};
auto flip=[](SM2 x){swap(x.first,x.second);return x;};
SM ti=SM::mul_identity();
SM ei=SM::mul_identity();
SM2 ti2(ti,ti),ei2(ei,ei);
LCT lct(f,g,g,flip,ti2,ei2);
int n;
cin>>n;
vector< vector<int> > G(n);
vector<int> X,Y;
for(int i=1;i<n;i++){
int a,b;
cin>>a>>b;
X.emplace_back(a);
Y.emplace_back(b);
G[a].emplace_back(b);
G[b].emplace_back(a);
}
vector<LCT::Node*> vs(n*2-1);
for(int i=0;i<(int)vs.size();i++)
vs[i]=lct.create(Node(i,ti2,ei2));
vector< map<int, int> > rev(n);
int idx=n;
{
using P = pair<int, int>;
queue<P> q;
q.emplace(0,-1);
while(!q.empty()){
int v,p;
tie(v,p)=q.front();q.pop();
if(~p){
lct.link(vs[p],vs[idx]);
lct.link(vs[idx],vs[v]);
rev[p][v]=rev[v][p]=idx++;
}
for(int u:G[v])
if(u!=p) q.emplace(u,v);
}
}
int q;
cin>>q;
for(int i=0;i<q;i++){
char c;
cin>>c;
if(c=='x'){
int v,a,b,c,d;
cin>>v>>a>>b>>c>>d;
int z=rev[X[v]][Y[v]];
lct.expose(vs[z]);
SM sm;
sm[0][0]=a;sm[0][1]=b;
sm[1][0]=c;sm[1][1]=d;
vs[z]->val=SM2(sm,sm);
lct.pushup(vs[z]);
}
if(c=='g'){
int x,y;
cin>>x>>y;
lct.evert(vs[x]);
SM ans=lct.query(vs[y]).first;
cout<<ans[0][0]<<" "<<ans[0][1]<<" "<<ans[1][0]<<" "<<ans[1][1]<<"\n";
}
}
cout<<flush;
return 0;
}
/*
verified on 2019/10/25
https://yukicoder.me/problems/no/650
*/
//INSERT ABOVE HERE
signed main(){
YUKI_650();
return 0;
}
#endif
beet