Last active
March 26, 2025 04:36
-
-
Save cgiosy/cb61a651f031b99efe00d67d1959281d to your computer and use it in GitHub Desktop.
쉽고 짧고 빠른, 세그먼트 트리 레이지 프로퍼게이션 & 비츠
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* struct node { ... }; */ | |
int N; | |
node A[1<<LG_SZ]; | |
void qry(int s=0, int e=N-1, int i=1) { | |
if(l<=s && e<=r && A[i].apply(e-s+1)) return; | |
int m=s+e>>1; | |
A[i*2].down(A[i], m-s+1); | |
A[i*2+1].down(A[i], e-m); | |
if(l<=m) qry(s, m, i*2); | |
if(r>m) qry(m+1, e, i*2+1); | |
A[i]=A[i*2]+A[i*2+1]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* struct node { ... }; */ | |
struct seg { | |
int N; | |
vector<node> A; | |
seg(int n): N(n), A(1<<33-__builtin_clz(N)) { t=-1, l=0, r=N-1; qry(); } | |
void qry(int s, int e, int i) { | |
if(l<=s && e<=r && A[i].apply(e-s+1)) return; | |
int m=s+e>>1; | |
A[i*2].down(A[i], m-s+1); | |
A[i*2+1].down(A[i], e-m); | |
if(l<=m) qry(s, m, i*2); | |
if(r>m) qry(m+1, e, i*2+1); | |
A[i]=A[i*2]+A[i*2+1]; | |
} | |
void qry() { qry(0, N-1, 1); } | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* query variables */; | |
struct node { | |
/* member variables */ | |
inline node operator+(const node& rhs) const { | |
return {/* merge */}; | |
} | |
inline void down(const node& rhs, int sz) { | |
/* propagate */ | |
} | |
inline bool apply(int sz) { | |
/* apply and build query */ | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
int N, Q, t=-1, l, r=1e9; long x; | |
struct node { | |
long sum, lz, mx, mn; | |
inline node operator+(const node& rhs) const { | |
return {sum+rhs.sum, 0, max(mx, rhs.mx), min(mn, rhs.mn)}; | |
} | |
inline void down(const node& rhs, int sz) { | |
if(rhs.mx==rhs.mn) sum=rhs.mx*sz, mx=rhs.mx, mn=rhs.mn; | |
else lz+=rhs.lz, sum+=rhs.lz*sz, mx+=rhs.lz, mn+=rhs.lz; | |
} | |
inline bool apply(int sz) { | |
if(t==-1) { | |
if(sz>1) return false; | |
cin>>sum; mx=mn=sum; | |
return true; | |
} | |
if(t==3) { x+=sum; return true; } | |
if(t==1) { sum+=x*sz, lz+=x, mx+=x, mn+=x; return true; } | |
if(mx-mn>1) return false; | |
long m=sqrt(mx); | |
mn=sqrt(mn); | |
if(m==mn) sum=m*sz; | |
else sum+=(m-mx)*sz, lz+=m-mx; | |
mx=m; | |
return true; | |
} | |
} A[1<<17+1]; | |
void qry(int s=0, int e=N-1, int i=1) { | |
if(l<=s && e<=r && A[i].apply(e-s+1)) return; | |
int m=s+e>>1; | |
A[i*2].down(A[i], m-s+1); | |
A[i*2+1].down(A[i], e-m); | |
if(l<=m) qry(s, m, i*2); | |
if(r>m) qry(m+1, e, i*2+1); | |
A[i]=A[i*2]+A[i*2+1]; | |
} | |
int main() { | |
ios::sync_with_stdio(0);cin.tie(0); | |
cin>>N; | |
qry(); | |
cin>>Q; | |
while(Q--) { | |
cin>>t>>l>>r; --l, --r, x=0; | |
if(t==1) cin>>x; | |
qry(); | |
if(t==3) cout<<x<<'\n'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
int t, l, r; long x; | |
struct node { | |
long sum, lz, mx, mn; | |
inline node operator+(const node& rhs) const { | |
return {sum+rhs.sum, 0, max(mx, rhs.mx), min(mn, rhs.mn)}; | |
} | |
inline void down(const node& rhs, int sz) { | |
if(rhs.mx==rhs.mn) sum=rhs.mx*sz, mx=rhs.mx, mn=rhs.mn; | |
else lz+=rhs.lz, sum+=rhs.lz*sz, mx+=rhs.lz, mn+=rhs.lz; | |
} | |
inline bool apply(int sz) { | |
if(t==-1) { | |
if(sz>1) return false; | |
cin>>sum; mx=mn=sum; | |
return true; | |
} | |
if(t==3) { x+=sum; return true; } | |
if(t==1) { sum+=x*sz, lz+=x, mx+=x, mn+=x; return true; } | |
if(mx-mn>1) return false; | |
long m=sqrt(mx); | |
mn=sqrt(mn); | |
if(m==mn) sum=m*sz; | |
else sum+=(m-mx)*sz, lz+=m-mx; | |
mx=m; | |
return true; | |
} | |
}; | |
struct seg { | |
int N; | |
vector<node> A; | |
seg(int n): N(n), A(1<<33-__builtin_clz(N)) { t=-1, l=0, r=N-1; qry(); } | |
void qry(int s, int e, int i) { | |
if(l<=s && e<=r && A[i].apply(e-s+1)) return; | |
int m=s+e>>1; | |
A[i*2].down(A[i], m-s+1); | |
A[i*2+1].down(A[i], e-m); | |
if(l<=m) qry(s, m, i*2); | |
if(r>m) qry(m+1, e, i*2+1); | |
A[i]=A[i*2]+A[i*2+1]; | |
} | |
void qry() { qry(0, N-1, 1); } | |
}; | |
int main() { | |
ios::sync_with_stdio(0);cin.tie(0); | |
int N, Q; | |
cin>>N; | |
seg T(N); | |
cin>>Q; | |
while(Q--) { | |
cin>>t>>l>>r; --l, --r, x=0; | |
if(t==1) cin>>x; | |
T.qry(); | |
if(t==3) cout<<x<<'\n'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
설명:
장점:
단점:
고민: