├── CSES Solutions ├── Introductory Problems │ ├── Apple Division.cpp │ ├── Bit Strings.cpp │ ├── Chessboard and Queens.cpp │ ├── Coin Piles.cpp │ ├── Creating Strings.cpp │ ├── Digit Queries.cpp │ ├── Gray Code.cpp │ ├── Increasing Array.cpp │ ├── Missing Number.cpp │ ├── Number Spiral.cpp │ ├── Palindrome Reorder.cpp │ ├── Permutations.cpp │ ├── Repetitions.cpp │ ├── Tower of Hanoi.cpp │ ├── Trailing Zeros.cpp │ ├── Two Knights.cpp │ ├── Two Sets.cpp │ └── Weird Algorithm.cpp └── readme.txt ├── Codeforces-EDU ├── 2-pointers │ ├── Step1 │ │ ├── A - Merging Arrays .cpp │ │ ├── B - Number of Smaller .cpp │ │ └── C - Number of Equal .cpp │ └── Step2 │ │ ├── A - Segment with Small Sum.cpp │ │ ├── B - Segment with Big Sum.cpp │ │ └── C - Number of Segments with Small Sum.cpp ├── Binary Search │ ├── Step 1 │ │ ├── A-Binary Search.cpp │ │ ├── B-Closest to the Left.cpp │ │ ├── C-Closest to the Right.cpp │ │ └── D- Fast search.cpp │ └── Step 2 │ │ └── B-Ropes.cpp ├── DSU │ ├── Step 1 │ │ ├── A-Disjoint Sets Union.cpp │ │ ├── B-Disjoint Sets Union 2.cpp │ │ ├── C-Experience.cpp │ │ └── D - Cutting a graph.cpp │ ├── Step2 │ │ ├── A - People are leaving .cpp │ │ ├── B - Parking.cpp │ │ ├── C - Restructuring Company.cpp │ │ └── readme.txt │ └── Step3 │ │ ├── A. DSU with rollback.cpp │ │ └── C - Dynamic Connectivity Offline .cpp ├── SegmentTree │ ├── Part1 │ │ ├── Step1 │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ └── C.cpp │ │ ├── Step2 │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ ├── C.cpp │ │ │ └── D.cpp │ │ ├── Step3 │ │ │ ├── A.cpp │ │ │ ├── B.cpp │ │ │ ├── C.cpp │ │ │ ├── D.cpp │ │ │ └── E.cpp │ │ └── Step4 │ │ │ └── A.cpp │ └── Part2 │ │ ├── Step1 │ │ ├── A.cpp │ │ ├── B.cpp │ │ └── C.cpp │ │ └── Step2 │ │ ├── A.cpp │ │ ├── B.cpp │ │ ├── C.cpp │ │ ├── D.cpp │ │ ├── E.cpp │ │ └── F.cpp └── Suffix Array │ ├── Step1 │ └── Suffix Array - 1.cpp │ ├── Step2 │ └── Suffix Array - 2.cpp │ ├── Step3 │ ├── A - Substring Search.cpp │ └── B - Counting Substrings.cpp │ ├── Step4 │ └── Suffix Array and LCP.cpp │ └── Step5 │ ├── A - Number of Different Substrings.cpp │ ├── B - Longest Common Substring.cpp │ ├── C - Sorting Substrings.cpp │ ├── D - Borders.cpp │ └── E - Refrain.cpp ├── Material └── statements.pdf ├── README.md └── Templates ├── Graph Theory ├── 2_sat.cpp ├── Floyed.cpp ├── MaxFlow (Dinic).cpp ├── Maximum Bipartite Matching.cpp ├── SCC (trajan).cpp ├── dijkstra.cpp ├── dsu with roll back.cpp └── dsu.cpp ├── Math and Number Theory ├── AndSumForRange.cpp ├── BinaryTrie.cpp ├── Bitmask Operation.cpp ├── Combinatorics.cpp ├── EEA.cpp ├── FFT with mod.cpp ├── FFT.cpp ├── FWHT.cpp ├── Matrix Expo.cpp ├── NTT.cpp ├── Number TheoryFunctions.cpp ├── NumberOfDivisors (for large numbers).cpp ├── Pascal Triangle.cpp ├── Pollard.cpp ├── Solve equation.cpp ├── Xor Basis.cpp ├── from decimal to any base.cpp ├── int128.cpp ├── is_perfect_square.cpp ├── linear_sieve.cpp ├── phi.cpp └── primality test for large numbers.cpp ├── RQ Data Structure ├── 2dPrefixSum.cpp ├── FenwickTree.cpp ├── MergeSortSegmentTree.cpp ├── MoAlgorithm.cpp ├── PartialSum.cpp ├── PersistentSegmentTree.cpp ├── SegmentTree.cpp ├── SegmentTreeBeats.cpp ├── Sparse Table.cpp ├── Treap with lazy propegation.cpp ├── Treap.cpp ├── coordinate compression(one array).cpp ├── coordinateCopmression.cpp └── partial sum 2d.cpp ├── Some DSA ├── LCS (Nlog(N)).cpp ├── LIS.cpp ├── PermutaionHashing.cpp ├── Random.cpp ├── SOS dp.cpp ├── completeSearch.cpp └── orderd set , multiset .cpp ├── Strings ├── Hashing (substring).cpp ├── Hashing Treap.cpp ├── Hashing.cpp ├── HashingSegmentTree.cpp ├── Kmp.cpp ├── PalindromicTree.cpp ├── Suffix Array and LCP.cpp ├── SuffixArray.cpp ├── Trie.cpp ├── Z algorithm.cpp └── get_words.cpp └── Trees ├── Centroid Decomposition.cpp ├── dsu on tree (My Invented Style).cpp ├── dsu on tree [sack] .cpp └── dsu on tree.cpp /CSES Solutions/Introductory Problems/Apple Division.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | int n;cin>>n; 8 | ll s[n],sum=0; 9 | for(auto&it:s) cin>>it,sum+=it; 10 | ll ans=LLONG_MAX; 11 | for(int mask=1;mask<(1<>i)&1) sum2+=s[i]; 15 | ans=min(ans,abs(sum2-sum+sum2)); 16 | } 17 | cout< 2 | using namespace std; 3 | #define ll long long 4 | ll powmod(ll a,ll b,ll m) 5 | { 6 | ll ans=1; 7 | while(b>0) 8 | { 9 | if(b&1) ans=(ans*a)%m; 10 | a=(a*a)%m; 11 | b>>=1; 12 | } 13 | return ans; 14 | } 15 | signed main() 16 | { 17 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 18 | ll n;cin>>n; 19 | cout< 2 | using namespace std; 3 | #define ll long long 4 | char s[8][8];bool col[8],digL[16],digR[16]; 5 | ll solve(int row=0) 6 | { 7 | if(row==8) return 1; 8 | int ans=0; 9 | for(int i=0;i<8;i++) 10 | { 11 | if(s[row][i]=='.'&&!col[i]&&!digL[row+i]&&!digR[row-i+7]) 12 | { 13 | col[i]=digL[row+i]=digR[row-i+7]=1; 14 | ans+=solve(row+1); 15 | col[i]=digL[row+i]=digR[row-i+7]=0; 16 | } 17 | } 18 | return ans; 19 | } 20 | signed main() 21 | { 22 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 23 | for(int i=0;i<8;i++) for(int o=0;o<8;o++) cin>>s[i][o]; 24 | cout< 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | int t;cin>>t;while(t--) 8 | { 9 | ll a,b;cin>>a>>b; 10 | if(a>b) swap(a,b); 11 | if((a+b)%3==0) 12 | { 13 | if(a*2>=b) 14 | { 15 | cout<<"YES\n"; 16 | continue; 17 | } 18 | } 19 | cout<<"NO\n"; 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /CSES Solutions/Introductory Problems/Creating Strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | string s;cin>>s; 8 | sort(s.begin(),s.end()); 9 | setst; 10 | do{ 11 | st.insert(s); 12 | }while(next_permutation(s.begin(),s.end())); 13 | cout< 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | int t;cin>>t;while(t--) 8 | { 9 | ll n;cin>>n; 10 | ll x=9,num=0,c=0,sum=0; 11 | while(sum 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | int n;cin>>n; 8 | int sz=1<>s(sz,0); 10 | for(int idx=0;idx=0;o--) 31 | { 32 | cout< 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | int n;cin>>n; 8 | ll s[n]; 9 | for(auto&it:s) cin>>it; 10 | ll ans=0; 11 | for(int i=1;i 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | int n;cin>>n; 8 | setst; 9 | for(int i=1;i<=n;i++) st.insert(i); 10 | while(--n) 11 | { 12 | int x;cin>>x; 13 | st.erase(x); 14 | } 15 | cout<<*st.begin(); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /CSES Solutions/Introductory Problems/Number Spiral.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | int t;cin>>t;while(t--) 8 | { 9 | ll a,b;cin>>a>>b; 10 | if(a>=b) 11 | { 12 | if(a&1) 13 | { 14 | cout<<(a-1)*(a-1)+1+(b-1)<<'\n'; 15 | } 16 | else 17 | { 18 | cout< 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | string s;cin>>s; 8 | int f[26]={}; 9 | for(auto it:s) f[it-'A']++; 10 | int odd=0;char od; 11 | for(int i=0;i<26;i++) if(f[i]&1) odd++,od=char(i+'A'); 12 | if(odd>1) 13 | { 14 | cout<<"NO SOLUTION"; 15 | return 0; 16 | } 17 | for(int i=0;i<26;i++) 18 | { 19 | int x=f[i]/2; 20 | while(x--) cout<=0;i--) 25 | { 26 | int x=f[i]; 27 | while(x--) cout< 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | int n;cin>>n; 8 | if(n==1) 9 | { 10 | cout<<1; 11 | } 12 | else if(n==2||n==3) 13 | { 14 | cout<<"NO SOLUTION"; 15 | } 16 | else 17 | { 18 | for(int i=2;i<=n;i+=2) cout< 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | string s;cin>>s; 8 | int ans=0; 9 | for(int i=0;i 2 | using namespace std; 3 | #define ll long long 4 | void solve(int res,int a=1,int c=2,int b=3) 5 | { 6 | if(res==0) return; 7 | solve(res-1,a,b,c); 8 | cout<>n; 15 | cout<<(1< 2 | using namespace std; 3 | #define ll long long 4 | ll calc(ll n,ll d) 5 | { 6 | ll ret=0,x=1; 7 | for(int i=1;x<=n;i++) x*=d,ret+=n/x; 8 | return ret; 9 | } 10 | signed main() 11 | { 12 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 13 | ll n;cin>>n; 14 | cout< 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | ll n;cin>>n;for(ll i=1;i<=n;i++) 8 | { 9 | cout< 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | ll n;cin>>n; 8 | ll all=n*(n+1)/2; 9 | if(all&1) 10 | { 11 | cout<<"NO"; 12 | } 13 | else 14 | { 15 | cout<<"YES\n"; 16 | ll sum=0,c=0; 17 | bool vis[n+1]={}; 18 | for(int i=n;i>=1;i--) 19 | { 20 | if(sum+i<=all/2) sum+=i,vis[i]=1,c++; 21 | } 22 | cout< 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | ll n;cin>>n; 8 | cout< 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | int n,m;cin>>n>>m; 8 | int a[n],b[m]; 9 | for(int i=0;i>a[i]; 12 | } 13 | for(int i=0;i>b[i]; 16 | } 17 | int i=0,j=0; 18 | while(i 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | int n,m;cin>>n>>m; 8 | int a[n]; 9 | for(int i=0;i>a[i]; 12 | } 13 | int idx=0; 14 | for(int i=0;i>x; 17 | while(idx 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | int n,m;cin>>n>>m; 8 | int a[n]; 9 | for(int i=0;i>a[i]; 12 | } 13 | int b[m]; 14 | for(int i=0;i>b[i]; 17 | } 18 | int l=0,r=0;ll ans=0; 19 | for(int i=0;i 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | ll n,target;cin>>n>>target; 8 | ll s[n]; 9 | for(int i=0;i>s[i]; 12 | } 13 | ll l=0,r=0,sum=0,ans=0; 14 | while(l 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | ll n,target;cin>>n>>target; 8 | ll s[n]; 9 | for(int i=0;i>s[i]; 12 | } 13 | ll l=0,r=0,sum=0,ans=LLONG_MAX; 14 | while(l=target) 22 | { 23 | ans=min(ans,r-l); 24 | } 25 | sum-=s[l]; 26 | l++; 27 | } 28 | if(ans==LLONG_MAX) ans=-1; 29 | cout< 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | ll n,target;cin>>n>>target; 8 | ll s[n]; 9 | for(int i=0;i>s[i]; 12 | } 13 | ll l=0,r=0,sum=0,ans=0; 14 | while(l 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | int n,k;cin>>n>>k; 8 | int s[n]; 9 | for(auto&it:s)cin>>it; 10 | while(k--) 11 | { 12 | int x;cin>>x; 13 | int l=0,r=n-1,ans=0; 14 | while(l<=r) 15 | { 16 | int mid=l+r>>1; 17 | if(s[mid]==x) 18 | { 19 | ans=1; 20 | break; 21 | } 22 | else if(s[mid]>x)r=mid-1; 23 | else l=mid+1; 24 | } 25 | if(ans)cout<<"YES\n"; 26 | else cout<<"NO\n"; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Codeforces-EDU/Binary Search/Step 1 /B-Closest to the Left.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | int n,k;cin>>n>>k; 8 | int s[n+1]; 9 | for(int i=1;i<=n;i++)cin>>s[i]; 10 | while(k--) 11 | { 12 | int x;cin>>x; 13 | int l=1,r=n,ans=0; 14 | while(l<=r) 15 | { 16 | int mid=l+r>>1; 17 | if(s[mid]>x)r=mid-1; 18 | else ans=mid,l=mid+1; 19 | } 20 | cout< 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | int n,k;cin>>n>>k; 8 | int s[n+1]; 9 | for(int i=1;i<=n;i++)cin>>s[i]; 10 | while(k--) 11 | { 12 | int x;cin>>x; 13 | int l=1,r=n,ans=n+1; 14 | while(l<=r) 15 | { 16 | int mid=l+r>>1; 17 | if(s[mid]>=x)ans=mid,r=mid-1; 18 | else l=mid+1; 19 | } 20 | cout< 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | int n,k;cin>>n; 8 | int s[n]; 9 | for(auto &it:s)cin>>it; 10 | sort(s,s+n); 11 | cin>>k;while(k--) 12 | { 13 | int a,b;cin>>a>>b;; 14 | cout<<(upper_bound(s,s+n,b)-s)-(lower_bound(s,s+n,a)-s)<<' '; 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Codeforces-EDU/Binary Search/Step 2/B-Ropes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define ld long double 5 | ll n,k;ld s[10005]; 6 | bool can(ld mid) 7 | { 8 | ll ret=0; 9 | for(int i=0;i=k; 14 | } 15 | signed main() 16 | { 17 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 18 | cin>>n>>k; 19 | for(int i=0;i>s[i]; 20 | int iterations=300;ld l=0,r=1e7,ans=0; 21 | while(iterations--&&l<=r) 22 | { 23 | ld mid=(l+r)/2; 24 | if(can(mid)) 25 | { 26 | ans=mid; 27 | l=mid; 28 | } 29 | else 30 | { 31 | r=mid; 32 | } 33 | } 34 | cout< 2 | using namespace std; 3 | #define ll long long 4 | struct DSU 5 | { 6 | int parent[200005]; 7 | int group[200005]; 8 | DSU() 9 | { 10 | for(int i=0;i<200005;i++) 11 | { 12 | parent[i]=i; 13 | group[i]=1; 14 | } 15 | } 16 | int find(int i) 17 | { 18 | if(parent[i]==i) 19 | { 20 | return i; 21 | } 22 | return parent[i]= find(parent[i]); 23 | } 24 | bool samegroup(int x,int y) 25 | { 26 | return find(x)==find(y); 27 | } 28 | void merge(int x,int y) 29 | { 30 | int leader1=find(x); 31 | int leader2=find(y); 32 | if(leader1==leader2) 33 | { 34 | return; 35 | } 36 | if(group[leader1]>group[leader2]) 37 | { 38 | parent[leader2]=leader1; 39 | group[leader1]+=group[leader2]; 40 | } 41 | else 42 | { 43 | parent[leader1]=leader2; 44 | group[leader2]+=group[leader1]; 45 | } 46 | } 47 | int getsize(int x) 48 | { 49 | return group[find(x)]; 50 | } 51 | }; 52 | int main() 53 | { 54 | DSU d; 55 | int t,n;cin>>n>>t;while(t--) 56 | { 57 | string s;int a,b; 58 | cin>>s>>a>>b; 59 | if(s=="union") 60 | { 61 | d.merge(a,b); 62 | } 63 | else 64 | { 65 | if(d.samegroup(a,b)) 66 | { 67 | cout<<"YES\n"; 68 | } 69 | else 70 | { 71 | cout<<"NO\n"; 72 | } 73 | } 74 | } 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /Codeforces-EDU/DSU/Step 1/B-Disjoint Sets Union 2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | const int N=300005; 5 | int parent[N],group[N],l[N],h[N]; 6 | void init() 7 | { 8 | for(int i=0;igroup[leader2]) 33 | { 34 | swap(leader1,leader2); 35 | } 36 | parent[leader1]=leader2; 37 | group[leader2]+=group[leader1]; 38 | l[leader2]=min(l[leader1],l[leader2]); 39 | h[leader2]=max(h[leader1],h[leader2]); 40 | } 41 | int groupsize(int x) 42 | { 43 | return group[find(x)]; 44 | } 45 | int minimal(int x) 46 | { 47 | return l[find(x)]; 48 | } 49 | int maximal(int x) 50 | { 51 | return h[find(x)]; 52 | } 53 | int main() 54 | { 55 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 56 | init(); 57 | int t,n;cin>>n>>t;while(t--) 58 | { 59 | string s;cin>>s; 60 | if(s=="union") 61 | { 62 | int a,b;cin>>a>>b; 63 | merge(a,b); 64 | } 65 | else 66 | { 67 | int x;cin>>x; 68 | cout< 2 | using namespace std; 3 | #define ll long long 4 | ll parent[200005],score[200005],group[200005]; 5 | ll find(ll i) 6 | { 7 | if(parent[i]==i) 8 | { 9 | return i; 10 | } 11 | return find(parent[i]); 12 | } 13 | void merge(ll x,ll y) 14 | { 15 | int leader1=find(x); 16 | int leader2=find(y); 17 | if(leader1==leader2) 18 | { 19 | return; 20 | } 21 | if(group[leader1]>group[leader2]) 22 | { 23 | swap(leader1,leader2); 24 | } 25 | group[leader2]+=group[leader1]; 26 | score[leader1]-=score[leader2]; 27 | parent[leader1]=leader2; 28 | } 29 | ll ans(ll x) 30 | { 31 | if(parent[x]==x) 32 | { 33 | return score[x]; 34 | } 35 | return score[x]+ans(parent[x]); 36 | } 37 | void init() 38 | { 39 | for(int i=0;i<200005;i++) 40 | { 41 | parent[i]=i; 42 | group[i]=1; 43 | } 44 | } 45 | int main() 46 | { 47 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 48 | init(); 49 | int n,t;cin>>n>>t;while(t--) 50 | { 51 | string s;cin>>s; 52 | if(s=="add") 53 | { 54 | ll a,b;cin>>a>>b; 55 | ll w=find(a); 56 | score[w]+=b; 57 | } 58 | else if(s=="join") 59 | { 60 | ll a,b;cin>>a>>b; 61 | merge(a,b); 62 | } 63 | else 64 | { 65 | ll x;cin>>x; 66 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define int long long 5 | struct dsu{ 6 | int sz;vectorparent,group; 7 | dsu(int n) 8 | { 9 | sz=n; 10 | parent=vector(n+1); 11 | group=vector(n+1,1); 12 | iota(parent.begin(),parent.end(),0); 13 | } 14 | int find(int node) 15 | { 16 | if(parent[node]==node) return node; 17 | return find(parent[node]); 18 | } 19 | void merge(int u,int v) 20 | { 21 | int leader1=find(u); 22 | int leader2=find(v); 23 | if(leader1==leader2) return; 24 | if(group[leader1]>group[leader2]) swap(leader1,leader2); 25 | parent[leader1]=leader2; 26 | group[leader2]+=group[leader1]; 27 | } 28 | bool sameGroup(int u,int v) 29 | { 30 | return find(u)==find(v); 31 | } 32 | }; 33 | signed main() 34 | { 35 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 36 | int n,m,q;cin>>n>>m>>q; 37 | while(m--) 38 | { 39 | int a;cin>>a>>a; 40 | } 41 | arrayqu[q]; 42 | for(int i=0;i>op>>u>>v; 46 | if(op=="ask") qu[i]={0,u,v}; 47 | else qu[i]={1,u,v}; 48 | } 49 | dsu d(n); 50 | dequeans; 51 | for(int i=q-1;i>=0;i--) 52 | { 53 | auto [op,u,v]=qu[i]; 54 | if(op==0) 55 | { 56 | if(d.sameGroup(u,v)) ans.push_front("YES\n"); 57 | else ans.push_front("NO\n"); 58 | } 59 | else 60 | { 61 | d.merge(u,v); 62 | } 63 | } 64 | for(auto it:ans) 65 | { 66 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define int long long 5 | struct dsu{ 6 | int sz;vectorparent,group,mx; 7 | dsu(int n) 8 | { 9 | sz=n; 10 | parent=vector(n+1); 11 | group=vector(n+1,1); 12 | iota(parent.begin(),parent.end(),0); 13 | mx=parent; 14 | } 15 | int find(int node) 16 | { 17 | if(parent[node]==node) return node; 18 | return find(parent[node]); 19 | } 20 | void merge(int u,int v) 21 | { 22 | int leader1=find(u); 23 | int leader2=find(v); 24 | if(leader1==leader2) return; 25 | if(group[leader1]>group[leader2]) swap(leader1,leader2); 26 | parent[leader1]=leader2; 27 | group[leader2]+=group[leader1]; 28 | mx[leader2]=max(mx[leader2],mx[leader1]); 29 | } 30 | bool sameGroup(int u,int v) 31 | { 32 | return find(u)==find(v); 33 | } 34 | int get(int node) 35 | { 36 | return mx[find(node)]; 37 | } 38 | }; 39 | signed main() 40 | { 41 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 42 | int n,q;cin>>n>>q; 43 | dsu d(n+2); 44 | while(q--) 45 | { 46 | char op;cin>>op; 47 | if(op=='?') 48 | { 49 | int idx;cin>>idx; 50 | int ans=d.get(idx); 51 | if(ans>n) 52 | { 53 | cout<<-1<<'\n'; 54 | } 55 | else 56 | { 57 | cout<>idx; 63 | d.merge(idx,idx+1); 64 | } 65 | } 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Codeforces-EDU/DSU/Step2/B - Parking.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define int long long 5 | struct dsu{ 6 | int sz;vectorparent; 7 | dsu(int n) 8 | { 9 | sz=n; 10 | parent=vector(n+1); 11 | iota(parent.begin(),parent.end(),0); 12 | } 13 | int find(int node) 14 | { 15 | if(parent[node]==node) return node; 16 | return parent[node]=find(parent[node]); 17 | } 18 | void merge(int u,int v) 19 | { 20 | int leader1=find(u); 21 | int leader2=find(v); 22 | if(leader1==leader2) return; 23 | if(leader1>leader2) swap(leader1,leader2); 24 | parent[leader1]=leader2; 25 | } 26 | bool sameGroup(int u,int v) 27 | { 28 | return find(u)==find(v); 29 | } 30 | }; 31 | signed main() 32 | { 33 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 34 | int n;cin>>n; 35 | dsu d(n+2); 36 | for(int i=1;i<=n;i++) 37 | { 38 | int x;cin>>x; 39 | int par=d.find(x); 40 | if(par==n+1) 41 | { 42 | par=d.find(1); 43 | d.merge(1,par+1); 44 | } 45 | else 46 | { 47 | d.merge(par,par+1); 48 | } 49 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define int long long 5 | struct dsu{ 6 | int sz;vectorp1,p2,group,group2,mx; 7 | dsu(int n) 8 | { 9 | sz=n; 10 | p1=vector(n+1); 11 | group=vector(n+1,1); 12 | group2=group; 13 | iota(p1.begin(),p1.end(),0); 14 | mx=p2=p1; 15 | } 16 | int find1(int node) 17 | { 18 | if(p1[node]==node) return node; 19 | return find1(p1[node]); 20 | } 21 | int find2(int node) 22 | { 23 | if(p2[node]==node) return node; 24 | return find2(p2[node]); 25 | } 26 | void merge1(int u,int v) 27 | { 28 | int leader1=find1(u); 29 | int leader2=find1(v); 30 | if(leader1==leader2) return; 31 | if(group[leader1]>group[leader2]) swap(leader1,leader2); 32 | p1[leader1]=leader2; 33 | group[leader2]+=group[leader1]; 34 | } 35 | void merge2(int u,int v) 36 | { 37 | int leader1=find2(u); 38 | int leader2=find2(v); 39 | if(leader1==leader2) return; 40 | if(group2[leader1]>group2[leader2]) swap(leader1,leader2); 41 | p2[leader1]=leader2; 42 | group2[leader2]+=group2[leader1]; 43 | mx[leader2]=max(mx[leader2],mx[leader1]); 44 | } 45 | bool sameGroup(int u,int v) 46 | { 47 | return find1(u)==find1(v); 48 | } 49 | int get(int node) 50 | { 51 | return mx[find2(node)]; 52 | } 53 | }; 54 | signed main() 55 | { 56 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 57 | int n,q;cin>>n>>q; 58 | dsu d(n+2); 59 | while(q--) 60 | { 61 | int op,u,v;cin>>op>>u>>v; 62 | if(op==1) 63 | { 64 | d.merge1(u,v); 65 | } 66 | else if(op==2) 67 | { 68 | for(int i=d.get(u);i 2 | using namespace std; 3 | #define ll long long 4 | struct dsu 5 | { 6 | vectorparent,group;stack>st;int comp; 7 | dsu(int n) 8 | { 9 | comp=n-1; 10 | parent=vector(n); 11 | iota(parent.begin(),parent.end(),0); 12 | group=vector(n,1); 13 | } 14 | int find(int i) 15 | { 16 | if(parent[i]==i) 17 | { 18 | return i; 19 | } 20 | return find(parent[i]); 21 | } 22 | bool samegroup(int x,int y) 23 | { 24 | return find(x)==find(y); 25 | } 26 | void merge(int x,int y) 27 | { 28 | int leader1=find(x); 29 | int leader2=find(y); 30 | arrayins={-1,-1,-1,-1,-1}; 31 | st.push(ins); 32 | if(leader1==leader2) return; 33 | st.pop(); 34 | if(group[leader1]>group[leader2]) swap(leader1,leader2); 35 | ins={leader1,parent[leader1],leader2,group[leader2],comp}; 36 | st.push(ins); 37 | comp--; 38 | group[leader2]+=group[leader1]; 39 | parent[leader1]=leader2; 40 | } 41 | void rollback() 42 | { 43 | arraylst=st.top(); 44 | st.pop(); 45 | if(lst[0]==-1) return; 46 | comp=lst[4]; 47 | parent[lst[0]]=lst[1]; 48 | group[lst[2]]=lst[3]; 49 | } 50 | void addCheckPoint() 51 | { 52 | st.push({-5,-5,-5,-5,-5}); 53 | } 54 | void undo() 55 | { 56 | while(st.size()&&st.top()[0]!=-5) 57 | { 58 | rollback(); 59 | } 60 | st.pop(); 61 | } 62 | int answer() 63 | { 64 | return comp; 65 | } 66 | int getsize(int x) 67 | { 68 | return group[find(x)]; 69 | } 70 | }; 71 | signed main() 72 | { 73 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 74 | int n,q;cin>>n>>q; 75 | dsu d(n+1); 76 | while(q--) 77 | { 78 | string op;cin>>op; 79 | if(op=="persist") 80 | { 81 | d.addCheckPoint(); 82 | } 83 | else if(op=="rollback") 84 | { 85 | d.undo(); 86 | cout<>a>>b; 91 | d.merge(a,b); 92 | cout< 2 | using namespace std; 3 | #define ll long long 4 | const int N=6e5+5; 5 | int n,q,sz,ans[N];vector>ed;vector>seg,add(N),cut(N); 6 | struct dsu 7 | { 8 | vectorparent,group;stack>st;int comp; 9 | dsu(){} 10 | dsu(int n) 11 | { 12 | comp=n-1; 13 | parent=vector(n); 14 | iota(parent.begin(),parent.end(),0); 15 | group=vector(n,1); 16 | } 17 | int find(int i) 18 | { 19 | if(parent[i]==i) 20 | { 21 | return i; 22 | } 23 | return find(parent[i]); 24 | } 25 | bool samegroup(int x,int y) 26 | { 27 | return find(x)==find(y); 28 | } 29 | void merge(int x,int y) 30 | { 31 | int leader1=find(x); 32 | int leader2=find(y); 33 | arrayins={-1,-1,-1,-1,-1}; 34 | st.push(ins); 35 | if(leader1==leader2) return; 36 | st.pop(); 37 | if(group[leader1]>group[leader2]) swap(leader1,leader2); 38 | ins={leader1,parent[leader1],leader2,group[leader2],comp}; 39 | st.push(ins); 40 | comp--; 41 | group[leader2]+=group[leader1]; 42 | parent[leader1]=leader2; 43 | } 44 | void rollback() 45 | { 46 | arraylst=st.top(); 47 | st.pop(); 48 | if(lst[0]==-1) return; 49 | comp=lst[4]; 50 | parent[lst[0]]=lst[1]; 51 | group[lst[2]]=lst[3]; 52 | } 53 | int answer() 54 | { 55 | return comp; 56 | } 57 | int getsize(int x) 58 | { 59 | return group[find(x)]; 60 | } 61 | }d; 62 | void build(int l,int r,int node) 63 | { 64 | for(auto it:seg[node]) 65 | { 66 | d.merge(ed[it][0],ed[it][1]); 67 | } 68 | if(l==r) 69 | { 70 | ans[l]=d.answer(); 71 | for(auto it:seg[node]) 72 | { 73 | d.rollback(); 74 | } 75 | return; 76 | } 77 | int mid=l+r>>1; 78 | build(l,mid,2*node+1); 79 | build(mid+1,r,2*node+2); 80 | for(auto it:seg[node]) 81 | { 82 | d.rollback(); 83 | } 84 | } 85 | void update(int l,int r,int node,int lx,int rx,int idx) 86 | { 87 | if(rrx) return; 88 | if(l>=lx&&r<=rx) 89 | { 90 | seg[node].push_back(idx); 91 | return; 92 | } 93 | int mid=l+r>>1; 94 | update(l,mid,2*node+1,lx,rx,idx); 95 | update(mid+1,r,2*node+2,lx,rx,idx); 96 | } 97 | void init() 98 | { 99 | sz=1; 100 | while(sz<=q+5) sz<<=1; 101 | seg=vector>(sz<<1); 102 | } 103 | signed main() 104 | { 105 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 106 | cin>>n>>q; 107 | map,int>mp; 108 | ed.push_back({0,0}); 109 | vectorqueries; 110 | for(int i=1;i<=q;i++) 111 | { 112 | char op;cin>>op; 113 | if(op=='?') 114 | { 115 | queries.push_back(i); 116 | } 117 | else 118 | { 119 | int a,b;cin>>a>>b; 120 | if(a>b) swap(a,b); 121 | if(mp.find({a,b})==mp.end()) 122 | { 123 | ed.push_back({a,b}); 124 | mp[{a,b}]=ed.size()-1; 125 | } 126 | int idx=mp[{a,b}]; 127 | if(op=='+') 128 | { 129 | add[idx].push_back(i); 130 | } 131 | else 132 | { 133 | cut[idx].push_back(i); 134 | } 135 | } 136 | } 137 | for(int i=1;i 2 | using namespace std; 3 | #define ll long long 4 | struct SegmentTree{ 5 | private: 6 | #define L 2*node+1 7 | #define R 2*node+2 8 | #define mid (l+r>>1) 9 | int sz;vectorseg; 10 | void build(int l,int r,int node,vector&arr) 11 | { 12 | if(l==r) 13 | { 14 | if(lrq) 44 | { 45 | return 0; 46 | } 47 | if(l>=lq&&r<=rq) 48 | { 49 | return seg[node]; 50 | } 51 | ll lft=query(l,mid,L,lq,rq); 52 | ll rgt=query(mid+1,r,R,lq,rq); 53 | return lft+rgt; 54 | } 55 | public: 56 | SegmentTree(vector&arr) 57 | { 58 | sz=1;int n=arr.size(); 59 | while(sz(sz*2); 61 | build(0,sz-1,0,arr); 62 | } 63 | void update(int idx,ll val) 64 | { 65 | update(0,sz-1,0,idx,val); 66 | } 67 | ll query(int l,int r) 68 | { 69 | return query(0,sz-1,0,l,r); 70 | } 71 | #undef L 72 | #undef R 73 | }; 74 | signed main() 75 | { 76 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 77 | int n,q;cin>>n>>q; 78 | vectorv(n); 79 | for(int i=0;i>v[i]; 82 | } 83 | SegmentTree tree(v); 84 | while(q--) 85 | { 86 | int op;cin>>op; 87 | if(op==1) 88 | { 89 | ll idx,val;cin>>idx>>val; 90 | tree.update(idx,val); 91 | } 92 | else 93 | { 94 | int l,r;cin>>l>>r; 95 | cout< 2 | using namespace std; 3 | #define ll long long 4 | struct SegmentTree{ 5 | private: 6 | #define L 2*node+1 7 | #define R 2*node+2 8 | #define mid (l+r>>1) 9 | int sz;vectorseg;ll skip=LLONG_MAX; 10 | ll merge(ll x,ll y) 11 | { 12 | return min(x,y); 13 | } 14 | void build(int l,int r,int node,vector&arr) 15 | { 16 | if(l==r) 17 | { 18 | if(lrq) 48 | { 49 | return skip; 50 | } 51 | if(l>=lq&&r<=rq) 52 | { 53 | return seg[node]; 54 | } 55 | ll lft=query(l,mid,L,lq,rq); 56 | ll rgt=query(mid+1,r,R,lq,rq); 57 | return merge(lft,rgt); 58 | } 59 | public: 60 | SegmentTree(vector&arr) 61 | { 62 | sz=1;int n=arr.size(); 63 | while(sz(sz*2,skip); 65 | build(0,sz-1,0,arr); 66 | } 67 | void update(int idx,ll val) 68 | { 69 | update(0,sz-1,0,idx,val); 70 | } 71 | ll query(int l,int r) 72 | { 73 | return query(0,sz-1,0,l,r); 74 | } 75 | #undef L 76 | #undef R 77 | }; 78 | signed main() 79 | { 80 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 81 | int n,q;cin>>n>>q; 82 | vectorv(n); 83 | for(int i=0;i>v[i]; 86 | } 87 | SegmentTree tree(v); 88 | while(q--) 89 | { 90 | int op;cin>>op; 91 | if(op==1) 92 | { 93 | ll idx,val;cin>>idx>>val; 94 | tree.update(idx,val); 95 | } 96 | else 97 | { 98 | int l,r;cin>>l>>r; 99 | cout< 2 | using namespace std; 3 | #define ll long long 4 | struct SegmentTree{ 5 | private: 6 | struct node{ 7 | int val,frq; 8 | node():val(1e9),frq(0){} 9 | }; 10 | node ret; 11 | int sz;vectorseg; 12 | node merge(node a,node b) 13 | { 14 | node ret; 15 | ret.frq=0; 16 | ret.val=min(a.val,b.val); 17 | if(ret.val==a.val)ret.frq+=a.frq; 18 | if(ret.val==b.val)ret.frq+=b.frq; 19 | return ret; 20 | } 21 | void update(int l,int r,int node,int idx,int val) 22 | { 23 | if(l==r) 24 | { 25 | seg[node].val=val; 26 | seg[node].frq=1; 27 | return; 28 | } 29 | int mid=(l+r)/2; 30 | if(idx<=mid) 31 | { 32 | update(l,mid,2*node+1,idx,val); 33 | } 34 | else 35 | { 36 | update(mid+1,r,2*node+2,idx,val); 37 | } 38 | seg[node]=merge(seg[2*node+1],seg[2*node+2]); 39 | } 40 | node query(int l,int r,int node,int lx,int rx) 41 | { 42 | if(rrx) 43 | { 44 | return ret; 45 | } 46 | if(r<=rx&&l>=lx) 47 | { 48 | return seg[node]; 49 | } 50 | int mid=(l+r)/2; 51 | return merge(query(l,mid,2*node+1,lx,rx),query(mid+1,r,2*node+2,lx,rx)); 52 | } 53 | public: 54 | SegmentTree (int n) 55 | { 56 | sz=1; 57 | while(sz(sz*2); 59 | } 60 | void update(int idx,int val) 61 | { 62 | update(0,sz-1,0,idx,val); 63 | } 64 | pairquery(int l,int r) 65 | { 66 | node ret=query(0,sz-1,0,l,r); 67 | return {ret.val,ret.frq}; 68 | } 69 | }; 70 | signed main() 71 | { 72 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 73 | int n,q;cin>>n>>q; 74 | SegmentTree tree(n); 75 | for(int i=0;i>x; 78 | tree.update(i,x); 79 | } 80 | while(q--) 81 | { 82 | int op;cin>>op; 83 | if(op==1) 84 | { 85 | int idx,v;cin>>idx>>v; 86 | tree.update(idx,v); 87 | } 88 | else 89 | { 90 | int l,r;cin>>l>>r; 91 | pairans=tree.query(l,r-1); 92 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define int long long 5 | struct SegmentTree{ 6 | private: 7 | struct node{ 8 | int pre,suf,mx,sum; 9 | node():pre(0),suf(0),mx(0),sum(0){} 10 | node(int v):sum(v),mx(max(v, 0LL)),suf(max(v, 0LL)),pre(max(v, 0LL)){} 11 | }; 12 | int sz;vectorseg; 13 | node merge(node a,node b) 14 | { 15 | node ret; 16 | ret.sum=a.sum+b.sum; 17 | ret.pre=max(a.pre,a.sum+b.pre); 18 | ret.suf=max(b.suf,b.sum+a.suf); 19 | ret.mx=max({a.mx,b.mx,a.suf+b.pre}); 20 | return ret; 21 | } 22 | void update(int l,int r,int node,int idx,int val) 23 | { 24 | if(l==r) 25 | { 26 | seg[node]={val}; 27 | return; 28 | } 29 | int mid=(l+r)/2; 30 | if(idx<=mid) 31 | { 32 | update(l,mid,2*node+1,idx,val); 33 | } 34 | else 35 | { 36 | update(mid+1,r,2*node+2,idx,val); 37 | } 38 | seg[node]=merge(seg[2*node+1],seg[2*node+2]); 39 | } 40 | 41 | public: 42 | SegmentTree(int n){ 43 | sz=1; 44 | while(sz(sz*2); 46 | } 47 | void update(int idx,int val) 48 | { 49 | update(0,sz-1,0,idx,val); 50 | } 51 | int query() 52 | { 53 | return seg[0].mx; 54 | } 55 | }; 56 | signed main() 57 | { 58 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 59 | int n,q;cin>>n>>q; 60 | SegmentTree tree(n); 61 | for(int i=0;i>x; 64 | tree.update(i,x); 65 | } 66 | cout<>idx>>val; 70 | tree.update(idx,val); 71 | cout< 2 | using namespace std; 3 | #define ll long long 4 | struct SegmentTree{ 5 | private: 6 | int sz;vectorseg; 7 | int merge(int a,int b) 8 | { 9 | return a+b; 10 | } 11 | void update(int l,int r,int node,int idx) 12 | { 13 | if(l==r) 14 | { 15 | seg[node]^=1; 16 | return; 17 | } 18 | int mid=(l+r)/2; 19 | if(idx<=mid) 20 | { 21 | update(l,mid,2*node+1,idx); 22 | } 23 | else 24 | { 25 | update(mid+1,r,2*node+2,idx); 26 | } 27 | seg[node]=merge(seg[2*node+1],seg[2*node+2]); 28 | } 29 | int query(int l,int r,int node,int k) 30 | { 31 | if(l==r) 32 | { 33 | return l; 34 | } 35 | int lft=seg[2*node+1]; 36 | int mid=(l+r)/2; 37 | if(k<=lft) 38 | { 39 | return query(l,mid,2*node+1,k); 40 | } 41 | return query(mid+1,r,2*node+2,k-lft); 42 | } 43 | public: 44 | SegmentTree(int n) 45 | { 46 | sz=1; 47 | while(sz(sz*2); 49 | } 50 | void update(int idx) 51 | { 52 | update(0,sz-1,0,idx); 53 | } 54 | int query(int k) 55 | { 56 | return query(0,sz-1,0,k); 57 | } 58 | }; 59 | signed main() 60 | { 61 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 62 | int n,q;cin>>n>>q; 63 | SegmentTree tree(n); 64 | for(int i=0;i>x; 67 | if(x)tree.update(i); 68 | } 69 | while(q--) 70 | { 71 | int op;cin>>op; 72 | if(op==1) 73 | { 74 | int idx;cin>>idx; 75 | tree.update(idx); 76 | } 77 | else 78 | { 79 | int k;cin>>k; 80 | cout< 2 | using namespace std; 3 | #define ll long long 4 | struct SegmentTree{ 5 | private: 6 | int sz;vectorseg; 7 | int merge(int a,int b) 8 | { 9 | return max(a,b); 10 | } 11 | void update(int l,int r,int node,int idx,int val) 12 | { 13 | if(l==r) 14 | { 15 | seg[node]=val; 16 | return; 17 | } 18 | int mid=(l+r)/2; 19 | if(idx<=mid) 20 | { 21 | update(l,mid,2*node+1,idx,val); 22 | } 23 | else 24 | { 25 | update(mid+1,r,2*node+2,idx,val); 26 | } 27 | seg[node]=merge(seg[2*node+1],seg[2*node+2]); 28 | } 29 | int query(int l,int r,int node,int k) 30 | { 31 | if(l==r) 32 | { 33 | return l; 34 | } 35 | int lft=seg[2*node+1]; 36 | int mid=(l+r)/2; 37 | if(k<=lft) 38 | { 39 | return query(l,mid,2*node+1,k); 40 | } 41 | return query(mid+1,r,2*node+2,k); 42 | } 43 | public: 44 | SegmentTree(int n) 45 | { 46 | sz=1; 47 | while(sz(sz*2); 49 | } 50 | void update(int idx,int val) 51 | { 52 | update(0,sz-1,0,idx,val); 53 | } 54 | int query(int k) 55 | { 56 | if(seg[0]>n>>q; 64 | SegmentTree tree(n); 65 | for(int i=0;i>x; 68 | tree.update(i,x); 69 | } 70 | while(q--) 71 | { 72 | int op;cin>>op; 73 | if(op==1) 74 | { 75 | int idx,val;cin>>idx>>val; 76 | tree.update(idx,val); 77 | } 78 | else 79 | { 80 | int k;cin>>k; 81 | cout< 2 | using namespace std; 3 | #define ll long long 4 | struct SegmentTree{ 5 | private: 6 | int sz;vectorseg; 7 | int merge(int a,int b) 8 | { 9 | return max(a,b); 10 | } 11 | void update(int l,int r,int node,int idx,int val) 12 | { 13 | if(l==r) 14 | { 15 | seg[node]=val; 16 | return; 17 | } 18 | int mid=(l+r)/2; 19 | if(idx<=mid) 20 | { 21 | update(l,mid,2*node+1,idx,val); 22 | } 23 | else 24 | { 25 | update(mid+1,r,2*node+2,idx,val); 26 | } 27 | seg[node]=merge(seg[2*node+1],seg[2*node+2]); 28 | } 29 | int query(int l,int r,int node,int k,int idx) 30 | { 31 | if(seg[node](sz*2); 50 | } 51 | void update(int idx,int val) 52 | { 53 | update(0,sz-1,0,idx,val); 54 | } 55 | int query(int idx,int k) 56 | { 57 | return query(0,sz-1,0,k,idx); 58 | } 59 | }; 60 | signed main() 61 | { 62 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 63 | int n,q;cin>>n>>q; 64 | SegmentTree tree(n); 65 | for(int i=0;i>x; 68 | tree.update(i,x); 69 | } 70 | while(q--) 71 | { 72 | int op;cin>>op; 73 | if(op==1) 74 | { 75 | int idx,val;cin>>idx>>val; 76 | tree.update(idx,val); 77 | } 78 | else 79 | { 80 | int k,idx;cin>>k>>idx; 81 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define int long long 5 | const int N=2e5; 6 | ll s[2*N]; 7 | struct segmentTree 8 | { 9 | int sz;vectorseg;int ret=0; 10 | segmentTree(int n) 11 | { 12 | sz=1; 13 | while(szrx||r=lx&&r<=rx) 57 | { 58 | return seg[node]; 59 | } 60 | int mid=(l+r)/2; 61 | int a=query(l,mid,2*node,lx,rx); 62 | int b=query(mid+1,r,2*node+1,lx,rx); 63 | return a+b; 64 | } 65 | }; 66 | signed main() 67 | { 68 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 69 | int n;cin>>n; 70 | segmentTree tree(n); 71 | ll x[n+1]; 72 | for(int i=1;i<=n;i++) 73 | { 74 | cin>>x[i]; 75 | } 76 | tree.build(1,n,1); 77 | for(int i=1;i<=n;i++) 78 | { 79 | cout< 2 | using namespace std; 3 | #define ll long long 4 | int n; 5 | struct SegmentTree{ 6 | private: 7 | int sz=1;vectorseg; 8 | int merge(int a,int b) 9 | { 10 | return a+b; 11 | } 12 | void update(int l,int r,int node,int idx,int val) 13 | { 14 | if(l==r) 15 | { 16 | seg[node]=val; 17 | return; 18 | } 19 | int mid=(l+r)/2; 20 | if(idx<=mid) 21 | { 22 | update(l,mid,2*node+1,idx,val); 23 | } 24 | else 25 | { 26 | update(mid+1,r,2*node+2,idx,val); 27 | } 28 | seg[node]=merge(seg[2*node+1],seg[2*node+2]); 29 | } 30 | int query(int l,int r,int node,int k) 31 | { 32 | if(l==r) 33 | { 34 | return l; 35 | } 36 | int rgt=seg[2*node+2]; 37 | int mid=l+r>>1; 38 | if(rgt>k) 39 | { 40 | return query(mid+1,r,2*node+2,k); 41 | } 42 | return query(l,mid,2*node+1,k-rgt); 43 | } 44 | public: 45 | SegmentTree(){ 46 | while(sz(sz*2,0); 48 | } 49 | void update(int idx,int val) 50 | { 51 | update(0,sz-1,0,idx,val); 52 | } 53 | int query(int k) 54 | { 55 | return query(0,sz-1,0,k); 56 | } 57 | }; 58 | signed main() 59 | { 60 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 61 | cin>>n; 62 | int s[n+1]; 63 | SegmentTree tree; 64 | for(int i=1;i<=n;i++) 65 | { 66 | cin>>s[i]; 67 | tree.update(i,1); 68 | } 69 | int ans[n+1]={}; 70 | for(int i=n;i>=1;i--) 71 | { 72 | int x=tree.query(s[i]); 73 | ans[i]=x; 74 | tree.update(x,0); 75 | } 76 | for(int i=1;i<=n;i++) 77 | { 78 | cout< 2 | using namespace std; 3 | #define ll long long 4 | int n; 5 | struct SegmentTree{ 6 | private: 7 | int sz=1,skip=0;vectorseg; 8 | int merge(int a,int b) 9 | { 10 | return a+b; 11 | } 12 | void update(int l,int r,int node,int idx,int val) 13 | { 14 | if(l==r) 15 | { 16 | seg[node]=val; 17 | return; 18 | } 19 | int mid=(l+r)/2; 20 | if(idx<=mid) 21 | { 22 | update(l,mid,2*node+1,idx,val); 23 | } 24 | else 25 | { 26 | update(mid+1,r,2*node+2,idx,val); 27 | } 28 | seg[node]=merge(seg[2*node+1],seg[2*node+2]); 29 | } 30 | int query(int l,int r,int node,int lx,int rx) 31 | { 32 | if(l>rx||r=lx&&r<=rx) 37 | { 38 | return seg[node]; 39 | } 40 | int mid=l+r>>1; 41 | int lft=query(l,mid,2*node+1,lx,rx); 42 | int rgt=query(mid+1,r,2*node+2,lx,rx); 43 | return merge(lft,rgt); 44 | } 45 | public: 46 | SegmentTree(){ 47 | while(sz(sz*2,0); 49 | } 50 | void update(int idx,int val) 51 | { 52 | update(0,sz-1,0,idx,val); 53 | } 54 | int query(int l,int r) 55 | { 56 | return query(0,sz-1,0,l,r); 57 | } 58 | }; 59 | signed main() 60 | { 61 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 62 | cin>>n;n*=2; 63 | int s[n+1]; 64 | int vis[200005]={}; 65 | SegmentTree tree; 66 | for(int i=1;i<=n;i++) 67 | { 68 | cin>>s[i]; 69 | if(!vis[s[i]]) 70 | { 71 | tree.update(i,1); 72 | } 73 | else 74 | { 75 | tree.update(i,-1); 76 | } 77 | vis[s[i]]=i; 78 | } 79 | int ans[n]; 80 | for(int i=1;i<=n;i++) 81 | { 82 | if(vis[s[i]]==0)continue; 83 | int q=tree.query(i,vis[s[i]]); 84 | int num=vis[s[i]]-i-1; 85 | ans[s[i]]=(num-q)/2; 86 | tree.update(vis[s[i]],1); 87 | vis[s[i]]=0; 88 | } 89 | for(int i=1;i<=n/2;i++) 90 | { 91 | cout< 2 | using namespace std; 3 | #define ll long long 4 | int n; 5 | struct SegmentTree{ 6 | private: 7 | int sz=1,skip=0;vectorseg; 8 | int merge(int a,int b) 9 | { 10 | return a+b; 11 | } 12 | void update(int l,int r,int node,int idx,int val) 13 | { 14 | if(l==r) 15 | { 16 | seg[node]=val; 17 | return; 18 | } 19 | int mid=(l+r)/2; 20 | if(idx<=mid) 21 | { 22 | update(l,mid,2*node+1,idx,val); 23 | } 24 | else 25 | { 26 | update(mid+1,r,2*node+2,idx,val); 27 | } 28 | seg[node]=merge(seg[2*node+1],seg[2*node+2]); 29 | } 30 | int query(int l,int r,int node,int lx,int rx) 31 | { 32 | if(l>rx||r=lx&&r<=rx) 37 | { 38 | return seg[node]; 39 | } 40 | int mid=l+r>>1; 41 | int lft=query(l,mid,2*node+1,lx,rx); 42 | int rgt=query(mid+1,r,2*node+2,lx,rx); 43 | return merge(lft,rgt); 44 | } 45 | public: 46 | SegmentTree(){ 47 | while(sz(sz*2,0); 49 | } 50 | void update(int idx,int val) 51 | { 52 | update(0,sz-1,0,idx,val); 53 | } 54 | int query(int l,int r) 55 | { 56 | return query(0,sz-1,0,l,r); 57 | } 58 | }; 59 | signed main() 60 | { 61 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 62 | cin>>n;n*=2; 63 | int s[n+1]; 64 | int vis[200005]={}; 65 | SegmentTree tree; 66 | for(int i=1;i<=n;i++) 67 | { 68 | cin>>s[i]; 69 | if(!vis[s[i]]) 70 | { 71 | tree.update(i,1); 72 | } 73 | else 74 | { 75 | tree.update(i,-1); 76 | } 77 | vis[s[i]]=i; 78 | } 79 | int ans[n]; 80 | for(int i=1;i<=n;i++) 81 | { 82 | if(vis[s[i]]==0)continue; 83 | int q=tree.query(i,vis[s[i]]); 84 | ans[s[i]]=q; 85 | tree.update(vis[s[i]],1); 86 | vis[s[i]]=0; 87 | } 88 | for(int i=1;i<=n/2;i++) 89 | { 90 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define int long long 5 | int n,q; 6 | struct SegmentTree{ 7 | private: 8 | int sz=1,skip=0;vectorseg; 9 | int merge(int a,int b) 10 | { 11 | return a+b; 12 | } 13 | void update(int l,int r,int node,int idx,int val) 14 | { 15 | if(l==r) 16 | { 17 | seg[node]+=val; 18 | return; 19 | } 20 | int mid=(l+r)/2; 21 | if(idx<=mid) 22 | { 23 | update(l,mid,2*node+1,idx,val); 24 | } 25 | else 26 | { 27 | update(mid+1,r,2*node+2,idx,val); 28 | } 29 | seg[node]=merge(seg[2*node+1],seg[2*node+2]); 30 | } 31 | int query(int l,int r,int node,int lx,int rx) 32 | { 33 | if(l>rx||r=lx&&r<=rx) 38 | { 39 | return seg[node]; 40 | } 41 | int mid=l+r>>1; 42 | int lft=query(l,mid,2*node+1,lx,rx); 43 | int rgt=query(mid+1,r,2*node+2,lx,rx); 44 | return merge(lft,rgt); 45 | } 46 | public: 47 | SegmentTree(){ 48 | while(sz(sz*2,0); 50 | } 51 | void update(int idx,int val) 52 | { 53 | update(0,sz-1,0,idx,val); 54 | } 55 | int query(int l,int r) 56 | { 57 | return query(0,sz-1,0,l,r); 58 | } 59 | }; 60 | signed main() 61 | { 62 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 63 | cin>>n>>q; 64 | SegmentTree tree; 65 | while(q--) 66 | { 67 | int op;cin>>op; 68 | if(op==1) 69 | { 70 | int l,r,val;cin>>l>>r>>val; 71 | tree.update(l+1,val); 72 | tree.update(r+1,-val); 73 | } 74 | else 75 | { 76 | int idx;cin>>idx; 77 | cout< 2 | using namespace std; 3 | #define ll long long 4 | struct SegmentTree{ 5 | private: 6 | int sz=1,skip=0;vectorseg; 7 | ll merge(ll a,ll b) 8 | { 9 | return a+b; 10 | } 11 | void update(int l,int r,int node,int idx,ll val) 12 | { 13 | if(l==r) 14 | { 15 | seg[node]=val; 16 | return; 17 | } 18 | int mid=(l+r)/2; 19 | if(idx<=mid) 20 | { 21 | update(l,mid,2*node+1,idx,val); 22 | } 23 | else 24 | { 25 | update(mid+1,r,2*node+2,idx,val); 26 | } 27 | seg[node]=merge(seg[2*node+1],seg[2*node+2]); 28 | } 29 | ll query(int l,int r,int node,int lx,int rx) 30 | { 31 | if(rrx) 32 | { 33 | return skip; 34 | } 35 | if(l>=lx&&r<=rx) 36 | { 37 | return seg[node]; 38 | } 39 | int mid=(l+r)/2; 40 | return merge(query(l,mid,2*node+1,lx,rx),query(mid+1,r,2*node+2,lx,rx)); 41 | } 42 | public: 43 | SegmentTree(int n) 44 | { 45 | while(sz(sz*2,0); 47 | } 48 | void update(int idx,ll val) 49 | { 50 | update(0,sz-1,0,idx,val); 51 | } 52 | ll query(int l,int r) 53 | { 54 | return query(0,sz-1,0,l,r); 55 | } 56 | }; 57 | signed main() 58 | { 59 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 60 | int n,q;cin>>n; 61 | SegmentTree tree(n); 62 | for(int i=0;i>x; 65 | if(i&1)x=-x; 66 | tree.update(i,x); 67 | } 68 | cin>>q; 69 | while(q--) 70 | { 71 | int op;cin>>op; 72 | if(!op) 73 | { 74 | ll idx,val;cin>>idx>>val; 75 | if(idx%2==0)val*=-1; 76 | tree.update(idx-1,val); 77 | } 78 | else 79 | { 80 | int l,r;cin>>l>>r; 81 | ll ans=tree.query(l-1,r-1); 82 | if(l%2==0)ans*=-1; 83 | cout< 2 | using namespace std; 3 | #define ll long long 4 | struct SegmentTree{ 5 | private: 6 | int sz=1;vectorseg; 7 | ll merge(ll a,ll b) 8 | { 9 | return a+b; 10 | } 11 | void update(int l,int r,int node,int lx,int rx,ll val) 12 | { 13 | if(l>=lx&&r<=rx) 14 | { 15 | seg[node]=merge(seg[node],val); 16 | return; 17 | } 18 | if(rrx) 19 | { 20 | return; 21 | } 22 | int mid=(l+r)/2; 23 | update(l,mid,2*node+1,lx,rx,val); 24 | update(mid+1,r,2*node+2,lx,rx,val); 25 | } 26 | ll query(int l,int r,int node,int idx) 27 | { 28 | if(l==r) 29 | { 30 | return seg[node]; 31 | } 32 | int mid=(l+r)/2; 33 | if(idx<=mid) 34 | { 35 | return merge(query(l,mid,2*node+1,idx),seg[node]); 36 | } 37 | return merge(query(mid+1,r,2*node+2,idx),seg[node]); 38 | } 39 | public: 40 | SegmentTree(int n) 41 | { 42 | while(sz(sz*2,0); 44 | } 45 | void update(int l,int r,ll val) 46 | { 47 | update(0,sz-1,0,l,r,val); 48 | } 49 | ll query(int idx) 50 | { 51 | return query(0,sz-1,0,idx); 52 | } 53 | }; 54 | signed main() 55 | { 56 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 57 | int n,q;cin>>n>>q; 58 | SegmentTree tree(n); 59 | while(q--) 60 | { 61 | int op;cin>>op; 62 | if(op==1) 63 | { 64 | ll l,r,val;cin>>l>>r>>val; 65 | tree.update(l,r-1,val); 66 | } 67 | else 68 | { 69 | int idx;cin>>idx; 70 | cout< 2 | using namespace std; 3 | #define ll long long 4 | struct SegmentTree{ 5 | private: 6 | int sz=1;vectorseg; 7 | ll merge(ll a,ll b) 8 | { 9 | return max(a,b); 10 | } 11 | void update(int l,int r,int node,int lx,int rx,ll val) 12 | { 13 | if(l>=lx&&r<=rx) 14 | { 15 | seg[node]=merge(seg[node],val); 16 | return; 17 | } 18 | if(rrx) 19 | { 20 | return; 21 | } 22 | int mid=(l+r)/2; 23 | update(l,mid,2*node+1,lx,rx,val); 24 | update(mid+1,r,2*node+2,lx,rx,val); 25 | } 26 | ll query(int l,int r,int node,int idx) 27 | { 28 | if(l==r) 29 | { 30 | return seg[node]; 31 | } 32 | int mid=(l+r)/2; 33 | if(idx<=mid) 34 | { 35 | return merge(query(l,mid,2*node+1,idx),seg[node]); 36 | } 37 | return merge(query(mid+1,r,2*node+2,idx),seg[node]); 38 | } 39 | public: 40 | SegmentTree(int n) 41 | { 42 | while(sz(sz*2,0); 44 | } 45 | void update(int l,int r,ll val) 46 | { 47 | update(0,sz-1,0,l,r,val); 48 | } 49 | ll query(int idx) 50 | { 51 | return query(0,sz-1,0,idx); 52 | } 53 | }; 54 | signed main() 55 | { 56 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 57 | int n,q;cin>>n>>q; 58 | SegmentTree tree(n); 59 | while(q--) 60 | { 61 | int op;cin>>op; 62 | if(op==1) 63 | { 64 | ll l,r,val;cin>>l>>r>>val; 65 | tree.update(l,r-1,val); 66 | } 67 | else 68 | { 69 | int idx;cin>>idx; 70 | cout< 2 | using namespace std; 3 | #define int long long 4 | struct SegmentTree{ 5 | private: 6 | int sz=1,skip; 7 | vectorseg,lazy; 8 | void propegate(int l,int r,int node) 9 | { 10 | if(lazy[node]==LLONG_MAX)return; 11 | if(l!=r) 12 | { 13 | lazy[2*node+1]=lazy[2*node+2]=lazy[node]; 14 | } 15 | seg[node]=lazy[node]; 16 | lazy[node]=LLONG_MAX; 17 | } 18 | void update(int l,int r,int node,int lx,int rx,int val) 19 | { 20 | propegate(l,r,node); 21 | if(l>rx||r=lx&&r<=rx) 26 | { 27 | lazy[node]=val; 28 | propegate(l,r,node); 29 | return; 30 | } 31 | int mid=l+r>>1; 32 | update(l,mid,2*node+1,lx,rx,val); 33 | update(mid+1,r,2*node+2,lx,rx,val); 34 | } 35 | int query(int l,int r,int node,int idx) 36 | { 37 | propegate(l,r,node); 38 | if(l==r) 39 | { 40 | return seg[node]; 41 | } 42 | int mid=l+r>>1; 43 | if(idx<=mid) 44 | { 45 | return query(l,mid,2*node+1,idx); 46 | } 47 | else 48 | { 49 | return query(mid+1,r,2*node+2,idx); 50 | } 51 | } 52 | public: 53 | SegmentTree(int n){ 54 | while(sz<=n)sz*=2; 55 | seg=vector(sz*2,0); 56 | lazy=vector(sz*2,LLONG_MAX); 57 | } 58 | void update(int l,int r,int val) 59 | { 60 | update(0,sz-1,0,l,r,val); 61 | } 62 | int query(int idx) 63 | { 64 | return query(0,sz-1,0,idx); 65 | } 66 | }; 67 | signed main() 68 | { 69 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 70 | int n,q;cin>>n>>q; 71 | SegmentTree tree(n); 72 | while(q--) 73 | { 74 | int op;cin>>op; 75 | if(op==1) 76 | { 77 | int l,r,val;cin>>l>>r>>val; 78 | tree.update(l+1,r,val); 79 | } 80 | else 81 | { 82 | int idx;cin>>idx; 83 | cout< 2 | using namespace std; 3 | #define int long long 4 | struct SegmentTree{ 5 | private: 6 | int sz=1,skip=LLONG_MAX; 7 | vectorseg,lazy; 8 | void propegate(int l,int r,int node) 9 | { 10 | if(lazy[node]==0)return; 11 | if(l!=r) 12 | { 13 | lazy[2*node+1]+=lazy[node]; 14 | lazy[2*node+2]+=lazy[node]; 15 | 16 | } 17 | seg[node]+=lazy[node]; 18 | lazy[node]=0; 19 | } 20 | void update(int l,int r,int node,int lx,int rx,int val) 21 | { 22 | propegate(l,r,node); 23 | if(l>rx||r=lx&&r<=rx) 28 | { 29 | lazy[node]+=val; 30 | propegate(l,r,node); 31 | return; 32 | } 33 | int mid=l+r>>1; 34 | update(l,mid,2*node+1,lx,rx,val); 35 | update(mid+1,r,2*node+2,lx,rx,val); 36 | seg[node]=min(seg[2*node+1],seg[2*node+2]); 37 | } 38 | int query(int l,int r,int node,int lx,int rx) 39 | { 40 | propegate(l,r,node); 41 | if(l>=lx&&r<=rx) 42 | { 43 | return seg[node]; 44 | } 45 | if(l>rx||r>1; 50 | return min(query(l,mid,2*node+1,lx,rx),query(mid+1,r,2*node+2,lx,rx)); 51 | } 52 | public: 53 | SegmentTree(int n){ 54 | while(sz<=n)sz*=2; 55 | seg=vector(sz*2,0); 56 | lazy=vector(sz*2,0); 57 | } 58 | void update(int l,int r,int val) 59 | { 60 | update(0,sz-1,0,l,r,val); 61 | } 62 | int query(int l,int r) 63 | { 64 | return query(0,sz-1,0,l,r); 65 | } 66 | }; 67 | signed main() 68 | { 69 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 70 | int n,q;cin>>n>>q; 71 | SegmentTree tree(n); 72 | while(q--) 73 | { 74 | int op;cin>>op; 75 | if(op==1) 76 | { 77 | int l,r,val;cin>>l>>r>>val; 78 | tree.update(l+1,r,val); 79 | } 80 | else 81 | { 82 | int l,r;cin>>l>>r; 83 | cout< 2 | using namespace std; 3 | #define int long long 4 | const int mod=1e9+7; 5 | int mul(int a,int b) 6 | { 7 | a%=mod; 8 | b%=mod; 9 | return (a*b)%mod; 10 | } 11 | int add(int a,int b) 12 | { 13 | a%=mod; 14 | b%=mod; 15 | return (a+b)%mod; 16 | } 17 | struct SegmentTree{ 18 | private: 19 | int sz=1,skip=0; 20 | vectorseg,lazy; 21 | void propegate(int l,int r,int node) 22 | { 23 | if(lazy[node]==1)return; 24 | if(l!=r) 25 | { 26 | lazy[2*node+1]=mul(lazy[2*node+1],lazy[node]); 27 | lazy[2*node+2]=mul(lazy[2*node+2],lazy[node]); 28 | } 29 | seg[node]=mul(seg[node],lazy[node]); 30 | lazy[node]=1; 31 | } 32 | void update(int l,int r,int node,int lx,int rx,int val) 33 | { 34 | propegate(l,r,node); 35 | if(l>rx||r=lx&&r<=rx) 40 | { 41 | lazy[node]=mul(lazy[node],val); 42 | propegate(l,r,node); 43 | return; 44 | } 45 | int mid=l+r>>1; 46 | update(l,mid,2*node+1,lx,rx,val); 47 | update(mid+1,r,2*node+2,lx,rx,val); 48 | seg[node]=add(seg[2*node+1],seg[2*node+2]); 49 | } 50 | int query(int l,int r,int node,int lx,int rx) 51 | { 52 | propegate(l,r,node); 53 | if(l>=lx&&r<=rx) 54 | { 55 | return seg[node]; 56 | } 57 | if(l>rx||r>1; 62 | return add(query(l,mid,2*node+1,lx,rx),query(mid+1,r,2*node+2,lx,rx)); 63 | } 64 | public: 65 | void update(int l,int r,int val) 66 | { 67 | update(0,sz-1,0,l,r,val); 68 | } 69 | int query(int l,int r) 70 | { 71 | return query(0,sz-1,0,l,r); 72 | } 73 | SegmentTree(int n){ 74 | while(sz<=n)sz*=2; 75 | seg=vector(sz*2,1); 76 | lazy=vector(sz*2,1LL); 77 | for(int i=1;i>n>>q; 87 | SegmentTree tree(n); 88 | while(q--) 89 | { 90 | int op;cin>>op; 91 | if(op==1) 92 | { 93 | int l,r,val;cin>>l>>r>>val; 94 | tree.update(l+1,r,val); 95 | } 96 | else 97 | { 98 | int l,r;cin>>l>>r; 99 | cout< 2 | using namespace std; 3 | #define int long long 4 | const int mod=1e9+7; 5 | struct SegmentTree{ 6 | private: 7 | int sz=1,skip=-1; 8 | vectorseg,lazy; 9 | void propegate(int l,int r,int node) 10 | { 11 | if(lazy[node]==1)return; 12 | if(l!=r) 13 | { 14 | lazy[2*node+1]|=lazy[node]; 15 | lazy[2*node+2]|=lazy[node]; 16 | } 17 | seg[node]|=lazy[node]; 18 | lazy[node]=0; 19 | } 20 | void update(int l,int r,int node,int lx,int rx,int val) 21 | { 22 | propegate(l,r,node); 23 | if(l>rx||r=lx&&r<=rx) 28 | { 29 | lazy[node]|=val; 30 | propegate(l,r,node); 31 | return; 32 | } 33 | int mid=l+r>>1; 34 | update(l,mid,2*node+1,lx,rx,val); 35 | update(mid+1,r,2*node+2,lx,rx,val); 36 | seg[node]=seg[2*node+1]&seg[2*node+2]; 37 | } 38 | int query(int l,int r,int node,int lx,int rx) 39 | { 40 | propegate(l,r,node); 41 | if(l>=lx&&r<=rx) 42 | { 43 | return seg[node]; 44 | } 45 | if(l>rx||r>1; 50 | return query(l,mid,2*node+1,lx,rx)&query(mid+1,r,2*node+2,lx,rx); 51 | } 52 | public: 53 | void update(int l,int r,int val) 54 | { 55 | update(0,sz-1,0,l,r,val); 56 | } 57 | int query(int l,int r) 58 | { 59 | return query(0,sz-1,0,l,r); 60 | } 61 | SegmentTree(int n){ 62 | while(sz<=n)sz*=2; 63 | seg=vector(sz*2,0); 64 | lazy=vector(sz*2,0); 65 | } 66 | }; 67 | signed main() 68 | { 69 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 70 | int n,q;cin>>n>>q; 71 | SegmentTree tree(n); 72 | while(q--) 73 | { 74 | int op;cin>>op; 75 | if(op==1) 76 | { 77 | int l,r,val;cin>>l>>r>>val; 78 | tree.update(l+1,r,val); 79 | } 80 | else 81 | { 82 | int l,r;cin>>l>>r; 83 | cout< 2 | using namespace std; 3 | #define int long long 4 | struct SegmentTree{ 5 | private: 6 | int sz=1,skip=0; 7 | vectorseg,lazy; 8 | void propegate(int l,int r,int node) 9 | { 10 | if(lazy[node]==0)return; 11 | if(l!=r) 12 | { 13 | lazy[2*node+1]+=lazy[node]; 14 | lazy[2*node+2]+=lazy[node]; 15 | } 16 | int len=r-l+1; 17 | seg[node]+=len*lazy[node]; 18 | lazy[node]=0; 19 | } 20 | void update(int l,int r,int node,int lx,int rx,int val) 21 | { 22 | propegate(l,r,node); 23 | if(l>rx||r=lx&&r<=rx) 28 | { 29 | lazy[node]+=val; 30 | propegate(l,r,node); 31 | return; 32 | } 33 | int mid=l+r>>1; 34 | update(l,mid,2*node+1,lx,rx,val); 35 | update(mid+1,r,2*node+2,lx,rx,val); 36 | seg[node]=seg[2*node+1]+seg[2*node+2]; 37 | } 38 | int query(int l,int r,int node,int lx,int rx) 39 | { 40 | propegate(l,r,node); 41 | if(l>=lx&&r<=rx) 42 | { 43 | return seg[node]; 44 | } 45 | if(l>rx||r>1; 50 | return query(l,mid,2*node+1,lx,rx)+query(mid+1,r,2*node+2,lx,rx); 51 | } 52 | public: 53 | void update(int l,int r,int val) 54 | { 55 | update(0,sz-1,0,l,r,val); 56 | } 57 | int query(int l,int r) 58 | { 59 | return query(0,sz-1,0,l,r); 60 | } 61 | SegmentTree(int n){ 62 | while(sz<=n)sz*=2; 63 | seg=vector(sz*2,0); 64 | lazy=vector(sz*2,0); 65 | } 66 | }; 67 | signed main() 68 | { 69 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 70 | int n,q;cin>>n>>q; 71 | SegmentTree tree(n); 72 | while(q--) 73 | { 74 | int op;cin>>op; 75 | if(op==1) 76 | { 77 | int l,r,val;cin>>l>>r>>val; 78 | tree.update(l+1,r,val); 79 | } 80 | else 81 | { 82 | int l,r;cin>>l>>r; 83 | cout< 2 | using namespace std; 3 | #define int long long 4 | struct SegmentTree{ 5 | private: 6 | int sz=1,skip=LLONG_MAX; 7 | vectorseg,lazy; 8 | void propegate(int l,int r,int node) 9 | { 10 | if(lazy[node]==LLONG_MAX)return; 11 | if(l!=r) 12 | { 13 | lazy[2*node+1]=lazy[node]; 14 | lazy[2*node+2]=lazy[node]; 15 | } 16 | seg[node]=lazy[node]; 17 | lazy[node]=LLONG_MAX; 18 | } 19 | void update(int l,int r,int node,int lx,int rx,int val) 20 | { 21 | propegate(l,r,node); 22 | if(l>rx||r=lx&&r<=rx) 27 | { 28 | lazy[node]=val; 29 | propegate(l,r,node); 30 | return; 31 | } 32 | int mid=l+r>>1; 33 | update(l,mid,2*node+1,lx,rx,val); 34 | update(mid+1,r,2*node+2,lx,rx,val); 35 | seg[node]=min(seg[2*node+1],seg[2*node+2]); 36 | } 37 | int query(int l,int r,int node,int lx,int rx) 38 | { 39 | propegate(l,r,node); 40 | if(l>=lx&&r<=rx) 41 | { 42 | return seg[node]; 43 | } 44 | if(l>rx||r>1; 49 | return min(query(l,mid,2*node+1,lx,rx),query(mid+1,r,2*node+2,lx,rx)); 50 | } 51 | public: 52 | void update(int l,int r,int val) 53 | { 54 | update(0,sz-1,0,l,r,val); 55 | } 56 | int query(int l,int r) 57 | { 58 | return query(0,sz-1,0,l,r); 59 | } 60 | SegmentTree(int n){ 61 | while(sz<=n)sz*=2; 62 | seg=vector(sz*2,0); 63 | lazy=vector(sz*2,LLONG_MAX); 64 | } 65 | }; 66 | signed main() 67 | { 68 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 69 | int n,q;cin>>n>>q; 70 | SegmentTree tree(n); 71 | while(q--) 72 | { 73 | int op;cin>>op; 74 | if(op==1) 75 | { 76 | int l,r,val;cin>>l>>r>>val; 77 | tree.update(l+1,r,val); 78 | } 79 | else 80 | { 81 | int l,r;cin>>l>>r; 82 | cout< 2 | using namespace std; 3 | #define int long long 4 | struct SegmentTree{ 5 | private: 6 | int sz=1,skip=0; 7 | vectorseg,lazy; 8 | void propegate(int l,int r,int node) 9 | { 10 | if(lazy[node]==LLONG_MAX)return; 11 | if(l!=r) 12 | { 13 | lazy[2*node+1]=lazy[node]; 14 | lazy[2*node+2]=lazy[node]; 15 | } 16 | int len=r-l+1; 17 | seg[node]=len*lazy[node]; 18 | lazy[node]=LLONG_MAX; 19 | } 20 | void update(int l,int r,int node,int lx,int rx,int val) 21 | { 22 | propegate(l,r,node); 23 | if(l>rx||r=lx&&r<=rx) 28 | { 29 | lazy[node]=val; 30 | propegate(l,r,node); 31 | return; 32 | } 33 | int mid=l+r>>1; 34 | update(l,mid,2*node+1,lx,rx,val); 35 | update(mid+1,r,2*node+2,lx,rx,val); 36 | seg[node]=seg[2*node+1]+seg[2*node+2]; 37 | } 38 | int query(int l,int r,int node,int lx,int rx) 39 | { 40 | propegate(l,r,node); 41 | if(l>=lx&&r<=rx) 42 | { 43 | return seg[node]; 44 | } 45 | if(l>rx||r>1; 50 | return query(l,mid,2*node+1,lx,rx)+query(mid+1,r,2*node+2,lx,rx); 51 | } 52 | public: 53 | void update(int l,int r,int val) 54 | { 55 | update(0,sz-1,0,l,r,val); 56 | } 57 | int query(int l,int r) 58 | { 59 | return query(0,sz-1,0,l,r); 60 | } 61 | SegmentTree(int n){ 62 | while(sz<=n)sz*=2; 63 | seg=vector(sz*2,0); 64 | lazy=vector(sz*2,LLONG_MAX); 65 | } 66 | }; 67 | signed main() 68 | { 69 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 70 | int n,q;cin>>n>>q; 71 | SegmentTree tree(n); 72 | while(q--) 73 | { 74 | int op;cin>>op; 75 | if(op==1) 76 | { 77 | int l,r,val;cin>>l>>r>>val; 78 | tree.update(l+1,r,val); 79 | } 80 | else 81 | { 82 | int l,r;cin>>l>>r; 83 | cout< 2 | using namespace std; 3 | #define ll long long 4 | vectorSuffixArray(string &s) 5 | { 6 | s+='$'; 7 | int n=s.size(); 8 | vector>suf(n); 9 | for(int i=0;ip(n),c(n); 12 | for(int i=0;i,int>>su(n); 23 | for(int i=0;i>s; 40 | vectorsuf=SuffixArray(s); 41 | for(auto it:suf) cout< 2 | using namespace std; 3 | #define ll long long 4 | void countSort(vector&p,vector&c) 5 | { 6 | int n=p.size(); 7 | vectorcnt(n); 8 | for(auto it:c) cnt[it]++; 9 | vectorp_new(n),pos(n); 10 | pos[0]=0; 11 | for(int i=1;iSuffixArray(string &s) 21 | { 22 | s+='$'; 23 | int n=s.size(); 24 | vector>suf(n); 25 | for(int i=0;ip(n),c(n); 28 | for(int i=0;ic_new(n); 41 | c_new[p[0]]=0; 42 | for(int i=1;ilast={c[p[i-1]],c[(p[i-1]+(1<cur={c[p[i]],c[(p[i]+(1<>s; 58 | vectorsuf=SuffixArray(s); 59 | for(auto it:suf) cout< 2 | using namespace std; 3 | #define ll long long 4 | void countSort(vector&p,vector&c) 5 | { 6 | int n=p.size(); 7 | vectorcnt(n); 8 | for(auto it:c) cnt[it]++; 9 | vectorp_new(n),pos(n); 10 | pos[0]=0; 11 | for(int i=1;iSuffixArray(string &s) 21 | { 22 | s+='$'; 23 | int n=s.size(); 24 | vector>suf(n); 25 | for(int i=0;ip(n),c(n); 28 | for(int i=0;ic_new(n); 41 | c_new[p[0]]=0; 42 | for(int i=1;ilast={c[p[i-1]],c[(p[i-1]+(1<cur={c[p[i]],c[(p[i]+(1<>s; 58 | vectorsuf=SuffixArray(s); 59 | int q;cin>>q;while(q--) 60 | { 61 | string x;cin>>x; 62 | int l=0,r=suf.size()-1,n=s.size(),sz=x.size();string ans="No\n"; 63 | while(l<=r) 64 | { 65 | int mid=l+r>>1; 66 | string w=s.substr(suf[mid],min(n-suf[mid],sz)); 67 | if(wx) 72 | { 73 | r=mid-1; 74 | } 75 | else 76 | { 77 | ans="Yes\n"; 78 | break; 79 | } 80 | } 81 | cout< 2 | using namespace std; 3 | #define ll long long 4 | void countSort(vector&p,vector&c) 5 | { 6 | int n=p.size(); 7 | vectorcnt(n); 8 | for(auto it:c) cnt[it]++; 9 | vectorp_new(n),pos(n); 10 | pos[0]=0; 11 | for(int i=1;iSuffixArray(string &s) 21 | { 22 | s+='$'; 23 | int n=s.size(); 24 | vector>suf(n); 25 | for(int i=0;ip(n),c(n); 28 | for(int i=0;ic_new(n); 41 | c_new[p[0]]=0; 42 | for(int i=1;ilast={c[p[i-1]],c[(p[i-1]+(1<cur={c[p[i]],c[(p[i]+(1<>s; 58 | vectorsuf=SuffixArray(s); 59 | int q;cin>>q;while(q--) 60 | { 61 | string x;cin>>x; 62 | int l=0,r=suf.size()-1,n=s.size()-1,sz=x.size(),idx=n+1; 63 | while(l<=r) 64 | { 65 | int mid=l+r>>1; 66 | string w=s.substr(suf[mid],min(n-suf[mid],sz)); 67 | if(w>x) idx=mid,r=mid-1; 68 | else l=mid+1; 69 | } 70 | int ans=idx; 71 | l=0,r=suf.size()-1,n=suf.size()-1,sz=x.size(),idx=n+1; 72 | while(l<=r) 73 | { 74 | int mid=l+r>>1; 75 | string w=s.substr(suf[mid],min(n-suf[mid],sz)); 76 | if(w>=x) idx=mid,r=mid-1; 77 | else l=mid+1; 78 | } 79 | ans-=idx; 80 | cout< 2 | using namespace std; 3 | #define ll long long 4 | void countSort(vector&p,vector&c) 5 | { 6 | int n=p.size(); 7 | vectorcnt(n); 8 | for(auto it:c) cnt[it]++; 9 | vectorp_new(n),pos(n); 10 | pos[0]=0; 11 | for(int i=1;i,vector>SuffixArray_LCP(string &s) 21 | { 22 | s+='$'; 23 | int n=s.size(); 24 | vector>suf(n); 25 | for(int i=0;ip(n),c(n); 28 | for(int i=0;ic_new(n); 41 | c_new[p[0]]=0; 42 | for(int i=1;ilast={c[p[i-1]],c[(p[i-1]+(1<cur={c[p[i]],c[(p[i]+(1<lcp(n); 53 | k=0; 54 | for(int i=0;i>s; 67 | pair,vector>v=SuffixArray_LCP(s); 68 | for(auto it:v.first) cout< 2 | using namespace std; 3 | #define ll long long 4 | void countSort(vector&p,vector&c) 5 | { 6 | int n=p.size(); 7 | vectorcnt(n); 8 | for(auto it:c) cnt[it]++; 9 | vectorp_new(n),pos(n); 10 | pos[0]=0; 11 | for(int i=1;i,vector>SuffixArray_LCP(string &s) 21 | { 22 | s+='$'; 23 | int n=s.size(); 24 | vector>suf(n); 25 | for(int i=0;ip(n),c(n); 28 | for(int i=0;ic_new(n); 41 | c_new[p[0]]=0; 42 | for(int i=1;ilast={c[p[i-1]],c[(p[i-1]+(1<cur={c[p[i]],c[(p[i]+(1<lcp(n); 53 | k=0; 54 | for(int i=0;i>s; 67 | auto[p,lcp]=SuffixArray_LCP(s); 68 | ll ans=0; 69 | for(int i=0;i 2 | using namespace std; 3 | #define ll long long 4 | void countSort(vector&p,vector&c) 5 | { 6 | int n=p.size(); 7 | vectorcnt(n); 8 | for(auto it:c) cnt[it]++; 9 | vectorp_new(n),pos(n); 10 | pos[0]=0; 11 | for(int i=1;i,vector>SuffixArray_LCP(string &s) 21 | { 22 | s+='$'; 23 | int n=s.size(); 24 | vector>suf(n); 25 | for(int i=0;ip(n),c(n); 28 | for(int i=0;ic_new(n); 41 | c_new[p[0]]=0; 42 | for(int i=1;ilast={c[p[i-1]],c[(p[i-1]+(1<cur={c[p[i]],c[(p[i]+(1<lcp(n); 53 | k=0; 54 | for(int i=0;i>s>>t; 67 | int ns=s.size(),nt=t.size(); 68 | char x=char('a'-1); 69 | s=s+x+t; 70 | auto[p,lcp]=SuffixArray_LCP(s); 71 | int ans=0,idx=0; 72 | for(int i=1;ins&&p[i-1]ans) ans=lcp[i],idx=p[i]; 75 | if(p[i-1]ns&&p[i]ans) ans=lcp[i],idx=p[i]; 76 | } 77 | string out=s.substr(idx,ans); 78 | cout< 2 | using namespace std; 3 | #define ll long long 4 | void countSort(vector&p,vector&c) 5 | { 6 | int n=p.size(); 7 | vectorcnt(n); 8 | for(auto it:c) cnt[it]++; 9 | vectorp_new(n),pos(n); 10 | pos[0]=0; 11 | for(int i=1;i,3>SuffixArray_LCP(string &s) 21 | { 22 | s+=char(32); 23 | int n=s.size(); 24 | vector>suf(n); 25 | for(int i=0;ip(n),c(n); 28 | for(int i=0;ic_new(n); 41 | c_new[p[0]]=0; 42 | for(int i=1;ilast={c[p[i-1]],c[(p[i-1]+(1<cur={c[p[i]],c[(p[i]+(1<lcp(n); 53 | k=0; 54 | for(int i=0;iseg;int sz,skip=INT_MAX; 64 | int merge(int a,int b) 65 | { 66 | return min(a,b); 67 | } 68 | void update(int l,int r,int node,int idx,int val) 69 | { 70 | if(l==r) 71 | { 72 | seg[node]=val; 73 | return; 74 | } 75 | int mid=l+r>>1; 76 | if(midrx||r=lx&&r<=rx)return seg[node]; 90 | int mid=l+r>>1; 91 | int a=query(l,mid,2*node+1,lx,rx); 92 | int b=query(mid+1,r,2*node+2,lx,rx); 93 | return merge(a,b); 94 | } 95 | void SegmentTree(int n) 96 | { 97 | sz=1; 98 | while(sz<=n)sz<<=1; 99 | seg=vector(sz<<1,skip); 100 | } 101 | void update(int idx,int val) 102 | { 103 | update(0,sz-1,0,idx,val); 104 | } 105 | int query(int l,int r) 106 | { 107 | return query(0,sz-1,0,l,r); 108 | } 109 | string s;vectorc,p,lcp; 110 | bool com(paira,pairb) 111 | { 112 | if(a.first==b.first) return a.secondsuf2) 115 | { 116 | int cp=query(suf2+1,suf),len=a.second-a.first+1,len2=b.second-b.first+1; 117 | if(len<=cp&&len2<=cp) 118 | { 119 | if(len==len2) return a.first>s; 143 | array,3>x=SuffixArray_LCP(s); 144 | c=x[0],p=x[1],lcp=x[2]; 145 | SegmentTree(s.size()+5); 146 | for(int i=0;i>n; 148 | pairv[n]; 149 | for(auto&[a,b]:v) cin>>a>>b,--a,--b; 150 | sort(v,v+n,com); 151 | for(auto[a,b]:v) cout<<++a<<' '<<++b<<'\n'; 152 | return 0; 153 | } 154 | -------------------------------------------------------------------------------- /Codeforces-EDU/Suffix Array/Step5/D - Borders.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define int long long 5 | void countSort(vector&p,vector&c) 6 | { 7 | int n=p.size(); 8 | vectorcnt(n); 9 | for(auto it:c) cnt[it]++; 10 | vectorp_new(n),pos(n); 11 | pos[0]=0; 12 | for(int i=1;i,vector>SuffixArray_LCP(string &s) 22 | { 23 | s+='$'; 24 | int n=s.size(); 25 | vector>suf(n); 26 | for(int i=0;ip(n),c(n); 29 | for(int i=0;ic_new(n); 42 | c_new[p[0]]=0; 43 | for(int i=1;ilast={c[p[i-1]],c[(p[i-1]+(1<cur={c[p[i]],c[(p[i]+(1<lcp(n); 54 | k=0; 55 | for(int i=0;i>s; 68 | auto [p,lcp]=SuffixArray_LCP(s); 69 | vectornxt(lcp.size()+1,lcp.size()),prev(lcp.size()+1,-1); 70 | stackst; 71 | for(int i=0;i=0;i--) 78 | { 79 | while(st.size()&&lcp[i]<=lcp[st.top()]) prev[st.top()]=i,st.pop(); 80 | st.push(i); 81 | } 82 | ll sz=s.size(); 83 | ll ans=sz*(sz-1)/2; 84 | for(int i=0;i 2 | using namespace std; 3 | #define ll long long 4 | #define int long long 5 | void countSort(vector&p,vector&c) 6 | { 7 | int n=p.size(); 8 | vectorcnt(n); 9 | for(auto it:c) cnt[it]++; 10 | vectorp_new(n),pos(n); 11 | pos[0]=0; 12 | for(int i=1;i,vector>SuffixArray_LCP(string &s) 22 | { 23 | s+='$'; 24 | int n=s.size(); 25 | vector>suf(n); 26 | for(int i=0;ip(n),c(n); 29 | for(int i=0;ic_new(n); 42 | c_new[p[0]]=0; 43 | for(int i=1;ilast={c[p[i-1]],c[(p[i-1]+(1<cur={c[p[i]],c[(p[i]+(1<lcp(n); 54 | k=0; 55 | for(int i=0;i>n>>m; 68 | string s=""; 69 | for(int i=0;i>x; 72 | s.push_back(char('a'+x-1)); 73 | } 74 | auto [p,lcp]=SuffixArray_LCP(s); 75 | vectornxt(lcp.size()+1,lcp.size()),prev(lcp.size()+1,-1); 76 | stackst; 77 | for(int i=0;i=0;i--) 84 | { 85 | while(st.size()&&lcp[i]<=lcp[st.top()]) prev[st.top()]=i,st.pop(); 86 | st.push(i); 87 | } 88 | ll sz=s.size(); 89 | ll ans=n,len=n,idx=0; 90 | for(int i=0;ians) 94 | { 95 | ans=x; 96 | len=lcp[i]; 97 | idx=p[i]; 98 | } 99 | } 100 | cout<adj[N]; 4 | vector>components; 5 | stackst; 6 | bool in[N],vis[N]; 7 | void trajan(int node) 8 | { 9 | lowLink[node]=dfn[node]=timer++; 10 | st.push(node); 11 | in[node]=1; 12 | for(auto it:adj[node]) 13 | { 14 | if(dfn[it]==-1) 15 | { 16 | trajan(it); 17 | lowLink[node]=min(lowLink[node],lowLink[it]); 18 | } 19 | else if(in[it]) 20 | { 21 | lowLink[node]=min(lowLink[node],dfn[it]); 22 | } 23 | } 24 | if(lowLink[node]==dfn[node]) 25 | { 26 | vectorcomponent={}; 27 | int x=-1; 28 | while(x!=node) 29 | { 30 | x=st.top(); 31 | st.pop(); 32 | comp[x]=components.size(); 33 | in[x]=0; 34 | component.push_back(x); 35 | } 36 | components.push_back(component); 37 | root[comp[node]]=node; 38 | } 39 | } 40 | void init() 41 | { 42 | memset(dfn,-1,sizeof dfn); 43 | memset(_2satCom,-1,sizeof _2satCom); 44 | for(int i=1;i<=n+n;i++) 45 | { 46 | if(dfn[i]==-1) 47 | { 48 | trajan(i); 49 | } 50 | } 51 | } 52 | int Not(int x) 53 | { 54 | return 2*n-x+1; 55 | } 56 | bool solve_2Sat() 57 | { 58 | for(int i=1;i<=n;i++) 59 | { 60 | if(comp[i]==comp[Not(i)]) 61 | { 62 | return 0; 63 | } 64 | } 65 | return 1; 66 | } 67 | void assignVals() 68 | { 69 | for(int i=0;i 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | int n,m,k;cin>>n>>m>>k; 8 | int dis[n+5][n+5];int home[k+5]; 9 | for(int i=0;i>x>>y>>z; 19 | dis[x][y]=z; 20 | dis[y][x]=z; 21 | } 22 | for(int i=1;i<=n;i++) 23 | { 24 | dis[i][i]=0; 25 | } 26 | for(int i=1;i<=n;i++) 27 | { 28 | for(int o=1;o<=n;o++) 29 | { 30 | for(int j=1;j<=n;j++) 31 | { 32 | dis[o][j]=min(dis[o][j],dis[o][i]+dis[i][j]); 33 | } 34 | } 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Templates/Graph Theory/MaxFlow (Dinic).cpp: -------------------------------------------------------------------------------- 1 | struct Dinic{ 2 | struct FlowEdge{ 3 | int v,u; 4 | ll cap,flow=0; 5 | FlowEdge(int v,int u,ll cap):v(v),u(u),cap(cap){} 6 | }; 7 | const ll flow_inf=1e18; 8 | vectoredges;vector>adj;vectorlevel,ptr;queueq; 9 | int n,m=0,s,t; 10 | Dinic(int n,int start,int goal):n(n),s(start),t(goal) 11 | { 12 | adj.resize(n); 13 | level.resize(n); 14 | ptr.resize(n); 15 | } 16 | void add_edge(int v,int u,ll cap) 17 | { 18 | edges.emplace_back(v,u,cap); 19 | edges.emplace_back(u,v,0); 20 | adj[v].push_back(m); 21 | adj[u].push_back(m+1); 22 | m+=2; 23 | } 24 | bool bfs() 25 | { 26 | while(!q.empty()) 27 | { 28 | auto v=q.front(); 29 | q.pop(); 30 | for(int id:adj[v]) 31 | { 32 | if(edges[id].cap==edges[id].flow) continue; 33 | if (level[edges[id].u] != -1) continue; 34 | level[edges[id].u]=level[v]+1; 35 | q.push(edges[id].u); 36 | } 37 | } 38 | return ~level[t]; 39 | } 40 | ll dfs(int v,ll pushed) 41 | { 42 | if(pushed==0) return 0; 43 | if(v==t) return pushed; 44 | for(int&cid=ptr[v];cid<(int)adj[v].size();cid++) 45 | { 46 | int id=adj[v][cid]; 47 | int u=edges[id].u; 48 | if(level[v]+1!=level[u]) continue; 49 | ll tr=dfs(u,min(pushed,edges[id].cap-edges[id].flow)); 50 | if(tr==0) continue; 51 | edges[id].flow+=tr; 52 | edges[id^1].flow-=tr; 53 | return tr; 54 | } 55 | return 0; 56 | } 57 | ll flow() 58 | { 59 | ll f=0; 60 | while(true) 61 | { 62 | fill(level.begin(),level.end(),-1); 63 | level[s]=0; 64 | q.push(s); 65 | if(!bfs()) break; 66 | fill(ptr.begin(),ptr.end(),0); 67 | while(ll pushed=dfs(s,flow_inf)) 68 | { 69 | f+=pushed; 70 | } 71 | } 72 | return f; 73 | } 74 | }; 75 | -------------------------------------------------------------------------------- /Templates/Graph Theory/Maximum Bipartite Matching.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | const int N=105; 5 | int mat[N];vectoradj[N];bool vis[N]; 6 | bool can_match(int node) 7 | { 8 | if(vis[node]) return 0; 9 | vis[node]=1; 10 | for(auto it:adj[node]) 11 | { 12 | if(mat[it]==-1||can_match(mat[it])) 13 | { 14 | mat[it]=node; 15 | return 1; 16 | } 17 | } 18 | return 0; 19 | } 20 | signed main() 21 | { 22 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 23 | int n,m,q;cin>>n>>m>>q; 24 | setst;mapidx;int c=1; 25 | for(int i=0;i>a>>b; 28 | if(idx[a]==0) idx[a]=c++; 29 | if(idx[b]==0) idx[b]=c++; 30 | adj[idx[a]].push_back(idx[b]); 31 | st.insert(idx[a]); 32 | } 33 | memset(mat,-1,sizeof mat); 34 | int ans=0; 35 | for(auto it:st) memset(vis,0,sizeof vis),ans+=can_match(it); 36 | cout<adj[N]; 4 | vector>components; 5 | stackst; 6 | bool in[N],vis[N]; 7 | void dfs(int node) 8 | { 9 | vis[node]=1; 10 | for(auto it:adj[node]) 11 | { 12 | if(!vis[it]) 13 | { 14 | dfs(it); 15 | } 16 | } 17 | } 18 | void trajan(int node) 19 | { 20 | lowLink[node]=dfn[node]=timer++; 21 | st.push(node); 22 | in[node]=1; 23 | for(auto it:adj[node]) 24 | { 25 | if(dfn[it]==-1) 26 | { 27 | trajan(it); 28 | lowLink[node]=min(lowLink[node],lowLink[it]); 29 | } 30 | else if(in[it]) 31 | { 32 | lowLink[node]=min(lowLink[node],dfn[it]); 33 | } 34 | } 35 | if(lowLink[node]==dfn[node]) 36 | { 37 | vectorcomponent={}; 38 | int x=-1; 39 | while(x!=node) 40 | { 41 | x=st.top(); 42 | st.pop(); 43 | comp[x]=components.size(); 44 | in[x]=0; 45 | component.push_back(x); 46 | } 47 | components.push_back(component); 48 | } 49 | } 50 | void init() 51 | { 52 | memset(dfn,-1,sizeof dfn); 53 | for(int i=1;i<=n;i++) 54 | { 55 | if(dfn[i]==-1) 56 | { 57 | trajan(i); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Templates/Graph Theory/dijkstra.cpp: -------------------------------------------------------------------------------- 1 | const int N=1e5+5; 2 | vector>adj[N];vectorcost(N,-1); 3 | void dijkstra(int start) 4 | { 5 | priority_queue,deque>,greater>>pq; 6 | pq.push({0,start}); 7 | while(pq.size()) 8 | { 9 | pairp=pq.top(); 10 | pq.pop(); 11 | int node=p.second,nodecost=p.first; 12 | if(cost[node]!=-1) 13 | { 14 | continue; 15 | } 16 | cost[node]=nodecost; 17 | for(auto [node2,cost2]:adj[node]) 18 | { 19 | if(cost[node2]==-1) 20 | { 21 | pq.push({nodecost+cost2,node2}); 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Templates/Graph Theory/dsu with roll back.cpp: -------------------------------------------------------------------------------- 1 | // dsu with roll back 2 | struct dsu 3 | { 4 | int parent[N],group[N];stack>st; 5 | dsu() 6 | { 7 | for(int i=0;igroup[leader2]) 30 | { 31 | swap(leader1,leader2); 32 | } 33 | arrayp={0,0,0,0}; 34 | p[0]=leader2,p[1]=group[leader2]; 35 | p[2]=leader1,p[3]=parent[leader1]; 36 | st.push(p); 37 | if(leader1==leader2) 38 | { 39 | return; 40 | } 41 | group[leader2]+=group[leader1]; 42 | parent[leader1]=leader2; 43 | } 44 | int getsize(int x) 45 | { 46 | return group[find(x)]; 47 | } 48 | void rollBack() 49 | { 50 | if(st.empty())return; 51 | auto &[a,b,c,d]=st.top(); 52 | st.pop(); 53 | group[a]=b; 54 | parent[c]=d; 55 | } 56 | }; 57 | -------------------------------------------------------------------------------- /Templates/Graph Theory/dsu.cpp: -------------------------------------------------------------------------------- 1 | struct dsu 2 | { 3 | int parent[N],group[N]; 4 | dsu() 5 | { 6 | for(int i=0;igroup[leader2]) 33 | { 34 | parent[leader2]=leader1; 35 | group[leader1]+=group[leader2]; 36 | } 37 | else 38 | { 39 | parent[leader1]=leader2; 40 | group[leader2]+=group[leader1]; 41 | } 42 | } 43 | int getsize(int x) 44 | { 45 | return group[find(x)]; 46 | } 47 | }; 48 | -------------------------------------------------------------------------------- /Templates/Math and Number Theory/AndSumForRange.cpp: -------------------------------------------------------------------------------- 1 | ll msb(ll n) 2 | { 3 | ll msb=-1; 4 | while(n>0) 5 | { 6 | n=n/2; 7 | msb++; 8 | } 9 | return msb; 10 | } 11 | //bitwise and of a range datatype ll 12 | ll andRange(ll l,ll r){ 13 | ll ans=0; 14 | for(ll i=msb(r);i>=0;i--) 15 | { 16 | ll la = 1; 17 | if((l&(la<=0;i--) 15 | { 16 | bool idx=(n>>i)&1; 17 | if(cur->child[idx]==0) cur->child[idx]=new Node(); 18 | cur->frq[idx]++; 19 | cur=cur->child[idx]; 20 | } 21 | } 22 | void del(int n,int i,Node *cur) 23 | { 24 | if(i==-1)return; 25 | bool idx=(n>>i)&1; 26 | del(n,i-1,cur->child[idx]); 27 | cur->frq[idx]--; 28 | if(cur->frq[idx]==0) 29 | { 30 | delete cur->child[idx]; 31 | cur->child[idx]=0; 32 | } 33 | } 34 | int query(int n) 35 | { 36 | int ret=0;Node *cur=root; 37 | for(int i=29;i>=0;i--) 38 | { 39 | bool idx=(n>>i)&1; 40 | if(cur->child[idx^1]==0) cur=cur->child[idx]; 41 | else cur=cur->child[idx^1],ret|=(1< 2 | using namespace std; 3 | #define ll long long 4 | bool Knowbit(ll n,int i) 5 | { 6 | return (n>>i)&1; 7 | } 8 | ll Setbit(ll n,int i) 9 | { 10 | return n|(1<>n; 29 | int s[n]; 30 | for(int i=0;i>s[i]; 31 | int ans=0; 32 | for(int mask=0;mask<(1<odd) ans++; 44 | } 45 | cout< fac,inv,finv; 5 | ll nCr(ll x,ll y) 6 | { 7 | if(x<0||y>x||y<0)return(0); 8 | return(fac[x]*finv[y]%MOD*finv[x-y]%MOD); 9 | } 10 | ll nPr(ll x,ll y) 11 | { 12 | if(x<0||y>x||y<0)return 0; 13 | return fac[x]*finv[x-y]%MOD; 14 | } 15 | ll power(ll b,ll n) 16 | { 17 | b%=MOD; 18 | ll s=1; 19 | while(n) 20 | { 21 | if(n%2==1)s=s*b%MOD; 22 | b=b*b%MOD; 23 | n/=2; 24 | } 25 | return s; 26 | } 27 | void init(int n,ll mod) 28 | { 29 | fac.resize(n+1); 30 | inv.resize(n+1); 31 | finv.resize(n+1); 32 | MOD=mod; 33 | fac[0]=inv[0]=inv[1]=finv[0]=finv[1]=1; 34 | for(ll i=1;i<=n;++i)fac[i]=fac[i-1]*i%MOD; 35 | for(ll i=2;i<=n;++i)inv[i]=MOD-MOD/i*inv[MOD%i]%MOD; 36 | for(ll i=2;i<=n;++i)finv[i]=finv[i-1]*inv[i]%MOD; 37 | } 38 | ll mul(ll a,ll b) 39 | { 40 | return ((a%MOD)*(b%MOD))%MOD; 41 | } 42 | ll add(ll a,ll b) 43 | { 44 | return ((a%MOD)+(b%MOD))%MOD; 45 | } 46 | ll sub(ll a,ll b) 47 | { 48 | return (((a-b)%MOD)+MOD)%MOD; 49 | } 50 | ll divide(ll a,ll b) 51 | { 52 | return mul(a,power(b,MOD-2)); 53 | } 54 | ll Inv(int x) 55 | { 56 | return power(x,MOD-2); 57 | } 58 | ll catalan(int n) 59 | { 60 | return (nCr(2*n,n)*Inv(n+1))%MOD; 61 | } 62 | ll StarsAndPars(ll n,ll k) 63 | { 64 | return nCr(n+k-1,k-1); 65 | } 66 | }; 67 | using namespace combinatorics; 68 | -------------------------------------------------------------------------------- /Templates/Math and Number Theory/EEA.cpp: -------------------------------------------------------------------------------- 1 | //ax+by=gcd(a,b) 2 | ll extended_euclid(ll a,ll b,ll &x,ll &y) 3 | { 4 | if(b==0) 5 | { 6 | x=1,y=0; 7 | return a; 8 | } 9 | ll g=extended_euclid(b,a%b,y,x); 10 | y-=(a/b)*x; 11 | return g; 12 | } 13 | //generate another x and y 14 | pair Bezouts_identity(ll a,ll b,ll x,ll y,ll any_number) 15 | { 16 | return {x+any_number*(b/__gcd(a,b)),y-any_number*(a/(__gcd(a,b)))}; 17 | } 18 | -------------------------------------------------------------------------------- /Templates/Math and Number Theory/FFT with mod.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | const int N = 1e5+5,mod=1009; 5 | const long double PI = acos(-1); 6 | typedef complex C; 7 | #define rep(i,a,b) for(int i = a; i < b; i++) 8 | void fft(vector& a) { 9 | int n = a.size(), L = 31 - __builtin_clz(n); 10 | static vector> R(2, 1); 11 | static vector rt(2, 1); // (^ 10% faster if double) 12 | for (static int k = 2; k < n; k *= 2) { 13 | R.resize(n); rt.resize(n); 14 | auto x = polar(1.0L, acos(-1.0L) / k); 15 | rep(i,k,2*k) rt[i] = R[i] = i&1 ? R[i/2] * x : R[i/2]; 16 | } 17 | vector rev(n); 18 | rep(i,0,n) rev[i] = (rev[i / 2] | (i & 1) << L) / 2; 19 | rep(i,0,n) if (i < rev[i]) swap(a[i], a[rev[i]]); 20 | for (int k = 1; k < n; k *= 2) 21 | for (int i = 0; i < n; i += 2 * k) rep(j,0,k) { 22 | // C z = rt[j+k] * a[i+j+k]; // (25% faster if hand-rolled) /// include-line 23 | auto x = (double *)&rt[j+k], y = (double *)&a[i+j+k]; /// exclude-line 24 | C z(x[0]*y[0] - x[1]*y[1], x[0]*y[1] + x[1]*y[0]); /// exclude-line 25 | a[i + j + k] = a[i + j] - z; 26 | a[i + j] += z; 27 | } 28 | } 29 | vector conv(const vector & a, const vector & b) { 30 | if (a.empty() || b.empty()) return {}; 31 | vector res(a.size() + b.size() - 1); 32 | int L = 32 - __builtin_clz(res.size()), n = 1 << L; 33 | vector in(n), out(n); 34 | copy(a.begin(),a.end(), begin(in)); 35 | rep(i,0,b.size()) in[i].imag(b[i]); 36 | fft(in); 37 | for (C& x : in) x *= x; 38 | rep(i,0,n) out[i] = in[-i & (n - 1)] - conj(in[i]); 39 | fft(out); 40 | rep(i,0,res.size()) res[i] = (mod+(ll)round(imag(out[i]) / (4 * n))) % mod; 41 | return res; 42 | } 43 | signed main() 44 | { 45 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 46 | int n,m,k;cin>>n>>m>>k; 47 | int f[m+1]={}; 48 | for(int i=0;i>x; 51 | f[x]++; 52 | } 53 | priority_queue>>pq; 54 | for(int i=0;i<=m;i++) 55 | { 56 | if(f[i]) 57 | { 58 | f[i]%=mod; 59 | pq.push({-f[i],vector(f[i]+1,1)}); 60 | } 61 | } 62 | while(pq.size()>1) 63 | { 64 | auto [a,b]=pq.top(); 65 | pq.pop(); 66 | auto [x,y]=pq.top(); 67 | pq.pop(); 68 | vectormul=conv(b,y); 69 | pq.push({-mul.size(),mul}); 70 | } 71 | cout< 2 | using namespace std; 3 | #define ll long long 4 | using cd = complex; 5 | const double PI = acos(-1); 6 | 7 | void fft(vector &a, bool invert) { 8 | int n = a.size(); 9 | 10 | for (int i = 1, j = 0; i < n; i++) { 11 | int bit = n >> 1; 12 | for (; j & bit; bit >>= 1) 13 | j ^= bit; 14 | j ^= bit; 15 | 16 | if (i < j) 17 | swap(a[i], a[j]); 18 | } 19 | 20 | for (int len = 2; len <= n; len <<= 1) { 21 | double ang = 2 * PI / len * (invert ? -1 : 1); 22 | cd wlen(cos(ang), sin(ang)); 23 | for (int i = 0; i < n; i += len) { 24 | cd w(1); 25 | for (int j = 0; j < len / 2; j++) { 26 | cd u = a[i + j], v = a[i + j + len / 2] * w; 27 | a[i + j] = u + v; 28 | a[i + j + len / 2] = u - v; 29 | w *= wlen; 30 | } 31 | } 32 | } 33 | 34 | if (invert) { 35 | for (cd &x: a) 36 | x /= n; 37 | } 38 | } 39 | 40 | vector multiply(vector const &a, vector const &b) { 41 | vector fa(a.begin(), a.end()), fb(b.begin(), b.end()); 42 | int n = 1; 43 | while (n < a.size() + b.size()) 44 | n <<= 1; 45 | fa.resize(n); 46 | fb.resize(n); 47 | 48 | fft(fa, false); 49 | fft(fb, false); 50 | for (int i = 0; i < n; i++) 51 | fa[i] *= fb[i]; 52 | fft(fa, true); 53 | 54 | vector result(n); 55 | for (int i = 0; i < n; i++) 56 | result[i] = round(fa[i].real()); 57 | return result; 58 | } 59 | signed main() 60 | { 61 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 62 | int k,n,m;cin>>k>>n>>m; 63 | vectora(k+k); 64 | for(int i=0;i>x; 67 | a[x]++; 68 | } 69 | vectorb(k+k); 70 | for(int i=0;i>x; 73 | b[x]++; 74 | } 75 | vectorans=multiply(a,b); 76 | for(int i=2;i<=k+k;i++) 77 | { 78 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define int long long 5 | template 6 | struct FWHT { 7 | int fast(int b, int e) { 8 | int res = 1; 9 | for (; e; e >>= 1, b = 1ll * b * b % MOD) 10 | if (e & 1) 11 | res = 1ll * res * b % MOD; 12 | return res; 13 | } 14 | 15 | inline int add(int x, int y) { 16 | return x + y - (x + y >= MOD ? MOD : 0); 17 | } 18 | 19 | inline int sub(int x, int y) { 20 | return x - y + (x - y < 0 ? MOD : 0); 21 | } 22 | 23 | void FST(vector &a, bool inv) { 24 | for (int n = (int)a.size(), step = 1; step < n; step *= 2) { 25 | for (int i = 0; i < n; i += 2 * step) 26 | for(int j=i;j(add(u, v), sub(u, v)); // XOR /// include-line 32 | } 33 | } 34 | if (inv) { 35 | int divisor = fast((int)a.size(), MOD - 2); 36 | for (int &x: a) x = 1ll * x * divisor % MOD; // XOR only /// include-line 37 | } 38 | } 39 | 40 | vector conv(vector a, vector b) { 41 | FST(a, 0); 42 | FST(b, 0); 43 | for(int i=0;i<(int)a.size();i++) a[i] = 1ll * a[i] * b[i] % MOD; 44 | FST(a, 1); 45 | return a; 46 | } 47 | }; 48 | const int N=2e5+5; 49 | int n,m,w[N],par[N];vector>adj[N];vector>component;bool vis[N]; 50 | void dfs(int node) 51 | { 52 | vis[node]=1; 53 | for(auto [a,b]:adj[node]) 54 | { 55 | if(!par[a]) 56 | { 57 | par[a]=node; 58 | w[a]=b; 59 | dfs(a); 60 | } 61 | else if(vis[a]&&a!=par[node]) 62 | { 63 | vectorcomp={b}; 64 | for(int node2=node;node2!=a;node2=par[node2]) 65 | { 66 | comp.push_back(w[node2]); 67 | } 68 | component.push_back(comp); 69 | } 70 | } 71 | vis[node]=0; 72 | } 73 | signed main() 74 | { 75 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 76 | cin>>n>>m; 77 | int all=0; 78 | while(m--) 79 | { 80 | int a,b,c;cin>>a>>b>>c; 81 | adj[a].push_back({b,c}); 82 | adj[b].push_back({a,c}); 83 | all^=c; 84 | } 85 | par[1]=1; 86 | dfs(1); 87 | FWHT<1000000007>ft1; 88 | FWHT<1000000009>ft2; 89 | vectorans(1<<17),ans2(1<<17); 90 | ans[all]=ans2[all]=1; 91 | for(auto s:component) 92 | { 93 | vectorfrq(1<<17); 94 | for(auto it:s) 95 | { 96 | frq[it]++; 97 | } 98 | ans=ft1.conv(ans,frq); 99 | ans2=ft2.conv(ans2,frq); 100 | } 101 | for(int i=0;i<(1<<17);i++) 102 | { 103 | if(ans[i]||ans2[i]) 104 | { 105 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define int long long 5 | const int mod=1e9+7; 6 | struct Matrix { 7 | vector>m; 8 | Matrix() { 9 | m.assign(2,vector(2,0)); 10 | } 11 | Matrix operator*(Matrix&other)const{ 12 | Matrix ans; 13 | for (ll i = 0; i < 2; i++) 14 | for (ll j = 0; j < 2; j++) 15 | for (ll k = 0; k < 2; k++) 16 | ans.m[i][j]+=m[i][k]*other.m[k][j], 17 | ans.m[i][j]%=mod; 18 | return ans; 19 | } 20 | }; 21 | signed main() 22 | { 23 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 24 | Matrix base; 25 | base.m={ 26 | {1,0}, 27 | {0,0} 28 | }; 29 | Matrix trans; 30 | trans.m={ 31 | {19,7}, 32 | {6,20} 33 | }; 34 | auto powmod=[&](ll pw){ 35 | while(pw) 36 | { 37 | if(pw&1)base=base*trans; 38 | trans=trans*trans; 39 | pw/=2; 40 | } 41 | }; 42 | int n;cin>>n; 43 | powmod(n); 44 | cout< 2 | using namespace std; 3 | #define ll long long 4 | struct NTT { 5 | static const int MAXF = 1 << 20; 6 | int pr; 7 | int rts[MAXF + 1]; 8 | int bitrev[MAXF]; 9 | 10 | int fpow(int a, int k, int p) { 11 | if (!k) return 1; 12 | int res = a, tmp = a; 13 | k--; 14 | while (k) { 15 | if (k & 1) { 16 | res = (long long) res * tmp % p; 17 | } 18 | tmp = (long long) tmp * tmp % p; 19 | k >>= 1; 20 | } 21 | return res; 22 | } 23 | void init(int pr, int pw) { 24 | this->pr = pr; 25 | int k = 0; while ((1 << k) < MAXF) k++; 26 | bitrev[0] = 0; 27 | for (int i = 1; i < MAXF; i++) { 28 | bitrev[i] = bitrev[i >> 1] >> 1 | ((i & 1) << k - 1); 29 | } 30 | pw = fpow(pw, (pr - 1) / MAXF, pr); 31 | rts[0] = 1; 32 | for (int i = 1; i <= MAXF; i++) { 33 | rts[i] = (long long) rts[i - 1] * pw % pr; 34 | } 35 | } 36 | void dft(int a[], int n, int sign) { 37 | int d = 0; while ((1 << d) * n != MAXF) d++; 38 | for (int i = 0; i < n; i++) { 39 | if (i < (bitrev[i] >> d)) { 40 | swap(a[i], a[bitrev[i] >> d]); 41 | } 42 | } 43 | for (int len = 2; len <= n; len <<= 1) { 44 | int delta = MAXF / len * sign; 45 | for (int i = 0; i < n; i += len) { 46 | int *x = a + i,*y = a + i + (len >> 1), *w = sign > 0 ? rts : rts + MAXF; 47 | for (int k = 0; k + k < len; k++) { 48 | int z = (long long) *y * *w % pr; 49 | *y = *x - z, *x = *x + z; 50 | if (*y < 0) *y += pr; 51 | if (*x >= pr) *x -= pr; 52 | x++, y++, w += delta; 53 | } 54 | } 55 | } 56 | if (sign < 0) { 57 | int in = fpow(n, pr - 2, pr); 58 | for (int i = 0; i < n; i++) { 59 | a[i] = (long long) a[i] * in % pr; 60 | } 61 | } 62 | } 63 | void multiply(int a[], int b[], int na, int nb, int c[]) { 64 | static int fa[MAXF], fb[MAXF]; 65 | int n = na + nb - 1; while (n != (n & -n)) n += n & -n; 66 | for (int i = 0; i < n; i++) fa[i] = fb[i] = 0; 67 | for (int i = 0; i < na; i++) fa[i] = a[i]; 68 | for (int i = 0; i < nb; i++) fb[i] = b[i]; 69 | dft(fa, n, 1), dft(fb, n, 1); 70 | for (int i = 0; i < n; i++) fa[i] = (long long) fa[i] * fb[i] % pr; 71 | dft(fa, n, -1); 72 | for (int i = 0; i < n; i++) c[i] = fa[i]; 73 | } 74 | vector multiply(vector a, vector b) { 75 | static int fa[MAXF], fb[MAXF], fc[MAXF]; 76 | int na = a.size(), nb = b.size(); 77 | for (int i = 0; i < na; i++) fa[i] = a[i]; 78 | for (int i = 0; i < nb; i++) fb[i] = b[i]; 79 | multiply(fa, fb, na, nb, fc); 80 | int k = na + nb - 1; 81 | vector res(k); 82 | for (int i = 0; i < k; i++) res[i] = fc[i]; 83 | return res; 84 | } 85 | } ntt; 86 | vectorpower(vector&I,int n) 87 | { 88 | if(n==1) 89 | { 90 | return I; 91 | } 92 | vectorret=power(I,n/2); 93 | ret=ntt.multiply(ret,ret); 94 | if(n&1) 95 | { 96 | ret=ntt.multiply(ret,I); 97 | } 98 | return ret; 99 | } 100 | signed main() 101 | { 102 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 103 | int n,k;cin>>n>>k; 104 | vectorf(10); 105 | while(k--) 106 | { 107 | int x;cin>>x; 108 | f[x]=1; 109 | } 110 | ntt.init(998244353,3); 111 | vectorall=power(f,n/2); 112 | int ans=0; 113 | for(auto it:all) 114 | { 115 | ans+=(1LL*it*it)%998244353;; 116 | ans%=998244353; 117 | } 118 | cout< 2 | using namespace std; 3 | #define ll long long 4 | const int N=1e5+5,mod=1e9+7; 5 | bool is_prime(ll n) 6 | { 7 | if(n==1||n==0)return 0; 8 | else if(n==2)return 1; 9 | else if(n%2==0)return 0; 10 | for(ll i=2;i*i<=n;i++) 11 | { 12 | if(n%i==0)return 0; 13 | } 14 | return 1; 15 | } 16 | ll gcd(ll a,ll b) 17 | { 18 | if(a>b) swap(a,b); 19 | if(a==0)return b; 20 | return gcd(b%a,a); 21 | } 22 | int lcm(int a,int b) 23 | { 24 | return a/gcd(a,b)*b; 25 | } 26 | ll power(ll a,ll b) 27 | { 28 | if(b==0)return 1; 29 | ll x=power(a,b/2); 30 | x*=x; 31 | if(b%2!=0)x*=a; 32 | return x; 33 | } 34 | vector isPrime(N,true); 35 | void sieve() 36 | { 37 | isPrime[0] = false; 38 | isPrime[1] = false; 39 | 40 | for(ll i=2;i*iisPrime(N,1);vectorprimes; 52 | void linearSieve() 53 | { 54 | isPrime[0]=isPrime[1]=0; 55 | for(ll i=2;i=N) break; 61 | isPrime[i*it]=0; 62 | if(i%it==0) break; 63 | } 64 | } 65 | } 66 | vector> primeFactorze(ll n) 67 | { 68 | vector> arr; 69 | for(ll i=2;i*i<=n;i++) 70 | { 71 | int w=0; 72 | while(n%i==0) 73 | { 74 | w++; 75 | n/=i; 76 | } 77 | if(w)arr.push_back({i,w}); 78 | } 79 | if(n>1)arr.push_back({n,1}); 80 | return arr; 81 | } 82 | vector divisors(long long n) 83 | { 84 | vectorarr; 85 | for (long long i = 1; i * i <= n; i++) 86 | { 87 | if (n % i == 0) 88 | { 89 | arr.push_back(i); 90 | if (i * i != n) 91 | { 92 | arr.push_back(n / i); 93 | } 94 | } 95 | } 96 | return arr; 97 | } 98 | ll powmod(ll x, ll y) 99 | { 100 | ll res = 1; 101 | x = x % mod; 102 | if (x == 0) return 0; 103 | while (y > 0) 104 | { 105 | if (y & 1) 106 | res = (res*x) % mod; 107 | y = y>>1; 108 | x = (x*x) % mod; 109 | } 110 | return res; 111 | } 112 | ll add(ll a,ll b) 113 | { 114 | return ((a%mod)+(b%mod))%mod; 115 | } 116 | ll mul(ll a,ll b) 117 | { 118 | return ((a%mod)*(b%mod))%mod; 119 | } 120 | ll sub(ll a,ll b) 121 | { 122 | return ((((a%mod)-(b%mod))%mod)+mod)%mod; 123 | } 124 | ll divide(ll a,ll b) 125 | { 126 | return mul(a,powmod(b,mod-2)); 127 | } 128 | signed main() 129 | { 130 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 131 | 132 | return 0; 133 | } 134 | -------------------------------------------------------------------------------- /Templates/Math and Number Theory/NumberOfDivisors (for large numbers).cpp: -------------------------------------------------------------------------------- 1 | ll numberOfDivisors(ll n) 2 | { 3 | int primes[]={2,3,5,7,11,13,17,19}; 4 | ll num=1,ans=1; 5 | for (int it:primes) 6 | { 7 | int c=0; 8 | while(n%it==0) 9 | { 10 | n/=it; 11 | c++; 12 | } 13 | ans*=c+1; 14 | num*=it; 15 | } 16 | int all=0; 17 | for(int i=1;icnt_primes; 6 | vector
    primes,divisors; 7 | ul modMul(ul a,ul b,const ul mod) 8 | { 9 | ll ret=a*b-mod*(ul)((db)a*b/mod); 10 | return ret+((ret<0)-(ret>=(ll)mod))*mod; 11 | } 12 | ul modPow(ul a,ul b,const ul mod) 13 | { 14 | if (b==0) 15 | { 16 | return 1; 17 | } 18 | ul res=modPow(a,b/2,mod); 19 | res=modMul(res,res,mod); 20 | return b&1?modMul(res,a,mod):res; 21 | } 22 | 23 | bool rabin_miller(ul n) 24 | { 25 | if(n<2||n%6%4!=1) 26 | { 27 | return n-2<2; 28 | } 29 | ul A[]={2,325,9375,28178,450775,9780504,1795265022},s=__builtin_ctzll(n-1),d=n>>s; 30 | for(auto a:A) 31 | { 32 | ul p=modPow(a,d,n),i=s; 33 | while(p!=1&&p!=n-1&&a%n&&i--) 34 | { 35 | p=modMul(p,p,n); 36 | } 37 | if(p!=n-1&&i!=s) 38 | { 39 | return 0; 40 | } 41 | } 42 | return 1; 43 | } 44 | ul pollard(ul n) // return some nontrivial factor of n 45 | { 46 | auto f=[n,this](ul x){return modMul(x,x,n)+1;}; 47 | ul x=0,y=0,t=30,prd=2,i=1,q; 48 | while(t++%40||__gcd(prd,n)==1) 49 | { /// speedup: don't take gcd every it 50 | if (x == y) 51 | { 52 | x=++i,y=f(x); 53 | } 54 | if((q=modMul(prd,max(x,y)-min(x,y),n))) 55 | { 56 | prd=q; 57 | } 58 | x=f(x),y=f(f(y)); 59 | } 60 | return __gcd(prd,n); 61 | } 62 | void factor_rec(ul n, map &cnt) { 63 | if (n == 1) return; 64 | if (rabin_miller(n)) { 65 | ++cnt[n]; 66 | return; 67 | } 68 | ul u = pollard(n); 69 | factor_rec(u, cnt), factor_rec(n / u, cnt); 70 | } 71 | void calcDivisorsRec(ul cur, int i) { 72 | if (i >= primes.size()) { 73 | divisors.push_back(cur); 74 | return; 75 | } 76 | int r = cnt_primes[primes[i]]; 77 | for (int j = 0; j <= r; j++) { 78 | calcDivisorsRec(cur, i + 1); 79 | cur = cur * primes[i]; 80 | } 81 | } 82 | void calcDivisors(ul x) { 83 | cnt_primes.clear(); 84 | primes.clear(); 85 | divisors.clear(); 86 | factor_rec(x, cnt_primes); 87 | for (auto &u : cnt_primes) { 88 | primes.push_back(u.first); 89 | } 90 | calcDivisorsRec(1, 0); 91 | } 92 | }pollard; 93 | -------------------------------------------------------------------------------- /Templates/Math and Number Theory/Solve equation.cpp: -------------------------------------------------------------------------------- 1 | pair equ(ll a,ll b,ll c) 2 | { 3 | ll x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 *a); 4 | ll x2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a); 5 | return {x1,x2}; 6 | } 7 | -------------------------------------------------------------------------------- /Templates/Math and Number Theory/Xor Basis.cpp: -------------------------------------------------------------------------------- 1 | struct XorBasis{ 2 | int d,sz;vectorbasis; 3 | XorBasis(int n) 4 | { 5 | sz=0; 6 | d=n+1; 7 | basis=vector(d); 8 | } 9 | void insert(int mask) 10 | { 11 | for(int i=d-1;i>=0;i--) 12 | { 13 | if((mask>>i)&1) 14 | { 15 | if(!basis[i]) 16 | { 17 | basis[i]=mask; 18 | sz++; 19 | return; 20 | } 21 | mask^=basis[i]; 22 | } 23 | } 24 | } 25 | bool check(int mask) 26 | { 27 | for(int i=d-1;i>=0;i--) 28 | { 29 | if((mask>>i)&1) 30 | { 31 | if(!basis[i]) 32 | { 33 | return 1; 34 | } 35 | mask^=basis[i]; 36 | } 37 | } 38 | return 0; 39 | } 40 | }; 41 | -------------------------------------------------------------------------------- /Templates/Math and Number Theory/from decimal to any base.cpp: -------------------------------------------------------------------------------- 1 | double log(int n,int base) 2 | { 3 | return log(n)/log(base); 4 | } 5 | -------------------------------------------------------------------------------- /Templates/Math and Number Theory/int128.cpp: -------------------------------------------------------------------------------- 1 | __int128 read() { 2 | __int128 x = 0, f = 1; 3 | char ch = getchar(); 4 | while (ch < '0' || ch > '9') { 5 | if (ch == '-') f = -1; 6 | ch = getchar(); 7 | } 8 | while (ch >= '0' && ch <= '9') { 9 | x = x * 10 + ch - '0'; 10 | ch = getchar(); 11 | } 12 | return x * f; 13 | } 14 | void print(__int128 x) { 15 | if (x < 0) { 16 | putchar('-'); 17 | x = -x; 18 | } 19 | if (x > 9) print(x / 10); 20 | putchar(x % 10 + '0'); 21 | } 22 | bool cmp(__int128 x, __int128 y) { return x > y; } 23 | -------------------------------------------------------------------------------- /Templates/Math and Number Theory/is_perfect_square.cpp: -------------------------------------------------------------------------------- 1 | bool is_perfect_square(ll x) 2 | { 3 | ll y = (ll)sqrtl(x); 4 | while(y * y < x) y++; 5 | while(y * y > x) y--; 6 | return x == y * y; 7 | } 8 | -------------------------------------------------------------------------------- /Templates/Math and Number Theory/linear_sieve.cpp: -------------------------------------------------------------------------------- 1 | const int N=1e5+5; 2 | vectorisPrime(N,true);vectorp; 3 | void linear_sieve() 4 | { 5 | isPrime[0]=isPrime[1]=0; 6 | for(ll i=2;i=N) break; 12 | isPrime[i*p[o]]=0; 13 | if(i%p[o]==0) break; 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Templates/Math and Number Theory/phi.cpp: -------------------------------------------------------------------------------- 1 | const int N=5000006; 2 | ll phi[N]; 3 | void sieve() 4 | { 5 | for(int i=0;i1) ret-=ret/n; 34 | return ret; 35 | } 36 | -------------------------------------------------------------------------------- /Templates/Math and Number Theory/primality test for large numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | ll mulmod(ll a, ll b, ll m) 6 | { 7 | ll res = 0; 8 | while(b > 0) 9 | { 10 | if(b & 1) res = ((m-res) > a) ? res+a : res+a-m; 11 | b >>= 1; 12 | if (b) a = ((m-a) > a) ? a+a : a+a-m; 13 | } 14 | return res; 15 | } 16 | 17 | ll power(ll a, ll b, ll m) 18 | { 19 | if(b == 0) return 1; 20 | if(b & 1) return mulmod(a, power(a, b - 1, m), m); 21 | ll tmp = power(a, b / 2, m); 22 | return mulmod(tmp, tmp, m); 23 | } 24 | 25 | bool prime(ll n) 26 | { 27 | if(n<=1)return 0; 28 | for(int i=0;i<10;i++) 29 | { 30 | ll tmp = (rand() % (n - 1)) + 1; 31 | if(power(tmp, n - 1, n) != 1) 32 | return false; 33 | } 34 | return true; 35 | } 36 | signed main() 37 | { 38 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 39 | ll n;cin>>n; 40 | if(prime(n)) 41 | { 42 | cout<<"Prime"; 43 | } 44 | else 45 | { 46 | cout<<"Not Prime"; 47 | } 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Templates/RQ Data Structure/2dPrefixSum.cpp: -------------------------------------------------------------------------------- 1 | void _2d_prefix() 2 | { 3 | for(int i=1;i<=n;i++) 4 | { 5 | for(int o=1;o<=m;o++) 6 | { 7 | s[i][o]+=s[i][o-1]; 8 | } 9 | } 10 | for(int i=1;i<=n;i++) 11 | { 12 | for(int o=1;o<=m;o++) 13 | { 14 | s[i][o]+=s[i-1][o]; 15 | } 16 | } 17 | } 18 | int query(int l1,int r1,int l2,int r2) 19 | { 20 | int x=f[l2][r2]-f[l1-1][r2]; 21 | int y=f[l2][r1-1]-f[l1-1][r1-1]; 22 | return x-y; 23 | } 24 | -------------------------------------------------------------------------------- /Templates/RQ Data Structure/FenwickTree.cpp: -------------------------------------------------------------------------------- 1 | struct FenwickTree{ 2 | vectorbit;int sz; 3 | FenwickTree(int n) 4 | { 5 | sz=n+1; 6 | bit=vector(sz); 7 | } 8 | void update(int idx,ll val) 9 | { 10 | while(idx0) 20 | { 21 | ret+=bit[idx]; 22 | idx-=idx&-idx; 23 | } 24 | return ret; 25 | } 26 | ll prefix(int l,int r) 27 | { 28 | return query(r)-query(l-1); 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Templates/RQ Data Structure/MergeSortSegmentTree.cpp: -------------------------------------------------------------------------------- 1 | struct MergeSortSegmentTree{ 2 | private: 3 | vector>seg;int sz,skip=0; 4 | void merge(vector&ret,vector&a,vector&b) 5 | { 6 | int l=0,r=0; 7 | ret.reserve(a.size()+b.size()); 8 | while(l&s,int l,int r,int node) 23 | { 24 | if(l==r) 25 | { 26 | if(l>1; 33 | build(s,l,mid,2*node+1); 34 | build(s,mid+1,r,2*node+2); 35 | merge(seg[node],seg[2*node+1],seg[2*node+2]); 36 | } 37 | int query(int l,int r,int node,int lx,int rx,int k) 38 | { 39 | if(l>rx||r=lx&&r<=rx) 44 | { 45 | return lower_bound(seg[node].begin(),seg[node].end(),k)-seg[node].begin(); 46 | } 47 | int mid=l+r>>1; 48 | return query(l,mid,2*node+1,lx,rx,k)+query(mid+1,r,2*node+2,lx,rx,k); 49 | } 50 | public: 51 | MergeSortSegmentTree(vector&s){ 52 | sz=1; 53 | while(sz>(sz*2); 55 | build(s,0,sz-1,0); 56 | } 57 | int query(int l,int r,int val) 58 | { 59 | return query(0,sz-1,0,l,r,val); 60 | } 61 | }; 62 | -------------------------------------------------------------------------------- /Templates/RQ Data Structure/MoAlgorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | const int N=100005; 5 | const int Q=100005; 6 | const int SQ=317; 7 | struct query{ 8 | int l,r,q_idx,block_idx; 9 | query(){} 10 | query(int _l,int _r,int _q_idx) 11 | { 12 | l=_l-1,r=_r-1,q_idx=_q_idx,block_idx=_l/SQ; 13 | } 14 | bool operator<(const query&y)const{ 15 | if(block_idx!=y.block_idx) 16 | { 17 | return block_idxqueries[i].r)remove(r--); 40 | while(lqueries[i].l)add(--l); 42 | q_ans[queries[i].q_idx]=res; 43 | } 44 | } 45 | signed main() 46 | { 47 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 48 | cin>>n>>q; 49 | for(int i=0;i>s[i]; 52 | } 53 | for(int i=0;i>l>>r; 56 | queries[i]=query(l,r,i); 57 | } 58 | MO_process(); 59 | for(int i=0;i 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | int n,q;cin>>n>>q; 8 | ll s[n]={}; 9 | for(int i=0;i>s[i]; 10 | ll par[n+1]={}; 11 | while(q--) 12 | { 13 | ll l,r,val;cin>>l>>r>>val; 14 | --l,--r; 15 | par[l]+=val; 16 | par[r+1]-=val; 17 | } 18 | for(int i=1;ival; 32 | if(r) val+=r->val; 33 | } 34 | }; 35 | vector roots; 36 | int sz; 37 | node* set(int idx , int val , node *cur , int l , int r){ 38 | if(l == r) return new node(val); 39 | int md = l + (r-l) / 2; 40 | if(idx <= md) return new node(set(idx , val , cur->l , l , md) , cur->r); 41 | return new node(cur->l , set(idx , val , cur->r , md+1 , r)); 42 | } 43 | ll query(int l , int r , node *cur , int lx , int rx){ 44 | if(lx >= l && rx <= r) return cur->val; 45 | if(lx > r || rx < l) return 0; 46 | int md = lx + (rx-lx) / 2; 47 | return query(l , r , cur->l , lx , md) + query(l , r , cur->r , md+1 , rx); 48 | } 49 | }; 50 | -------------------------------------------------------------------------------- /Templates/RQ Data Structure/SegmentTree.cpp: -------------------------------------------------------------------------------- 1 | struct SegmentTree{ 2 | private: 3 | vectorseg;int sz,skip=INT_MAX; 4 | int merge(int a,int b) 5 | { 6 | return min(a,b); 7 | } 8 | void update(int l,int r,int node,int idx,int val) 9 | { 10 | if(l==r) 11 | { 12 | seg[node]=val; 13 | return; 14 | } 15 | int mid=l+r>>1; 16 | if(midrx||r=lx&&r<=rx)return seg[node]; 30 | int mid=l+r>>1; 31 | int a=query(l,mid,2*node+1,lx,rx); 32 | int b=query(mid+1,r,2*node+2,lx,rx); 33 | return merge(a,b); 34 | } 35 | public: 36 | SegmentTree(int n) 37 | { 38 | sz=1; 39 | while(sz<=n)sz<<=1; 40 | seg=vector(sz<<1,skip); 41 | } 42 | void update(int idx,int val) 43 | { 44 | update(0,sz-1,0,idx,val); 45 | } 46 | int query(int l,int r) 47 | { 48 | return query(0,sz-1,0,l,r); 49 | } 50 | }; 51 | -------------------------------------------------------------------------------- /Templates/RQ Data Structure/SegmentTreeBeats.cpp: -------------------------------------------------------------------------------- 1 | #define int long long 2 | struct SegmentTreeBeats{ 3 | private: 4 | #define L 2*node+1 5 | #define R 2*node+2 6 | #define mid (l+r>>1) 7 | #define inf 1e15+5 8 | struct Node{ 9 | int sum,mx,secMx,mxCnt,mn,mnCnt,secMn,lazyAdd,lazySet; 10 | Node(int summ,int mxx,int secMxx,int mxCntt,int mnn,int secMnn,int mnCntt,int add=0,int set=-1) 11 | :sum(summ),mx(mxx),secMx(secMxx),mxCnt(mxCntt), 12 | mn(mnn),secMn(secMnn),mnCnt(mnCntt), 13 | lazyAdd(add),lazySet(set){} 14 | }; 15 | int sz;vectorseg; 16 | Node merge(Node&a,Node&b) 17 | { 18 | Node ret(0,-inf,-inf,0,inf,inf,0,0,-1); 19 | ret.sum=a.sum+b.sum; 20 | ret.mx=max(a.mx,b.mx);// max 21 | ret.secMx=max(a.secMx,b.secMx); 22 | if(ret.mx==a.mx) ret.mxCnt+=a.mxCnt; 23 | else ret.secMx=max(ret.secMx,a.mx); 24 | if(ret.mx==b.mx) ret.mxCnt+=b.mxCnt; 25 | else ret.secMx=max(ret.secMx,b.mx); 26 | ret.mn=min(a.mn,b.mn);// min 27 | ret.secMn=min(a.secMn,b.secMn); 28 | if(ret.mn==a.mn) ret.mnCnt+=a.mnCnt; 29 | else ret.secMn=min(ret.secMn,a.mn); 30 | if(ret.mn==b.mn) ret.mnCnt+=b.mnCnt; 31 | else ret.secMn=min(ret.secMn,b.mn); 32 | return ret; 33 | } 34 | void pushSet(int l,int r,int node,int val) 35 | { 36 | seg[node]=Node(val*(r-l+1),val,-inf,r-l+1,val,inf,r-l+1,0,val); 37 | } 38 | void pushMin(int l,int r,int node,int mn) 39 | { 40 | if(seg[node].mn>=mn) 41 | { 42 | pushSet(l,r,node,mn); 43 | return; 44 | } 45 | if(seg[node].mx>mn) 46 | { 47 | if(seg[node].secMn==seg[node].mx) seg[node].secMn=mn; 48 | int val=seg[node].mx-mn; 49 | seg[node].sum-=val*seg[node].mxCnt; 50 | seg[node].mx=mn; 51 | } 52 | } 53 | void pushMax(int l,int r,int node,int mx) 54 | { 55 | if(seg[node].mx<=mx) 56 | { 57 | pushSet(l,r,node,mx); 58 | return; 59 | } 60 | if(seg[node].mn&v) 103 | { 104 | if(l==r) 105 | { 106 | if(lrx||r=lx&&r<=rx) 120 | { 121 | pushAdd(l,r,node,val); 122 | return; 123 | } 124 | propegate(l,r,node); 125 | updateAdd(l,mid,L,lx,rx,val); 126 | updateAdd(mid+1,r,R,lx,rx,val); 127 | seg[node]=merge(seg[L],seg[R]); 128 | } 129 | void updateSet(int l,int r,int node,int lx,int rx,int val) 130 | { 131 | if(l>rx||r=lx&&r<=rx) 133 | { 134 | pushSet(l,r,node,val); 135 | return; 136 | } 137 | propegate(l,r,node); 138 | updateSet(l,mid,L,lx,rx,val); 139 | updateSet(mid+1,r,R,lx,rx,val); 140 | seg[node]=merge(seg[L],seg[R]); 141 | } 142 | void updateMin(int l,int r,int node,int lx,int rx,int mn) 143 | { 144 | if(rrx||seg[node].mx<=mn) return; 145 | if(l>=lx&&r<=rx&&mn>seg[node].secMx) 146 | { 147 | pushMin(l,r,node,mn); 148 | return; 149 | } 150 | propegate(l,r,node); 151 | updateMin(l,mid,L,lx,rx,mn); 152 | updateMin(mid+1,r,R,lx,rx,mn); 153 | seg[node]=merge(seg[L],seg[R]); 154 | } 155 | void updateMax(int l,int r,int node,int lx,int rx,int mx) 156 | { 157 | if(l>rx||r=lx&&r<=rx&&mxrx||r=lx&&r<=rx)return seg[node].sum; 172 | propegate(l,r,node); 173 | return querySum(l,mid,L,lx,rx)+querySum(mid+1,r,R,lx,rx); 174 | } 175 | int queryMin(int l,int r,int node,int lx,int rx) 176 | { 177 | if(l>rx||r=lx&&r<=rx)return seg[node].mn; 179 | propegate(l,r,node); 180 | return min(queryMin(l,mid,L,lx,rx),queryMin(mid+1,r,R,lx,rx)); 181 | } 182 | int queryMax(int l,int r,int node,int lx,int rx) 183 | { 184 | if(l>rx||r=lx&&r<=rx)return seg[node].mx; 186 | propegate(l,r,node); 187 | return max(queryMax(l,mid,L,lx,rx),queryMax(mid+1,r,R,lx,rx)); 188 | } 189 | #undef L 190 | #undef R 191 | #undef mid 192 | public: 193 | SegmentTreeBeats(vector&v) 194 | { 195 | int n=v.size()+5; 196 | sz=1; 197 | while(sz<=n)sz<<=1; 198 | seg=vector(sz<<1,Node(0,-inf,-inf,0,inf,inf,0,0,-1)); 199 | build(0,sz-1,0,v); 200 | } 201 | void updateAdd(int l,int r,int val) 202 | { 203 | updateAdd(0,sz-1,0,l,r,val); 204 | } 205 | void updateSet(int l,int r,int val) 206 | { 207 | updateSet(0,sz-1,0,l,r,val); 208 | } 209 | void updateMin(int l,int r,int mn) 210 | { 211 | updateMin(0,sz-1,0,l,r,mn); 212 | } 213 | void updateMax(int l,int r,int mx) 214 | { 215 | updateMax(0,sz-1,0,l,r,mx); 216 | } 217 | int queryMax(int l,int r) 218 | { 219 | return queryMax(0,sz-1,0,l,r); 220 | } 221 | int queryMin(int l,int r) 222 | { 223 | return queryMin(0,sz-1,0,l,r); 224 | } 225 | int querySum(int l,int r) 226 | { 227 | return querySum(0,sz-1,0,l,r); 228 | } 229 | #undef inf 230 | }; 231 | -------------------------------------------------------------------------------- /Templates/RQ Data Structure/Sparse Table.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | const int N=2e5+5; 5 | ll n,q,s[N],T[N][22],Log[N]; 6 | ll merge(ll a,ll b) 7 | { 8 | return min(a,b); 9 | } 10 | void build() 11 | { 12 | for(int i=0;i>i)&1) 35 | { 36 | return merge(query(l+(1<>1]+1; 53 | } 54 | } 55 | signed main() 56 | { 57 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 58 | preCount(); 59 | cin>>n>>q; 60 | for(int i=0;i>s[i]; 63 | } 64 | build(); 65 | while(q--) 66 | { 67 | int l,r;cin>>l>>r; 68 | cout<c={0,0}; 7 | Node(){} 8 | Node(char k){ 9 | key=k; 10 | } 11 | }; 12 | Node*root=0; 13 | int getSize(Node*t) 14 | { 15 | return t?t->sz:0; 16 | } 17 | Treap(string&s) 18 | { 19 | for(int i=0;isz=getSize(t->c[0])+getSize(t->c[1])+1; 27 | return t; 28 | } 29 | void propegate(Node*t) 30 | { 31 | if(!t||!t->rev) return; 32 | swap(t->c[0],t->c[1]); 33 | if(t->c[0]) t->c[0]->rev^=1; 34 | if(t->c[1]) t->c[1]->rev^=1; 35 | t->rev=0; 36 | } 37 | arraysplit(Node*t,int k) 38 | { 39 | if(!t) return {0,0}; 40 | propegate(t); 41 | if(getSize(t->c[0])>=k) 42 | { 43 | auto ret=split(t->c[0],k); 44 | t->c[0]=ret[1]; 45 | return {ret[0],fix(t)}; 46 | } 47 | else 48 | { 49 | auto ret=split(t->c[1],k-getSize(t->c[0])-1); 50 | t->c[1]=ret[0]; 51 | return {fix(t),ret[1]}; 52 | } 53 | } 54 | Node*merge(Node*u,Node*v) 55 | { 56 | propegate(u); 57 | propegate(v); 58 | if(!u||!v) return u?u:v; 59 | if(u->pri>v->pri) 60 | { 61 | u->c[1]=merge(u->c[1],v); 62 | return fix(u); 63 | } 64 | else 65 | { 66 | v->c[0]=merge(u,v->c[0]); 67 | return fix(v); 68 | } 69 | } 70 | void reverse(int l,int r) 71 | { 72 | auto a=split(root,l-1); 73 | auto b=split(a[1],r-l+1); 74 | b[0]->rev^=1; 75 | root=merge(a[0],merge(b[0],b[1])); 76 | } 77 | char getChar(int idx) 78 | { 79 | auto a=split(root,idx-1); 80 | auto b=split(a[1],1); 81 | propegate(b[0]); 82 | char ret=b[0]->key; 83 | merge(a[0],merge(b[0],b[1])); 84 | return ret; 85 | } 86 | void print(Node*t) 87 | { 88 | if(!t) return; 89 | print(t->c[0]); 90 | cout<key<<' '; 91 | print(t->c[1]); 92 | } 93 | }; 94 | -------------------------------------------------------------------------------- /Templates/RQ Data Structure/Treap.cpp: -------------------------------------------------------------------------------- 1 | std::mt19937_64 rnd(std::chrono::system_clock::now().time_since_epoch().count()); 2 | struct Treap{ 3 | struct Node{ 4 | int key=0,val=0,pri=rnd(),sz=1; 5 | arrayc={0,0}; 6 | Node(){} 7 | Node(int k,int x=0){ 8 | key=k; 9 | val=x; 10 | } 11 | }; 12 | Node*root=0; 13 | int getSize(Node*t) 14 | { 15 | return t?t->sz:0; 16 | } 17 | Treap(int n) 18 | { 19 | for(int i=1;i<=n;i++) 20 | { 21 | root=merge(root,new Node(i)); 22 | } 23 | } 24 | Node*fix(Node*t) 25 | { 26 | t->sz=getSize(t->c[0])+getSize(t->c[1])+1; 27 | return t; 28 | } 29 | arraysplit(Node*t,int k) 30 | { 31 | if(!t) return {0,0}; 32 | if(getSize(t->c[0])>=k) 33 | { 34 | auto ret=split(t->c[0],k); 35 | t->c[0]=ret[1]; 36 | return {ret[0],fix(t)}; 37 | } 38 | else 39 | { 40 | auto ret=split(t->c[1],k-getSize(t->c[0])-1); 41 | t->c[1]=ret[0]; 42 | return {fix(t),ret[1]}; 43 | } 44 | } 45 | Node*merge(Node*u,Node*v) 46 | { 47 | if(!u||!v) return u?u:v; 48 | if(u->pri>v->pri) 49 | { 50 | u->c[1]=merge(u->c[1],v); 51 | return fix(u); 52 | } 53 | else 54 | { 55 | v->c[0]=merge(u,v->c[0]); 56 | return fix(v); 57 | } 58 | } 59 | void print(Node*t) 60 | { 61 | if(!t) return; 62 | print(t->c[0]); 63 | cout<key<<' '; 64 | print(t->c[1]); 65 | } 66 | }; 67 | -------------------------------------------------------------------------------- /Templates/RQ Data Structure/coordinate compression(one array).cpp: -------------------------------------------------------------------------------- 1 | void compress(vector&a,int start) 2 | { 3 | int n=a.size(); 4 | vector>pairs(n); 5 | for(int i=0;i0&&pairs[i-1].first!=pairs[i].first) 14 | { 15 | nxt++; 16 | } 17 | a[pairs[i].second]=nxt; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Templates/RQ Data Structure/coordinateCopmression.cpp: -------------------------------------------------------------------------------- 1 | struct coordinateCopmression{ 2 | private: 3 | vectorinit; 4 | void compress(vector&v) 5 | { 6 | sort(v.begin(),v.end()); 7 | v.erase(unique(v.begin(),v.end()),v.end()); 8 | } 9 | public: 10 | coordinateCopmression(vector&v) 11 | { 12 | init=v; 13 | compress(init); 14 | } 15 | int index(ll val) 16 | { 17 | return lower_bound(init.begin(),init.end(),val)-init.begin(); 18 | } 19 | ll initVal(int idx) 20 | { 21 | return init[idx]; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Templates/RQ Data Structure/partial sum 2d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | signed main() 5 | { 6 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 7 | int n,m,q;cin>>n>>m>>q; 8 | int s[n+5][m+5]={}; 9 | while(q--) 10 | { 11 | int a,b,x,y,k;cin>>a>>b>>x>>y>>k; 12 | s[a][b]+=k; 13 | s[a][y+1]-=k; 14 | s[x+1][b]-=k; 15 | s[x+1][y+1]+=k; 16 | } 17 | for(int i=1;i<=n;i++) 18 | { 19 | for(int o=1;o<=m;o++) 20 | { 21 | s[i][o]=s[i-1][o]+s[i][o-1]-s[i-1][o-1]+s[i][o]; 22 | } 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Templates/Some DSA/LCS (Nlog(N)).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | struct FenwickTree{ 5 | int N;vectorbit; 6 | FenwickTree(int n,int init=0) 7 | { 8 | N=n+1; 9 | bit=vector(n+1,init); 10 | } 11 | void update(int idx,int val) 12 | { 13 | while(idx0) 23 | { 24 | ret=max(ret,bit[idx]); 25 | idx-=idx&-idx; 26 | } 27 | return ret; 28 | } 29 | }; 30 | signed main() 31 | { 32 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 33 | int n;cin>>n; 34 | int pos[n+1]; 35 | for(int i=1;i<=n;i++) 36 | { 37 | int x;cin>>x; 38 | pos[x]=i; 39 | } 40 | pairs[n]; 41 | for(int i=0;i>s[i].first; 44 | s[i].second=i+1; 45 | } 46 | sort(s,s+n,[&](pair&i,pair&j){ 47 | return pos[i.first] ans; 4 | ans.push_back(nums[0]); 5 | for (int i = 1; i < n; i++) { 6 | if (nums[i] > ans.back()) { 7 | ans.push_back(nums[i]); 8 | } 9 | else { 10 | int low = lower_bound(ans.begin(), ans.end(),nums[i])- ans.begin(); 11 | ans[low] = nums[i]; 12 | } 13 | } 14 | return ans.size(); 15 | } 16 | -------------------------------------------------------------------------------- /Templates/Some DSA/PermutaionHashing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | const int N=2e5+5; 5 | std::mt19937_64 rng(std::chrono::system_clock::now().time_since_epoch().count()); 6 | ll rand(ll l,ll r) 7 | { 8 | return uniform_int_distribution(l, r)(rng); 9 | } 10 | struct node{ 11 | ll a,b,c,d,e; 12 | node():a(0),b(0),c(0),d(0),e(0){} 13 | node(ll x,ll xx,ll xxx,ll xxxx,ll xxxxx):a(x),b(xx),c(xxx),d(xxxx),e(xxxxx){} 14 | node operator+(const node&x) 15 | { 16 | node w; 17 | w.a=a+x.a,w.b=b+x.b,w.c=c+x.c,w.d=d+x.d,w.e=e+x.e; 18 | return w; 19 | } 20 | bool operator==(const node&x) 21 | { 22 | return a==x.a&&b==x.b&&c==x.c&&d==x.d&&e==x.e; 23 | } 24 | void operator+=(const node&x) 25 | { 26 | a+=x.a,b+=x.b,c+=x.c,d+=x.d,e+=x.e; 27 | } 28 | node operator=(const node&x) 29 | { 30 | a=x.a,b=x.b,c=x.c,d=x.d,e=x.e; 31 | return node(a,b,c,d,e); 32 | } 33 | }; 34 | node f[N],per[N]; 35 | void init() 36 | { 37 | for(int i=1;i>t;while(t--) 45 | { 46 | int n;cin>>n; 47 | ll s[n+1]={}; 48 | for(int i=1;i<=n;i++) cin>>s[i]; 49 | node pre[n+5],suf[n+5]; 50 | for(int i=1;i<=n;i++) pre[i]=suf[i]=f[s[i]]; 51 | for(int i=2;i<=n;i++) pre[i]+=pre[i-1]; 52 | for(int i=n-1;i>=1;i--) suf[i]+=suf[i+1]; 53 | } 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Templates/Some DSA/Random.cpp: -------------------------------------------------------------------------------- 1 | std::mt19937_64 rng(std::chrono::system_clock::now().time_since_epoch().count()); 2 | ll rand(ll l,ll r) 3 | { 4 | return uniform_int_distribution(l, r)(rng); 5 | } 6 | -------------------------------------------------------------------------------- /Templates/Some DSA/SOS dp.cpp: -------------------------------------------------------------------------------- 1 | struct SOS{ 2 | vectordp;int MXN,Log; 3 | SOS(int n,vector&v) 4 | { 5 | Log=n; 6 | MXN=1<(MXN); 8 | dp=v; 9 | } 10 | void forward1(){ // adding element to all its super set 11 | for(int bit=0;bit=0;i--)if(i&(1<=0;i--)if(i&(1<sub; 2 | for(int mask=0;mask<(1< 2 | #include 3 | #include 4 | using namespace std; 5 | using namespace __gnu_pbds; 6 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update>// set 7 | 8 | typedef tree, rb_tree_tag,tree_order_statistics_node_update> ordered_multiset; 9 | #define ll long long 10 | void myerase(ordered_set &t, int v){ 11 | int rank = t.order_of_key(v);//Number of elements that are less than v in t 12 | ordered_set::iterator it = t.find_by_order(rank); //Iterator that points to the (rank+1)th element in t 13 | t.erase(it); 14 | } 15 | int main() 16 | { 17 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 18 | ordered_multiset st; 19 | int n; 20 | cin >> n; 21 | long long sum = 0; 22 | for (int i = 0 ; i < n;i++) 23 | { 24 | int x; 25 | cin >> x; 26 | st.insert(x); 27 | sum += st.order_of_key(x); 28 | } 29 | cout << sum << endl; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Templates/Strings/Hashing (substring).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | const int N=1e6+5,base1=127,base2=131,mod1=1e9+7,mod2=2e9+11; 5 | int n,q,pw1[N],pw2[N];pairpre[N],suf[N]; 6 | void init() 7 | { 8 | pw1[0]=pw2[0]=1; 9 | for(int i=1;iarr[]) 16 | { 17 | int a=0,b=0; 18 | for(int i=0;iget(int l,int r,pairarr[]) 28 | { 29 | auto ret=arr[r]; 30 | int sz=r-l+1; 31 | --l; 32 | if(l>=0) 33 | { 34 | ret.first-=(1LL*arr[l].first*pw1[sz])%mod1; 35 | if(ret.first<0) ret.first+=mod1; 36 | ret.second-=(1LL*arr[l].second*pw2[sz])%mod2; 37 | if(ret.second<0) ret.second+=mod2; 38 | } 39 | return ret; 40 | } 41 | bool pal(int l,int r) 42 | { 43 | return get(l,r,pre)==get(n-r-1,n-l-1,suf); 44 | } 45 | signed main() 46 | { 47 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 48 | init(); 49 | string s;cin>>s; 50 | n=s.size(); 51 | Hash(s,pre); 52 | reverse(s.begin(),s.end()); 53 | Hash(s,suf); 54 | reverse(s.begin(),s.end()); 55 | if(pal(0,n-1)) 56 | { 57 | cout<<"YES\n"; 58 | } 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Templates/Strings/Hashing Treap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define int long long 5 | const int N=1e6+5,mod=1e9+7; 6 | ll base=31,pw[N+1]; 7 | void init() 8 | { 9 | pw[0]=1; 10 | for(int i=1;ic={0,0}; 21 | Node(){} 22 | Node(int k){ 23 | val=h=k; 24 | } 25 | }; 26 | Node*root=0; 27 | int getSize(Node*t) 28 | { 29 | return t?t->sz:0; 30 | } 31 | int getHash(Node*t) 32 | { 33 | return t?t->h:0; 34 | } 35 | Treap(string&s) 36 | { 37 | for(int i=0;isz=getSize(t->c[0])+getSize(t->c[1])+1; 45 | t->h=(((getHash(t->c[0])*base+t->val)%mod)*pw[getSize(t->c[1])]+getHash(t->c[1]))%mod; 46 | return t; 47 | } 48 | arraysplit(Node*t,int k) 49 | { 50 | if(!t) return {0,0}; 51 | if(getSize(t->c[0])>=k) 52 | { 53 | auto ret=split(t->c[0],k); 54 | t->c[0]=ret[1]; 55 | return {ret[0],fix(t)}; 56 | } 57 | else 58 | { 59 | auto ret=split(t->c[1],k-getSize(t->c[0])-1); 60 | t->c[1]=ret[0]; 61 | return {fix(t),ret[1]}; 62 | } 63 | } 64 | Node*merge(Node*u,Node*v) 65 | { 66 | if(!u||!v) return u?u:v; 67 | if(u->pri>v->pri) 68 | { 69 | u->c[1]=merge(u->c[1],v); 70 | return fix(u); 71 | } 72 | else 73 | { 74 | v->c[0]=merge(u,v->c[0]); 75 | return fix(v); 76 | } 77 | } 78 | void deleteRange(int l,int r) 79 | { 80 | auto a=split(root,l-1); 81 | auto b=split(a[1],r-l+1); 82 | root=merge(a[0],b[1]); 83 | } 84 | int getHash(int l,int r) 85 | { 86 | auto a=split(root,l-1); 87 | auto b=split(a[1],r-l+1); 88 | int ret=b[0]->h; 89 | root=merge(a[0],merge(b[0],b[1])); 90 | return ret; 91 | } 92 | void addChar(int pos,char c) 93 | { 94 | auto a=split(root,pos-1); 95 | root=merge(merge(a[0],new Node(c-'a'+1)),a[1]); 96 | } 97 | void print(Node*t) 98 | { 99 | if(!t) return; 100 | print(t->c[0]); 101 | cout<h<<' '; 102 | print(t->c[1]); 103 | } 104 | }; 105 | int rev(int i,int n) 106 | { 107 | return n-i+1; 108 | } 109 | signed main() 110 | { 111 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 112 | init(); 113 | int n,q;cin>>n>>q; 114 | string s;cin>>s; 115 | string f=s; 116 | reverse(f.begin(),f.end()); 117 | Treap a(s),b(f); 118 | while(q--) 119 | { 120 | int op;cin>>op; 121 | if(op==1) 122 | { 123 | int l,r;cin>>l>>r; 124 | int l2=rev(l,n),r2=rev(r,n); 125 | n-=(r-l+1); 126 | a.deleteRange(l,r); 127 | b.deleteRange(r2,l2); 128 | } 129 | else if(op==2) 130 | { 131 | n++; 132 | int pos;char c;cin>>c>>pos; 133 | a.addChar(pos,c); 134 | b.addChar(rev(pos,n),c); 135 | } 136 | else 137 | { 138 | int l,r;cin>>l>>r; 139 | int l2=rev(l,n),r2=rev(r,n); 140 | if(a.getHash(l,r)==b.getHash(r2,l2)) 141 | { 142 | cout<<"yes\n"; 143 | } 144 | else 145 | { 146 | cout<<"no\n"; 147 | } 148 | } 149 | } 150 | return 0; 151 | } 152 | -------------------------------------------------------------------------------- /Templates/Strings/Hashing.cpp: -------------------------------------------------------------------------------- 1 | struct Hashing{ 2 | private: 3 | int mod1=1e9+7,mod2=2e9+11; 4 | ll base1,base2,h1,h2,inv1,inv2,*pw1,*pw2,len; 5 | dequed; 6 | ll power(ll a,ll b,ll m) 7 | { 8 | ll ans=1; 9 | while(b>0) 10 | { 11 | if(b&1) 12 | { 13 | ans=(ans*a)%m; 14 | } 15 | a=(a*a)%m; 16 | b>>=1; 17 | } 18 | return ans; 19 | } 20 | public: 21 | Hashing(int sz,ll x=31,ll y=37){ 22 | base1=x; 23 | base2=y; 24 | h1=h2=len=0; 25 | inv1=power(x,mod1-2,mod1); 26 | inv2=power(y,mod2-2,mod2); 27 | pw1=new ll[sz+1]; 28 | pw2=new ll[sz+1]; 29 | pw1[0]=pw2[0]=1; 30 | for(int i=1;i<=sz;i++) 31 | { 32 | pw1[i]=(x*pw1[i-1])%mod1; 33 | pw2[i]=(y*pw2[i-1])%mod2; 34 | } 35 | } 36 | void push_back(char x) 37 | { 38 | x=x-'a'+1; 39 | h1=(h1*base1)%mod1; 40 | h1=(h1+x)%mod1; 41 | h2=(h2*base2)%mod2; 42 | h2=(h2+x)%mod2; 43 | len++; 44 | d.emplace_back(x); 45 | } 46 | void push_front(char x) 47 | { 48 | x=x-'a'+1; 49 | h1=(h1+(x*pw1[len])%mod1)%mod1; 50 | h2=(h2+(x*pw2[len])%mod2)%mod2; 51 | len++; 52 | d.emplace_front(x); 53 | } 54 | void pop_back() 55 | { 56 | if(len==0)return; 57 | char x=d.back(); 58 | d.pop_back(); 59 | h1=(h1-x+mod1)%mod1; 60 | h1=(h1*inv1)%mod1; 61 | h2=(h2-x+mod2)%mod2; 62 | h2=(h2*inv2)%mod2; 63 | len--; 64 | } 65 | void pop_front() 66 | { 67 | if(len==0)return; 68 | char x=d.front(); 69 | d.pop_front(); 70 | len--; 71 | h1=((h1-x*pw1[len]%mod1)+mod1)%mod1; 72 | h2=((h2-x*pw2[len]%mod2)+mod2)%mod2; 73 | } 74 | void clear() 75 | { 76 | h1=h2=len=0; 77 | d.clear(); 78 | } 79 | bool operator==(const Hashing &H)const{ 80 | return H.h1==h1&&H.h2==h2; 81 | } 82 | string GetString() 83 | { 84 | return string(d.begin(),d.end()); 85 | } 86 | pairGetHash() 87 | { 88 | return {h1,h2}; 89 | } 90 | }; 91 | -------------------------------------------------------------------------------- /Templates/Strings/HashingSegmentTree.cpp: -------------------------------------------------------------------------------- 1 | #define int long long 2 | const int N=1e5+5,mod1=1e9+7,mod2=2e9+11; 3 | ll base1=31,base2=37,pw1[N+1],pw2[N+1],inv1[N+1],inv2[N+1]; 4 | ll powmod(ll a,ll b,ll m) 5 | { 6 | ll ans=1; 7 | while(b>0) 8 | { 9 | if(b&1) 10 | { 11 | ans=(ans*a)%m; 12 | } 13 | a=(a*a)%m; 14 | b>>=1; 15 | } 16 | return ans; 17 | } 18 | void init() 19 | { 20 | pw1[0]=pw2[0]=inv1[0]=inv2[0]=1; 21 | int temp1=powmod(base1,mod1-2,mod1); 22 | int temp2=powmod(base2,mod2-2,mod2); 23 | for(int i=1;i>seg;int sz; 34 | pairmerge(pairl,pairr) 35 | { 36 | pairret=l; 37 | ret.first=(ret.first+r.first)%mod1; 38 | ret.second=(ret.second+r.second)%mod2; 39 | return ret; 40 | } 41 | void update(int l,int r,int node,int idx,int ch) 42 | { 43 | if(l==r) 44 | { 45 | seg[node]={(ch*pw1[idx])%mod1,(ch*pw2[idx])%mod2}; 46 | return; 47 | } 48 | int mid=l+r>>1; 49 | if(idx<=mid)update(l,mid,2*node+1,idx,ch); 50 | else update(mid+1,r,2*node+2,idx,ch); 51 | seg[node]=merge(seg[2*node+1],seg[2*node+2]); 52 | } 53 | pairquery(int l,int r,int node,int lx,int rx) 54 | { 55 | if(l>=lx&&r<=rx) 56 | { 57 | return seg[node]; 58 | } 59 | if(l>rx||r>1; 61 | pairlft=query(l,mid,2*node+1,lx,rx); 62 | pairrgt=query(mid+1,r,2*node+2,lx,rx); 63 | return merge(lft,rgt); 64 | } 65 | public: 66 | HashingSegmentTree(int n) 67 | { 68 | sz=1; 69 | while(sz<=n)sz*=2; 70 | seg=vector>(sz*2); 71 | } 72 | void update(int idx,char ch) 73 | { 74 | update(0,sz-1,0,idx,ch-'a'+1); 75 | } 76 | pairquery(int l,int r) 77 | { 78 | pairret=query(0,sz-1,0,l,r); 79 | ret.first=(ret.first*inv1[l-1])%mod1; 80 | ret.second=(ret.second*inv2[l-1])%mod2; 81 | return ret; 82 | } 83 | }; 84 | bool isPalindrome(HashingSegmentTree &a,HashingSegmentTree &b,int &l,int &r,int &n) 85 | { 86 | return (a.query(l,r)==b.query(n-r+1,n-l+1)); 87 | } 88 | -------------------------------------------------------------------------------- /Templates/Strings/Kmp.cpp: -------------------------------------------------------------------------------- 1 | const int N=1e5+5; 2 | struct KMP{ 3 | int longestPrefix[N]={};vectorans; 4 | void calcPrefix(string patern) 5 | { 6 | int n=patern.size(); 7 | for(int i=1,idx=0;i0&&patern[idx]!=patern[i])idx=longestPrefix[idx-1]; 10 | if(patern[i]==patern[idx])idx++; 11 | longestPrefix[i]=idx; 12 | } 13 | } 14 | void kmp(string s,string pat) 15 | { 16 | int n=s.size(),m=pat.size(); 17 | calcPrefix(pat); 18 | for(int i=0,idx=0;i0&&s[i]!=pat[idx])idx=longestPrefix[idx-1]; 21 | if(s[i]==pat[idx])idx++; 22 | if(idx==m)ans.push_back(i-m+1),idx=longestPrefix[idx-1]; 23 | } 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Templates/Strings/PalindromicTree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | struct PalindromicTree 5 | { 6 | struct node { 7 | int nxt[26],len,st, en,link,cnt,oc; 8 | }; 9 | string s;vectort; 10 | int sz,last; 11 | PalindromicTree(){} 12 | PalindromicTree(string _s){ 13 | s=_s; 14 | int n=s.size(); 15 | t.clear(); 16 | t.resize(n + 9); 17 | sz=2,last=2; 18 | t[1].len=-1,t[1].link=1; 19 | t[2].len=0,t[2].link=1; 20 | } 21 | int add(int pos) 22 | { 23 | int cur=last,curlen=0; 24 | int ch=s[pos]-'a'; 25 | while(1) 26 | { 27 | curlen=t[cur].len; 28 | if(pos-1-curlen>=0&&s[pos-1-curlen]==s[pos]) 29 | { 30 | break; 31 | } 32 | cur=t[cur].link; 33 | } 34 | if(t[cur].nxt[ch]) 35 | { 36 | last=t[cur].nxt[ch]; 37 | t[last].oc++; 38 | return 0; 39 | } 40 | sz++; 41 | last=sz; 42 | t[sz].oc=1; 43 | t[sz].len=t[cur].len+2; 44 | t[cur].nxt[ch]=sz; 45 | t[sz].en=pos; 46 | t[sz].st=pos-t[sz].len+1; 47 | if(t[sz].len==1) 48 | { 49 | t[sz].link=2; 50 | t[sz].cnt=1; 51 | return 1; 52 | } 53 | while(1) 54 | { 55 | cur=t[cur].link; 56 | curlen=t[cur].len; 57 | if(pos-1-curlen>=0&&s[pos-1-curlen]==s[pos]) 58 | { 59 | t[sz].link=t[cur].nxt[ch]; 60 | break; 61 | } 62 | } 63 | t[sz].cnt=1+t[t[sz].link].cnt; 64 | return 1; 65 | } 66 | void calc_occurrences() 67 | { 68 | for(int i=sz;i>=3;i--) 69 | { 70 | t[t[i].link].oc+=t[i].oc; 71 | } 72 | } 73 | string Max() 74 | { 75 | int len=0,a=0,b=0; 76 | for(int i=sz;i>=3;i--) 77 | { 78 | if(t[i].en-t[i].st+1>len) 79 | { 80 | len=t[i].en-t[i].st+1; 81 | a=t[i].st,b=t[i].en; 82 | } 83 | } 84 | return s.substr(a,len); 85 | } 86 | }; 87 | signed main() 88 | { 89 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 90 | string s;cin>>s; 91 | PalindromicTree tree(s); 92 | for(int i=0;i&p,vector&c) 2 | { 3 | int n=p.size(); 4 | vectorcnt(n); 5 | for(auto it:c) cnt[it]++; 6 | vectorp_new(n),pos(n); 7 | pos[0]=0; 8 | for(int i=1;i,vector>SuffixArray_LCP(string &s) 18 | { 19 | s+='$'; 20 | int n=s.size(); 21 | vector>suf(n); 22 | for(int i=0;ip(n),c(n); 25 | for(int i=0;ic_new(n); 38 | c_new[p[0]]=0; 39 | for(int i=1;ilast={c[p[i-1]],c[(p[i-1]+(1<cur={c[p[i]],c[(p[i]+(1<lcp(n); 50 | k=0; 51 | for(int i=0;i&p,vector&c) 2 | { 3 | int n=p.size(); 4 | vectorcnt(n); 5 | for(auto it:c) cnt[it]++; 6 | vectorp_new(n),pos(n); 7 | pos[0]=0; 8 | for(int i=1;iSuffixArray(string &s) 18 | { 19 | s+='$'; 20 | int n=s.size(); 21 | vector>suf(n); 22 | for(int i=0;ip(n),c(n); 25 | for(int i=0;ic_new(n); 38 | c_new[p[0]]=0; 39 | for(int i=1;ilast={c[p[i-1]],c[(p[i-1]+(1<cur={c[p[i]],c[(p[i]+(1<child[idx]==0) 18 | { 19 | cur->child[idx]=new Node(); 20 | } 21 | cur=cur->child[idx]; 22 | cur->Prefix++; 23 | } 24 | cur->IsEnd++; 25 | } 26 | bool SearchWord(string &s) 27 | { 28 | Node*cur=root; 29 | for(auto it:s) 30 | { 31 | int idx=it-'a'; 32 | if(cur->child[idx]==0)return 0; 33 | cur=cur->child[idx]; 34 | } 35 | return cur->IsEnd; 36 | } 37 | int CountWord(string &s) 38 | { 39 | Node*cur=root; 40 | for(auto it:s) 41 | { 42 | int idx=it-'a'; 43 | if(cur->child[idx]==0)return 0; 44 | cur=cur->child[idx]; 45 | } 46 | return cur->IsEnd; 47 | } 48 | int CountPrefix(string &s) 49 | { 50 | Node*cur=root; 51 | for(auto it:s) 52 | { 53 | int idx=it-'a'; 54 | if(cur->child[idx]==0)return 0; 55 | cur=cur->child[idx]; 56 | } 57 | return cur->Prefix; 58 | } 59 | }; 60 | -------------------------------------------------------------------------------- /Templates/Strings/Z algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | vectorZ; 5 | void build(string&s) 6 | { 7 | Z=vector(s.size()); 8 | int l=0,r=0; 9 | for(int i=1;i=r||i+Z[i-l]>=r) 12 | { 13 | l=i; 14 | r=max(r,i); 15 | while(r>s; 31 | build(s); 32 | // your sol here 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Templates/Strings/get_words.cpp: -------------------------------------------------------------------------------- 1 | vector get_words(string str) 2 | { 3 | vector ret; 4 | string word; 5 | stringstream ss(str); 6 | while (ss >> word) { 7 | ret.push_back(word); 8 | } 9 | return ret; 10 | } 11 | -------------------------------------------------------------------------------- /Templates/Trees/Centroid Decomposition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define int long long 5 | const int N=2e5+5,Log=23; 6 | int n,q,anc[N][Log],sz[N],level[N],parent[N],best[N];vectoradj[N];bool vis[N]; 7 | void BuildAncestors(int node,int par) 8 | { 9 | level[node]=level[par]+1; 10 | anc[node][0]=par; 11 | for(int o=1;o=0;o--) 25 | { 26 | if(k&(1<=0;i--) 39 | { 40 | if(anc[u][i]!=anc[v][i]) 41 | { 42 | u=anc[u][i]; 43 | v=anc[v][i]; 44 | } 45 | } 46 | return anc[u][0]; 47 | } 48 | void dfs(int node,int par) 49 | { 50 | if(vis[node])return; 51 | sz[node]=1; 52 | for(auto it:adj[node]) 53 | { 54 | if(it!=par&&!vis[it]) 55 | { 56 | dfs(it,node); 57 | sz[node]+=sz[it]; 58 | } 59 | } 60 | } 61 | int findCentriod(int node,int par,int size) 62 | { 63 | for(auto it:adj[node]) 64 | { 65 | if(it!=par&&!vis[it]&&sz[it]>size/2) return findCentriod(it,node,size); 66 | } 67 | return node; 68 | } 69 | void initCentroid(int node,int par) 70 | { 71 | dfs(node,0); 72 | node=findCentriod(node,0,sz[node]); 73 | vis[node]=1; 74 | parent[node]=par; 75 | for(auto it:adj[node]) 76 | { 77 | if(!vis[it]) 78 | { 79 | initCentroid(it,node); 80 | } 81 | } 82 | } 83 | int dis(int a,int b) 84 | { 85 | int lc=LCA(a,b); 86 | return level[a]+level[b]-2*level[lc]; 87 | } 88 | void update(int node) 89 | { 90 | best[node]=0; 91 | int u=node; 92 | while(parent[u]!=0) 93 | { 94 | u=parent[u]; 95 | best[u]=min(best[u],dis(u,node)); 96 | } 97 | } 98 | int query(int node) 99 | { 100 | int ret=best[node]; 101 | int u=node; 102 | while(parent[u]!=0) 103 | { 104 | u=parent[u]; 105 | ret=min(ret,best[u]+dis(u,node)); 106 | } 107 | return ret; 108 | } 109 | signed main() 110 | { 111 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 112 | cin>>n>>q; 113 | for(int i=1;i>a>>b; 115 | adj[a].push_back(b); 116 | adj[b].push_back(a); 117 | } 118 | for(auto&it:best) it=2e5; 119 | BuildAncestors(1,0); 120 | initCentroid(1,0); 121 | update(1); 122 | while(q--) 123 | { 124 | int op,node;cin>>op>>node; 125 | if(op==1) 126 | { 127 | update(node); 128 | } 129 | else 130 | { 131 | cout< 2 | using namespace std; 3 | #define ll long long 4 | const int N=200005; 5 | int n,c[N],sz[N],parent[N],p[N],lvl[N],ans[N];setst[N];vectoradj[N]; 6 | void dfs(int node,int par,int level) 7 | { 8 | lvl[node]=level; 9 | p[node]=par; 10 | for(auto it:adj[node]) 11 | { 12 | if(it!=par) 13 | { 14 | dfs(it,node,level+1); 15 | } 16 | } 17 | } 18 | int find(int node) 19 | { 20 | if(parent[node]==node) return node; 21 | return parent[node]=find(parent[node]); 22 | } 23 | void init() 24 | { 25 | for(int i=1;i<=n;i++) 26 | { 27 | parent[i]=i; 28 | sz[i]=1; 29 | st[i].insert(c[i]); 30 | } 31 | } 32 | void merge(int a,int b) 33 | { 34 | a=find(a); 35 | b=find(b); 36 | if(a==b) return; 37 | if(sz[b]>sz[a]) swap(a,b); 38 | sz[a]+=sz[b]; 39 | for(auto it:st[b]) st[a].insert(it); 40 | parent[b]=a; 41 | st[b].clear(); 42 | } 43 | bool com(int a,int b) 44 | { 45 | return lvl[a]>lvl[b]; 46 | } 47 | signed main() 48 | { 49 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 50 | cin>>n; 51 | for(int i=1;i<=n;i++) cin>>c[i]; 52 | for(int i=1;i>a>>b; 55 | adj[a].push_back(b); 56 | adj[b].push_back(a); 57 | } 58 | dfs(1,0,0); 59 | init(); 60 | int node[n]; 61 | iota(node,node+n,1); 62 | sort(node,node+n,com); 63 | for(int i=0;i 2 | using namespace std; 3 | #define ll long long 4 | const int N=200005; 5 | int n,c[N],sz[N],ans[N];vectoradj[N],subTree[N];mapm; 6 | void dfs(int node,int par) 7 | { 8 | sz[node]=1; 9 | for(auto it:adj[node]) 10 | { 11 | if(it!=par) 12 | { 13 | dfs(it,node); 14 | sz[node]+=sz[it]; 15 | } 16 | } 17 | } 18 | void sack(int node,int par,bool keep) 19 | { 20 | // find big child 21 | int mx=-1,bigCh=-1; 22 | for(auto it:adj[node]) 23 | { 24 | if(it!=par&&mx>n; 73 | for(int i=1;i<=n;i++) cin>>c[i]; 74 | for(int i=1;i>a>>b; 77 | adj[a].push_back(b); 78 | adj[b].push_back(a); 79 | } 80 | dfs(1,1); 81 | sack(1,1,0); 82 | for(int i=1;i<=n;i++) cout< 2 | using namespace std; 3 | #define ll long long 4 | const int N=5e5+5; 5 | int n,q,sz[N],lvl[N],cnt[N],mask[N];vector>qu[N];vectoradj[N];string s,ans[N];bool big[N]; 6 | void dfs0(int node,int l) 7 | { 8 | lvl[node]=l; 9 | sz[node]=1; 10 | for(auto it:adj[node]) 11 | { 12 | dfs0(it,l+1); 13 | sz[node]+=sz[it]; 14 | } 15 | } 16 | void add(int node,int p,int x) 17 | { 18 | mask[lvl[node]]^=(1<<(s[node]-'a')); 19 | for(auto u:adj[node]) 20 | { 21 | if(u!=p&&!big[u]) 22 | { 23 | add(u,node,x); 24 | } 25 | } 26 | } 27 | void dfs(int node,int par,bool keep) 28 | { 29 | int mx=-1,bigChild=-1; 30 | for(auto u:adj[node]) 31 | { 32 | if(u!=par&&sz[u]>mx) 33 | { 34 | mx=sz[u],bigChild=u; 35 | } 36 | } 37 | for(auto u:adj[node]) 38 | { 39 | if(u!=par&&u!=bigChild) 40 | { 41 | dfs(u,node,0); 42 | } 43 | } 44 | if(bigChild!=-1) 45 | { 46 | dfs(bigChild,node,1); 47 | big[bigChild]=1; 48 | } 49 | add(node,par,1); 50 | // here answer queries 51 | for(auto [h,i]:qu[node]) 52 | { 53 | if(__builtin_popcount(mask[h])<=1) 54 | { 55 | ans[i]="Yes\n"; 56 | } 57 | else 58 | { 59 | ans[i]="No\n"; 60 | } 61 | } 62 | if(bigChild!=-1) 63 | { 64 | big[bigChild]=0; 65 | } 66 | if(keep==0) 67 | { 68 | add(node,par,-1); 69 | } 70 | } 71 | --------------------------------------------------------------------------------