#include <bits/stdc++.h> using namespace std; #define PII pair<int, int> #define VI vector<int> #define VPII vector<PII> #define LL long long #define LD long double #define f first #define s second #define MP make_pair #define PB push_back #define endl '\n' // UWAGA #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for(int i = 0; i < (int)(n); ++i) #define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i) #define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i) #define random_shuffle(V) shuffle(V, rng) #define sim template<class n sim, class s> ostream & operator << (ostream &p, pair<n, s> x) {return p << "<" << x.f << ", " << x.s << ">";} sim> auto operator << (ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type {int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} sim, class s> void mini(n &p, s y) {if(p>y) p = y;} sim, class s> void maxi(n &p, s y) {if(p<y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define I(x) #x " =", (x), " " #define A(a, i) #a "[" #i " =", i, "] =", a[i], " " const int MXN = 3015; string a, b; int na, nb; int ih[MXN][MXN]; // pierwszy wymiar to l, drugi to i, j int iv[MXN][MXN]; int ih2[MXN][MXN]; int iv2[MXN][MXN]; // calc() function from https://www.sciencedirect.com/science/article/pii/S0166218X07002727 void calc(int p1, int mx) // O((mx - p1) * nb) { FOR(j, 0, nb)ih[p1-1][j] = j; FOR(l, p1-1, mx)iv[l][0] = 0; FOR(l, p1, mx) FOR(j, 1, nb) if(a[l] != b[j]) { ih[l][j] = max(iv[l][j-1], ih[l-1][j]); iv[l][j] = min(iv[l][j-1], ih[l-1][j]); } else { ih[l][j] = iv[l][j-1]; iv[l][j] = ih[l-1][j]; } } void calc_rev(int mi, int p2) { FOR(j, 0, nb+1)ih2[p2+1][j] = nb-j+1; FORD(l, p2+1, mi)iv2[l][0] = 0; FORD(l, p2, mi) FORD(j, nb, 1) if(a[l] != b[j]) { ih2[l][j] = max(iv2[l][j+1], ih2[l+1][j]); iv2[l][j] = min(iv2[l][j+1], ih2[l+1][j]); } else { ih2[l][j] = iv2[l][j+1]; iv2[l][j] = ih2[l+1][j]; } } int pom[MXN]; int pom_rev[MXN]; void pre(int l, int j) { REP(v, nb+2)pom[v] = 0; FOR(v, 0, j) { pom[ih[l][v]+1]++; pom[v+1]--; } FOR(i, 1, nb+1)pom[i] += pom[i-1]; } void pre_rev(int l, int i) { REP(v, nb+2)pom_rev[v] = 0; FOR(v, i, nb) { pom_rev[ih2[l][v]+1]++; pom_rev[nb-v+2]--; } FOR(i, 1, nb+1)pom_rev[i] += pom_rev[i-1]; } int restore(int l, int i, int j) { return pom[i]; } int restore_rev(int l, int i, int j) { return pom_rev[nb - j + 1]; } // l int go(int p1, int k1, int p2, int k2) { return restore(k1, p2, k2); } // l int go_rev(int p1, int k1, int p2, int k2) { return restore_rev(p1, p2, k2); } const int MXQ = 100 * 1000 + 4; int p1[MXQ]; int k1[MXQ]; int p2[MXQ]; int k2[MXQ]; int res[MXQ]; void process(int mid, int i) { pre(k1[i], k2[i]); pre_rev(p1[i], p2[i]); FOR(sr2, p2[i]-1, k2[i]) { int act = go_rev(p1[i], mid, p2[i], sr2) + go(mid+1, k1[i], sr2+1, k2[i]); maxi(res[i], act); } } void go(VI que, int a, int b) { if(que.size() == 0)return; int mid = (a + b) / 2; calc(mid+1, b); calc_rev(a, mid); VI pom[2]; for(auto i: que) { if(p1[i] <= mid && mid <= k1[i]) process(mid, i); else if(p1[i] < mid)pom[0].PB(i); else pom[1].PB(i); } go(pom[0], a, mid-1); go(pom[1], mid+1, b); } int main() { ios_base::sync_with_stdio(0); int q; cin >> na >> nb >> q; cin >> a; cin >> b; a = " " + a; b = " " + b; VI que; FOR(i, 1, q) { cin >> p1[i] >> k1[i] >> p2[i] >> k2[i]; que.PB(i); } go(que, 1, na); FOR(i, 1, q)cout << res[i] << endl; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | #include <bits/stdc++.h> using namespace std; #define PII pair<int, int> #define VI vector<int> #define VPII vector<PII> #define LL long long #define LD long double #define f first #define s second #define MP make_pair #define PB push_back #define endl '\n' // UWAGA #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for(int i = 0; i < (int)(n); ++i) #define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i) #define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i) #define random_shuffle(V) shuffle(V, rng) #define sim template<class n sim, class s> ostream & operator << (ostream &p, pair<n, s> x) {return p << "<" << x.f << ", " << x.s << ">";} sim> auto operator << (ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type {int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} sim, class s> void mini(n &p, s y) {if(p>y) p = y;} sim, class s> void maxi(n &p, s y) {if(p<y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define I(x) #x " =", (x), " " #define A(a, i) #a "[" #i " =", i, "] =", a[i], " " const int MXN = 3015; string a, b; int na, nb; int ih[MXN][MXN]; // pierwszy wymiar to l, drugi to i, j int iv[MXN][MXN]; int ih2[MXN][MXN]; int iv2[MXN][MXN]; // calc() function from https://www.sciencedirect.com/science/article/pii/S0166218X07002727 void calc(int p1, int mx) // O((mx - p1) * nb) { FOR(j, 0, nb)ih[p1-1][j] = j; FOR(l, p1-1, mx)iv[l][0] = 0; FOR(l, p1, mx) FOR(j, 1, nb) if(a[l] != b[j]) { ih[l][j] = max(iv[l][j-1], ih[l-1][j]); iv[l][j] = min(iv[l][j-1], ih[l-1][j]); } else { ih[l][j] = iv[l][j-1]; iv[l][j] = ih[l-1][j]; } } void calc_rev(int mi, int p2) { FOR(j, 0, nb+1)ih2[p2+1][j] = nb-j+1; FORD(l, p2+1, mi)iv2[l][0] = 0; FORD(l, p2, mi) FORD(j, nb, 1) if(a[l] != b[j]) { ih2[l][j] = max(iv2[l][j+1], ih2[l+1][j]); iv2[l][j] = min(iv2[l][j+1], ih2[l+1][j]); } else { ih2[l][j] = iv2[l][j+1]; iv2[l][j] = ih2[l+1][j]; } } int pom[MXN]; int pom_rev[MXN]; void pre(int l, int j) { REP(v, nb+2)pom[v] = 0; FOR(v, 0, j) { pom[ih[l][v]+1]++; pom[v+1]--; } FOR(i, 1, nb+1)pom[i] += pom[i-1]; } void pre_rev(int l, int i) { REP(v, nb+2)pom_rev[v] = 0; FOR(v, i, nb) { pom_rev[ih2[l][v]+1]++; pom_rev[nb-v+2]--; } FOR(i, 1, nb+1)pom_rev[i] += pom_rev[i-1]; } int restore(int l, int i, int j) { return pom[i]; } int restore_rev(int l, int i, int j) { return pom_rev[nb - j + 1]; } // l int go(int p1, int k1, int p2, int k2) { return restore(k1, p2, k2); } // l int go_rev(int p1, int k1, int p2, int k2) { return restore_rev(p1, p2, k2); } const int MXQ = 100 * 1000 + 4; int p1[MXQ]; int k1[MXQ]; int p2[MXQ]; int k2[MXQ]; int res[MXQ]; void process(int mid, int i) { pre(k1[i], k2[i]); pre_rev(p1[i], p2[i]); FOR(sr2, p2[i]-1, k2[i]) { int act = go_rev(p1[i], mid, p2[i], sr2) + go(mid+1, k1[i], sr2+1, k2[i]); maxi(res[i], act); } } void go(VI que, int a, int b) { if(que.size() == 0)return; int mid = (a + b) / 2; calc(mid+1, b); calc_rev(a, mid); VI pom[2]; for(auto i: que) { if(p1[i] <= mid && mid <= k1[i]) process(mid, i); else if(p1[i] < mid)pom[0].PB(i); else pom[1].PB(i); } go(pom[0], a, mid-1); go(pom[1], mid+1, b); } int main() { ios_base::sync_with_stdio(0); int q; cin >> na >> nb >> q; cin >> a; cin >> b; a = " " + a; b = " " + b; VI que; FOR(i, 1, q) { cin >> p1[i] >> k1[i] >> p2[i] >> k2[i]; que.PB(i); } go(que, 1, na); FOR(i, 1, q)cout << res[i] << endl; } |