Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int,int>
- #define vec vector
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvl(vector <ll> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvv(vector <vector <int> > &v){
- for (auto x: v) cv(x);
- cout<<"\n";
- }
- string S;
- int n;
- vector <ll> pf, lvp;
- const int p = 29, mod = 1e9 + 7;
- int nm(char x){
- return x - 'a' + 1;
- }
- ll H(int l, int r){
- ll k = r - l + 1;
- ll res = pf[r];
- if (l > 0){
- res -= pf[l-1] * lvp[k] % mod;
- }
- if (res < 0) res += mod;
- return res;
- }
- int l1, r1, l2, r2;
- int cmp(){
- int lf, rt, md;
- //бинописком ищем длину наибольшего общего префикса
- lf = 0;
- rt = min(r2 - l2, r1 - l1);
- //cout<<"lf rt = "<<lf<<' '<<rt<<"\n";
- //cout<<"l r = "<<l1<<' '<<r1<<' '<<l2<<' '<<r2<<"\n";
- while (rt - lf > 1){
- md = (lf + rt) / 2;
- ll a,b;
- a = H(l1, l1 + md);
- b = H(l2, l2 + md);
- //cout<<"md = "<<md<<"\n";
- //cout<<"a b = "<<a<<' '<<b<<"\n";
- if (a == b){
- lf = md;
- }
- else rt= md;
- }
- //cout<<"rt = "<<rt<<"\n";
- rt--;
- if (rt<0)rt=0;
- while (S[l1+rt]==S[l2+rt] && l1 + rt <= r1 && l2 + rt <= r2) rt++;
- //cout<<"rt = "<<rt<<"\n";
- if (l1 + rt > r1 && l2 + rt > r2){
- return 0;
- }
- if (l1 + rt > r1) {
- return -1;
- }
- if (l2 + rt > r2){
- return 1;
- }
- if (S[l1 + rt] > S[l2 + rt]){
- //cout<<"four\n";
- return 1;
- }
- if (S[l1 + rt] < S[l2 + rt]){
- //cout<<"five\n";
- return -1;
- }
- if (S[l1 + rt] == S[l2 + rt]){
- //cout<<"six\n";
- return 0;
- }
- return -1;
- }
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cin>>S;
- n = S.size();
- lvp.assign(n+5, 1);
- for (int i = 1; i<=n;++i) lvp[i] = lvp[i-1] * p % mod;
- pf.assign(n, nm(S[0]));
- for (int i =1;i<n;++i){
- pf[i] = (pf[i-1] * p % mod + nm(S[i])) % mod;
- }
- int q; cin>>q;
- int ans;
- for (int i=0;i<q;++i){
- cin>>l1>>r1>>l2>>r2;
- ans = cmp();
- //cout<<"ans = "<<ans<<"\n";
- cout<<ans<<"\n";
- }
- }
- /*
- abcabcabc
- aaaaabbbbbccccc
- abbaabba
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement