text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chmax(T &m, const T &v) {
if (v > m) return m = v, true;
return false;
}
template <typename T>
bool chmin(T &m, const T &v) {
if (v < m) return m = v, true;
return false;
}
template <typename T>
bool chinc(T &v1, T &v2) {
if (v1 > v2) return swap(v1, v2), true;
return false;
}
template <typename T>
bool chdec(T &v1, T &v2) {
if (v1 < v2) return swap(v1, v2), true;
return false;
}
static const int HMAX32 = INT32_MAX / 2;
static const int HMIN32 = -HMAX32;
static const int64_t HMAX64 = INT64_MAX / 2;
static const int64_t HMIN64 = -HMAX64;
namespace fio {
template <typename T>
inline T abs(T a) {
return a > 0 ? a : -a;
}
struct Cg {
inline char operator()() { return (char)getchar(); }
};
struct Cp {
inline void operator()(char x) { putchar(x); }
};
struct Cpe {
inline void operator()(char x) { putc(x, stderr); }
};
inline bool IS(char x) { return x == 10 || x == 13 || x == ' '; }
template <typename T>
struct Fr {
T P;
inline Fr &operator,(int &x) {
x = 0;
char t = P();
while ((t < '0' || t > '9') && t != '-') t = P();
bool f = 0;
if (t == '-') t = P(), f = 1;
x = t - '0';
for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
if (f) x = -x;
return *this;
}
inline operator int() {
int x;
*this, x;
return x;
}
inline Fr &operator,(long long &x) {
x = 0;
char t = P();
while ((t < '0' || t > '9') && t != '-') t = P();
bool f = 0;
if (t == '-') t = P(), f = 1;
x = t - '0';
for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
if (f) x = -x;
return *this;
}
inline operator long long() {
long long x;
*this, x;
return x;
}
inline Fr &operator,(char &x) {
for (x = P(); IS(x); x = P()) continue;
return *this;
}
inline operator char() {
char x;
*this, x;
return x;
}
inline Fr &operator,(char *x) {
char t = P();
for (; IS(t); t = P()) continue;
if (~t) {
for (; !IS(t) && ~t; t = P()) *x++ = t;
}
*x++ = 0;
return *this;
}
inline Fr &operator,(double &x) {
x = 0;
char t = P();
while ((t < '0' || t > '9') && t != '-') t = P();
bool f = 0;
if (t == '-') t = P(), f = 1;
x = t - '0';
for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
if (t == '.') {
double u = 0.1;
for (t = P(); t >= '0' && t <= '9'; t = P(), u *= 0.1) x += u * (t - '0');
}
if (f) x = -x;
return *this;
}
inline operator double() {
double x;
*this, x;
return x;
}
inline Fr &operator,(long double &x) {
x = 0;
char t = P();
while ((t < '0' || t > '9') && t != '-') t = P();
bool f = 0;
if (t == '-') t = P(), f = 1;
x = t - '0';
for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
if (t == '.') {
double u = 0.1;
for (t = P(); t >= '0' && t <= '9'; t = P(), u *= 0.1) x += u * (t - '0');
}
if (f) x = -x;
return *this;
}
inline operator long double() {
long double x;
*this, x;
return x;
}
inline Fr &operator,(unsigned int &x) {
x = 0;
char t = P();
while (t < '0' || t > '9') t = P();
x = t - '0';
for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
return *this;
}
inline operator unsigned int() {
unsigned int x;
*this, x;
return x;
}
inline Fr &operator,(unsigned long long &x) {
x = 0;
char t = P();
while (t < '0' || t > '9') t = P();
x = t - '0';
for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
return *this;
}
inline operator unsigned long long() {
unsigned long long x;
*this, x;
return x;
}
};
template <typename T>
struct Fw {
T P;
inline Fw &operator,(int x) {
if (x) {
if (x < 0) P('-'), x = -x;
char s[10];
int c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator()(int x) {
if (x) {
if (x < 0) P('-'), x = -x;
char s[10];
int c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator,(unsigned int x) {
if (x) {
char s[10];
int c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator()(unsigned int x) {
if (x) {
char s[10];
int c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator,(long long x) {
if (x) {
if (x < 0) P('-'), x = -x;
char s[19];
int c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator()(long long x) {
if (x) {
if (x < 0) P('-'), x = -x;
char s[19];
int c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator,(unsigned long long x) {
if (x) {
char s[20];
int c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator()(unsigned long long x) {
if (x) {
char s[20];
int c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator,(char x) {
P(x);
return *this;
}
inline Fw &operator()(char x) {
P(x);
return *this;
}
inline Fw &operator,(const char *x) {
while (*x) P(*x++);
return *this;
}
inline Fw &operator()(const char *x) {
while (*x) P(*x++);
return *this;
}
inline Fw &operator()(double x, int y) {
if (y) {
double t = 0.5;
for (int i = y; i--;) t *= 0.1;
if (x >= 0)
x += t;
else
x -= t, P('-');
*this, (long long)(abs(x));
P('.');
if (x < 0) x = -x;
while (y--) {
x *= 10;
x -= floor(x * 0.1) * 10;
P(((int)x) % 10 + '0');
}
} else if (x >= 0)
*this, (long long)(x + 0.5);
else
*this, (long long)(x - 0.5);
;
return *this;
}
inline Fw &operator()(long double x, int y) {
if (y) {
double t = 0.5;
for (int i = y; i--;) t *= 0.1;
if (x >= 0)
x += t;
else
x -= t, P('-');
*this, (long long)(abs(x));
P('.');
if (x < 0) x = -x;
while (y--) {
x *= 10;
x -= floor(x * 0.1) * 10;
P(((int)x) % 10 + '0');
}
} else if (x >= 0)
*this, (long long)(x + 0.5);
else
*this, (long long)(x - 0.5);
;
return *this;
}
};
} // namespace fio
fio::Fw<fio::Cp> fout;
fio::Fw<fio::Cpe> ferr;
fio::Fr<fio::Cg> fin;
static const int maxn = 200005;
int n;
int arr[maxn];
vector<int> occ[35];
int main() {
fin, n;
for (int b = 0, bend = 32; b < bend; b++) occ[b].push_back(0);
for (int i = 1, iend = n; i <= iend; i++) {
fin, arr[i];
for (int b = 0, bend = 32; b < bend; b++)
if (arr[i] & (1 << b)) occ[b].push_back(i);
}
for (int b = 0, bend = 32; b < bend; b++) occ[b].push_back(n + 1);
map<int, int> pres;
int64_t ans = 0;
for (int i = 1, iend = n; i <= iend; i++) {
int left = 0, right = n + 1;
for (int b = 0, bend = 32; b < bend; b++)
if (((arr[i] >> b) & 1) == 0) {
auto it_r = lower_bound(begin(occ[b]), end(occ[b]), i);
auto it_l = prev(it_r);
chmin(right, *it_r - 1);
chmax(left, *it_l + 1);
}
if (pres.count(arr[i])) chmax(left, pres[arr[i]] + 1);
pres[arr[i]] = i;
ans += 1LL * (right - i + 1) * (i - left + 1) - 1;
}
printf("%lld\n", 1LL * n * (n - 1) / 2 - ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
const int mlog = 32;
int n;
int p[maxn];
int L[maxn][35], R[maxn][35];
int posL[maxn], posR[maxn];
int a[maxn];
set<int> s;
bool cmp(int x, int y) { return p[x] > p[y]; }
int main() {
long long ans = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &p[i]);
for (int i = 0; i <= mlog; i++) L[0][i] = 0, R[n + 1][i] = n + 1;
for (int i = 1; i <= n; i++) posL[i] = 0, posR[i] = n + 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= mlog; j++) L[i][j] = L[i - 1][j];
for (int j = 0; j <= mlog; j++)
if (p[i] & (1 << j)) L[i][j] = i;
for (int j = 0; j <= mlog; j++)
if ((p[i] & (1 << j)) == 0) posL[i] = max(posL[i], L[i][j]);
}
for (int i = n; i >= 1; i--) {
for (int j = 0; j <= mlog; j++) R[i][j] = R[i + 1][j];
for (int j = 0; j <= mlog; j++)
if (p[i] & (1 << j)) R[i][j] = i;
for (int j = 0; j <= mlog; j++)
if ((p[i] & (1 << j)) == 0) posR[i] = min(posR[i], R[i][j]);
}
for (int i = 1; i <= n; i++) a[i] = i;
sort(&a[1], &a[n + 1], cmp);
s.insert(0);
s.insert(n + 1);
for (int i = 1; i <= n; i++) {
int x = a[i];
s.insert(x);
auto it = s.find(x);
auto il = it, ir = it;
il--;
ir++;
ans += (long long)(x - *il) * (*ir - x) -
(long long)(x - max(*il, posL[x])) * (min(*ir, posR[x]) - x);
}
printf("%I64d", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
const int N = 2e5 + 5;
int n, a[N];
static inline int lg(int x) { return 32 - __builtin_clz(x) - 1; }
struct Sparse {
static const int mk = 19;
int* tb[mk];
int* orr[mk];
Sparse(int* a, int n) {
for (int i = 0; i < mk; i++) {
tb[i] = new int[n + 5];
orr[i] = new int[n + 5];
}
for (int i = 0; i < n; i++) {
tb[0][i] = a[i];
orr[0][i] = a[i];
}
for (int k = 1; k <= lg(n); k++) {
for (int i = 0; i < n; i++) {
int nx = i + (1 << (k - 1));
if (nx < n) {
tb[k][i] = max(tb[k - 1][i], tb[k - 1][nx]);
orr[k][i] = orr[k - 1][i] | orr[k - 1][nx];
} else {
tb[k][i] = tb[k - 1][i];
orr[k][i] = orr[k - 1][i];
}
}
}
}
int getmax(int l, int r) {
int g = lg(r - l + 1);
return max(tb[g][l], tb[g][r - (1 << g) + 1]);
}
int getor(int l, int r) {
int g = lg(r - l + 1);
return orr[g][l] | orr[g][r - (1 << g) + 1];
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
Sparse st(a, n);
int K = lg(n) + 2;
ll ans = 0;
for (int i = 0; i < n; i++) {
int curOR = a[i];
int curIdx = i;
int prevIdx = i;
int add = 0;
while (true) {
curIdx++;
prevIdx = curIdx;
curOR = st.getor(i, curIdx);
int cor = curOR;
if ((curOR | a[curIdx + 1]) == curOR) {
for (int k = K; k >= 0; k--) {
if (curIdx + (1 << k) >= n) continue;
int ni = curIdx + (1 << k);
if (st.getor(i, ni) == curOR) {
cor |= st.orr[k][curIdx];
curIdx = ni;
}
}
}
if (curIdx >= n) break;
int r = prevIdx - 1;
int cmax = st.getmax(i, curIdx);
for (int k = K; k >= 0; k--) {
if (r + (1 << k) > curIdx) continue;
int ni = r + (1 << k);
if (st.getmax(i, ni) < curOR) {
cmax = max(cmax, st.tb[k][r]);
r = ni;
}
}
add += (r - prevIdx + 1);
}
ans += add;
}
cout << ans << '\n';
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("-Ofast", "-funroll-all-loops")
using namespace std;
const int N = 2e5 + 10;
int L[N], R[N], n, a[N], f[30], s[N], top;
long long res;
signed main() {
cin >> n;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
a[0] = 2e9;
for (int i = 1; i <= n; i++) {
while (top && a[s[top]] < a[i]) --top;
L[i] = s[top];
s[++top] = i;
}
top = 0;
a[n + 1] = 2e9;
s[++top] = n + 1;
for (int i = n; i >= 1; i--) {
while (top && a[s[top]] <= a[i]) --top;
R[i] = s[top];
s[++top] = i;
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 30; j++)
if (!(a[i] >> j & 1)) L[i] = max(L[i], f[j]);
for (int j = 0; j < 30; j++)
if (a[i] >> j & 1) f[j] = i;
}
for (int j = 0; j < 30; j++) f[j] = n + 1;
for (int i = n; i >= 1; i--) {
for (int j = 0; j < 30; j++)
if (!(a[i] >> j & 1)) R[i] = min(R[i], f[j]);
for (int j = 0; j < 30; j++)
if (a[i] >> j & 1) f[j] = i;
}
for (int i = 1; i <= n; i++) res += 1LL * (i - L[i]) * (R[i] - i);
cout << 1LL * n * (n + 1) / 2 - res;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template <typename T>
inline void chkmax(T &x, T y) {
if (x < y) x = y;
}
template <typename T>
inline void chkmin(T &x, T y) {
if (x > y) x = y;
}
inline int read() {
int x = 0;
char c = getchar();
while (c < 48) c = getchar();
while (c > 47) x = x * 10 + (c ^ 48), c = getchar();
return x;
}
const int maxn = 2e5 + 10;
int n, A[maxn];
int lef[maxn], rig[maxn];
int st[maxn], ls[maxn], rs[maxn];
void solve() {
cin >> n;
int top = 0, pos[32];
memset(pos, 0, sizeof pos);
for (int i = (1), iend = (n); i <= iend; ++i) {
A[i] = read();
lef[i] = 1;
for (int j = (0), jend = (29); j <= jend; ++j) {
if (A[i] >> j & 1) {
pos[j] = i;
} else {
chkmax(lef[i], pos[j] + 1);
}
}
while (top && A[st[top]] < A[i]) top--;
ls[i] = st[top] + 1, st[++top] = i;
}
top = 0, st[0] = n + 1;
fill(pos, pos + 30, n + 1);
for (int i = (n), iend = (1); i >= iend; --i) {
rig[i] = n;
for (int j = (0), jend = (29); j <= jend; ++j) {
if (A[i] >> j & 1) {
pos[j] = i;
} else {
chkmin(rig[i], pos[j] - 1);
}
}
while (top && A[st[top]] <= A[i]) top--;
rs[i] = st[top] - 1, st[++top] = i;
}
long long ans = 0;
for (int i = (1), iend = (n); i <= iend; ++i) {
int L = max(lef[i], ls[i]);
int R = min(rig[i], rs[i]);
ans += 1ll * (R - i + 1) * (i - L + 1);
}
ans = 1ll * n * (n + 1) / 2 - ans;
cout << ans << endl;
}
signed main() {
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int toint(const string& s) {
stringstream ss;
ss << s;
int x;
ss >> x;
return x;
}
string tostring(int number) {
stringstream ss;
ss << number;
return ss.str();
}
long long pw(long long a, long long b) {
if (b == 0) return 1;
if (b == 1) return a;
long long ans = pw(a, b / 2);
if (b & 1) return (((ans * ans) % 1000000007 * a) % 1000000007);
return (ans * ans) % 1000000007;
}
long long const NMAX = 2e5 + 20;
long long arr[NMAX];
int pos_left[NMAX][33], pos_right[NMAX][33];
stack<int> st;
int pre[NMAX], suf[NMAX];
map<pair<int, int>, vector<int> > part;
int main() {
long long n;
scanf("%lld", &n);
for (int i = 1; i <= n + 1; i++)
for (int j = 0; j <= 32; j++) pos_right[i][j] = n + 1;
for (int i = 1; i <= n; i++) {
scanf("%lld", &arr[i]);
for (int j = 0; j <= 32; j++)
pos_left[i][j] = ((1ll << j) & arr[i]) ? i : pos_left[i - 1][j];
}
for (int i = n; i >= 1; i--)
for (int j = 0; j <= 32; j++)
pos_right[i][j] = ((1ll << j) & arr[i]) ? i : pos_right[i + 1][j];
for (int i = 1; i <= n; i++) {
while (!st.empty() && arr[st.top()] <= arr[i]) st.pop();
pre[i] = (st.empty()) ? 0 : st.top();
st.push(i);
}
while (!st.empty()) st.pop();
for (int i = n; i >= 1; i--) {
while (!st.empty() && arr[st.top()] <= arr[i]) st.pop();
suf[i] = (st.empty()) ? n + 1 : st.top();
st.push(i);
}
long long ans = (n * (n + 1)) / 2;
for (int i = 1; i <= n; i++) {
int l = 0, r = n + 1;
for (int j = 0; j <= 32; j++)
if (!((1ll << j) & arr[i])) {
l = max(pos_left[i][j], l);
r = min(pos_right[i][j], r);
}
l = max(l, pre[i]);
r = min(r, suf[i]);
part[make_pair(l, r)].push_back(i);
}
for (auto& entry : part) {
pair<int, int> por = entry.first;
auto& v = entry.second;
int prev = por.first;
for (auto in : v) {
long long lef = in - prev;
long long righ = por.second - in;
ans -= lef * righ;
prev = in;
}
}
printf("%lld\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int N;
int A[200000];
int Tl[200000][30], Tr[200000][30];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
vector<pair<int, int> > vs;
for (int i = 0; i < (N); i++)
cin >> A[i], vs.push_back(pair<int, int>(A[i], i));
for (int i = 0; i < (N); i++)
for (int j = 0; j < (30); j++) Tl[i][j] = -1, Tr[i][j] = N;
for (int i = 0; i < (N); i++)
for (int j = 0; j < (30); j++)
if (A[i] & (1 << j)) Tl[i][j] = Tr[i][j] = i;
for (int i = 0; i < (N - 1); i++)
for (int j = 0; j < (30); j++) Tl[i + 1][j] = max(Tl[i + 1][j], Tl[i][j]);
for (int i = N - 1; i > 0; i--)
for (int j = 0; j < (30); j++) Tr[i - 1][j] = min(Tr[i - 1][j], Tr[i][j]);
sort((vs).begin(), (vs).end(), greater<pair<int, int> >());
set<int> rm;
long long s = 0;
for (pair<int, int> p : vs) {
int i = p.second;
auto it = rm.lower_bound(i);
int l = 0, r = N - 1;
if (it != rm.end()) r = (*it) - 1;
if (it != rm.begin()) l = *(--it) + 1;
int ll = l, rr = r;
for (int j = 0; j < (30); j++) {
if (A[i] & (1 << j)) continue;
ll = max(ll, Tl[i][j] + 1);
rr = min(rr, Tr[i][j] - 1);
}
s += 1LL * (i - l + 1) * (r - i + 1) - 1LL * (i - ll + 1) * (rr - i + 1);
rm.insert(i);
}
cout << s << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 10;
const int M = 1000000007;
const double PI = acos(-1);
const int oo = 1000000000;
int n, l, r, v[N], seg[4 * N], bef[N];
void build(int u, int s, int e) {
if (s == e) {
seg[u] = v[s];
return;
}
build(u * 2, s, (s + e) / 2);
build(u * 2 + 1, (s + e) / 2 + 1, e);
seg[u] = seg[u * 2] | seg[u * 2 + 1];
}
int get(int u, int s, int e) {
if (s > r || e < l) return 0;
if (s >= l && e <= r) return seg[u];
return get(u * 2, s, (s + e) / 2) | get(u * 2 + 1, (s + e) / 2 + 1, e);
}
int lefto, ps;
void gt(int u, int s, int e, bool f) {
if (s == e) {
if ((v[s] | ps) == ps) lefto = s;
return;
}
if (f) {
int x = get(u * 2, s, (s + e) / 2);
if ((x | ps) == ps) {
lefto = (s + e) / 2;
gt(u * 2 + 1, (s + e) / 2 + 1, e, f);
} else
gt(u * 2, s, (s + e) / 2, f);
} else {
int x = get(u * 2 + 1, (s + e) / 2 + 1, e);
if ((x | ps) == ps) {
lefto = (s + e) / 2 + 1;
gt(u * 2, s, (s + e) / 2, f);
} else
gt(u * 2 + 1, (s + e) / 2 + 1, e, f);
}
}
map<int, int> ls;
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
scanf("%d", &v[i]);
bef[i] = ls[v[i]];
ls[v[i]] = i;
}
build(1, 1, n);
long long ans = 1ll * n * (n - 1) / 2;
for (int i = 1; i <= n; ++i) {
l = bef[i] + 1;
r = i;
ps = v[i];
gt(1, 1, n, 0);
int left = lefto;
left = max(left, l);
l = i;
r = n;
gt(1, 1, n, 1);
ans -= 1ll * (i - left + 1) * (lefto - i + 1) - 1;
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
inline int maxi(int a, int b) { return (a > b) ? a : b; }
inline void gtmx(int &a, int b) {
if (a < b) a = b;
}
int mx[22][200010];
int tab[200010], pwr[22];
int nxt[200010][33];
int tmp[33];
inline int query(int l, int r) {
int ned = tab[r - l + 1];
return maxi(mx[ned][l], mx[ned][r - pwr[ned] + 1]);
}
int main(void) {
pwr[0] = 1;
for (int i = 1; i <= 21; ++i) {
pwr[i] = pwr[i - 1] << 1;
}
int pnt = 0;
for (int j = 1; j <= 200000; j++) {
if (j >= pwr[pnt + 1]) pnt++;
tab[j] = pnt;
}
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &mx[0][i]);
}
for (int i = 1; i <= 20; ++i) {
for (int j = 1; j <= n; ++j) {
if (j + pwr[i - 1] > n) {
mx[i][j] = mx[i - 1][j];
continue;
}
mx[i][j] = maxi(mx[i - 1][j], mx[i - 1][j + pwr[i - 1]]);
}
}
memset(tmp, 255, sizeof(tmp));
for (int i = n; i; --i) {
for (int j = 0; j <= 30; ++j) {
if (mx[0][i] & (1 << j)) tmp[j] = i;
}
memcpy(nxt[i], tmp, sizeof(tmp));
}
register long long res = 0;
register int i, j, cur, cx, l, r, md;
for (i = 1; i <= n; ++i) {
memcpy(tmp, nxt[i], sizeof(tmp));
sort(&tmp[0], &tmp[31]);
tmp[31] = n + 1;
cur = mx[0][i], cx = i;
for (j = 0; j <= 30; ++j) {
if (tmp[j] == tmp[j + 1] || tmp[j] == -1) continue;
cur |= mx[0][tmp[j]];
if (query(i, tmp[j]) == cur) continue;
l = tmp[j], r = tmp[j + 1], md;
while (l + 1 < r) {
md = (l + r) >> 1;
if (query(tmp[j], md) < cur) {
l = md;
} else {
r = md;
}
}
res += (l - tmp[j] + 1);
}
}
printf("%I64d", res);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200300;
int n, m, a[N], v[N], sum[N];
long long ans;
inline void read(int &x) {
x = 0;
char c = getchar();
int f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = 10 * x + c - '0';
c = getchar();
}
x *= f;
}
struct ST {
int v[N][19], p[N][19], log2[N], n;
inline void init(int x) {
n = x;
log2[1] = 0;
for (int i = 2; i <= n; i++) {
log2[i] = log2[i - 1];
if ((1 << log2[i] + 1) <= i) log2[i]++;
}
for (int i = 1; i <= n; i++) v[i][0] = a[i], p[i][0] = i;
for (int j = 1; j <= log2[n]; j++)
for (int i = 1; i <= n - (1 << j) + 1; i++) {
v[i][j] = ((v[i][j - 1]) > (v[i + (1 << (j - 1))][j - 1])
? (v[i][j - 1])
: (v[i + (1 << (j - 1))][j - 1]));
if (v[i][j - 1] > v[i + (1 << (j - 1))][j - 1])
p[i][j] = p[i][j - 1];
else
p[i][j] = p[i + (1 << (j - 1))][j - 1];
}
}
inline pair<int, int> query(int l, int r) {
int k = log2[r - l + 1];
return pair<int, int>(
((v[l][k]) > (v[r - (1 << k) + 1][k]) ? (v[l][k])
: (v[r - (1 << k) + 1][k])),
v[l][k] > v[r - (1 << k) + 1][k] ? p[l][k] : p[r - (1 << k) + 1][k]);
}
} st;
struct ST1 {
int v[N][19], log2[N], n;
inline void init(int x) {
n = x;
log2[1] = 0;
for (int i = 2; i <= n; i++) {
log2[i] = log2[i - 1];
if ((1 << log2[i] + 1) <= i) log2[i]++;
}
for (int i = 1; i <= n; i++) v[i][0] = a[i];
for (int j = 1; j <= log2[n]; j++)
for (int i = 1; i <= n - (1 << j) + 1; i++)
v[i][j] = (v[i][j - 1] | v[i + (1 << (j - 1))][j - 1]);
}
inline int query(int l, int r) {
int k = log2[r - l + 1];
return (v[l][k] | v[r - (1 << k) + 1][k]);
}
} st1;
inline void solve(int l, int r) {
if (l > r) return;
pair<int, int> p = st.query(l, r);
int pos = p.second;
int L = pos, R = pos;
for (int j = 18; j >= 0; j--)
if (L - (1 << j) >= l) {
int tmp = st1.query(L - (1 << j), L - 1);
if ((a[pos] & tmp) == tmp) L -= (1 << j);
}
for (int j = 18; j >= 0; j--)
if (R + (1 << j) <= r) {
int tmp = st1.query(R + 1, R + (1 << j));
if ((a[pos] & tmp) == tmp) R += (1 << j);
}
ans += (long long)(pos - L + 1) * (R - pos + 1);
solve(l, pos - 1);
solve(pos + 1, r);
}
int main() {
read(n);
for (int i = 1; i <= n; i++) read(a[i]);
st.init(n);
st1.init(n);
solve(1, n);
printf("%lld", (long long)n * (n + 1) / 2 - ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline bool EQ(double num1, double bin) { return fabs(num1 - bin) < 1e-9; }
const int INF = 1 << 29;
inline int two(int num1) { return 1 << num1; }
inline void set_bit(int& num1, int bin) { num1 |= two(bin); }
inline void unset_bit(int& num1, int bin) { num1 &= ~two(bin); }
inline int last_bit(int num1) { return num1 & (-num1); }
inline int ones(int num1) {
int res = 0;
while (num1 && ++res) num1 -= num1 & (-num1);
return res;
}
template <class tests>
void chmax(tests& num1, const tests& bin) {
num1 = max(num1, bin);
}
template <class tests>
void chmin(tests& num1, const tests& bin) {
num1 = min(num1, bin);
}
const int maxn = 1e5 + 5;
const int MX = 1000;
using namespace std;
int main() {
long long n;
vector<pair<int, int>> v;
vector<int> nns1, nns2, c, d;
cin >> n;
for (int i = 0; i < n; i++) {
int ttm;
cin >> ttm;
v.push_back(make_pair(ttm, i));
nns1.push_back(n);
nns2.push_back(-1);
c.push_back(n);
d.push_back(-1);
}
vector<pair<int, int>> vv1;
for (int i = 0; i < n; i++) {
while (vv1.size() != 0 && vv1[vv1.size() - 1] < v[i]) {
pair<int, int> ttm = vv1[vv1.size() - 1];
vv1.pop_back();
nns1[ttm.second] = i;
}
vv1.push_back(v[i]);
}
vv1.clear();
for (int i = n - 1; i >= 0; i--) {
while (vv1.size() != 0 && vv1[vv1.size() - 1] < v[i]) {
pair<int, int> ttm = vv1[vv1.size() - 1];
vv1.pop_back();
nns2[ttm.second] = i;
}
vv1.push_back(v[i]);
}
vv1.clear();
for (int i = 0; i < n; i++) {
while (vv1.size() != 0 && ((vv1[vv1.size() - 1].first | v[i].first) !=
vv1[vv1.size() - 1].first)) {
pair<int, int> ttm = vv1[vv1.size() - 1];
vv1.pop_back();
c[ttm.second] = i;
}
vv1.push_back(v[i]);
}
vv1.clear();
for (int i = n - 1; i >= 0; i--) {
while (vv1.size() != 0 && ((vv1[vv1.size() - 1].first | v[i].first) !=
vv1[vv1.size() - 1].first)) {
pair<int, int> ttm = vv1[vv1.size() - 1];
vv1.pop_back();
d[ttm.second] = i;
}
vv1.push_back(v[i]);
}
long long tot = 0;
for (int i = 0; i < n; i++) {
long long mx = min(nns1[i], c[i]);
long long mn = max(nns2[i], d[i]);
long long nns1 = mx - i;
long long nns2 = i - mn;
tot += nns1 * nns2;
}
cout << (n * (n + 1)) / 2 - tot << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 17, lg = 18, lgv = 30, inf = 2e9;
int n, cer[lgv], mx[lg][maxn], lg2[maxn];
multiset<int> s;
int get(int l, int r) {
if (l < 0) l = 0;
if (r <= l) return inf;
int k = lg2[r - l];
return max(mx[k][l], mx[k][r - (1 << k)]);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
memset(cer, -1, sizeof cer);
for (int i = 0; i <= lgv; i++) s.insert(-1);
for (int i = 2; i < maxn; i++) lg2[i] = lg2[i / 2] + 1;
cin >> n;
for (int i = 0; i < n; i++) cin >> mx[0][i];
int *a = mx[0];
for (int k = 1; k < lg; k++)
for (int i = 0; i + (1 << k) <= n; i++)
mx[k][i] = max(mx[k - 1][i], mx[k - 1][i + (1 << k - 1)]);
long long ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < lgv; j++)
if (a[i] >> j & 1) {
s.erase(s.find(cer[j]));
s.insert(cer[j] = i);
}
int cur = 0;
for (auto it = --s.end(); *it != -1; it--) {
cur |= a[*it];
int lo = *prev(it), hi = *it + 1;
while (hi - lo > 1) {
int mid = lo + hi >> 1;
(get(mid, i + 1) >= cur ? lo : hi) = mid;
}
ans += *it - lo;
}
}
cout << ans << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
int T[18][200001], lg[200001], a[200001];
vector<pair<int, int> > v;
map<int, int> m;
void PreProcess(int n) {
for (int i = 2; i <= n; ++i) lg[i] = 1 + lg[i / 2];
for (int i = 1; i <= n; ++i) {
T[0][i] = a[i];
v.push_back(make_pair(a[i], i));
}
sort(v.begin(), v.end(), greater<pair<int, int> >());
m[1] = n;
m[n + 1] = n + 1;
for (int k = 1; k <= lg[n]; ++k)
for (int i = 1, j = i + (1 << (k - 1)); j <= n; ++i, ++j)
T[k][i] = (T[k - 1][i] | T[k - 1][j]);
}
int ORQuery(int i, int j) {
int k = lg[j - i + 1];
j -= ((1 << k) - 1);
return (T[k][i] | T[k][j]);
}
int BinarySearchLeft(int x, int y, int val) {
int l = y, r = y;
while (x <= y) {
int mid = (x + y) / 2;
if (ORQuery(mid, r) == val)
l = mid, y = mid - 1;
else
x = mid + 1;
}
return l;
}
int BinarySearchRight(int x, int y, int val) {
int l = x, r = x;
while (x <= y) {
int mid = (x + y) / 2;
if (ORQuery(l, mid) == val)
r = mid, x = mid + 1;
else
y = mid - 1;
}
return r;
}
pair<int, int> GetInterval(int i) {
auto it = m.lower_bound(i);
if (it->first != i) --it;
int l = it->first, r = it->second;
m.erase(l);
return make_pair(l, r);
}
void InsertInterval(int l, int r) {
if (l <= r) m[l] = r;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
PreProcess(n);
long long ans = 1ll * n * (n + 1) / 2;
for (auto p : v) {
int val, i, x, y;
tie(val, i) = p;
tie(x, y) = GetInterval(i);
int l = BinarySearchLeft(x, i, val);
int r = BinarySearchRight(i, y, val);
ans -= 1ll * (i - l + 1) * (r - i + 1);
InsertInterval(x, i - 1);
InsertInterval(i + 1, y);
}
printf("%lld\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void swap(long long &a, long long &b) {
long long t = a;
a = b;
b = t;
}
void print(vector<long long> &v) {
for (long long i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
}
long long power(long long x, long long y) {
long long ans = 1;
while (y > 0) {
if (y % 2 == 1) {
ans = (ans * x) % 1000000007;
}
x = (x * x) % 1000000007;
y = y / 2;
}
return ans;
}
long long modinv(long long x) { return power(x, 1000000007 - 2); }
long long n;
vector<long long> arr(200007);
vector<long long> tree(4 * 200007);
void buildtree(vector<long long> &tree, long long strt, long long end,
long long node) {
if (strt == end) {
tree[node] = strt;
return;
}
long long mid = (strt + end) / 2;
buildtree(tree, strt, mid, 2 * node);
buildtree(tree, mid + 1, end, 2 * node + 1);
if (arr[tree[2 * node]] > arr[tree[2 * node + 1]]) {
tree[node] = tree[2 * node];
} else {
tree[node] = tree[2 * node + 1];
}
}
long long query(vector<long long> &tree, long long strt, long long end,
long long node, long long l, long long r) {
if (l > end || r < strt)
return -1;
else if (strt >= l && end <= r)
return tree[node];
long long mid = (strt + end) / 2;
long long a1 = query(tree, strt, mid, 2 * node, l, r);
long long a2 = query(tree, mid + 1, end, 2 * node + 1, l, r);
if (a1 == -1) return a2;
if (a2 == -1) return a1;
if (arr[a1] > arr[a2]) {
return a1;
} else {
return a2;
}
}
long long ans = 0;
long long mask[33];
long long lefti[200007];
long long righti[200007];
void solve(long long l, long long r) {
if (r <= l) return;
long long idx = query(tree, 0, n - 1, 1, l, r);
solve(l, idx - 1);
solve(idx + 1, r);
if (righti[idx] <= r) {
ans += (r - righti[idx] + 1) * (idx - l + 1);
}
if (lefti[idx] >= l) {
ans += (lefti[idx] - l + 1) * (r - idx + 1);
}
if (righti[idx] <= r && lefti[idx] >= l) {
ans -= (r - righti[idx] + 1) * (lefti[idx] - l + 1);
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> arr[i];
}
lefti[0] = -1;
long long z = 1;
for (long long i = 0; i < 32; i++) {
if (arr[0] & (z << i)) {
mask[i] = 0;
} else {
mask[i] = -1;
}
}
for (long long i = 1; i < n; i++) {
long long z1 = -1;
for (long long j = 0; j < 32; j++) {
if (!(arr[i] & (z << j))) {
z1 = max(z1, mask[j]);
} else {
mask[j] = i;
}
}
lefti[i] = z1;
}
righti[n - 1] = n;
for (long long i = 0; i < 32; i++) {
if (arr[n - 1] & (z << i)) {
mask[i] = n - 1;
} else {
mask[i] = n;
}
}
for (long long i = n - 2; i >= 0; i--) {
long long z1 = n;
for (long long j = 0; j < 32; j++) {
if (!(arr[i] & (z << j))) {
z1 = min(z1, mask[j]);
} else {
mask[j] = i;
}
}
righti[i] = z1;
}
buildtree(tree, 0, n - 1, 1);
solve(0, n - 1);
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
int n, a[N];
static inline int lg(int x) { return 32 - __builtin_clz(x) - 1; }
struct Sparse {
static const int mk = 19;
int* tb[mk];
int* orr[mk];
Sparse(int* a, int n) {
for (int i = 0; i < mk; i++) {
tb[i] = new int[n + 5];
orr[i] = new int[n + 5];
}
for (int i = 0; i < n; i++) {
tb[0][i] = a[i];
orr[0][i] = a[i];
}
for (int k = 1; k <= lg(n); k++) {
for (int i = 0; i < n; i++) {
int nx = i + (1 << (k - 1));
if (nx < n) {
tb[k][i] = max(tb[k - 1][i], tb[k - 1][nx]);
orr[k][i] = orr[k - 1][i] | orr[k - 1][nx];
} else {
tb[k][i] = tb[k - 1][i];
orr[k][i] = orr[k - 1][i];
}
}
}
}
int getmax(int l, int r) {
int g = lg(r - l + 1);
return max(tb[g][l], tb[g][r - (1 << g) + 1]);
}
int getor(int l, int r) {
int g = lg(r - l + 1);
return orr[g][l] | orr[g][r - (1 << g) + 1];
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
Sparse st(a, n);
int K = lg(n) + 2;
ll ans = 0;
for (int i = 0; i < n; i++) {
int curOR = a[i];
int curIdx = i;
int prevIdx = i;
int add = 0;
while (true) {
curIdx++;
prevIdx = curIdx;
curOR = st.getor(i, curIdx);
int cor = curOR;
if (st.getor(i, curIdx + 1) == curOR) {
for (int k = K; k >= 0; k--) {
if (curIdx + (1 << k) >= n) continue;
int ni = curIdx + (1 << k);
if (st.getor(i, ni) == curOR) {
cor |= st.orr[k][curIdx];
curIdx = ni;
}
}
}
if (curIdx >= n) break;
int r = prevIdx - 1;
int cmax = st.getmax(i, curIdx);
for (int k = K; k >= 0; k--) {
if (r + (1 << k) > curIdx) continue;
int ni = r + (1 << k);
if (st.getmax(i, ni) < curOR) {
cmax = max(cmax, st.tb[k][r]);
r = ni;
}
}
add += (r - prevIdx + 1);
}
ans += add;
}
cout << ans << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int a[maxn], L[maxn], R[maxn];
int nxt[maxn][31], pre[maxn][31];
int n;
int main() {
iostream::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
stack<int> st;
st.push(0);
a[0] = 1e9 + 5;
for (int i = 1; i <= n; ++i) {
while (a[st.top()] <= a[i]) st.pop();
L[i] = st.top() + 1;
st.push(i);
}
while (st.size()) st.pop();
st.push(n + 1);
a[n + 1] = 1e9 + 5;
for (int i = n; i > 0; --i) {
while (a[st.top()] < a[i]) st.pop();
R[i] = st.top() - 1;
st.push(i);
}
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= 30; ++j)
if (a[i] >> j & 1)
pre[i][j] = i;
else
pre[i][j] = pre[i - 1][j];
}
memset(nxt[n + 1], 127, sizeof(nxt[n + 1]));
for (int i = n; i > 0; --i) {
for (int j = 0; j <= 30; ++j)
if (a[i] >> j & 1)
nxt[i][j] = i;
else
nxt[i][j] = nxt[i + 1][j];
}
int64_t res = 0;
for (int i = 1; i <= n; ++i) {
int l = L[i] - 1, r = R[i] + 1;
for (int j = 0; j <= 30; ++j)
if (!(a[i] >> j & 1)) {
l = max(l, pre[i - 1][j]);
r = min(r, nxt[i + 1][j]);
}
res += 1ll * (R[i] - i + 1) * (i - L[i] + 1) - 1ll * (r - i) * (i - l);
}
cout << res;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
template <class T>
inline T biton(T n, T pos) {
return n | ((T)1 << pos);
}
template <class T>
inline T bitoff(T n, T pos) {
return n & ~((T)1 << pos);
}
template <class T>
inline T ison(T n, T pos) {
return (bool)(n & ((T)1 << pos));
}
template <class T>
inline T gcd(T a, T b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
template <typename T>
string NumberToString(T Number) {
ostringstream second;
second << Number;
return second.str();
}
inline int nxt() {
int aaa;
scanf("%d", &aaa);
return aaa;
}
inline long long int lxt() {
long long int aaa;
scanf("%lld", &aaa);
return aaa;
}
inline double dxt() {
double aaa;
scanf("%lf", &aaa);
return aaa;
}
template <class T>
inline T bigmod(T p, T e, T m) {
T ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p) % m;
p = (p * p) % m;
}
return (T)ret;
}
int ar[200010];
int pref[30][200010];
int suf[30][200010];
int l[200010];
int r[200010];
long long int go(long long int i, long long int bam, long long int dan,
long long int bambit, long long int danbit) {
long long int ans = 0;
bool f = 0;
if (bambit >= bam) {
f = 1;
ans += (bambit - bam + 1) * (dan - i + 1);
}
if (danbit <= dan) {
if (f) {
ans += (i - bambit) * (dan - danbit + 1);
} else {
ans += (dan - danbit + 1) * (i - bam + 1);
}
}
return ans;
}
int main() {
int n = nxt();
for (int i = 0; i < n; i++) {
ar[i] = nxt();
}
for (int i = 0; i < 30; i++) {
int last = -1;
for (int j = 0; j < n; j++) {
pref[i][j] = last;
if (ison(ar[j], i)) last = j;
}
}
for (int i = 0; i < 30; i++) {
int last = 1e9;
for (int j = n - 1; j >= 0; j--) {
suf[i][j] = last;
if (ison(ar[j], i)) last = j;
}
}
stack<int> st;
l[0] = 0;
for (int i = 0; i < n; i++) {
if (st.empty() || ar[st.top()] >= ar[i]) {
st.push(i);
l[i] = i;
} else {
while (st.size() && ar[st.top()] < ar[i]) st.pop();
if (st.empty())
l[i] = 0;
else
l[i] = st.top() + 1;
st.push(i);
}
}
while (st.size()) st.pop();
for (int i = n - 1; i >= 0; i--) {
if (st.empty() || ar[st.top()] > ar[i]) {
st.push(i);
r[i] = i;
} else {
while (st.size() && ar[st.top()] <= ar[i]) st.pop();
if (st.empty())
r[i] = n - 1;
else
r[i] = st.top() - 1;
st.push(i);
}
}
long long int ans = 0;
for (int i = 0; i < n; i++) {
int bam = l[i];
int dan = r[i];
int bambit = -1;
int danbit = 1e9;
for (int j = 0; j < 30; j++) {
if (ison(ar[i], j) == 0) {
bambit = max(pref[j][i], bambit);
danbit = min(danbit, suf[j][i]);
}
}
ans += go(i, bam, dan, bambit, danbit);
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200042, B = 30;
int tab[MAXN], gol[MAXN][32], gor[MAXN][32];
int bigl[MAXN], bigr[MAXN];
int main() {
ios_base::sync_with_stdio(0);
int n;
cin >> n;
for (int i = (1); i <= (n); ++i) {
int x;
cin >> x;
tab[i] = x;
}
for (int i = (1); i <= (n); ++i) {
for (int j = (0); j <= (B); ++j) {
if ((1 << j) & tab[i])
gol[i][j] = i;
else
gol[i][j] = gol[i - 1][j];
}
}
for (int j = (0); j <= (B); ++j) gor[n + 1][j] = n + 1;
for (int i = n; i >= 1; i--) {
for (int j = (0); j <= (B); ++j) {
if ((1 << j) & tab[i])
gor[i][j] = i;
else
gor[i][j] = gor[i + 1][j];
}
}
stack<int> S;
for (int i = (1); i <= (n); ++i) {
while (!S.empty() && tab[S.top()] < tab[i]) S.pop();
if (S.empty())
bigl[i] = 0;
else
bigl[i] = S.top();
S.push(i);
}
while (!S.empty()) S.pop();
for (int i = n; i >= 1; i--) {
while (!S.empty() && tab[S.top()] <= tab[i]) S.pop();
if (S.empty())
bigr[i] = n + 1;
else
bigr[i] = S.top();
S.push(i);
}
long long res = 0;
for (int i = (1); i <= (n); ++i) {
int l = bigl[i] + 1, r = bigr[i] - 1;
int lb = 0, rb = n + 1;
for (int b = (0); b <= (B); ++b) {
if (((1 << b) & tab[i]) == 0) lb = max(lb, gol[i][b]);
if (((1 << b) & tab[i]) == 0) rb = min(rb, gor[i][b]);
}
if (lb >= l) res += 1LL * (lb - l + 1) * (r - i + 1);
if (rb <= r) res += 1LL * (r - rb + 1) * (i - l + 1);
if (lb >= l && rb <= r) res -= 1LL * (lb - l + 1) * (r - rb + 1);
}
cout << res << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, a[N], prefmx[N], prefor[N], suffmx[N], suffor[N];
long long ans;
void rec(int l, int r) {
if (l == r || l > r) return;
int m = l + r >> 1;
rec(l, m);
rec(m + 1, r);
prefmx[m + 1] = a[m + 1], prefor[m + 1] = a[m + 1];
for (int i = m + 2; i <= r; ++i)
prefmx[i] = max(prefmx[i - 1], a[i]), prefor[i] = (prefor[i - 1] | a[i]);
suffmx[m] = suffor[m] = a[m];
for (int i = m - 1; i >= l; --i)
suffmx[i] = max(suffmx[i + 1], a[i]), suffor[i] = (suffor[i + 1] | a[i]);
vector<int> v1, v2;
for (int i = m; i >= l; --i) v2.emplace_back(suffmx[i]);
for (int i = m + 1; i <= r; ++i) v1.emplace_back(prefmx[i]);
for (int i = m + 1; i <= r; ++i) {
if (prefmx[i] != prefor[i]) continue;
auto to = upper_bound((v2).begin(), (v2).end(), prefmx[i]) - v2.begin();
to--;
int lf = 0, rg = to;
int pos = -1;
while (lf <= rg) {
int mid = rg + lf >> 1;
if ((suffor[m - mid] | prefor[i]) == prefmx[i])
pos = mid, lf = mid + 1;
else
rg = mid - 1;
}
ans += (1 + pos);
}
for (int i = m; i >= l; --i) {
if (suffmx[i] != suffor[i]) continue;
auto to = lower_bound((v1).begin(), (v1).end(), suffmx[i]) - v1.begin();
to--;
int lf = 0, rg = to;
int pos = -1;
while (lf <= rg) {
int mid = rg + lf >> 1;
if ((prefor[m + 1 + mid] | suffor[i]) == suffmx[i])
pos = mid, lf = mid + 1;
else
rg = mid - 1;
}
ans += (1 + pos);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", a + i);
rec(1, n);
ans += n;
ans = ((n * 1ll * (n + 1)) / 2) - ans;
printf("%lld\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char getc() {
char c = getchar();
while ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9'))
c = getchar();
return c;
}
int gcd(int n, int m) { return m == 0 ? n : gcd(m, n % m); }
int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return x * f;
}
int n, a[200010], pre[200010][32], suf[200010][32], PRE[200010], SUF[200010];
long long ans;
signed main() {
n = read();
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 32; j++) pre[i][j] = pre[i - 1][j];
for (int j = 0; j < 32; j++)
if (a[i - 1] & (1 << j)) pre[i][j] = i - 1;
}
for (int j = 0; j < 32; j++) suf[n + 1][j] = n + 1;
for (int i = n; i >= 1; i--) {
for (int j = 0; j < 32; j++) suf[i][j] = suf[i + 1][j];
for (int j = 0; j < 32; j++)
if (a[i + 1] & (1 << j)) suf[i][j] = i + 1;
}
a[0] = a[n + 1] = 1000000010;
for (int i = 1; i <= n; i++) {
int j = i - 1;
while (a[j] < a[i]) j = PRE[j];
PRE[i] = j;
}
SUF[n + 1] = n + 1;
for (int i = n; i >= 1; i--) {
int j = i + 1;
while (a[j] <= a[i]) j = SUF[j];
SUF[i] = j;
}
for (int i = 1; i <= n; i++) {
int x = PRE[i], y = SUF[i];
int u = PRE[i], v = SUF[i];
for (int j = 0; j < 32; j++)
if (!(a[i] & (1 << j))) u = max(u, pre[i][j]), v = min(v, suf[i][j]);
ans += 1ll * (SUF[i] - i) * (u - PRE[i]) + 1ll * (i - u) * (SUF[i] - v);
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = (1e6) + 7;
const int inf = (1e9) + 7;
const long long LLinf = (1e18) + 7;
const long double eps = 1e-9;
const long long mod = 1e9 + 7;
const int maxlog = 31;
stack<pair<int, int> > stos;
int tab[maxn];
int lewo[maxn];
int prawo[maxn];
int le[maxn][maxlog];
int pr[maxn][maxlog];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i < n + 1; i++) {
cin >> tab[i];
while (((int)(stos).size()) && stos.top().first < tab[i]) stos.pop();
if (((int)(stos).size())) lewo[i] = stos.top().second;
stos.push({tab[i], i});
}
while (((int)(stos).size())) stos.pop();
for (int i = n; i > 0; i--) {
while (((int)(stos).size()) && stos.top().first <= tab[i]) stos.pop();
if (((int)(stos).size()))
prawo[i] = stos.top().second;
else
prawo[i] = n + 1;
stos.push({tab[i], i});
}
for (int bit = 0; bit < maxlog; bit++) {
int curr = 0;
for (int i = 1; i < n + 1; i++) {
if (tab[i] & (1 << bit)) curr = i;
le[i][bit] = curr;
}
curr = n + 1;
for (int i = n; i > 0; i--) {
if (tab[i] & (1 << bit)) curr = i;
pr[i][bit] = curr;
}
}
long long res = 0LL;
for (int i = 1; i < n + 1; i++) {
int l = 0;
int p = n + 1;
for (int bit = 0; bit < maxlog; bit++) {
if (tab[i] & (1 << bit)) continue;
l = max(l, le[i][bit]);
p = min(p, pr[i][bit]);
}
if (l - lewo[i] > 0) res += (l - lewo[i]) * (long long)(prawo[i] - i);
if (prawo[i] - p > 0) res += (prawo[i] - p) * (long long)(i - lewo[i]);
if (prawo[i] - p > 0 && l - lewo[i] > 0)
res -= (prawo[i] - p) * (long long)(l - lewo[i]);
}
cout << res;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long readi() {
long long input = 0;
char c = ' ';
while (c < '-') {
c = getchar();
}
bool negative = false;
if (c == '-') {
negative = true;
c = getchar();
}
while (c >= '0') {
input = 10 * input + (c - '0');
c = getchar();
}
if (negative) {
input = -input;
}
return input;
}
void printi(long long output) {
if (output == 0) {
putchar('0');
return;
}
if (output < 0) {
putchar('-');
output = -output;
}
int aout[20];
int ilen = 0;
while (output) {
aout[ilen] = ((output % 10));
output /= 10;
ilen++;
}
for (int i = ilen - 1; i >= 0; i--) {
putchar(aout[i] + '0');
}
return;
}
string reads() {
string input = "";
char c = ' ';
while (c <= ' ') {
c = getchar();
}
while (c > ' ') {
input += c;
c = getchar();
}
return input;
}
void prints(string output) {
for (int i = 0; i < output.length(); i++) {
putchar(output[i]);
}
return;
}
int N;
int64_t arr[200010];
int lt[200010], rt[200010];
int cnt[200010][40];
int lbig[200010], rbig[200010];
vector<pair<int64_t, int64_t> > nums;
int64_t ans;
bool range(int bit, int L, int R) { return cnt[R][bit] - cnt[L - 1][bit]; }
int64_t choose2(int64_t x) { return x * (x - 1) / 2; }
int32_t main() {
ios_base::sync_with_stdio(false);
srand(time(NULL));
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> arr[i];
}
for (int i = 1; i <= N; i++) {
for (int j = 0; j < 35; j++) {
if (arr[i] & (1ll << j)) {
cnt[i][j]++;
}
}
}
for (int i = 1; i <= N; i++) {
for (int j = 0; j < 35; j++) {
cnt[i][j] += cnt[i - 1][j];
}
}
for (int i = 1; i <= N; i++) {
int lo = 1, hi = i;
while (hi > lo) {
int mid = (hi + lo) / 2;
bool flag = false;
for (int j = 0; j < 35; j++) {
if (arr[i] & (1ll << j)) {
continue;
}
if (range(j, mid, i - 1)) {
flag = true;
}
}
if (flag) {
lo = mid + 1;
} else {
hi = mid;
}
}
lt[i] = lo;
}
for (int i = 1; i <= N; i++) {
int lo = i, hi = N;
while (hi > lo) {
int mid = (hi + lo + 1) / 2;
bool flag = false;
for (int j = 0; j < 35; j++) {
if (arr[i] & (1ll << j)) {
continue;
}
if (range(j, i + 1, mid)) {
flag = true;
}
}
if (flag) {
hi = mid - 1;
} else {
lo = mid;
}
}
rt[i] = hi;
}
nums.push_back(make_pair(1000000000000000861ll, 0));
for (int i = 1; i <= N; i++) {
while (nums.back().first < arr[i]) {
nums.pop_back();
}
lbig[i] = nums.back().second + 1;
nums.push_back(make_pair(arr[i], i));
}
nums.clear();
nums.push_back(make_pair(1000000000000000861ll, N + 1));
for (int i = N; i > 0; i--) {
while (nums.back().first <= arr[i]) {
nums.pop_back();
}
rbig[i] = nums.back().second - 1;
nums.push_back(make_pair(arr[i], i));
}
nums.clear();
ans = choose2(N);
for (int i = 1; i <= N; i++) {
int64_t lo = max(lbig[i], lt[i]), hi = min(rbig[i], rt[i]);
ans -= choose2(hi - lo + 1);
ans += choose2(i - lo);
ans += choose2(hi - i);
}
cout << ans << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
const int LOG = 17;
int n, in[MAXN];
int take[MAXN];
int or_val[LOG + 1][MAXN];
int Or(int l, int r) {
int gap = r - l + 1;
return (or_val[take[gap]][l] | or_val[take[gap]][r - (1 << take[gap]) + 1]);
}
int main() {
take[1] = 0;
for (int i = 2; i < MAXN; ++i)
if ((1 << (take[i - 1] + 1)) <= i)
take[i] = take[i - 1] + 1;
else
take[i] = take[i - 1];
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d", &in[i]);
for (int i = 0; i < n; ++i) or_val[0][i] = in[i];
for (int l = 0; l < LOG; ++l)
for (int i = 0; i < n; ++i) {
or_val[l + 1][i] = or_val[l][i];
if (i + (1 << l) < n) or_val[l + 1][i] |= or_val[l][i + (1 << l)];
}
vector<int> idx;
for (int i = 0; i < n; ++i) idx.push_back(i);
sort(idx.begin(), idx.end(), [](int f, int s) { return in[f] > in[s]; });
long long bad_ivals = 0;
set<pair<int, int> > ival;
ival.insert(make_pair(0, n - 1));
for (int e : idx) {
auto it = ival.lower_bound(make_pair(e + 1, -1));
--it;
int lb = it->first, rb = it->second;
ival.erase(*it);
if (lb < e) ival.insert(make_pair(lb, e - 1));
if (e < rb) ival.insert(make_pair(e + 1, rb));
int il = e, ir = e;
for (int gap = 1 << LOG; gap > 0;) {
if (il - gap < lb || Or(il - gap, e) != in[e]) {
gap >>= 1;
continue;
}
il -= gap;
}
for (int gap = 1 << LOG; gap > 0;) {
if (ir + gap > rb || Or(e, ir + gap) != in[e]) {
gap >>= 1;
continue;
}
ir += gap;
}
bad_ivals += (e - il + 1) * 1LL * (ir - e + 1);
}
cout << (n * (n - 1LL)) / 2 + n - bad_ivals << '\n';
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const int maxn = 3e5 + 10;
int n, N;
int a[maxn], b1[maxn], b2[maxn];
int seg[maxn << 2];
void update(int i, int v, int l = 0, int r = N, int id = 0) {
if (r - l == 1) {
seg[id] = v;
return;
}
int mid = (l + r) >> 1;
if (i < mid)
update(i, v, l, mid, ((id << 1) + 1));
else
update(i, v, mid, r, ((id << 1) + 2));
seg[id] = seg[((id << 1) + 1)] | seg[((id << 1) + 2)];
}
int solve(int l1, int r1, int l = 0, int r = N, int id = 0) {
if (l >= r1 || r <= l1) return 0;
if (l >= l1 && r <= r1) return seg[id];
int mid = (l + r) >> 1;
return solve(l1, r1, l, mid, ((id << 1) + 1)) |
solve(l1, r1, mid, r, ((id << 1) + 2));
}
int32_t main() {
scanf("%d", &n);
int x = 1;
while (x <= n) x <<= 1;
N = x;
for (int i = 0; i < n; i++) scanf("%d", &a[i]), update(i, a[i]);
vector<int> s;
s.push_back(-1);
for (int i = 0; i < n; i++) {
if (s.back() == -1) {
b1[i] = 0, s.push_back(i);
continue;
}
while (s.back() != -1 && a[i] > a[s.back()]) s.pop_back();
b1[i] = s.back() + 1;
s.push_back(i);
}
s.clear();
s.push_back(n);
for (int i = n - 1; i >= 0; i--) {
if (s.back() == n) {
b2[i] = n - 1, s.push_back(i);
continue;
}
while (s.back() != n && a[i] >= a[s.back()]) s.pop_back();
b2[i] = s.back() - 1;
s.push_back(i);
}
long long ans = 0;
for (int i = 0; i < n; i++) {
int l = b1[i] - 1, r = i;
while (r - l > 1) {
int mid = (l + r) >> 1;
int x = solve(mid, i + 1);
if ((x & a[i]) == x)
r = mid;
else
l = mid;
}
int pt1 = r;
l = i, r = b2[i] + 1;
while (r - l > 1) {
int mid = (l + r) >> 1;
int x = solve(i, mid + 1);
if ((x & a[i]) == x)
l = mid;
else
r = mid;
}
int pt2 = l;
long long L = i - pt1 + 1;
long long R = pt2 - i + 1;
long long res = L * R * 1LL;
L = i - b1[i] + 1;
R = b2[i] - i + 1;
res = ((L * R) - res) * 1LL;
ans += res;
}
printf("%lld\n", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long k = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
k = k * 10 + ch - '0';
ch = getchar();
}
return k * f;
}
long long s[200010], l[200010], r[200010], top;
long long n, a[200010], t[800010], ans = 0;
inline void build(long long l, long long r, long long nod) {
if (l == r) {
t[nod] = a[l];
return;
}
long long mid = l + r >> 1;
build(l, mid, nod * 2);
build(mid + 1, r, nod * 2 + 1);
t[nod] = t[nod * 2] | t[nod * 2 + 1];
}
inline long long ssum(long long l, long long r, long long i, long long j,
long long nod) {
if (l >= i && r <= j) return t[nod];
long long mid = l + r >> 1, ans = 0;
if (i <= mid) ans |= ssum(l, mid, i, j, nod * 2);
if (j > mid) ans |= ssum(mid + 1, r, i, j, nod * 2 + 1);
return ans;
}
signed main() {
n = read();
for (long long i = 1; i <= n; i++) a[i] = read();
build(1, n, 1);
top = 0;
for (long long i = 1; i <= n; i++) {
while (top && a[s[top]] <= a[i]) r[s[top]] = i - 1, top--;
s[++top] = i;
}
while (top) r[s[top--]] = n;
top = 0;
for (long long i = n; i; i--) {
while (top && a[s[top]] < a[i]) l[s[top]] = i + 1, top--;
s[++top] = i;
}
while (top) l[s[top--]] = 1;
for (long long i = 1; i <= n; i++) {
if (l[i] == r[i]) continue;
long long L = l[i], R = i, ansl = l[i] - 1;
while (L <= R) {
long long mid = L + R >> 1;
if (ssum(1, n, mid, i, 1) > a[i])
ansl = mid, L = mid + 1;
else
R = mid - 1;
}
L = i, R = r[i];
long long ansr = r[i] + 1;
while (L <= R) {
long long mid = L + R >> 1;
if (ssum(1, n, i, mid, 1) > a[i])
ansr = mid, R = mid - 1;
else
L = mid + 1;
}
if (ansl != l[i] - 1) ans += (ansl - l[i] + 1) * (r[i] - i + 1);
if (ansr != r[i] + 1) ans += (i - l[i] + 1) * (r[i] - ansr + 1);
if (ansl != l[i] - 1 && ansr != r[i] + 1)
ans -= (ansl - l[i] + 1) * (r[i] - ansr + 1);
}
printf("%lld", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5;
const int P = 998244353;
int n, a[N], b[50];
int l[N], r[N];
map<int, int> f;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
cin >> n;
long long re = (long long)n * (n + 1) / 2;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
l[i] = 1 + f[a[i]];
f[a[i]] = i;
for (int j = 0; j < 30; j++) {
if (a[i] >> j & 1) {
b[j] = i;
} else
l[i] = max(l[i], b[j] + 1);
}
}
for (int i = 0; i < 30; i++) b[i] = 1 + n;
for (int i = n; i > 0; --i) {
r[i] = 1 + n;
for (int j = 0; j < 30; j++) {
if (a[i] >> j & 1) {
b[j] = i;
} else
r[i] = min(r[i], b[j] - 1);
}
re -= (long long)(i - l[i] + 1) * (r[i] - i + 1);
}
cout << re;
}
|
#include <bits/stdc++.h>
using namespace std;
int A[200010];
set<int> proc;
int N;
int btchng[200010][31][2];
long long calc(int idx) {
int val = A[idx];
auto lm = proc.lower_bound(idx);
int lmr = *lm;
lm--;
int lml = *lm;
int fidx = lmr, pidx = lml;
for (int j = 0; j < 31; j++)
if (!(val & (1 << j))) {
fidx = min(fidx, btchng[idx][j][1]);
pidx = max(pidx, btchng[idx][j][0]);
}
long long selr = lmr - fidx, sell = pidx - lml;
proc.insert(idx);
return (idx - lml) * selr + (lmr - idx) * sell - sell * selr;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> N;
for (int i = 1; i <= N; i++) cin >> A[i];
for (int j = 0; j < 31; j++) btchng[0][j][0] = -1;
for (int j = 0; j < 31; j++) btchng[N + 1][j][1] = N + 1;
for (int i = 1; i <= N; i++)
for (int j = 0; j < 31; j++)
btchng[i][j][0] = (A[i] & (1 << j)) ? i : btchng[i - 1][j][0];
for (int i = N; i >= 1; i--)
for (int j = 0; j < 31; j++)
btchng[i][j][1] = (A[i] & (1 << j)) ? i : btchng[i + 1][j][1];
proc.insert(0);
proc.insert(N + 1);
vector<int> vals(N);
for (int i = 0; i < N; i++) vals[i] = i + 1;
sort(vals.rbegin(), vals.rend(),
[](int lhs, int rhs) { return A[lhs] < A[rhs]; });
long long total = 0;
for (auto x : vals) total += calc(x);
cout << total << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
inline void gtmx(int &a, int b) {
if (a < b) a = b;
}
inline void gtmn(int &a, int b) {
if (a > b) a = b;
}
int a[200010];
int l[200010], r[200010];
int lst[33];
map<int, int> mp;
int main(void) {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
memset(lst, 255, sizeof(lst));
long long res = 1ll * n * (n - 1);
res >>= 1;
for (int i = 1; i <= n; ++i) {
int mx = -1;
if (mp.find(a[i]) != mp.end()) {
mx = mp[a[i]];
}
mp[a[i]] = i;
for (int j = 30; ~j; --j) {
if (a[i] & (1 << j)) {
lst[j] = i;
} else {
gtmx(mx, lst[j]);
}
}
l[i] = (~mx) ? i - mx : i;
}
memset(lst, 63, sizeof(lst));
for (int i = n; i; --i) {
int mn = n + 1;
for (int j = 30; ~j; --j) {
if (a[i] & (1 << j)) {
lst[j] = i;
} else {
gtmn(mn, lst[j]);
}
}
r[i] = (mn <= n) ? mn - i : n - i + 1;
}
for (int i = 1; i <= n; ++i) {
res -= 1ll * l[i] * r[i] - 1;
}
printf("%I64d", res);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, a[200010];
long long ans;
int ri[200010][50];
int pre2[200010];
set<long long> s;
int cal(int l, int r) {
if (l == r) return a[l];
int x = pre2[r - l + 1];
int x1 = ri[l][x], x2 = ri[r - (1 << x) + 1][x];
return x1 | x2;
}
long long poi[200010];
int main() {
scanf("%d", &n);
pre2[0] = -1;
for (int i = 1; i <= n; i++) pre2[i] = pre2[i >> 1] + 1;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
ri[i][0] = a[i];
poi[i] = (long long)1000000 * (long long)a[i] + (long long)i;
}
sort(poi + 1, poi + n + 1);
for (int j = 1; j <= 25; j++) {
for (int i = 1; i <= n; i++) {
if (i + (1 << (j - 1)) <= n)
ri[i][j] = ri[i][j - 1] | ri[i + (1 << (j - 1))][j - 1];
}
}
set<long long>::iterator it;
s.insert(0);
s.insert(n + 1);
for (int i = n; i; i--) {
int le, ri;
s.insert(poi[i] % (long long)1000000);
it = s.find(poi[i] % (long long)1000000);
--it;
le = *it;
++it;
++it;
ri = *it;
int w1 = 0, w2 = 0;
int el = poi[i] % (long long)1000000, er = ri - 1;
while (el <= er) {
int mid = el + er >> 1;
if (cal(poi[i] % (long long)1000000, mid) ==
poi[i] / (long long)1000000) {
w2 = mid;
el = mid + 1;
} else
er = mid - 1;
}
el = le + 1;
er = poi[i] % (long long)1000000;
while (el <= er) {
int mid = el + er >> 1;
if (cal(mid, poi[i] % (long long)1000000) ==
poi[i] / (long long)1000000) {
w1 = mid;
er = mid - 1;
} else
el = mid + 1;
}
ans += (long long)(max(w1 - 1 - (le + 1) + 1, 0)) *
(long long)(max(ri - 1 - poi[i] % (long long)1000000 + 1,
(long long)0));
ans += (long long)(poi[i] % (long long)1000000 - w1 + 1) *
(long long)(max(0, ri - 1 - (w2 + 1) + 1));
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
cin.sync_with_stdio(false);
long long int n;
cin >> n;
vector<long long int> a(n), start(n), end(n);
for (long long int i = 0; i < n; i++) {
cin >> a[i];
start[i] = i;
end[i] = i + 1;
}
stack<pair<long long int, long long int>> s;
for (long long int i = 0; i < n; i++) {
while (!s.empty() && (a[i] | s.top().first) == a[i]) {
s.pop();
}
if (s.empty()) {
start[i] = 0;
} else {
start[i] = s.top().second + 1;
}
s.push(make_pair(a[i], i));
}
stack<pair<long long int, long long int>> s2;
for (long long int i = n - 1; i >= 0; i--) {
while (!s2.empty() && (a[i] | s2.top().first) == a[i]) {
s2.pop();
}
if (s2.empty()) {
end[i] = n;
} else {
end[i] = s2.top().second;
}
s2.push(make_pair(a[i], i));
}
unordered_map<long long int, long long int> last;
long long int good = (n * (n - 1)) / 2;
for (long long int i = 0; i < n; i++) {
if (last.count(a[i]) && last[a[i]] >= start[i]) {
good -= (i - last[a[i]]) * (end[i] - i) - 1;
last[a[i]] = i;
continue;
}
last[a[i]] = i;
good -= (i - start[i] + 1) * (end[i] - i) - 1;
}
cout << good << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int toInt(string s) {
int r = 0;
istringstream sin(s);
sin >> r;
return r;
}
long long int a[200005], l[200005], r[200005];
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
;
long long int n;
cin >> n;
for (long long int i = 0; i <= n - 1; i++) {
cin >> a[i];
l[i] = -1;
r[i] = n;
}
stack<long long int> s, s2;
for (long long int i = 0; i <= n - 1; i++) {
if (s.size()) {
while (s.size() && ((a[s.top()] | a[i]) == a[i])) {
if (a[s.top()] == a[i]) break;
s.pop();
}
if (s.size()) l[i] = s.top();
}
s.push(i);
}
long long int ans = n * (n + 1) / 2;
for (long long int i = n - 1; i >= 0; i--) {
if (s2.size()) {
while (s2.size() && ((a[s2.top()] | a[i]) == a[i])) s2.pop();
if (s2.size()) r[i] = s2.top();
}
s2.push(i);
ans -= (i - l[i]) * (r[i] - i);
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int sparse_or[200000][20], sparse_max_pos[200000][20], h[200000];
long long result = 0;
void process_range(int l, int r) {
int lev = 0;
while ((1 << (lev + 1)) <= r - l + 1) {
lev++;
}
int mid;
{
int mpos1 = sparse_max_pos[l][lev];
int mpos2 = sparse_max_pos[r - (1 << lev) + 1][lev];
if (h[mpos1] > h[mpos2]) {
mid = mpos1;
} else {
mid = mpos2;
}
}
int left = mid, right = mid + 1;
for (int lev = 19; lev >= 0; lev--) {
if (left - (1 << lev) >= l - 1 &&
(sparse_or[left - (1 << lev) + 1][lev] | h[mid]) == h[mid]) {
left -= (1 << lev);
}
}
for (int lev = 19; lev >= 0; lev--) {
if (right + (1 << lev) <= r + 1 &&
(sparse_or[right][lev] | h[mid]) == h[mid]) {
right += 1 << lev;
}
}
result += 1LL * (mid + 1 - l) * (r + 1 - right);
result += 1LL * (left + 1 - l) * (r + 1 - mid);
result -= 1LL * (left + 1 - l) * (r + 1 - right);
if (l < mid) {
process_range(l, mid - 1);
}
if (mid < r) {
process_range(mid + 1, r);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> h[i];
sparse_or[i][0] = h[i];
sparse_max_pos[i][0] = i;
}
for (int lev = 1; lev < 20; lev++) {
for (int i = 0; i < n; i++) {
sparse_or[i][lev] = sparse_or[i][lev - 1];
sparse_max_pos[i][lev] = sparse_max_pos[i][lev - 1];
if (i + (1 << (lev - 1)) < n) {
sparse_or[i][lev] |= sparse_or[i + (1 << (lev - 1))][lev - 1];
if (h[sparse_max_pos[i][lev]] <
h[sparse_max_pos[i + (1 << (lev - 1))][lev - 1]]) {
sparse_max_pos[i][lev] =
sparse_max_pos[i + (1 << (lev - 1))][lev - 1];
}
}
}
}
process_range(0, n - 1);
cout << result << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int m, n, mn, ans, cnt, p[5005][4];
char c[5005];
int v[5005], vw;
int f(char c) {
switch (c) {
case 'L':
return 0;
case 'R':
return 1;
case 'U':
return 2;
case 'D':
return 3;
}
}
int solve(int x) {
for (int i = 0; i < mn; ++i) {
p[i][0] = i % n == 0 ? -1 : i - 1;
p[i][1] = i % n == n - 1 ? -1 : i + 1;
p[i][2] = i - n;
p[i][3] = i + n;
}
vw = x;
int ret = 0;
while (1) {
ret++;
v[x] = vw;
int z = f(c[x]);
int X = x;
while (c[X] == '.' || v[X] == vw) {
X = p[X][z];
if (!(0 <= X && X < mn)) return ret;
}
p[x][z] = X;
x = X;
}
}
int main() {
scanf("%d%d", &m, &n);
mn = m * n;
for (int i = 0; i < m; ++i) scanf("%s", c + i * n);
memset(v, -1, sizeof(v));
ans = cnt = 0;
for (int i = 0; i < mn; ++i) {
if (c[i] != '.') {
int tmp = solve(i);
if (tmp > ans) {
ans = tmp;
cnt = 1;
} else if (tmp == ans)
cnt++;
}
}
printf("%d %d\n", ans, cnt);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > p, pr;
vector<vector<int> > t;
int n, m;
inline int solve(int x, int y) {
p = pr;
int ans = 0, nx, ny, v, tmp;
t.assign(n * m, vector<int>(4, 1));
while (true) {
v = p[x][y];
p[x][y] = -1;
ans++;
nx = -1;
ny = -1;
if (v == 0) {
for (int i = y - 1; i >= 0 && ny == -1;)
if (p[x][i] >= 0)
ny = i;
else
i -= t[m * x + i][0];
if (ny == -1) break;
for (int i = y; i >= ny;) {
tmp = i;
i -= t[m * x + i][0];
t[m * x + tmp][0] = tmp - ny + 1;
}
y = ny;
} else if (v == 1) {
for (int i = y + 1; i < m && ny == -1;)
if (p[x][i] >= 0)
ny = i;
else
i += t[m * x + i][1];
if (ny == -1) break;
for (int i = y; i <= ny;) {
tmp = i;
i += t[m * x + i][1];
t[m * x + tmp][1] = ny - tmp + 1;
}
y = ny;
} else if (v == 2) {
for (int i = x - 1; i >= 0 && nx == -1;)
if (p[i][y] >= 0)
nx = i;
else
i -= t[i * m + y][2];
if (nx == -1) break;
for (int i = x; i >= nx;) {
tmp = i;
i -= t[i * m + y][2];
t[tmp * m + y][2] = tmp - nx + 1;
}
x = nx;
} else if (v == 3) {
for (int i = x + 1; i < n && nx == -1;)
if (p[i][y] >= 0)
nx = i;
else
i += t[i * m + y][3];
if (nx == -1) break;
for (int i = x; i <= nx;) {
tmp = i;
i += t[i * m + y][3];
t[tmp * m + y][3] = nx - tmp + 1;
}
x = nx;
} else
break;
}
return ans;
}
int main() {
cin >> n >> m;
p.resize(n, vector<int>(m, -1));
char ch;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> ch;
if (ch == 'L')
p[i][j] = 0;
else if (ch == 'R')
p[i][j] = 1;
else if (ch == 'U')
p[i][j] = 2;
else if (ch == 'D')
p[i][j] = 3;
}
}
pr = p;
int mans = 0, cans = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
if (pr[i][j] == -1) continue;
int sol = solve(i, j);
if (sol == mans)
cans++;
else if (sol > mans) {
mans = sol;
cans = 1;
}
}
cout << mans << ' ' << cans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char mp[5005][5005];
int n, m, t, nr;
int L[5005 << 1], R[5005 << 1], U[5005 << 1], D[5005 << 1];
void init() {
for (int i = t; i < t + n; i++) L[i] = R[i] = i;
for (int i = t; i < t + m; i++) U[i] = D[i] = i;
}
void remove(int idx) {
U[D[idx]] = U[idx];
D[U[idx]] = D[idx];
L[R[idx]] = L[idx];
R[L[idx]] = R[idx];
}
void resume(int idx) {
D[U[idx]] = U[D[idx]] = idx;
L[R[idx]] = R[L[idx]] = idx;
}
void solve(int i, int j, int d) {
int idx = i * m + j;
remove(idx);
if (mp[i][j] == 'L') {
if (L[idx] == i + t)
nr = (nr > d + 1 ? nr : d + 1);
else
solve(L[idx] / m, L[idx] % m, d + 1);
} else if (mp[i][j] == 'R') {
if (R[idx] == i + t)
nr = (nr > d + 1 ? nr : d + 1);
else
solve(R[idx] / m, R[idx] % m, d + 1);
} else if (mp[i][j] == 'U') {
if (U[idx] == j + t)
nr = (nr > d + 1 ? nr : d + 1);
else
solve(U[idx] / m, U[idx] % m, d + 1);
} else if (mp[i][j] == 'D') {
if (D[idx] == j + t)
nr = (nr > d + 1 ? nr : d + 1);
else
solve(D[idx] / m, D[idx] % m, d + 1);
}
resume(idx);
}
int main() {
int ne, me, idx, res, cnt;
while (~scanf("%d%d", &n, &m)) {
for (int i = 0; i < n; i++) scanf(" %s", mp[i]);
t = n * m;
res = -1;
cnt = 1;
init();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mp[i][j] == '.') continue;
ne = j + t, me = i + t;
idx = i * m + j;
U[idx] = U[ne];
D[idx] = ne;
D[U[idx]] = idx;
U[ne] = idx;
L[idx] = L[me];
R[idx] = me;
R[L[idx]] = idx;
L[me] = idx;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mp[i][j] == '.') continue;
nr = 0;
solve(i, j, 0);
if (nr == res) {
cnt++;
} else if (nr > res) {
res = nr;
cnt = 1;
}
}
}
printf("%d %d\n", res, cnt);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct Cell {
char c;
pair<int, int> a[4];
Cell() : c(5) {}
};
vector<vector<Cell> > b;
int n, m;
inline void remove(int x, int y) {
pair<int, int> u = b[x][y].a[0];
b[u.first][u.second].a[2] = b[x][y].a[2];
u = b[x][y].a[1];
b[u.first][u.second].a[3] = b[x][y].a[3];
u = b[x][y].a[2];
b[u.first][u.second].a[0] = b[x][y].a[0];
u = b[x][y].a[3];
b[u.first][u.second].a[1] = b[x][y].a[1];
b[x][y].c = 4;
}
int getNum(int x, int y) {
int r = 0;
while (x <= n && y <= m && x > 0 && y > 0) {
++r;
pair<int, int> next = b[x][y].a[b[x][y].c];
remove(x, y);
x = next.first, y = next.second;
}
return r;
}
int main(int argc, char** argv) {
char cc[256];
cc['.'] = 4;
cc['L'] = 0;
cc['U'] = 1;
cc['R'] = 2;
cc['D'] = 3;
if (argc > 1) freopen(argv[1], "r", stdin);
scanf("%d%d\n", &n, &m);
vector<vector<Cell> > a(n + 2, vector<Cell>(m + 2));
for (int i = (1); i < (n + 1); ++i) {
for (int j = (1); j < (m + 1); ++j) {
scanf("%c", &a[i][j].c);
a[i][j].c = cc[a[i][j].c];
a[i][j].a[0] =
(a[i][j - 1].c != 4) ? make_pair(i, j - 1) : a[i][j - 1].a[0];
a[i][j].a[1] =
(a[i - 1][j].c != 4) ? make_pair(i - 1, j) : a[i - 1][j].a[1];
}
scanf("\n");
}
for (int i = (n); i >= (1); --i)
for (int j = (m); j >= (1); --j) {
a[i][j].a[2] =
(a[i][j + 1].c != 4) ? make_pair(i, j + 1) : a[i][j + 1].a[2];
a[i][j].a[3] =
(a[i + 1][j].c != 4) ? make_pair(i + 1, j) : a[i + 1][j].a[3];
}
int mx = 0, cnt = 0;
for (int i = (1); i < (n + 1); ++i)
for (int j = (1); j < (m + 1); ++j)
if (a[i][j].c != 4) {
b = a;
int k = getNum(i, j);
if (k > mx)
mx = k, cnt = 1;
else if (k == mx)
++cnt;
}
printf("%d %d\n", mx, cnt);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline int count_bits(int x) {
x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4);
x = (x & 0x00ff00ff) + ((x & 0xff00ff00) >> 8);
x = (x & 0x0000ffff) + ((x & 0xffff0000) >> 16);
return x;
}
inline int count_bits_32(int x) {
x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4);
x = (x & 0x00ff00ff) + ((x & 0xff00ff00) >> 8);
x = (x & 0x0000ffff) + ((x & 0xffff0000) >> 16);
return x;
}
inline int count_bits_16(int x) {
x = (x & 0x00005555) + ((x & 0x0000aaaa) >> 1);
x = (x & 0x00003333) + ((x & 0x0000cccc) >> 2);
x = (x & 0x00000f0f) + ((x & 0x0000f0f0) >> 4);
x = (x & 0x000000ff) + ((x & 0x0000ff00) >> 8);
return x;
}
inline int count_bits_8(int x) {
x = (x & 0x00000055) + ((x & 0x000000aa) >> 1);
x = (x & 0x00000033) + ((x & 0x000000cc) >> 2);
x = (x & 0x0000000f) + ((x & 0x0000f0f0) >> 4);
return x;
}
int reverse_bits(int x) {
x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa);
x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);
x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0);
x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);
x = ((x >> 16) & 0x0000ffff) | ((x << 16) & 0xffff0000);
return x;
}
template <class T>
inline T low_bit(T x) {
return x & -x;
}
template <class T>
inline T high_bit(T x) {
T p = low_bit(x);
while (p != x) x -= p, p = low_bit(x);
return p;
}
template <class T>
inline void RD(T &x) {
cin >> x;
}
template <class T>
inline void RD(T &x0, T &x1) {
RD(x0), RD(x1);
}
template <class T>
inline void RD(T &x0, T &x1, T &x2) {
RD(x0), RD(x1), RD(x2);
}
template <class T>
inline void RD(T &x0, T &x1, T &x2, T &x3) {
RD(x0), RD(x1), RD(x2), RD(x3);
}
template <class T>
inline void RD(T &x0, T &x1, T &x2, T &x3, T &x4) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4);
}
template <class T>
inline void RD(T &x0, T &x1, T &x2, T &x3, T &x4, T &x5) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5);
}
template <class T>
inline void RST(T &A) {
memset(A, 0, sizeof(A));
}
template <class T>
inline void RST(T &A0, T &A1) {
RST(A0), RST(A1);
}
template <class T>
inline void RST(T &A0, T &A1, T &A2) {
RST(A0), RST(A1), RST(A2);
}
template <class T>
inline void RST(T &A0, T &A1, T &A2, T &A3) {
RST(A0), RST(A1), RST(A2), RST(A3);
}
template <class T>
inline void RST(T &A0, T &A1, T &A2, T &A3, T &A4) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);
}
template <class T>
inline void RST(T &A0, T &A1, T &A2, T &A3, T &A4, T &A5) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);
}
template <class T>
inline void FLC(T &A, int x) {
memset(A, x, sizeof(A));
}
template <class T>
inline void FLC(T &A0, T &A1, int x) {
FLC(A0, x), FLC(A1, x);
}
template <class T>
inline void FLC(T &A0, T &A1, T &A2, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x);
}
template <class T>
inline void FLC(T &A0, T &A1, T &A2, T &A3, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x);
}
template <class T>
inline void FLC(T &A0, T &A1, T &A2, T &A3, T &A4, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x);
}
template <class T>
inline void FLC(T &A0, T &A1, T &A2, T &A3, T &A4, T &A5, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x);
}
template <class T>
inline void checkMin(T &a, T b) {
if (b < a) a = b;
}
template <class T>
inline void checkMax(T &a, T b) {
if (b > a) a = b;
}
template <class T>
inline T min(T a, T b, T c) {
return min(min(a, b), c);
}
template <class T>
inline T max(T a, T b, T c) {
return max(max(a, b), c);
}
template <class T>
inline T sqr(T a) {
return a * a;
}
template <class T>
inline T cub(T a) {
return a * a * a;
}
template <class T>
inline double dist(T x0, T x1, T y0, T y1) {
return sqrt(sqr(x0 - x1) + sqr(y0 - y1));
}
template <class T>
inline T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
template <class T>
inline T lcm(T a, T b) {
return a * b / gcd(a, b);
}
template <class T>
inline T random(T l, T r) {
return rand() % (r - l + 1) + l;
}
const int MOD = 1000000007;
const int INF = 0x7fffffff;
const int MaxN = 5009;
const int MaxK = 5009;
int N, M, tot;
char A[MaxN][MaxN], B[MaxK];
int L[MaxK], R[MaxK], U[MaxK], D[MaxK];
int id[MaxN][MaxN];
void Init() {
for (int i = 1; i <= N; i++)
for (int j = 1; j <= M; j++) {
L[id[i][j]] = id[i][j - 1];
R[id[i][j]] = id[i][j + 1];
U[id[i][j]] = id[i - 1][j];
D[id[i][j]] = id[i + 1][j];
}
for (int i = 1; i <= N; i++)
for (int j = 1; j <= M; j++)
if (A[i][j] == '.') {
R[L[id[i][j]]] = R[id[i][j]];
L[R[id[i][j]]] = L[id[i][j]];
U[D[id[i][j]]] = U[id[i][j]];
D[U[id[i][j]]] = D[id[i][j]];
}
}
void Union(int now) {
R[L[now]] = R[now];
L[R[now]] = L[now];
U[D[now]] = U[now];
D[U[now]] = D[now];
}
int Find(int now) {
if (now == 0) return 0;
Union(now);
if (B[now] == 'U') return Find(U[now]) + 1;
if (B[now] == 'D') return Find(D[now]) + 1;
if (B[now] == 'L') return Find(L[now]) + 1;
if (B[now] == 'R') return Find(R[now]) + 1;
}
void Solve() {
int BestAns = 0, AnsNum = 0;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= M; j++)
if (A[i][j] != '.') {
Init();
int ret = Find(id[i][j]);
if (ret > BestAns) {
BestAns = ret;
AnsNum = 1;
} else if (ret == BestAns)
AnsNum++;
}
cout << BestAns << " " << AnsNum << "\n";
}
int main() {
cin >> N >> M;
tot = 0;
memset(id, 0, sizeof(id));
for (int i = 1; i <= N; i++)
for (int j = 1; j <= M; j++) {
cin >> A[i][j];
id[i][j] = ++tot;
B[tot] = A[i][j];
}
Solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<char> > g;
int n, m;
vector<vector<int> > nxtcol1, prvcol1, nxtrow1, prvrow1;
int solve(int r, int c) {
int ret = 0;
while (true) {
if (r >= n || c >= m) break;
ret++;
int i = r, j = c;
if (g[r][c] == 'L') {
c = prvcol1[r][j];
nxtcol1[r][c] = nxtcol1[r][j];
prvcol1[r][nxtcol1[r][j]] = c;
nxtrow1[prvrow1[r][j]][j] = nxtrow1[r][j];
prvrow1[nxtrow1[r][j]][j] = prvrow1[r][j];
} else if (g[r][c] == 'R') {
c = nxtcol1[r][j];
prvcol1[r][c] = prvcol1[r][j];
nxtcol1[r][prvcol1[r][j]] = c;
nxtrow1[prvrow1[r][j]][j] = nxtrow1[r][j];
prvrow1[nxtrow1[r][j]][j] = prvrow1[r][j];
} else if (g[r][c] == 'U') {
r = prvrow1[r][c];
nxtrow1[r][c] = nxtrow1[i][c];
prvrow1[nxtrow1[i][c]][c] = r;
nxtcol1[i][prvcol1[i][c]] = nxtcol1[i][c];
prvcol1[i][nxtcol1[i][c]] = prvcol1[i][c];
} else if (g[r][c] == 'D') {
r = nxtrow1[r][c];
prvrow1[r][c] = prvrow1[i][c];
nxtrow1[prvrow1[i][c]][c] = r;
nxtcol1[i][prvcol1[i][c]] = nxtcol1[i][c];
prvcol1[i][nxtcol1[i][c]] = prvcol1[i][c];
}
}
return ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
g.resize(n + 5);
vector<vector<int> > nxtcol(n + 5, vector<int>(m + 5));
vector<vector<int> > prvcol(n + 5, vector<int>(m + 5));
vector<vector<int> > nxtrow(n + 5, vector<int>(m + 5));
vector<vector<int> > prvrow(n + 5, vector<int>(m + 5));
for (int i = 0; i < n; i++) {
g[i].resize(m + 5);
for (int j = 0; j < m; j++) cin >> g[i][j];
}
for (int i = 0; i < n; i++) {
int last = m;
for (int j = m - 1; j >= 0; j--) {
if (g[i][j] != '.') {
nxtcol[i][j] = last;
prvcol[i][last] = j;
last = j;
}
}
prvcol[i][last] = m + 1;
}
for (int i = 0; i < m; i++) {
int last = n;
for (int j = n - 1; j >= 0; j--) {
if (g[j][i] != '.') {
nxtrow[j][i] = last;
prvrow[last][i] = j;
last = j;
}
}
prvrow[last][i] = n + 1;
}
int ans = 0, cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (g[i][j] != '.') {
nxtcol1 = nxtcol;
prvcol1 = prvcol;
nxtrow1 = nxtrow;
prvrow1 = prvrow;
int temp = solve(i, j);
if (ans < temp)
ans = temp, cnt = 1;
else if (ans == temp)
cnt++;
}
}
}
cout << ans << " " << cnt << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
int k;
bool debug = false;
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1}, ss;
map<char, int> cd;
struct uz {
int direct, u = 0, d = 0, l = 0, r = 0;
};
vector<char> mp[5005];
vector<uz> b[5005];
void init() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (mp[i][j] == '.')
b[i][j].direct = -1;
else
b[i][j].direct = cd[mp[i][j]];
b[i][j].u = i - 1;
b[i][j].d = i + 1;
b[i][j].l = j - 1;
b[i][j].r = j + 1;
}
}
}
void move(int i, int j, int direct) {
if (mp[i][j] != '.') {
ss++;
direct = b[i][j].direct;
}
b[b[i][j].u][j].d = b[i][j].d;
b[b[i][j].d][j].u = b[i][j].u;
b[i][b[i][j].l].r = b[i][j].r;
b[i][b[i][j].r].l = b[i][j].l;
if (direct == 0)
i = b[i][j].u;
else if (direct == 1)
i = b[i][j].d;
else if (direct == 2)
j = b[i][j].l;
else if (direct == 3)
j = b[i][j].r;
if (i > 0 && j > 0 && i <= n && j <= m) move(i, j, direct);
}
int main() {
scanf("%d%d", &n, &m);
char cc[m + 5];
cd['U'] = 0;
cd['D'] = 1;
cd['L'] = 2;
cd['R'] = 3;
for (int i = 1; i <= n; i++) {
scanf("%s", cc);
mp[i].push_back('.');
for (int j = 0; j < m; j++) mp[i].push_back(cc[j]);
}
for (int i = 0; i < n + 3; i++) b[i].resize(m + 2);
int ans = 0, ansc = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (mp[i][j] != '.') {
init();
ss = 0;
move(i, j, b[i][j].direct);
if (ss > ans) {
ans = ss;
ansc = 1;
} else if (ss == ans)
ansc++;
}
printf("%d %d\n", ans, ansc);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T, class... S>
void dbs(string str, T t, S... s) {
int idx = str.find(',');
cout << str.substr(0, idx) << " : " << t << ",";
dbs(str.substr(idx + 1), s...);
}
template <class S, class T>
ostream& operator<<(ostream& os, const pair<S, T>& p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, const set<T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
template <class S, class T>
ostream& operator<<(ostream& os, const map<S, T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
using namespace std;
int N, M;
int mp(int i, int j) { return M * i + j; }
pair<int, int> dir[100];
vector<char> xc = {'U', 'R', 'L', 'D'};
struct eph {
bool val = 1;
char c;
int U, L, R, D;
int get(char c) {
if (c == 'U') return U;
if (c == 'L') return L;
if (c == 'R') return R;
if (c == 'D') return D;
assert(false);
}
void fil(char c, int x) {
if (c == 'U') U = x;
if (c == 'L') L = x;
if (c == 'R') R = x;
if (c == 'D') D = x;
}
int nx() { return get(c); }
};
bool valid(pair<int, int> cur) {
return cur.first >= 0 && cur.first < N && cur.second >= 0 && cur.second < M;
}
vector<eph> cur;
void del(int x) {
if (cur[x].U >= 0) {
cur[cur[x].U].D = cur[x].D;
}
if (cur[x].D >= 0) {
cur[cur[x].D].U = cur[x].U;
}
if (cur[x].L >= 0) {
cur[cur[x].L].R = cur[x].R;
}
if (cur[x].R >= 0) {
cur[cur[x].R].L = cur[x].L;
}
}
int cnt = 0;
void tryItFor(int x) {
del(x);
int y = cur[x].nx();
if (y < 0) return;
cnt++;
tryItFor(y);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
dir['U'] = {-1, 0};
dir['D'] = {1, 0};
dir['R'] = {0, 1};
dir['L'] = {0, -1};
vector<string> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
vector<eph> nexx(N * M);
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
nexx[mp(i, j)].c = A[i][j];
for (auto u : xc) {
pair<int, int> cur = {i + dir[u].first, j + dir[u].second};
while (valid(cur) && A[cur.first][cur.second] == '.') {
cur = {cur.first + dir[u].first, cur.second + dir[u].second};
}
if (!valid(cur)) {
nexx[mp(i, j)].fil(u, -1);
} else {
nexx[mp(i, j)].fil(u, mp(cur.first, cur.second));
}
}
}
}
int cx = 0, best = -1;
for (int i = 0; i < N * M; i++) {
if (A[i / M][i % M] == '.') {
continue;
}
cnt = 1;
cur = nexx;
tryItFor(i);
if (cnt == best) {
cx++;
} else if (cnt > best) {
cx = 1;
best = cnt;
}
}
cout << best << ' ' << cx << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const unsigned long long MOD = 257;
int n, m;
string s[5005];
int L[5005], R[5005], U[5005], D[5005];
void init() {
int b, pos;
for (int i = 0; i < n; i++) {
b = -1;
for (int j = 0; j < m; j++) {
if (s[i][j] != '.') {
pos = i * m + j;
L[pos] = b;
if (b != -1) R[b] = pos;
b = pos;
}
}
R[b] = -1;
}
for (int i = 0; i < m; i++) {
b = -1;
for (int j = 0; j < n; j++) {
if (s[j][i] != '.') {
pos = j * m + i;
U[pos] = b;
if (b != -1) {
D[b] = pos;
}
b = pos;
}
}
D[b] = -1;
}
}
int dfs(int now) {
int x = now / m, y = now % m;
if (L[now] != -1) R[L[now]] = R[now];
if (R[now] != -1) L[R[now]] = L[now];
if (U[now] != -1) D[U[now]] = D[now];
if (D[now] != -1) U[D[now]] = U[now];
int go;
if (s[x][y] == 'U')
go = U[now];
else if (s[x][y] == 'D')
go = D[now];
else if (s[x][y] == 'L')
go = L[now];
else if (s[x][y] == 'R')
go = R[now];
if (go == -1) return 1;
return dfs(go) + 1;
}
int main() {
std::ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
int ans = 0, num = 0, tmp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (s[i][j] != '.') {
init();
tmp = dfs(i * m + j);
if (tmp == ans)
num++;
else if (tmp > ans) {
ans = tmp;
num = 1;
}
}
}
}
cout << ans << " " << num << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
struct _ {
_() { ios_base::sync_with_stdio(0); }
} _;
template <class A, class B>
ostream &operator<<(ostream &o, pair<A, B> t) {
o << "(" << t.first << ", " << t.second << ")";
return o;
}
template <class T>
string tostring(T first, int len = 0, char c = '0') {
stringstream ss;
ss << first;
string r = ss.str();
if (r.length() < len) r = string(len - r.length(), c) + r;
return r;
}
template <class T>
void PV(T a, T b, int n = 0, int w = 0, string s = " ") {
int c = 0;
while (a != b) {
cout << tostring(*a++, w, ' ');
if (a != b && (n == 0 || ++c % n))
cout << s;
else
cout << endl;
}
}
template <class T>
inline bool chmin(T &a, T b) {
return a > b ? a = b, 1 : 0;
}
template <class T>
inline bool chmax(T &a, T b) {
return a < b ? a = b, 1 : 0;
}
const long long linf = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;
const int N = 5005;
int n, m;
char s[N][N];
char ss[N][N];
vector<vector<int>> L, R, U, D;
vector<vector<int>> L1, R1, U1, D1;
int main() {
cin >> n >> m;
L.resize(n, vector<int>(m));
R.resize(n, vector<int>(m));
U.resize(n, vector<int>(m));
D.resize(n, vector<int>(m));
for (int i = 0; i < n; i++) cin >> s[i];
for (int i = 0; i < n; i++) {
{
int pre = -1;
for (int j = 0; j < m; j++) {
L[i][j] = pre;
if (s[i][j] != '.') pre = j;
}
}
{
int pre = -1;
for (int j = m - 1; j >= 0; j--) {
R[i][j] = pre;
if (s[i][j] != '.') pre = j;
}
}
}
for (int j = 0; j < m; j++) {
{
int pre = -1;
for (int i = 0; i < n; i++) {
U[i][j] = pre;
if (s[i][j] != '.') pre = i;
}
}
{
int pre = -1;
for (int i = n - 1; i >= 0; i--) {
D[i][j] = pre;
if (s[i][j] != '.') pre = i;
}
}
}
L1 = L;
R1 = R;
U1 = U;
D1 = D;
long long cc = 0;
int res = 0;
int num = 0;
memcpy(ss, s, sizeof(ss));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (ss[i][j] == '.') continue;
L = L1;
R = R1;
U = U1;
D = D1;
for (int first = 0; first < n; first++)
for (int second = 0; second < m; second++)
s[first][second] = ss[first][second];
int ci = i, cj = j, ni = -1, nj = -1;
int cnt = 0;
while (1) {
cc++;
if (ci < 0 || cj < 0 || s[ci][cj] == '.') break;
cnt++;
if (s[ci][cj] == 'L') ni = ci, nj = L[ci][cj];
if (s[ci][cj] == 'R') ni = ci, nj = R[ci][cj];
if (s[ci][cj] == 'U') ni = U[ci][cj], nj = cj;
if (s[ci][cj] == 'D') ni = D[ci][cj], nj = cj;
if (R[ci][cj] >= 0) L[ci][R[ci][cj]] = L[ci][cj];
if (L[ci][cj] >= 0) R[ci][L[ci][cj]] = R[ci][cj];
if (D[ci][cj] >= 0) U[D[ci][cj]][cj] = U[ci][cj];
if (U[ci][cj] >= 0) D[U[ci][cj]][cj] = D[ci][cj];
s[ci][cj] = '.';
ci = ni, cj = nj;
}
if (res < cnt) {
res = cnt;
num = 1;
} else if (res == cnt) {
num++;
}
}
cout << res << " " << num << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-12;
const int INF = 999999999;
int m, n, tot;
char g0[5000], g[5000];
short f0[5000][4], f[5000][4];
int Id(char c) {
switch (c) {
case 'U':
return 0;
case 'R':
return 1;
case 'D':
return 2;
case 'L':
return 3;
}
return -1;
}
void Read() {
char buf[5001];
scanf("%d%d", &m, &n);
for (int i = 0; i < m; ++i) {
scanf("%s", buf);
for (int j = 0; j < n; ++j) g0[i * n + j] = Id(buf[j]);
}
tot = m * n;
for (int i = 0; i < tot; ++i) {
f0[i][0] = i;
if (i >= n && g0[i] == -1) f0[i][0] = i - n;
f0[i][3] = i;
if (i % n != 0 && g0[i] == -1) f0[i][3] = i - 1;
}
for (int i = tot - 1; i >= 0; --i) {
f0[i][2] = i;
if (i < tot - n && g0[i] == -1) f0[i][2] = i + n;
f0[i][1] = i;
if ((i + 1) % n != 0 && g0[i] == -1) f0[i][1] = i + 1;
}
}
void Remove(int i) {
g[i] = -1;
if (i >= n) f[i][0] = i - n;
if (i % n != 0) f[i][3] = i - 1;
if (i < tot - n) f[i][2] = i + n;
if ((i + 1) % n != 0) f[i][1] = i + 1;
}
int Find(int i, int d) {
if (f[i][d] == i) return i;
return f[i][d] = Find(f[i][d], d);
}
int Com(int i) {
memcpy(g, g0, sizeof(g));
memcpy(f, f0, sizeof(f));
int res = 0;
while (1) {
++res;
int d = g[i];
Remove(i);
int next = Find(i, d);
if (g[next] == -1) break;
i = next;
}
return res;
}
void Solve() {
int res = 0;
int count = 0;
for (int i = 0; i < tot; ++i)
if (g0[i] != -1) {
int w = Com(i);
if (w > res) {
res = w;
count = 0;
}
if (w == res) ++count;
}
printf("%d %d\n", res, count);
}
int main() {
Read();
Solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxN = 5120;
char maze[maxN][maxN];
int nr, nc;
int L[maxN], R[maxN];
int U[maxN], D[maxN];
void getUDLR() {
for (int i = 0; i < nr; i++) {
int l = -1;
for (int j = 0; j < nc; j++)
if (maze[i][j] != '.') {
int idx = i * nc + j;
L[idx] = l;
if (l != -1) R[l] = idx;
l = idx;
}
R[l] = -1;
}
for (int i = 0; i < nc; i++) {
int u = -1;
for (int j = 0; j < nr; j++)
if (maze[j][i] != '.') {
int idx = j * nc + i;
U[idx] = u;
if (u != -1) D[u] = idx;
u = idx;
}
D[u] = -1;
}
}
void del(int i, int j) {
int idx = i * nc + j;
if (U[idx] != -1) D[U[idx]] = D[idx];
if (D[idx] != -1) U[D[idx]] = U[idx];
if (L[idx] != -1) R[L[idx]] = R[idx];
if (R[idx] != -1) L[R[idx]] = L[idx];
}
int dfs(int i, int j) {
del(i, j);
int idx = i * nc + j;
if (maze[i][j] == 'U') {
if (U[idx] == -1)
return 1;
else
return 1 + dfs(U[idx] / nc, j);
} else if (maze[i][j] == 'D') {
if (D[idx] == -1)
return 1;
else
return 1 + dfs(D[idx] / nc, j);
} else if (maze[i][j] == 'L') {
if (L[idx] == -1)
return 1;
else
return 1 + dfs(i, L[idx] % nc);
} else if (maze[i][j] == 'R') {
if (R[idx] == -1)
return 1;
else
return 1 + dfs(i, R[idx] % nc);
}
}
int main() {
scanf("%d %d", &nr, &nc);
for (int i = 0; i < nr; i++) scanf("%s", maze[i]);
int num, max = -1;
for (int i = 0; i < nr; i++)
for (int j = 0; j < nc; j++)
if (maze[i][j] != '.') {
getUDLR();
int tmp = dfs(i, j);
if (tmp > max)
max = tmp, num = 1;
else if (tmp == max)
num++;
}
printf("%d %d\n", max, num);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char **g;
int n, m;
struct nodes {
nodes *u, *r, *d, *l;
char dir;
};
nodes **dl;
template <class T>
void getarr(T **&a) {
a = (T **)malloc((n + 10) * sizeof(T *));
for (int i = 0; i < n + 10; i++) {
a[i] = (T *)malloc((m + 10) * sizeof(T));
}
}
void del(nodes *p) {
p->u->d = p->d;
p->d->u = p->u;
p->r->l = p->l;
p->l->r = p->r;
}
int to[300];
void init() {
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
if (i != 0 && j != 0)
dl[i][j].dir = to[g[i][j]];
else
dl[i][j].dir = -1;
if (i > 0)
dl[i][j].u = &dl[i - 1][j];
else
dl[i][j].u = &dl[n][j];
if (j > 0)
dl[i][j].l = &dl[i][j - 1];
else
dl[i][j].l = &dl[i][m];
if (i < n)
dl[i][j].d = &dl[i + 1][j];
else
dl[i][j].d = &dl[0][j];
if (j < m)
dl[i][j].r = &dl[i][j + 1];
else
dl[i][j].r = &dl[i][0];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (g[i][j] == '.') del(&dl[i][j]);
}
}
}
int solve(int x, int y) {
nodes *ptr = &dl[x][y];
int cc = 0;
while (ptr->dir != -1) {
del(ptr);
cc++;
switch (ptr->dir) {
case 0:
ptr = ptr->u;
break;
case 1:
ptr = ptr->r;
break;
case 2:
ptr = ptr->d;
break;
case 3:
ptr = ptr->l;
break;
}
}
return cc;
}
int main() {
to['U'] = 0;
to['R'] = 1;
to['D'] = 2;
to['L'] = 3;
scanf("%d%d", &n, &m);
getarr(g);
getarr(dl);
for (int i = 1; i <= n; i++) {
scanf("%s", g[i] + 1);
}
int ans1 = -1, ans2 = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (g[i][j] != '.') {
init();
int ret = solve(i, j);
if (ans1 < ret) {
ans1 = ret;
ans2 = 1;
} else if (ans1 == ret) {
ans2++;
}
}
}
}
printf("%d %d\n", ans1, ans2);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5005;
char a[maxn][maxn];
vector<int> l[maxn], r[maxn], u[maxn], d[maxn];
int m, n;
int tmp;
void hide(int x, int y) {
l[x][r[x][y]] = l[x][y];
r[x][l[x][y]] = r[x][y];
d[u[x][y]][y] = d[x][y];
u[d[x][y]][y] = u[x][y];
}
void show(int x, int y) {
l[x][r[x][y]] = y;
r[x][l[x][y]] = y;
d[u[x][y]][y] = x;
u[d[x][y]][y] = x;
}
void dfs(int i, int j) {
if (i == 0 || j == 0) return;
++tmp;
hide(i, j);
if (a[i][j] == 'L') dfs(i, l[i][j]);
if (a[i][j] == 'R') dfs(i, r[i][j]);
if (a[i][j] == 'D') dfs(d[i][j], j);
if (a[i][j] == 'U') dfs(u[i][j], j);
show(i, j);
}
int main() {
scanf("%d%d", &m, &n);
for (int i = 0; i <= m + 1; ++i)
l[i].resize(n + 1), r[i].resize(n + 2), d[i].resize(n + 2),
u[i].resize(n + 2);
for (int i = 1; i <= m; ++i) scanf("%s", a[i] + 1);
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (j > 1 && a[i][j - 1] != '.')
l[i][j] = j - 1;
else
l[i][j] = l[i][j - 1];
if (i > 1 && a[i - 1][j] != '.')
u[i][j] = i - 1;
else
u[i][j] = u[i - 1][j];
}
}
for (int i = m; i > 0; --i) {
for (int j = n; j > 0; --j) {
if (j < n && a[i][j + 1] != '.')
r[i][j] = j + 1;
else
r[i][j] = r[i][j + 1];
if (i < m && a[i + 1][j] != '.')
d[i][j] = i + 1;
else
d[i][j] = d[i + 1][j];
}
}
int ans = 0, cnt = 0;
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j)
if (a[i][j] != '.') {
tmp = 0;
dfs(i, j);
if (tmp > ans)
ans = tmp, cnt = 1;
else if (tmp == ans)
++cnt;
}
}
printf("%d %d", ans, cnt);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<vector<pair<int, int> > > > nextCell, fNextCell;
vector<vector<int> > a;
vector<vector<bool> > vis;
pair<int, int> findnextCell(int x, int y, int d) {
pair<int, int> st = nextCell[x][y][d];
if (st != make_pair(-1, -1) &&
(vis[st.first][st.second] || a[st.first][st.second] == -1))
nextCell[x][y][d] = findnextCell(st.first, st.second, d);
return nextCell[x][y][d];
}
int main() {
ios_base::sync_with_stdio(0);
int n, m;
cin >> n >> m;
a.resize(n, vector<int>(m, -1));
char c;
for (int i = 0; i < n; i++) {
cin.ignore();
for (int j = 0; j < m; j++) {
cin.get(c);
if (c == 'L') a[i][j] = 0;
if (c == 'R') a[i][j] = 1;
if (c == 'U') a[i][j] = 2;
if (c == 'D') a[i][j] = 3;
}
}
nextCell.resize(n, vector<vector<pair<int, int> > >(
m, vector<pair<int, int> >(4, make_pair(-1, -1))));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (j != 0) nextCell[i][j][0] = {i, j - 1};
if (j != m - 1) nextCell[i][j][1] = {i, j + 1};
if (i != 0) nextCell[i][j][2] = {i - 1, j};
if (i != n - 1) nextCell[i][j][3] = {i + 1, j};
}
}
fNextCell = nextCell;
pair<int, int> ans = {0, 0};
vis.resize(n, vector<bool>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == -1) continue;
vis.assign(n, vector<bool>(m, false));
nextCell = fNextCell;
pair<int, int> cur = {i, j};
int tAns = 0;
while (cur != make_pair(-1, -1)) {
vis[cur.first][cur.second] = true;
tAns++;
cur = findnextCell(cur.first, cur.second, a[cur.first][cur.second]);
}
if (tAns > ans.first) {
ans.first = tAns;
ans.second = 1;
} else if (tAns == ans.first)
ans.second++;
}
}
cout << ans.first << " " << ans.second;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5001;
struct data {
int u, d, l, r;
};
int n, m;
string a[maxn];
data g[maxn];
int get(int x, int y) { return x * m + y; }
void init() {
memset(g, 255, sizeof(g));
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
int l = -1;
for (int j = 0; j < m; j++)
if (a[i][j] != '.') {
g[get(i, j)].l = l;
l = get(i, j);
}
int r = -1;
for (int j = m - 1; j >= 0; j--)
if (a[i][j] != '.') {
g[get(i, j)].r = r;
r = get(i, j);
}
}
for (int j = 0; j < m; j++) {
int u = -1;
for (int i = 0; i < n; i++)
if (a[i][j] != '.') {
g[get(i, j)].u = u;
u = get(i, j);
}
int d = -1;
for (int i = n - 1; i >= 0; i--)
if (a[i][j] != '.') {
g[get(i, j)].d = d;
d = get(i, j);
}
}
}
void del(int now) {
if (g[now].u != -1) g[g[now].u].d = g[now].d;
if (g[now].d != -1) g[g[now].d].u = g[now].u;
if (g[now].l != -1) g[g[now].l].r = g[now].r;
if (g[now].r != -1) g[g[now].r].l = g[now].l;
}
void recover(int now) {
if (g[now].u != -1) g[g[now].u].d = now;
if (g[now].d != -1) g[g[now].d].u = now;
if (g[now].l != -1) g[g[now].l].r = now;
if (g[now].r != -1) g[g[now].r].l = now;
}
int dfs(int now) {
int ret = 1;
int x = now / m, y = now % m;
if (a[x][y] == 'D') {
int tmp = g[now].d;
if (tmp != -1) {
del(now);
ret += dfs(tmp);
recover(now);
}
} else if (a[x][y] == 'U') {
int tmp = g[now].u;
if (tmp != -1) {
del(now);
ret += dfs(tmp);
recover(now);
}
} else if (a[x][y] == 'L') {
int tmp = g[now].l;
if (tmp != -1) {
del(now);
ret += dfs(tmp);
recover(now);
}
} else {
int tmp = g[now].r;
if (tmp != -1) {
del(now);
ret += dfs(tmp);
recover(now);
}
}
return ret;
}
void solve() {
int ans = 0, anstot = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (a[i][j] != '.') {
int tmp = dfs(get(i, j));
if (tmp > ans) {
ans = tmp;
anstot = 1;
} else if (tmp == ans)
++anstot;
}
cout << ans << ' ' << anstot << endl;
}
int main() {
init();
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5000 + 5;
short dir[4][maxn][maxn];
int h, w;
char mat[maxn][maxn];
void init() {
for (int i = 0; i < h; ++i) {
dir[1][i][0] = -1;
for (int j = 1; j < w; ++j) dir[1][i][j] = j - 1;
dir[3][i][w - 1] = -1;
for (int j = w - 2; j >= 0; --j) dir[3][i][j] = j + 1;
}
for (int i = 0; i < w; ++i) {
dir[2][0][i] = -1;
for (int j = 1; j < h; ++j) dir[2][j][i] = j - 1;
dir[0][h - 1][i] = -1;
for (int j = h - 2; j >= 0; --j) dir[0][j][i] = j + 1;
}
}
int d[][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
struct dat {
int r, c, d;
dat(){};
dat(int p1, int p2, int p3) : r(p1), c(p2), d(p3){};
};
int main() {
scanf("%d%d", &h, &w);
for (int i = 0; i < h; ++i) {
scanf("%s", mat[i]);
for (int j = 0; j < w; ++j) {
if (mat[i][j] == '.')
mat[i][j] = -1;
else if (mat[i][j] == 'D')
mat[i][j] = 0;
else if (mat[i][j] == 'L')
mat[i][j] = 1;
else if (mat[i][j] == 'U')
mat[i][j] = 2;
else
mat[i][j] = 3;
}
}
int mx = 0, cn = 0;
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
if (mat[i][j] == -1) continue;
init();
int cnt = 0;
int r = i, c = j, d = mat[r][c];
while (r != -1 && c != -1) {
if (r < 0 || c < 0 || r >= h || c >= w) cout << "A" << endl;
int left = dir[1][r][c];
int right = dir[3][r][c];
if (left != -1) dir[3][r][left] = right;
if (right != -1) dir[1][r][right] = left;
int down = dir[0][r][c];
int up = dir[2][r][c];
if (up != -1) dir[0][up][c] = down;
if (down != -1) dir[2][down][c] = up;
if (mat[r][c] != -1) d = mat[r][c], cnt++;
if (d == 0 || d == 2)
r = dir[d][r][c];
else
c = dir[d][r][c];
}
if (cnt > mx)
mx = cnt, cn = 1;
else if (cnt == mx)
cn++;
}
}
printf("%d %d\n", mx, cn);
return 0;
}
|
#include <bits/stdc++.h>
namespace algo {
using namespace std;
const long double PI = 3.141592653589793238462643383279502884L;
const long double E = 2.718281828459045235360287471352662498L;
const long double EPS = 1e-12L;
template <typename T1, typename T2>
T1 bin_pow(T1 a, T2 n) {
T1 r = 1;
while (n > 0) {
if (n & 1) r *= a;
a *= a;
n /= 2;
}
return r;
}
template <typename T1, typename T2>
T1 bin_pow(T1 a, T2 n, const T1& m) {
if (m == 1) return 0;
T1 r = 1;
while (n > 0) {
if (n & 1) {
r *= a;
r %= m;
}
a *= a;
a %= m;
n /= 2;
}
return r;
}
template <typename T>
T gcd(const T& a, const T& b) {
if (b == 0) return abs(a);
return gcd(b, a % b);
}
template <typename T>
T gcd(const T& a, const T& b, T& x, T& y) {
if (b == 0) {
x = a < 0 ? -1 : 1;
y = 0;
return x * a;
}
T d = gcd(b, a % b, y, x);
y -= (a / b) * x;
return d;
}
template <typename T>
T lcm(const T& a, const T& b) {
return abs(a / gcd(a, b) * b);
}
template <typename T>
T mod_add(const T& a, const T& b, const T& m) {
if (a < m - b) return a + b;
return a - (m - b);
}
template <typename T>
T mod_sub(const T& a, const T& b, const T& m) {
if (a < b) return a + (m - b);
return a - b;
}
template <typename T>
T mod_mul(T a, T b, const T& m) {
if (b > a) swap(a, b);
T r = 0;
while (b > 0) {
if (b & 1) r = mod_add(r, a, m);
a = mod_add(a, a, m);
b /= 2;
}
return r;
}
template <typename T1, typename T2>
T1 mod_pow(T1 a, T2 n, const T1& m) {
if (m == 1) return 0;
T1 r = 1;
while (n > 0) {
if (n & 1) r = mod_mul(r, a, m);
a = mod_mul(a, a, m);
n /= 2;
}
return r;
}
template <typename T>
T mod_inv(const T& a, const T& m) {
T x, y;
T d = gcd(a, m, x, y);
if (d != 1) return 0;
if (x < 0) x += m;
return x;
}
struct Sieve {
int* lpf = nullptr;
int* phi = nullptr;
int size;
vector<int> primes;
Sieve(bool calcPhi = false, int n = 0xfffff) : size(n) {
lpf = new int[size + 1];
fill(lpf, lpf + size + 1, 0);
lpf[1] = 1;
if (calcPhi) {
phi = new int[size + 1];
phi[1] = 1;
for (int i = 2; i <= size; ++i) {
if (lpf[i] == 0) {
phi[i] = i - 1;
lpf[i] = i;
primes.push_back(i);
}
for (int j = 0, t; j < primes.size() && (t = i * primes[j]) <= size;
++j) {
lpf[t] = primes[j];
if (primes[j] == lpf[i]) {
phi[t] = phi[i] * primes[j];
break;
}
phi[t] = phi[i] * (primes[j] - 1);
}
}
} else {
for (int i = 2; i <= size; ++i) {
if (lpf[i] == 0) {
lpf[i] = i;
primes.push_back(i);
}
for (int j = 0, t; j < primes.size() && (t = i * primes[j]) <= size;
++j) {
lpf[t] = primes[j];
if (primes[j] == lpf[i]) break;
}
}
}
}
Sieve(int n) : Sieve(false, n) {}
~Sieve() {
delete[] lpf;
lpf = nullptr;
delete[] phi;
phi = nullptr;
}
};
template <typename T>
T phi(T n) {
T r = n;
for (T i = 2; i * i <= n; ++i) {
if (n % i == 0) {
r -= r / i;
do {
n /= i;
} while (n % i == 0);
}
}
if (n > 1) r -= r / n;
return r;
}
} // namespace algo
using namespace std;
using namespace algo;
namespace task {
const int a1[4] = {1, -1, 0, 0}, a2[4] = {0, 0, 1, -1};
struct ww {
int l, r;
} a[5010], b[5010], c[5010], d[5010];
int i, j, k, n, m, K, an, re;
int h[200], ty[5010], f1[5010], f2[5010];
char p[5010][5010];
inline int find(int x, int y) {
if (y == 0) return b[x].r;
if (y == 1) return b[x].l;
if (y == 2) return a[x].r;
if (y == 3) return a[x].l;
}
inline void del(int x) {
if (a[x].l) a[a[x].l].r = a[x].r;
if (a[x].r) a[a[x].r].l = a[x].l;
if (b[x].l) b[b[x].l].r = b[x].r;
if (b[x].r) b[b[x].r].l = b[x].l;
}
int main() {
h['D'] = 0, h['U'] = 1, h['R'] = 2, h['L'] = 3;
scanf("%d %d", &n, &m);
for (i = 1; i <= n; i++) scanf("%s", p[i] + 1);
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
if (p[i][j] != '.') {
ty[++K] = h[p[i][j]];
if (f1[i]) a[f1[i]].r = K, a[K].l = f1[i];
f1[i] = K;
if (f2[j]) b[f2[j]].r = K, b[K].l = f2[j];
f2[j] = K;
}
for (j = 1; j <= K; j++) c[j] = a[j], d[j] = b[j];
for (i = 1; i <= K; i++) {
int ans = 0, now = i;
for (j = 1; j <= K; j++) a[j] = c[j], b[j] = d[j];
for (; now;) {
int A = find(now, ty[now]);
del(now);
ans++;
now = A;
}
if (ans > an)
an = ans, re = 1;
else
re += ans == an;
}
printf("%d %d\n", an, re);
return 0;
}
} // namespace task
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(11);
cout.setf(ios::fixed);
return task::main();
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5001;
char a[maxn][maxn];
int b[maxn][maxn], d[maxn], x[maxn], y[maxn];
int f[maxn][4], g[maxn][4];
int n, m, tot;
int solve(int k) {
int i, s = 0;
memcpy(g, f, sizeof(f));
i = k;
while (i) {
s++;
g[g[i][0]][1] = g[i][1];
g[g[i][1]][0] = g[i][0];
g[g[i][2]][3] = g[i][3];
g[g[i][3]][2] = g[i][2];
i = g[i][d[i]];
}
return s;
}
void init(int k, int x, int y) {
int i;
for (i = x - 1; i >= 0; i--)
if (a[i][y] != '.') {
f[k][0] = b[i][y];
break;
}
for (i = x + 1; i < n; i++)
if (a[i][y] != '.') {
f[k][1] = b[i][y];
break;
}
for (i = y - 1; i >= 0; i--)
if (a[x][i] != '.') {
f[k][2] = b[x][i];
break;
}
for (i = y + 1; i < m; i++)
if (a[x][i] != '.') {
f[k][3] = b[x][i];
break;
}
}
int main() {
int i, j, k, s = 0, t = 1;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++) {
scanf("%s", a[i]);
for (j = 0; j < m; j++)
if (a[i][j] != '.') {
tot++;
b[i][j] = tot;
x[tot] = i;
y[tot] = j;
if (a[i][j] == 'U')
d[tot] = 0;
else if (a[i][j] == 'D')
d[tot] = 1;
else if (a[i][j] == 'L')
d[tot] = 2;
else if (a[i][j] == 'R')
d[tot] = 3;
}
}
for (i = 1; i <= tot; i++) init(i, x[i], y[i]);
for (i = 1; i <= tot; i++) {
k = solve(i);
if (k > s) {
s = k;
t = 1;
} else if (k == s)
t++;
}
printf("%d %d\n", s, t);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
int N, M;
struct P {
int y, x;
P(int y, int x) : y(y), x(x) {}
P() {}
bool is_valid() { return 0 <= y && y < N && 0 <= x && x < M; }
};
vector<P> nextto[4][5020];
vector<P> nextcopy[4][5020];
string field[5020];
void remove(int i, int j) {
for (int r = 0; r < (int)(4); ++r) {
int r1 = r, r2 = (r + 2) % 4;
P p1 = nextto[r1][i][j], p2 = nextto[r2][i][j];
if (p1.is_valid()) nextto[r2][p1.y][p1.x] = p2;
if (p2.is_valid()) nextto[r1][p2.y][p2.x] = p1;
}
}
int doit2(int cy, int cx) {
P p = P(cy, cx);
char d = '*';
int res = 0;
for (;;) {
if (!p.is_valid()) break;
d = field[p.y][p.x];
res++;
int did = -1;
if (d == 'D') did = 0;
if (d == 'R') did = 1;
if (d == 'U') did = 2;
if (d == 'L') did = 3;
P nextp = nextto[did][p.y][p.x];
remove(p.y, p.x);
p = nextp;
}
return res;
}
int main() {
while (cin >> N >> M) {
for (int i = 0; i < (int)(N); ++i) {
cin >> field[i];
for (int r = 0; r < (int)(4); ++r) {
nextto[r][i] = vector<P>(M);
for (int j = 0; j < (int)(M); ++j)
nextto[r][i][j] = P(i + dy[r], j + dx[r]);
}
}
for (int i = 0; i < (int)(N); ++i)
for (int j = 0; j < (int)(M); ++j)
if (field[i][j] == '.') remove(i, j);
for (int r = 0; r < (int)(4); ++r)
for (int k = 0; k < (int)(N); ++k) nextcopy[r][k] = nextto[r][k];
int res1 = 0, res2 = 0;
for (int i = 0; i < (int)(N); ++i)
for (int j = 0; j < (int)(M); ++j)
if (field[i][j] != '.') {
for (int r = 0; r < (int)(4); ++r)
for (int k = 0; k < (int)(N); ++k) nextto[r][k] = nextcopy[r][k];
int cur = doit2(i, j);
if (cur > res1) res1 = cur, res2 = 0;
if (cur == res1) res2++;
}
cout << res1 << " " << res2 << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 5050;
short **nd, **nu, **nl, **nr;
char field[N][N];
int n, m;
int last;
inline void del(int x, int y) {
if (nu[x][y] != -1) nd[nu[x][y]][y] = nd[x][y];
if (nd[x][y] != -1) nu[nd[x][y]][y] = nu[x][y];
if (nl[x][y] != -1) nr[x][nl[x][y]] = nr[x][y];
if (nr[x][y] != -1) nl[x][nr[x][y]] = nl[x][y];
}
inline void recover(int x, int y) {
if (nu[x][y] != -1) nd[nu[x][y]][y] = x;
if (nd[x][y] != -1) nu[nd[x][y]][y] = x;
if (nl[x][y] != -1) nr[x][nl[x][y]] = y;
if (nr[x][y] != -1) nl[x][nr[x][y]] = y;
}
int getInt(char delim) {
int ret = 0;
char c;
while ((c = getchar()) != delim) ret = ret * 10 + c - '0';
return ret;
}
inline void build() {
for (int i = 0; i < n; i++) {
last = -1;
for (int j = 0; j < m; j++)
if (field[i][j] != '.') {
nl[i][j] = last;
last = j;
}
last = -1;
for (int j = m - 1; j >= 0; j--)
if (field[i][j] != '.') {
nr[i][j] = last;
last = j;
}
}
for (int j = 0; j < m; j++) {
last = -1;
for (int i = 0; i < n; i++)
if (field[i][j] != '.') {
nu[i][j] = last;
last = i;
}
last = -1;
for (int i = n - 1; i >= 0; i--) {
if (field[i][j] != '.') {
nd[i][j] = last;
last = i;
}
}
}
}
int main() {
n = getInt(' ');
m = getInt('\n');
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
field[i][j] = getchar();
}
getchar();
}
nu = new short *[n + 1];
nr = new short *[n + 1];
nl = new short *[n + 1];
nd = new short *[n + 1];
for (int i = 0; i < n; i++) {
nu[i] = new short[m + 1];
nr[i] = new short[m + 1];
nl[i] = new short[m + 1];
nd[i] = new short[m + 1];
}
int maxAns = -1, cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
if (field[i][j] != '.') {
int cur = 1;
int x = i, y = j;
int nx = x, ny = y;
build();
while (true) {
const char cmd = field[x][y];
if (cmd == 'L')
ny = nl[nx][ny];
else if (cmd == 'R')
ny = nr[nx][ny];
else if (cmd == 'U')
nx = nu[nx][ny];
else
nx = nd[nx][ny];
if (nx == -1 || ny == -1) break;
del(x, y);
cur++;
x = nx;
y = ny;
}
if (cur > maxAns) {
maxAns = cur;
cnt = 1;
} else if (cur == maxAns)
cnt++;
}
}
printf("%d %d\n", maxAns, cnt);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int N, M;
vector<int> adj[5010][4];
vector<int> adj1[5010][4];
int n, m;
char grid[5050];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
void ini() {
int i, j, k, li, lj;
for (i = 0; i < 5001; i++)
for (j = 0; j < 4; j++) adj[i][j].clear();
for (i = 0; i < n; i++) {
lj = -1;
for (j = 0; j < m; j++) {
if (grid[i * m + j] != '.') {
if (lj != -1) {
adj[i * m + j][0].push_back(i * m + lj);
adj[i * m + lj][1].push_back(i * m + j);
}
lj = j;
}
}
}
for (j = 0; j < m; j++) {
li = -1;
for (i = 0; i < n; i++) {
if (grid[i * m + j] != '.') {
if (li != -1) {
adj[i * m + j][2].push_back(li * m + j);
adj[li * m + j][3].push_back(i * m + j);
}
li = i;
}
}
}
}
void ini2() {
int i, j, k;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) {
for (k = 0; k < 4; k++) {
adj1[i * m + j][k] = adj[i * m + j][k];
}
}
}
int get(int f) {
int i, j, ret = 0, k, lo, hi, mid, u, v;
ini2();
while (1) {
if (f != -1) {
if (adj1[f][0].size())
u = adj1[f][0][0];
else
u = -1;
if (adj1[f][1].size())
v = adj1[f][1][0];
else
v = -1;
if (u != -1 && v != -1) {
adj1[u][1][0] = v;
adj1[v][0][0] = u;
} else {
if (u != -1) adj1[u][1].clear();
if (v != -1) adj1[v][0].clear();
}
if (adj1[f][2].size())
u = adj1[f][2][0];
else
u = -1;
if (adj1[f][3].size())
v = adj1[f][3][0];
else
v = -1;
if (u != -1 && v != -1) {
adj1[u][3][0] = v;
adj1[v][2][0] = u;
} else {
if (u != -1) adj1[u][3].clear();
if (v != -1) adj1[v][2].clear();
}
if (grid[f] == 'L') {
if (adj1[f][0].size())
f = adj1[f][0][0];
else
f = -1;
} else if (grid[f] == 'R') {
if (adj1[f][1].size())
f = adj1[f][1][0];
else
f = -1;
} else if (grid[f] == 'U') {
if (adj1[f][2].size())
f = adj1[f][2][0];
else
f = -1;
} else if (grid[f] == 'D') {
if (adj1[f][3].size())
f = adj1[f][3][0];
else
f = -1;
}
ret++;
} else {
break;
}
}
return ret;
}
int main() {
int i, j;
scanf("%d %d", &n, &m);
for (i = 0; i < n; i++) scanf("%s", grid + i * m);
ini();
int mx = 0, l;
int cnt = 0, now, x;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) {
if (grid[i * m + j] == '.') continue;
now = get(i * m + j);
if (now > mx) {
mx = now;
cnt = 1;
} else if (now == mx)
cnt++;
}
printf("%d %d\n", mx, cnt);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
const int op[] = {2, 3, 0, 1};
const char str[] = "URDL";
struct E {
pair<int, int> d[4];
pair<int, int> next;
E() { memset(d, -1, sizeof(d)); }
};
vector<string> in;
vector<vector<E> > arr, def;
inline void MakeLink(int i, int j) {
for (int k = 0; k < 4; k++)
if (str[k] == in[i][j]) arr[i][j].next = arr[i][j].d[k];
}
int Func(int x, int y) {
queue<pair<int, int> > q;
q.push(make_pair(x, y));
int ans = 0;
while (!q.empty()) {
pair<int, int> v = q.front();
q.pop();
x = v.first;
y = v.second;
if (x == -1 || y == -1) return ans;
ans++;
for (int i = 0; i < 4; i++)
if (arr[x][y].d[i].first != -1) {
int nx = arr[x][y].d[i].first;
int ny = arr[x][y].d[i].second;
arr[nx][ny].d[op[i]] = arr[x][y].d[op[i]];
MakeLink(nx, ny);
}
q.push(make_pair(arr[x][y].next.first, arr[x][y].next.second));
}
return ans;
}
int main() {
int n, m;
cin >> n >> m;
in.resize(n);
arr.assign(n, vector<E>(m));
for (int i = 0; i < n; i++) cin >> in[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (in[i][j] != '.') {
for (int k = 0; k < 4; k++) {
int nx = i + dx[k];
int ny = j + dy[k];
while (nx >= 0 && nx < n && ny >= 0 && ny < m) {
if (in[nx][ny] != '.') {
arr[i][j].d[k] = make_pair(nx, ny);
break;
}
nx += dx[k];
ny += dy[k];
}
}
MakeLink(i, j);
}
def = arr;
pair<int, int> ans(0, 0);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (in[i][j] != '.') {
arr = def;
int now = Func(i, j);
if (now > ans.first)
ans = make_pair(now, 1);
else if (now == ans.first)
ans.second++;
}
cout << ans.first << " " << ans.second << endl;
return 0;
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:65777216")
using namespace std;
char D[5001][5001] = {0};
bool used[5001][5001] = {0};
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int move(int x, int y) {
if (D[x][y] == 'U') return 0;
if (D[x][y] == 'R') return 1;
if (D[x][y] == 'D') return 2;
if (D[x][y] == 'L') return 3;
return -1;
}
int n, m;
int calc(int x, int y) {
used[x][y] = true;
int t = move(x, y);
while (x + dx[t] >= 0 && x + dx[t] < n && y + dy[t] >= 0 && y + dy[t] < m) {
if (D[x + dx[t]][y + dy[t]] != '.' && !used[x + dx[t]][y + dy[t]]) {
return 1 + calc(x + dx[t], y + dy[t]);
}
x += dx[t];
y += dy[t];
}
return 1;
}
bool op(char a, char b) {
if (a == 'L' && b == 'R') return true;
if (a == 'R' && b == 'L') return true;
if (a == 'U' && b == 'D') return true;
if (a == 'D' && b == 'U') return true;
return false;
}
int calc1(int x, int y) {
int ret = 1;
int lx = x, ly = y;
bool need = false;
while (true) {
used[x][y] = true;
int t = move(x, y);
bool finded = false;
int px = x, py = y;
if (need) x = lx, y = ly;
lx = px;
ly = py;
need = false;
while (x + dx[t] >= 0 && x + dx[t] < n && y + dy[t] >= 0 && y + dy[t] < m) {
if (D[x + dx[t]][y + dy[t]] != '.' && !used[x + dx[t]][y + dy[t]]) {
ret++;
finded = true;
if (op(D[lx][ly], D[x + dx[t]][y + dy[t]])) need = true;
x += dx[t];
y += dy[t];
break;
}
x += dx[t];
y += dy[t];
}
if (!finded) break;
}
return ret;
}
int main() {
scanf("%d%d\n", &n, &m);
for (int i = 0; i < n; i++) gets(D[i]);
int mx = 0, cnt = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
for (int ii = 0; ii < n; ii++)
for (int jj = 0; jj < m; jj++) used[ii][jj] = 0;
if (D[i][j] != '.') {
int val = calc1(i, j);
if (val == mx) cnt++;
if (val > mx) mx = val, cnt = 1;
}
}
cout << mx << " " << cnt << endl;
return 0;
}
|
#include <bits/stdc++.h>
namespace algo {
using namespace std;
const long double PI = 3.141592653589793238462643383279502884L;
const long double E = 2.718281828459045235360287471352662498L;
const long double EPS = 1e-12L;
template <typename T1, typename T2>
T1 bin_pow(T1 a, T2 n) {
T1 r = 1;
while (n > 0) {
if (n & 1) r *= a;
a *= a;
n /= 2;
}
return r;
}
template <typename T1, typename T2>
T1 bin_pow(T1 a, T2 n, const T1& m) {
if (m == 1) return 0;
T1 r = 1;
while (n > 0) {
if (n & 1) {
r *= a;
r %= m;
}
a *= a;
a %= m;
n /= 2;
}
return r;
}
template <typename T>
T gcd(const T& a, const T& b) {
if (b == 0) return abs(a);
return gcd(b, a % b);
}
template <typename T>
T gcd(const T& a, const T& b, T& x, T& y) {
if (b == 0) {
x = a < 0 ? -1 : 1;
y = 0;
return x * a;
}
T d = gcd(b, a % b, y, x);
y -= (a / b) * x;
return d;
}
template <typename T>
T lcm(const T& a, const T& b) {
return abs(a / gcd(a, b) * b);
}
template <typename T>
T mod_add(const T& a, const T& b, const T& m) {
if (a < m - b) return a + b;
return a - (m - b);
}
template <typename T>
T mod_sub(const T& a, const T& b, const T& m) {
if (a < b) return a + (m - b);
return a - b;
}
template <typename T>
T mod_mul(T a, T b, const T& m) {
if (b > a) swap(a, b);
T r = 0;
while (b > 0) {
if (b & 1) r = mod_add(r, a, m);
a = mod_add(a, a, m);
b /= 2;
}
return r;
}
template <typename T1, typename T2>
T1 mod_pow(T1 a, T2 n, const T1& m) {
if (m == 1) return 0;
T1 r = 1;
while (n > 0) {
if (n & 1) r = mod_mul(r, a, m);
a = mod_mul(a, a, m);
n /= 2;
}
return r;
}
template <typename T>
T mod_inv(const T& a, const T& m) {
T x, y;
T d = gcd(a, m, x, y);
if (d != 1) return 0;
if (x < 0) x += m;
return x;
}
struct Sieve {
int* lpf = nullptr;
int* phi = nullptr;
int size;
vector<int> primes;
Sieve(bool calcPhi = false, int n = 0xfffff) : size(n) {
lpf = new int[size + 1];
fill(lpf, lpf + size + 1, 0);
lpf[1] = 1;
if (calcPhi) {
phi = new int[size + 1];
phi[1] = 1;
for (int i = 2; i <= size; ++i) {
if (lpf[i] == 0) {
phi[i] = i - 1;
lpf[i] = i;
primes.push_back(i);
}
for (int j = 0, t; j < primes.size() && (t = i * primes[j]) <= size;
++j) {
lpf[t] = primes[j];
if (primes[j] == lpf[i]) {
phi[t] = phi[i] * primes[j];
break;
}
phi[t] = phi[i] * (primes[j] - 1);
}
}
} else {
for (int i = 2; i <= size; ++i) {
if (lpf[i] == 0) {
lpf[i] = i;
primes.push_back(i);
}
for (int j = 0, t; j < primes.size() && (t = i * primes[j]) <= size;
++j) {
lpf[t] = primes[j];
if (primes[j] == lpf[i]) break;
}
}
}
}
Sieve(int n) : Sieve(false, n) {}
~Sieve() {
delete[] lpf;
lpf = nullptr;
delete[] phi;
phi = nullptr;
}
};
template <typename T>
T phi(T n) {
T r = n;
for (T i = 2; i * i <= n; ++i) {
if (n % i == 0) {
r -= r / i;
do {
n /= i;
} while (n % i == 0);
}
}
if (n > 1) r -= r / n;
return r;
}
} // namespace algo
using namespace std;
using namespace algo;
namespace task {
const int a1[4] = {1, -1, 0, 0}, a2[4] = {0, 0, 1, -1};
struct ww {
int l, r;
} a[5010], b[5010], c[5010], d[5010];
int i, j, k, n, m, K, an, re;
int h[200], ty[5010], f1[5010], f2[5010];
char p[5010][5010];
inline int find(int x, int y) {
if (y == 0) return b[x].r;
if (y == 1) return b[x].l;
if (y == 2) return a[x].r;
if (y == 3) return a[x].l;
}
inline void del(int x) {
if (a[x].l) a[a[x].l].r = a[x].r;
if (a[x].r) a[a[x].r].l = a[x].l;
if (b[x].l) b[b[x].l].r = b[x].r;
if (b[x].r) b[b[x].r].l = b[x].l;
}
int main() {
h['D'] = 0, h['U'] = 1, h['R'] = 2, h['L'] = 3;
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++) scanf("%s", p[i] + 1);
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
if (p[i][j] != '.') {
ty[++K] = h[p[i][j]];
if (f1[i]) a[f1[i]].r = K, a[K].l = f1[i];
f1[i] = K;
if (f2[j]) b[f2[j]].r = K, b[K].l = f2[j];
f2[j] = K;
}
for (j = 1; j <= K; j++) c[j] = a[j], d[j] = b[j];
for (i = 1; i <= K; i++) {
int ans = 0, now = i;
for (j = 1; j <= K; j++) a[j] = c[j], b[j] = d[j];
for (; now;) {
int A = find(now, ty[now]);
del(now);
ans++;
now = A;
}
if (ans > an)
an = ans, re = 1;
else
re += ans == an;
}
printf("%d %d\n", an, re);
return 0;
}
} // namespace task
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(11);
cout.setf(ios::fixed);
return task::main();
}
|
#include <bits/stdc++.h>
using namespace std;
struct Counter {
static int k;
Counter() { k++; }
~Counter() { k--; }
};
int Counter::k = 0;
template <typename T>
void pr(const string& name, T t) {
cout << name << " = " << t << endl;
}
template <typename T, typename... Types>
void pr(const string& names, T t, Types... rest) {
auto comma_pos = names.find(',');
cout << names.substr(0, comma_pos) << " = " << t << ", ";
auto next_name_pos = names.find_first_not_of(" \t\n", comma_pos + 1);
pr(string(names, next_name_pos), rest...);
}
void mod(long long& a, long long b) {
a %= b;
if (a < 0) a += b;
}
const long long MOD = 1000000007;
int N, M;
char C[5005];
int R[5005], L[5005], D[5005], U[5005];
map<pair<int, int>, int> ID;
string B[5005];
int dfs(int id) {
if (id == 0) return 0;
int pr = R[id];
int pl = L[id];
int pu = U[id];
int pd = D[id];
R[L[id]] = R[id];
L[R[id]] = L[id];
U[D[id]] = U[id];
D[U[id]] = D[id];
int ret;
if (C[id] == 'L') ret = dfs(L[id]) + 1;
if (C[id] == 'R') ret = dfs(R[id]) + 1;
if (C[id] == 'U') ret = dfs(U[id]) + 1;
if (C[id] == 'D') ret = dfs(D[id]) + 1;
R[pl] = id;
L[pr] = id;
U[pd] = id;
D[pu] = id;
return ret;
}
int main() {
cin >> N >> M;
for (int i = (0); i <= (N - 1); i++) cin >> B[i];
int c = 0;
for (int i = (0); i <= (N - 1); i++)
for (int j = (0); j <= (M - 1); j++) {
ID[{i, j}] = ++c;
C[c] = B[i][j];
}
for (int i = (0); i <= (N - 1); i++) {
int p = 0;
for (int j = (0); j <= (M - 1); j++) {
L[ID[{i, j}]] = p;
if (B[i][j] != '.') p = ID[{i, j}];
}
p = 0;
for (int j = (M - 1); j >= (0); j--) {
R[ID[{i, j}]] = p;
if (B[i][j] != '.') p = ID[{i, j}];
}
}
for (int j = (0); j <= (M - 1); j++) {
int p = 0;
for (int i = (0); i <= (N - 1); i++) {
U[ID[{i, j}]] = p;
if (B[i][j] != '.') p = ID[{i, j}];
}
p = 0;
for (int i = (N - 1); i >= (0); i--) {
D[ID[{i, j}]] = p;
if (B[i][j] != '.') p = ID[{i, j}];
}
}
int ans = 0, times = 1;
for (int i = (0); i <= (N - 1); i++)
for (int j = (0); j <= (M - 1); j++)
if (B[i][j] != '.') {
int x = dfs(ID[{i, j}]);
if (x > ans) ans = x, times = 0;
if (x >= ans) times++;
}
cout << ans << " " << times;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool umin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool umax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
char arr[5009][5009];
int n, m;
vector<int> L[5009], D[5009], U[5009], R[5009];
int tap0(int x, int y) {
if (L[x][y] == y) return y;
return L[x][y] = tap0(x, L[x][y]);
}
int tap1(int x, int y) {
if (R[x][y] == y) return y;
return R[x][y] = tap1(x, R[x][y]);
}
int tap2(int x, int y) {
if (U[x][y] == x) return x;
return U[x][y] = tap2(U[x][y], y);
}
int tap3(int x, int y) {
if (D[x][y] == x) return x;
return D[x][y] = tap3(D[x][y], y);
}
int dfs(int i, int j) {
L[i][j] = tap0(i, L[i][j - 1]);
R[i][j] = tap1(i, R[i][j + 1]);
U[i][j] = tap2(U[i - 1][j], j);
D[i][j] = tap3(D[i + 1][j], j);
if (arr[i][j] == 'L') {
if (tap0(i, j)) return dfs(i, tap0(i, j)) + 1;
return 1;
}
if (arr[i][j] == 'R') {
if (tap1(i, j)) return dfs(i, tap1(i, j)) + 1;
return 1;
}
if (arr[i][j] == 'U') {
if (tap2(i, j)) return dfs(tap2(i, j), j) + 1;
return 1;
}
if (arr[i][j] == 'D') {
if (tap3(i, j)) return dfs(tap3(i, j), j) + 1;
return 1;
}
assert(0);
}
void reset() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (arr[i][j] == '.')
L[i][j] = tap0(i, L[i][j - 1]);
else
L[i][j] = j;
}
for (int j = m; j >= 1; j--) {
if (arr[i][j] == '.')
R[i][j] = tap1(i, R[i][j + 1]);
else
R[i][j] = j;
}
}
for (int j = 1; j <= m; j++) {
for (int i = 1; i <= n; i++) {
if (arr[i][j] == '.')
U[i][j] = tap2(U[i - 1][j], j);
else
U[i][j] = i;
}
for (int i = n; i >= 1; i--) {
if (arr[i][j] == '.')
D[i][j] = tap3(D[i + 1][j], j);
else
D[i][j] = i;
}
}
}
int main() {
srand((unsigned)time(NULL));
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
L[i].resize(m + 3);
R[i].resize(m + 3);
D[i].resize(m + 3);
U[i].resize(m + 3);
for (int j = 1; j <= m; j++) scanf(" %c", &arr[i][j]);
}
U[0].resize(m + 3);
U[n + 1].resize(m + 3);
D[0].resize(m + 3);
D[n + 1].resize(m + 3);
int mx = 0, cnt = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (arr[i][j] != '.') {
reset();
int k = dfs(i, j);
if (umax(mx, k)) cnt = 0;
if (mx == k) cnt++;
}
printf("%d %d\n", mx, cnt);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, cnt, Max;
vector<vector<char> > vc, a;
vector<vector<int> > up, down, rght, lft;
inline void del(int i, int j) {
if (down[i][j] != n) up[down[i][j]][j] = up[i][j];
if (up[i][j] != -1) down[up[i][j]][j] = down[i][j];
if (rght[i][j] != m) lft[i][rght[i][j]] = lft[i][j];
if (lft[i][j] != -1) rght[i][lft[i][j]] = rght[i][j];
}
inline int dfs(int i, int j) {
if (a[i][j] == '.') return 0;
int ret = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (a[i][j] == '.') del(i, j);
while (1) {
if (i <= -1 || i >= n || j <= -1 || j >= m) break;
ret++;
del(i, j);
if (a[i][j] == 'U')
i = up[i][j];
else if (a[i][j] == 'D')
i = down[i][j];
else if (a[i][j] == 'R')
j = rght[i][j];
else
j = lft[i][j];
}
return ret;
}
int main() {
cin >> n >> m;
vc.resize(n);
up.resize(n);
down.resize(n);
lft.resize(n);
rght.resize(n);
for (int i = 0; i < n; i++) {
up[i].resize(m);
down[i].resize(m);
lft[i].resize(m);
rght[i].resize(m);
vc[i].resize(m);
for (int j = 0; j < m; j++) cin >> vc[i][j];
}
a = vc;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (vc[i][j] != '.') {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
up[i][j] = i - 1;
down[i][j] = i + 1;
rght[i][j] = j + 1;
lft[i][j] = j - 1;
}
int hlp = dfs(i, j);
if (hlp == Max) cnt++;
if (hlp > Max) Max = hlp, cnt = 1;
}
cout << Max << ' ' << cnt << endl;
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
struct fr {
int up;
int down;
int left;
int right;
};
int n, m, ans = 0, kol = 1, an = 0;
vector<vector<char>> x;
vector<vector<fr>> pos, cpos;
int main() {
cin >> n >> m;
x.resize(n);
pos.resize(n);
cpos.resize(n);
for (int i = 0; i < (int)(n); i++) {
x[i].resize(m);
pos[i].resize(m);
cpos[i].resize(m);
for (int j = 0; j < (int)(m); j++) cin >> x[i][j];
}
for (int i = 0; i < (int)(n); i++)
for (int j = 0; j < (int)(m); j++) {
bool q = false;
if (x[i][j] == '.') {
pos[i][j].up = -1;
pos[i][j].down = -1;
pos[i][j].left = -1;
pos[i][j].right = -1;
continue;
} else {
for (int k = j - 1; k >= 0; k--)
if (x[i][k] != '.') {
pos[i][j].left = k;
q = true;
break;
}
if (!q) pos[i][j].left = -1;
q = false;
for (int k = j + 1; k < m; k++)
if (x[i][k] != '.') {
pos[i][j].right = k;
q = true;
break;
}
if (!q) pos[i][j].right = -1;
q = false;
for (int k = i - 1; k >= 0; k--)
if (x[k][j] != '.') {
pos[i][j].up = k;
q = true;
break;
}
if (!q) pos[i][j].up = -1;
q = false;
for (int k = i + 1; k < n; k++)
if (x[k][j] != '.') {
pos[i][j].down = k;
q = true;
break;
}
if (!q) pos[i][j].down = -1;
}
}
for (int i = 0; i < (int)(n); i++)
for (int j = 0; j < (int)(m); j++) {
copy(pos.begin(), pos.end(), cpos.begin());
an = 0;
if (x[i][j] == '.')
continue;
else {
int p = i, l = j;
while (1) {
an++;
if (x[p][l] == 'L') {
if (cpos[p][l].left != -1) {
if (cpos[p][l].up != -1)
cpos[cpos[p][l].up][l].down = cpos[p][l].down;
if (cpos[p][l].down != -1)
cpos[cpos[p][l].down][l].up = cpos[p][l].up;
if (cpos[p][l].left != -1)
cpos[p][cpos[p][l].left].right = cpos[p][l].right;
if (cpos[p][l].right != -1)
cpos[p][cpos[p][l].right].left = cpos[p][l].left;
l = cpos[p][l].left;
} else
break;
} else if (x[p][l] == 'R') {
if (cpos[p][l].right != -1) {
if (cpos[p][l].up != -1)
cpos[cpos[p][l].up][l].down = cpos[p][l].down;
if (cpos[p][l].down != -1)
cpos[cpos[p][l].down][l].up = cpos[p][l].up;
if (cpos[p][l].left != -1)
cpos[p][cpos[p][l].left].right = cpos[p][l].right;
if (cpos[p][l].right != -1)
cpos[p][cpos[p][l].right].left = cpos[p][l].left;
l = cpos[p][l].right;
} else
break;
} else if (x[p][l] == 'U') {
if (cpos[p][l].up != -1) {
if (cpos[p][l].up != -1)
cpos[cpos[p][l].up][l].down = cpos[p][l].down;
if (cpos[p][l].down != -1)
cpos[cpos[p][l].down][l].up = cpos[p][l].up;
if (cpos[p][l].left != -1)
cpos[p][cpos[p][l].left].right = cpos[p][l].right;
if (cpos[p][l].right != -1)
cpos[p][cpos[p][l].right].left = cpos[p][l].left;
p = cpos[p][l].up;
} else
break;
} else if (x[p][l] == 'D') {
if (cpos[p][l].down != -1) {
if (cpos[p][l].up != -1)
cpos[cpos[p][l].up][l].down = cpos[p][l].down;
if (cpos[p][l].down != -1)
cpos[cpos[p][l].down][l].up = cpos[p][l].up;
if (cpos[p][l].left != -1)
cpos[p][cpos[p][l].left].right = cpos[p][l].right;
if (cpos[p][l].right != -1)
cpos[p][cpos[p][l].right].left = cpos[p][l].left;
p = cpos[p][l].down;
} else
break;
}
}
if (ans < an) {
ans = an;
kol = 1;
} else if (ans == an)
kol++;
}
}
cout << ans << " " << kol;
return 0;
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
const double PI = 2 * acos(0.0);
const double EPS = 1e-8;
const int INF = 1000000000;
int m, n, ans, cr, ca;
char s[5000][5001], ss[5000][5001];
int f[5000][5000];
vector<short> du[5000], dd[5000], dl[5000], dr[5000];
int fun(int r, int c) {
int rr = r, cc = c;
int res = 0;
while (true) {
cr = rr;
ca = cc;
char k = s[rr][cc];
++res;
s[rr][cc] = '.';
if (k == 'U') {
while (rr >= 0 && s[rr][cc] == '.') rr -= du[rr][cc];
du[cr][ca] = cr - rr;
if (rr < 0) return res;
} else if (k == 'D') {
while (rr < m && s[rr][cc] == '.') rr += dd[rr][cc];
dd[cr][ca] = rr - cr;
if (rr >= m) return res;
} else if (k == 'L') {
while (cc >= 0 && s[rr][cc] == '.') cc -= dl[rr][cc];
dl[cr][ca] = ca - cc;
if (cc < 0) return res;
} else if (k == 'R') {
while (cc < n && s[rr][cc] == '.') cc += dr[rr][cc];
dr[cr][ca] = cc - ca;
if (cc >= n) return res;
};
};
return 0;
};
int main() {
scanf("%d%d\n", &m, &n);
for (int ii = 0; ii < m; ++ii) {
du[ii].assign(1, n);
dl[ii].assign(1, n);
dd[ii].assign(1, n);
dr[ii].assign(1, n);
};
for (int i = 0; i < m; ++i) gets(ss[i]);
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j) f[i][j] = -1;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (ss[i][j] != '.') {
for (int ii = 0; ii < m; ++ii) {
du[ii].assign(n, 1);
dl[ii].assign(n, 1);
dd[ii].assign(n, 1);
dr[ii].assign(n, 1);
};
for (int ii = 0; ii < m; ++ii)
for (int jj = 0; jj < n; ++jj) {
s[ii][jj] = ss[ii][jj];
};
f[i][j] = fun(i, j);
ans = max(ans, f[i][j]);
};
int cnt = 0;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (f[i][j] == ans) ++cnt;
cout << ans << " " << cnt;
return 0;
};
|
#include <bits/stdc++.h>
using namespace std;
char aa[5001];
int n, m;
bool avail[5001];
int L[5001], R[5001], U[5001], D[5001];
int cnt[5001];
int encode(int i, int j) {
if (i < 0 || i >= n || j < 0 || j >= m) return -1;
return i * m + j;
}
void del(int cur) {
int ll = L[cur], rr = R[cur], uu = U[cur], dd = D[cur];
if (ll != -1) R[ll] = rr;
if (rr != -1) L[rr] = ll;
if (uu != -1) D[uu] = dd;
if (dd != -1) U[dd] = uu;
avail[cur] = 0;
}
int main() {
int i, j, ii, jj;
while (scanf("%d%d", &n, &m) == 2) {
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
char c;
scanf(" %c", &c);
aa[i * m + j] = c;
L[i * m + j] = encode(i, j - 1);
R[i * m + j] = encode(i, j + 1);
U[i * m + j] = encode(i - 1, j);
D[i * m + j] = encode(i + 1, j);
}
}
memset(cnt, 0, sizeof(cnt));
for (ii = 0; ii < n; ii++) {
for (jj = 0; jj < m; jj++) {
int now = encode(ii, jj);
if (aa[now] == '.') continue;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
L[i * m + j] = encode(i, j - 1);
R[i * m + j] = encode(i, j + 1);
U[i * m + j] = encode(i - 1, j);
D[i * m + j] = encode(i + 1, j);
avail[i * m + j] = 1;
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
int cur = encode(i, j);
if (aa[cur] == '.') del(cur);
}
}
int xx = 0;
while (now != -1) {
int temp = now;
if (aa[now] == 'L') {
now = L[now];
} else if (aa[now] == 'R') {
now = R[now];
} else if (aa[now] == 'U') {
now = U[now];
} else if (aa[now] == 'D') {
now = D[now];
}
del(temp);
xx++;
}
cnt[xx]++;
}
}
for (i = 5000; i >= 0; i--) {
if (cnt[i]) {
printf("%d %d\n", i, cnt[i]);
break;
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, cur, id[5010][5010], l[5010], r[5010], u[5010], d[5010], L[5010],
R[5010], U[5010], D[5010], a1, a2;
char c[5010][5010], dir[5010];
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
cin >> c[i][j];
if (c[i][j] != '.') {
id[i][j] = ++cur;
dir[cur] = c[i][j];
}
}
for (int i = 0; i < n; i++) {
int pre = 0;
for (int j = 0; j < m; j++)
if (c[i][j] != '.') L[id[i][j]] = pre, R[pre] = id[i][j], pre = id[i][j];
}
for (int j = 0; j < m; j++) {
int pre = 0;
for (int i = 0; i < n; i++)
if (c[i][j] != '.') U[id[i][j]] = pre, D[pre] = id[i][j], pre = id[i][j];
}
for (int i = 1; i < cur + 1; i++) {
for (int j = 1; j < cur + 1; j++)
l[j] = L[j], r[j] = R[j], u[j] = U[j], d[j] = D[j];
int p = 0, now = i;
while (now) {
p++;
l[r[now]] = l[now];
r[l[now]] = r[now];
u[d[now]] = u[now];
d[u[now]] = d[now];
if (dir[now] == 'L')
now = l[now];
else if (dir[now] == 'R')
now = r[now];
else if (dir[now] == 'U')
now = u[now];
else
now = d[now];
}
if (p > a1)
a1 = p, a2 = 1;
else if (p == a1)
a2++;
}
cout << a1 << " " << a2;
}
|
#include <bits/stdc++.h>
using namespace std;
static inline int Rint() {
struct X {
int dig[256];
X() {
for (int i = '0'; i <= '9'; ++i) dig[i] = 1;
dig['-'] = 1;
}
};
static X fuck;
int s = 1, v = 0, c;
for (; !fuck.dig[c = getchar()];)
;
if (c == '-')
s = 0;
else if (fuck.dig[c])
v = c ^ 48;
for (; fuck.dig[c = getchar()]; v = v * 10 + (c ^ 48))
;
return s ? v : -v;
}
template <typename T>
void cmax(T& a, const T& b) {
if (b > a) a = b;
}
template <typename T>
void cmin(T& a, const T& b) {
if (b < a) a = b;
}
int buff[5005];
int mem[5005];
int n, m;
int magic;
int L[5005], R[5005], U[5005], D[5005];
inline int valid(int i, int j) { return i >= 0 && j >= 0 && i < n && j < m; }
inline int id(int i, int j) {
if (!valid(i, j)) return -1;
return i * m + j;
}
inline int& at(int i, int j) { return buff[i * m + j]; }
char line[5005];
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int cvt[255];
void remove(int who) {
int a = L[who];
int b = R[who];
int c = U[who];
int d = D[who];
if (a != -1) R[a] = b;
if (b != -1) L[b] = a;
if (c != -1) D[c] = d;
if (d != -1) U[d] = c;
}
int main() {
for (int i = 0; i < 4; ++i) cvt["UDLR"[i]] = i;
cin >> n >> m;
magic = n * m;
for (int i = 0; i < n; ++i) {
scanf("%s", line);
for (int j = 0; j < m; ++j) at(i, j) = line[j];
}
int mans = 0, cans = 0;
for (int i = (0); i < (n); ++i)
for (int j = (0); j < (m); ++j)
if (at(i, j) != '.') {
memcpy(mem, buff, sizeof buff);
for (int a = 0; a < n; ++a)
for (int b = 0; b < m; ++b) {
L[id(a, b)] = id(a, b - 1);
R[id(a, b)] = id(a, b + 1);
U[id(a, b)] = id(a - 1, b);
D[id(a, b)] = id(a + 1, b);
}
int tot = 0;
int curr = id(i, j);
for (; curr != -1;) {
int d = cvt[buff[curr]];
remove(curr);
buff[curr] = '.';
++tot;
if (d == 0) {
int now = U[curr];
while (now != -1) {
if (buff[now] == '.') {
remove(now);
now = U[now];
} else {
break;
}
}
curr = now;
} else if (d == 1) {
int now = D[curr];
while (now != -1) {
if (buff[now] == '.') {
remove(now);
now = D[now];
} else {
break;
}
}
curr = now;
} else if (d == 2) {
int now = L[curr];
while (now != -1) {
if (buff[now] == '.') {
remove(now);
now = L[now];
} else {
break;
}
}
curr = now;
} else {
int now = R[curr];
while (now != -1) {
if (buff[now] == '.') {
remove(now);
now = R[now];
} else {
break;
}
}
curr = now;
}
}
if (tot > mans) {
mans = tot;
cans = 1;
} else if (tot == mans) {
++cans;
}
memcpy(buff, mem, sizeof buff);
}
cout << mans << " " << cans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 5001;
struct node {
pair<int, int> l, r, u, d;
node() {}
void assign(int id, pair<int, int> hh) {
if (id == 0) l = hh;
if (id == 1) r = hh;
if (id == 2) u = hh;
if (id == 3) d = hh;
}
};
vector<vector<node> > bb;
vector<vector<node> > xi;
char mm[MAX][MAX];
vector<vector<int> > ans;
int n, m;
int dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0};
inline bool core(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
int main() {
scanf("%d%d", &n, &m);
bb.resize(n), xi.resize(n), ans.resize(n);
for (int i = 0; i < n; i++)
bb[i].resize(m), xi[i].resize(m), ans[i].resize(m);
for (int i = 0; i < n; i++) scanf("%s", mm[i]);
for (int r = 0; r < n; r++) {
for (int c = 0; c < m; c++) {
if (mm[r][c] == '.') continue;
for (int d = 0; d < 4; d++) {
for (int k = 1; k <= max(n, m); k++) {
int x1 = r + k * dx[d], y1 = c + k * dy[d];
if (!core(x1, y1)) {
xi[r][c].assign(d, {-1, -1});
break;
} else {
if (mm[x1][y1] != '.') {
xi[r][c].assign(d, {x1, y1});
break;
}
}
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mm[i][j] == '.') continue;
for (int r = 0; r < n; r++) {
for (int c = 0; c < m; c++) bb[r][c] = xi[r][c];
}
int cnt = 0;
int rs = i, cs = j;
while (rs != -1) {
cnt++;
pair<int, int> l = bb[rs][cs].l, r = bb[rs][cs].r, u = bb[rs][cs].u,
d = bb[rs][cs].d;
if (l.first != -1) {
bb[l.first][l.second].r = r;
}
if (r.first != -1) {
bb[r.first][r.second].l = l;
}
if (u.first != -1) {
bb[u.first][u.second].d = d;
}
if (d.first != -1) {
bb[d.first][d.second].u = u;
}
if (mm[rs][cs] == 'L') {
pair<int, int> l = bb[rs][cs].l;
rs = l.first, cs = l.second;
} else if (mm[rs][cs] == 'R') {
pair<int, int> l = bb[rs][cs].r;
rs = l.first, cs = l.second;
} else if (mm[rs][cs] == 'D') {
pair<int, int> l = bb[rs][cs].d;
rs = l.first, cs = l.second;
} else if (mm[rs][cs] == 'U') {
pair<int, int> l = bb[rs][cs].u;
rs = l.first, cs = l.second;
}
}
ans[i][j] = cnt;
}
}
int mx = INT_MIN;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mm[i][j] == '.') continue;
mx = max(mx, ans[i][j]);
}
}
int cc = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mm[i][j] == '.') continue;
if (ans[i][j] == mx) cc++;
}
}
printf("%d %d\n", mx, cc);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<char> a[5001];
vector<int> usl[5011], usr[5011], usd[5011], usu[5011];
vector<int> l[5011], r[5011], d[5011], u[5011];
int n, m;
int dfs(int i, int j, int kol) {
if (i == 0 || i == n + 1 || j == 0 || j == m + 1) return kol - 1;
int tt;
int x, y, z, w;
x = l[i][j];
y = r[i][j];
z = u[i][j];
w = d[i][j];
if (x != 0 && x != m + 1) r[i][x] = y;
if (y != 0 && y != m + 1) l[i][y] = x;
if (z != 0 && z != n + 1) d[z][j] = w;
if (w != 0 && w != n + 1) u[w][j] = z;
int el;
if (a[i][j] == 'D')
return dfs(d[i][j], j, kol + 1);
else if (a[i][j] == 'U')
return dfs(u[i][j], j, kol + 1);
else if (a[i][j] == 'L')
return dfs(i, l[i][j], kol + 1);
else
return dfs(i, r[i][j], kol + 1);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int i, j, nom;
char x;
cin >> n >> m;
for (i = 1; i <= n; i++)
for (j = 1; j <= m + 3; j++)
a[i].push_back(0), usl[i].push_back(0), usr[i].push_back(0),
usu[i].push_back(0), usd[i].push_back(0);
for (i = 1; i <= n; i++)
for (j = 1; j <= m + 1; j++)
l[i].push_back(0), r[i].push_back(0), u[i].push_back(0),
d[i].push_back(0);
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++) cin >> x, a[i][j] = x;
for (i = 1; i <= n; i++) {
nom = 0;
for (j = 1; j <= m; j++) {
usl[i][j] = nom;
if (a[i][j] != '.') nom = j;
}
}
for (i = 1; i <= n; i++) {
nom = m + 1;
for (j = m; j >= 1; j--) {
usr[i][j] = nom;
if (a[i][j] != '.') nom = j;
}
}
for (i = 1; i <= m; i++) {
nom = 0;
for (j = 1; j <= n; j++) {
usu[j][i] = nom;
if (a[j][i] != '.') nom = j;
}
}
for (i = 1; i <= m; i++) {
nom = n + 1;
for (j = n; j >= 1; j--) {
usd[j][i] = nom;
if (a[j][i] != '.') nom = j;
}
}
for (int i1 = 1; i1 <= n; i1++)
for (int j1 = 1; j1 <= m; j1++)
l[i1][j1] = usl[i1][j1], r[i1][j1] = usr[i1][j1], u[i1][j1] = usu[i1][j1],
d[i1][j1] = usd[i1][j1];
int mx = 0, xr = 0, kol = 0;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
if (a[i][j] == '.') continue;
for (int i1 = 1; i1 <= n; i1++)
for (int j1 = 1; j1 <= m; j1++)
l[i1][j1] = usl[i1][j1], r[i1][j1] = usr[i1][j1],
u[i1][j1] = usu[i1][j1], d[i1][j1] = usd[i1][j1];
xr = dfs(i, j, 1);
if (xr > mx) mx = xr, kol = 0;
if (xr == mx) kol++;
}
}
cout << mx << ' ' << kol << '\n';
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int n, m, bst, ci;
char z[5002][5002];
vector<int> st[5002], dr[5002], sus[5002], jos[5002];
void rid(int i, int j) {
if (dr[i][j] != m + 1) st[i][dr[i][j]] = st[i][j];
if (st[i][j] != 0) dr[i][st[i][j]] = dr[i][j];
if (sus[i][j] != 0) jos[sus[i][j]][j] = jos[i][j];
if (jos[i][j] != n + 1) sus[jos[i][j]][j] = sus[i][j];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> (z[i] + 1);
for (int i = 1; i <= n; ++i) {
st[i].resize(m + 2);
dr[i].resize(m + 2);
sus[i].resize(m + 2);
jos[i].resize(m + 2);
}
for (int a = 1; a <= n; ++a)
for (int b = 1; b <= m; ++b) {
if (z[a][b] == '.') continue;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
st[i][j] = j - 1;
dr[i][j] = j + 1;
sus[i][j] = i - 1;
jos[i][j] = i + 1;
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
if (z[i][j] == '.') rid(i, j);
int x = a;
int y = b;
int nr = 0;
while (1 <= x && x <= n && 1 <= y && y <= m) {
rid(x, y);
if (z[x][y] == 'L')
y = st[x][y];
else if (z[x][y] == 'U')
x = sus[x][y];
else if (z[x][y] == 'R')
y = dr[x][y];
else
x = jos[x][y];
++nr;
}
if (nr > bst)
bst = nr, ci = 1;
else if (nr == bst)
++ci;
}
cout << bst << " " << ci << '\n';
return 0;
}
|
#include <bits/stdc++.h>
char a[5005][5005] = {0};
long b[5005][5005] = {0};
struct case1 {
long pre, next;
long up, down;
char ch;
} c[5005] = {0};
int main() {
long n, m, i, j;
long max = 0, num = 0;
long o = 0, p, q;
long last;
long x, s = 0, xx;
scanf("%ld%ld", &n, &m);
for (i = 1; i <= n; i++) scanf("%s", a[i] + 1);
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
if (a[i][j] != '.') b[i][j] = ++o;
for (p = 1; p <= n; p++)
for (q = 1; q <= m; q++)
if (a[p][q] != '.') {
for (i = 1; i <= n; i++) {
last = 0;
for (j = 1; j <= m; j++)
if (b[i][j]) {
c[b[i][j]].ch = a[i][j];
c[b[i][j]].pre = last;
c[b[i][j]].next = 0;
c[last].next = b[i][j];
last = b[i][j];
}
}
for (j = 1; j <= m; j++) {
last = 0;
for (i = 1; i <= n; i++)
if (b[i][j]) {
c[b[i][j]].up = last;
c[b[i][j]].down = 0;
c[last].down = b[i][j];
last = b[i][j];
}
}
for (x = b[p][q], s = 0; x; s++) {
if (c[x].ch == 'U') xx = c[x].up;
if (c[x].ch == 'D') xx = c[x].down;
if (c[x].ch == 'L') xx = c[x].pre;
if (c[x].ch == 'R') xx = c[x].next;
c[c[x].up].down = c[x].down;
c[c[x].down].up = c[x].up;
c[c[x].pre].next = c[x].next;
c[c[x].next].pre = c[x].pre;
x = xx;
}
if (s > max)
max = s, num = 1;
else if (s == max)
max = s, num++;
}
printf("%ld %ld\n", max, num);
return 0;
}
|
#include <bits/stdc++.h>
char ch[6000];
int up[6000], down[6000], left[6000], right[6000], vis[6000], st[6000];
char op[6000];
void add(int t) {
if (up[t] >= 0) down[up[t]] = t;
if (down[t] >= 0) up[down[t]] = t;
if (left[t] >= 0) right[left[t]] = t;
if (right[t] >= 0) left[right[t]] = t;
}
void del(int t) {
if (up[t] >= 0) down[up[t]] = down[t];
if (down[t] >= 0) up[down[t]] = up[t];
if (left[t] >= 0) right[left[t]] = right[t];
if (right[t] >= 0) left[right[t]] = left[t];
}
int dfs(int t) {
if (ch[t] == 'U' && up[t] < 0) return 1;
if (ch[t] == 'D' && down[t] < 0) return 1;
if (ch[t] == 'L' && left[t] < 0) return 1;
if (ch[t] == 'R' && right[t] < 0) return 1;
int next;
if (ch[t] == 'U') next = up[t];
if (ch[t] == 'D') next = down[t];
if (ch[t] == 'L') next = left[t];
if (ch[t] == 'R') next = right[t];
del(t);
next = dfs(next) + 1;
add(t);
return next;
}
int main() {
int n, m, num = 0;
scanf("%d %d", &n, &m);
getchar();
char *p = ch;
for (int i = 0; i < m; i++) vis[i] = -1;
for (int i = 0; i < n * m; i++) up[i] = down[i] = right[i] = left[i] = -1;
for (int i = 0; i < n; i++) {
gets(ch + i * m);
for (int j = 0, pre = -1; j < m; j++)
if (*(p++) != '.') {
int cur = i * m + j;
if (vis[j] >= 0) up[cur] = vis[j] * m + j, down[vis[j] * m + j] = cur;
vis[j] = i;
if (pre >= 0) left[cur] = i * m + pre, right[i * m + pre] = cur;
pre = j;
st[num++] = cur;
}
}
int ans = 0, ans_n = 0;
for (int i = 0; i < num; i++) {
int tmp = dfs(st[i]);
if (tmp > ans)
ans = tmp, ans_n = 1;
else if (tmp == ans)
ans_n++;
}
printf("%d %d\n", ans, ans_n);
}
|
#include <bits/stdc++.h>
using namespace std;
struct Node {
const int d, x, y;
Node* p[4];
Node(int d, int x, int y) : d(d), x(x), y(y) {
for (int i = 0; i < 4; ++i) {
p[i] = NULL;
}
}
void remove() {
for (int i = 0; i < 4; ++i) {
if (p[i] != NULL) {
p[i]->p[i ^ 1] = p[i ^ 1];
}
}
}
void recover() {
for (int i = 0; i < 4; ++i) {
if (p[i] != NULL) {
p[i]->p[i ^ 1] = this;
}
}
}
};
const char dir[] = "UDLR";
int gao(Node* p) {
if (p == NULL) {
return 0;
} else {
Node* t = p->p[p->d];
p->remove();
int ret = gao(t) + 1;
p->recover();
return ret;
}
}
int main() {
int r, c;
char ch;
Node* p;
scanf("%d%d", &r, &c);
vector<vector<Node*> > a(r, vector<Node*>(c));
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
scanf(" %c", &ch);
if (ch != '.') {
a[i][j] = new Node(find(dir, dir + 4, ch) - dir, i, j);
} else {
a[i][j] = NULL;
}
}
}
for (int i = 0; i < r; ++i) {
p = NULL;
for (int j = 0; j < c; ++j) {
if (a[i][j] != NULL) {
if (p != NULL) {
p->p[3] = a[i][j];
}
a[i][j]->p[2] = p;
p = a[i][j];
}
}
}
for (int j = 0; j < c; ++j) {
p = NULL;
for (int i = 0; i < r; ++i) {
if (a[i][j] != NULL) {
if (p != NULL) {
p->p[1] = a[i][j];
}
a[i][j]->p[0] = p;
p = a[i][j];
}
}
}
int ans = 0, cnt = 0;
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
if (a[i][j] != NULL) {
int tmp = gao(a[i][j]);
if (tmp > ans) {
ans = tmp;
cnt = 1;
} else if (tmp == ans) {
++cnt;
}
}
}
}
printf("%d %d\n", ans, cnt);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5005;
int id[maxn][maxn];
char s[maxn][maxn];
char c[maxn];
int l[maxn], r[maxn], u[maxn], d[maxn];
int n, m, t;
void init() {
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
l[id[i][j]] = id[i][j - 1];
r[id[i][j]] = id[i][j + 1];
u[id[i][j]] = id[i - 1][j];
d[id[i][j]] = id[i + 1][j];
}
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
if (s[i][j] == '.') {
r[l[id[i][j]]] = r[id[i][j]];
l[r[id[i][j]]] = l[id[i][j]];
u[d[id[i][j]]] = u[id[i][j]];
d[u[id[i][j]]] = d[id[i][j]];
}
}
}
}
void del(int k) {
r[l[k]] = r[k];
l[r[k]] = l[k];
u[d[k]] = u[k];
d[u[k]] = d[k];
}
int dfs(int cur) {
if (cur == 0) return 0;
del(cur);
if (c[cur] == 'U') return dfs(u[cur]) + 1;
if (c[cur] == 'D') return dfs(d[cur]) + 1;
if (c[cur] == 'L') return dfs(l[cur]) + 1;
if (c[cur] == 'R') return dfs(r[cur]) + 1;
return 0;
}
void work() {
int i, j;
int ans, max = 0, num;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
if (s[i][j] != '.') {
init();
ans = dfs(id[i][j]);
if (ans > max) {
max = ans;
num = 1;
} else if (ans == max)
num++;
}
}
}
printf("%d %d\n", max, num);
}
int main() {
int i, j;
while (scanf("%d%d", &n, &m) != EOF) {
for (i = 1; i <= n; i++) scanf("%s", &s[i][1]);
t = 0;
memset(id, 0, sizeof(id));
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
t++;
id[i][j] = t;
c[t] = s[i][j];
}
}
work();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf(1010101010);
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, 1, 0, -1};
int h, w;
vector<vector<int> > a;
vector<vector<int> > rowpred, rowsucc;
vector<vector<int> > colpred, colsucc;
int ii[5005], jj[5005];
inline void dorm(int y, int x) {
int rpred(rowpred[y][x]), rsucc(rowsucc[y][x]);
if (rpred != -1) rowsucc[y][rpred] = rsucc;
if (rsucc != -1) rowpred[y][rsucc] = rpred;
int cpred(colpred[x][y]), csucc(colsucc[x][y]);
if (cpred != -1) colsucc[x][cpred] = csucc;
if (csucc != -1) colpred[x][csucc] = cpred;
}
int sim(int y0, int x0) {
for (int i(0); i < (h); i++) {
int nj(0);
for (int j(0); j < (w); j++)
if (a[i][j] >= 0) jj[nj++] = j;
if (nj == 0) continue;
rowpred[i][jj[0]] = rowsucc[i][jj[nj - 1]] = -1;
for (int k(0); k < (nj - 1); k++)
rowpred[i][jj[k + 1]] = jj[k], rowsucc[i][jj[k]] = jj[k + 1];
}
for (int j(0); j < (w); j++) {
int ni(0);
for (int i(0); i < (h); i++)
if (a[i][j] >= 0) ii[ni++] = i;
if (ni == 0) continue;
colpred[j][ii[0]] = colsucc[j][ii[ni - 1]] = -1;
for (int k(0); k < (ni - 1); k++)
colpred[j][ii[k + 1]] = ii[k], colsucc[j][ii[k]] = ii[k + 1];
}
int cnt(0);
for (int y(y0), x(x0);;) {
cnt++;
int d(a[y][x]);
int yy, xx;
if (d == 0) {
if (colpred[x][y] == -1) goto klaar;
yy = colpred[x][y], xx = x;
} else if (d == 2) {
if (colsucc[x][y] == -1) goto klaar;
yy = colsucc[x][y], xx = x;
} else if (d == 3) {
if (rowpred[y][x] == -1) goto klaar;
yy = y, xx = rowpred[y][x];
} else if (d == 1) {
if (rowsucc[y][x] == -1) goto klaar;
yy = y, xx = rowsucc[y][x];
}
dorm(y, x);
y = yy, x = xx;
}
klaar:;
return cnt;
}
int main() {
cin >> h >> w;
a.resize(h);
for (int i(0); i < (h); i++) {
a[i].resize(w);
string s;
cin >> s;
for (int j(0); j < (w); j++) {
char c(s[j]);
int& r(a[i][j]);
if (c == 'U')
r = 0;
else if (c == 'R')
r = 1;
else if (c == 'D')
r = 2;
else if (c == 'L')
r = 3;
else
r = -1;
}
}
colpred = vector<vector<int> >(w, vector<int>(h));
colsucc = vector<vector<int> >(w, vector<int>(h));
rowpred = vector<vector<int> >(h, vector<int>(w));
rowsucc = vector<vector<int> >(h, vector<int>(w));
int mx(0), cnt(0);
for (int i(0); i < (h); i++)
for (int j(0); j < (w); j++)
if (a[i][j] >= 0) {
int re(sim(i, j));
if (re > mx)
mx = re, cnt = 1;
else if (re == mx)
cnt++;
}
cout << mx << " " << cnt << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef struct node {
node *L, *R, *U, *D;
char dir;
} node;
node** mat;
char** str;
int N, M;
void DEL(node* p) {
p->R->L = p->L;
p->L->R = p->R;
p->U->D = p->D;
p->D->U = p->U;
}
void init() {
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= M; j++) {
mat[i][j].dir = str[i][j];
if (i == 0) {
mat[i][j].U = &mat[N][j];
} else {
mat[i][j].U = &mat[i - 1][j];
}
if (i == N) {
mat[i][j].D = &mat[0][j];
} else {
mat[i][j].D = &mat[i + 1][j];
}
if (j == 0) {
mat[i][j].L = &mat[i][M];
} else {
mat[i][j].L = &mat[i][j - 1];
}
if (j == M) {
mat[i][j].R = &mat[i][0];
} else {
mat[i][j].R = &mat[i][j + 1];
}
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if ('.' == str[i][j]) {
DEL(&mat[i][j]);
}
}
}
}
int main() {
scanf("%d %d", &N, &M);
mat = new node*[N + 10];
str = new char*[N + 10];
for (int i = 0; i < N + 10; i++) {
mat[i] = new node[M + 10];
str[i] = new char[M + 10];
}
for (int i = 0; i < N + 4; i++) {
for (int j = 0; j < M + 4; j++) {
str[i][j] = 0;
}
}
for (int i = 0; i < N; i++) {
scanf("%s", str[i]);
}
int res = 0;
int cnt = 1;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if ('.' == str[i][j]) continue;
init();
node* cur = &mat[i][j];
int tmp = 0;
while (cur->dir != 0) {
tmp++;
switch (cur->dir) {
case 'U': {
cur = cur->U;
DEL(cur->D);
break;
}
case 'D': {
cur = cur->D;
DEL(cur->U);
break;
}
case 'R': {
cur = cur->R;
DEL(cur->L);
break;
}
case 'L': {
cur = cur->L;
DEL(cur->R);
break;
}
}
}
if (res < tmp) {
res = tmp;
cnt = 1;
} else if (res == tmp) {
cnt++;
}
}
}
printf("%d %d\n", res, cnt);
return 0;
}
|
#include <bits/stdc++.h>
char** mm;
int n, m;
int **le, **ri, **up, **down;
bool** used;
void init_ar(int** p, int n, int m) {
p = new int*[n];
int i;
for (i = 0; i < n; i++) p[i] = new int[m];
}
void init_next() {
int i, j;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) {
if (i == 0 || mm[i - 1][j] != '.')
up[i][j] = i - 1;
else
up[i][j] = up[i - 1][j];
if (j == 0 || mm[i][j - 1] != '.')
le[i][j] = j - 1;
else
le[i][j] = le[i][j - 1];
}
for (i = n - 1; i >= 0; i--)
for (j = m - 1; j >= 0; j--) {
if (i == n - 1 || mm[i + 1][j] != '.')
down[i][j] = i + 1;
else
down[i][j] = down[i + 1][j];
if (j == m - 1 || mm[i][j + 1] != '.')
ri[i][j] = j + 1;
else
ri[i][j] = ri[i][j + 1];
}
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) used[i][j] = 0;
}
bool valid(int y, int x) { return (y >= 0 && y < n && x >= 0 && x < m); }
int get_ver(int** com, int y, int x) {
if (!valid(y, x)) return y;
if (!used[y][x]) return y;
return com[y][x] = get_ver(com, com[y][x], x);
}
int get_hor(int** com, int y, int x) {
if (!valid(y, x)) return x;
if (!used[y][x]) return x;
return com[y][x] = get_hor(com, y, com[y][x]);
}
int proc(int y, int x) {
init_next();
int it;
for (it = 0;; it++) {
if (!valid(y, x)) break;
used[y][x] = true;
if (mm[y][x] == 'D')
y = get_ver(down, y, x);
else if (mm[y][x] == 'U')
y = get_ver(up, y, x);
else if (mm[y][x] == 'L')
x = get_hor(le, y, x);
else if (mm[y][x] == 'R')
x = get_hor(ri, y, x);
}
return it;
}
int main() {
scanf("%d %d", &n, &m);
mm = new char*[n];
le = new int*[n];
ri = new int*[n];
up = new int*[n];
down = new int*[n];
used = new bool*[n];
int i, j;
for (i = 0; i < n; i++) {
mm[i] = new char[m + 1];
le[i] = new int[m];
ri[i] = new int[m];
up[i] = new int[m];
down[i] = new int[m];
used[i] = new bool[m];
}
for (i = 0; i < n; i++) scanf("%s", mm[i]);
int res = 0;
int nres = 0;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
if (mm[i][j] != '.') {
int tmp = proc(i, j);
if (tmp > res) {
res = tmp;
nres = 1;
} else if (tmp == res) {
nres++;
}
}
printf("%d %d\n", res, nres);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxN = 5120;
char maze[maxN][maxN];
int nr, nc;
int L[maxN], R[maxN];
int U[maxN], D[maxN];
void getUDLR() {
for (int i = 0; i < nr; i++) {
int l = -1;
for (int j = 0; j < nc; j++)
if (maze[i][j] != '.') {
int idx = i * nc + j;
L[idx] = l;
if (l != -1) R[l] = idx;
l = idx;
}
R[l] = -1;
}
for (int i = 0; i < nc; i++) {
int u = -1;
for (int j = 0; j < nr; j++)
if (maze[j][i] != '.') {
int idx = j * nc + i;
U[idx] = u;
if (u != -1) D[u] = idx;
u = idx;
}
D[u] = -1;
}
}
void del(int i, int j) {
int idx = i * nc + j;
if (U[idx] != -1) D[U[idx]] = D[idx];
if (D[idx] != -1) U[D[idx]] = U[idx];
if (L[idx] != -1) R[L[idx]] = R[idx];
if (R[idx] != -1) L[R[idx]] = L[idx];
}
int dfs(int i, int j) {
del(i, j);
int idx = i * nc + j;
if (maze[i][j] == 'U') {
if (U[idx] == -1)
return 1;
else
return 1 + dfs(U[idx] / nc, j);
} else if (maze[i][j] == 'D') {
if (D[idx] == -1)
return 1;
else
return 1 + dfs(D[idx] / nc, j);
} else if (maze[i][j] == 'L') {
if (L[idx] == -1)
return 1;
else
return 1 + dfs(i, L[idx] % nc);
} else if (maze[i][j] == 'R') {
if (R[idx] == -1)
return 1;
else
return 1 + dfs(i, R[idx] % nc);
}
return 0;
}
int main() {
scanf("%d %d", &nr, &nc);
for (int i = 0; i < nr; i++) scanf("%s", maze[i]);
int num, max = -1;
for (int i = 0; i < nr; i++)
for (int j = 0; j < nc; j++)
if (maze[i][j] != '.') {
getUDLR();
int tmp = dfs(i, j);
if (tmp > max)
max = tmp, num = 1;
else if (tmp == max)
num++;
}
printf("%d %d\n", max, num);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool O[100000];
int r, c, to[4][100000];
char mp[100000];
inline int node(int i, int j) { return i * (c + 2) + j; }
inline int gd(char c) {
switch (c) {
case 'R':
return 0;
case 'D':
return 1;
case 'L':
return 2;
case 'U':
return 3;
}
}
int ans, cnt;
inline void rm(int v) {
for (int i = 0; i < 4; ++i) to[(i + 2) % 4][to[i][v]] = to[(i + 2) % 4][v];
}
inline void re(int v) {
for (int i = 0; i < 4; ++i) to[(i + 2) % 4][to[i][v]] = v;
}
void dfs(int v, int dep) {
if (O[v]) {
if (ans < dep)
ans = dep, cnt = 1;
else if (ans == dep)
++cnt;
return;
}
rm(v);
dfs(to[gd(mp[v])][v], dep + 1);
re(v);
}
int main() {
while (scanf("%d%d", &r, &c) != EOF) {
ans = 0, cnt = 0;
fill(O, O + (r + 2) * (c + 2), false);
fill(mp, mp + (r + 2) * (c + 2), 0);
for (int i = 1; i <= r; ++i)
for (int j = 1; j <= c; ++j) scanf(" %c", mp + node(i, j));
for (int i = 0; i < r + 2; ++i) O[node(i, 0)] = O[node(i, c + 1)] = true;
for (int j = 0; j < c + 2; ++j) O[node(0, j)] = O[node(r + 1, j)] = true;
for (int i = 1; i <= r; ++i) {
for (int j = 1; j < c + 2; ++j)
if (mp[node(i, j - 1)] != '.')
to[2][node(i, j)] = node(i, j - 1);
else
to[2][node(i, j)] = to[2][node(i, j - 1)];
for (int j = c; j >= 0; --j)
if (mp[node(i, j + 1)] != '.')
to[0][node(i, j)] = node(i, j + 1);
else
to[0][node(i, j)] = to[0][node(i, j + 1)];
}
for (int j = 1; j <= c; ++j) {
for (int i = 1; i < r + 2; ++i)
if (mp[node(i - 1, j)] != '.')
to[3][node(i, j)] = node(i - 1, j);
else
to[3][node(i, j)] = to[3][node(i - 1, j)];
for (int i = r; i >= 0; --i)
if (mp[node(i + 1, j)] != '.')
to[1][node(i, j)] = node(i + 1, j);
else
to[1][node(i, j)] = to[1][node(i + 1, j)];
}
for (int i = 1; i <= r; ++i)
for (int j = 1; j <= c; ++j)
if (mp[node(i, j)] != '.') dfs(node(i, j), 0);
printf("%d %d\n", ans, cnt);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int b = 0, b1 = 0;
int N, M;
int g[5010];
int delta[5010][5], d2[5010][5];
char c[256];
inline int valid(int x, int y) { return x >= 0 && y >= 0 && x < N && y < M; }
int dir[4][2] = {0, -1, -1, 0, 0, 1, 1, 0};
inline int iabs(int t) { return t > 0 ? t : -t; }
int main() {
c['L'] = 0;
c['U'] = 1;
c['R'] = 2;
c['D'] = 3;
c['.'] = -1;
cin >> N >> M;
string s;
for (int i = 0; i < (N); i++) {
cin >> s;
for (int j = 0; j < (M); j++) {
g[(i * M + j)] = c[s[j]];
for (int k = 0; k < (5); k++) delta[(i * M + j)][k] = 1;
}
}
memcpy(d2, delta, sizeof d2);
for (int i = 0; i < (N); i++) {
for (int j = 0; j < (M); j++) {
if (g[(i * M + j)] >= 0) {
memcpy(delta, d2, sizeof d2);
int now = 0;
int x = i, y = j;
while (valid(x, y)) {
now++;
int d = g[(x * M + y)];
int x2 = x, y2 = y, suc = 1;
delta[(x * M + y)][4] = -1;
while (1) {
if (!valid(x2, y2)) {
suc = 0;
break;
}
if (delta[(x2 * M + y2)][4] != -1 && g[(x2 * M + y2)] != -1) {
break;
}
int dx = delta[(x2 * M + y2)][d];
x2 += dir[d][0] * dx;
y2 += dir[d][1] * dx;
}
if (!suc) {
x = -1;
} else {
delta[(x * M + y)][d] = iabs(x2 - x) + iabs(y2 - y) + 1;
x = x2;
y = y2;
}
}
if (now > b) {
b = now;
b1 = 0;
}
if (now == b) b1++;
}
}
}
cout << b << ' ' << b1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct p {
int L, R, U, D;
} point[5005], pos[5005];
string g[5005];
void DEL(int x) {
pos[pos[x].D].U = pos[x].U;
pos[pos[x].U].D = pos[x].D;
pos[pos[x].R].L = pos[x].L;
pos[pos[x].L].R = pos[x].R;
}
int main() {
int m, n, i, j, l, sx, sy;
scanf("%d%d", &m, &n);
for (i = 0; i < m; i++) cin >> g[i];
n = g[0].size();
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (g[i][j] != '.') {
for (l = i - 1; l >= 0; l--)
if (g[l][j] != '.') break;
if (l < 0)
point[i * n + j].U = -1;
else
point[i * n + j].U = l * n + j;
for (l = i + 1; l < m; l++)
if (g[l][j] != '.') break;
if (l >= m)
point[i * n + j].D = -1;
else
point[i * n + j].D = l * n + j;
for (l = j - 1; l >= 0; l--)
if (g[i][l] != '.') break;
if (l < 0)
point[i * n + j].L = -1;
else
point[i * n + j].L = i * n + l;
for (l = j + 1; l < n; l++)
if (g[i][l] != '.') break;
if (l >= n)
point[i * n + j].R = -1;
else
point[i * n + j].R = i * n + l;
}
}
}
int r1 = 0, r2 = 0, cnt, t, x, y;
for (sx = 0; sx < m; sx++) {
for (sy = 0; sy < n; sy++)
if (g[sx][sy] != '.') {
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
pos[i * n + j] = point[i * n + j];
}
}
if (g[sx][sy] == 'D')
t = pos[sx * n + sy].D;
else if (g[sx][sy] == 'U')
t = pos[sx * n + sy].U;
else if (g[sx][sy] == 'R')
t = pos[sx * n + sy].R;
else if (g[sx][sy] == 'L')
t = pos[sx * n + sy].L;
DEL(sx * n + sy);
cnt = 1;
while (t != -1) {
cnt++;
x = t / n;
y = t % n;
DEL(t);
if (g[x][y] == 'D')
t = pos[t].D;
else if (g[x][y] == 'U')
t = pos[t].U;
else if (g[x][y] == 'R')
t = pos[t].R;
else if (g[x][y] == 'L')
t = pos[t].L;
}
if (cnt > r1) {
r1 = cnt;
r2 = 1;
} else if (cnt == r1) {
r2++;
}
}
}
printf("%d %d\n", r1, r2);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 5000;
const int Maxd = 4;
const string dir = "LURD.";
struct pos {
int c;
pair<int, int> to[Maxd];
};
int n, m;
vector<pos> B[Maxn];
int res, cnt;
void Try(int r, int c, int col) {
if (r == -1 || c == -1) {
if (col == res)
cnt++;
else if (col > res) {
res = col;
cnt = 1;
}
} else {
for (int i = 0; i < Maxd; i++) {
pair<int, int> p = B[r][c].to[i];
if (p.first != -1 && p.second != -1)
B[p.first][p.second].to[(i + 2) % Maxd] = B[r][c].to[(i + 2) % Maxd];
}
int wh = B[r][c].c;
Try(B[r][c].to[wh].first, B[r][c].to[wh].second, col + 1);
for (int i = 0; i < Maxd; i++) {
pair<int, int> p = B[r][c].to[i];
if (p.first != -1 && p.second != -1)
B[p.first][p.second].to[(i + 2) % Maxd] = make_pair(r, c);
}
}
}
int main() {
cin >> n >> m;
cin.ignore();
for (int i = 0; i < n; i++) {
B[i].resize(m);
for (int j = 0; j < m; j++) {
char c;
cin >> c;
B[i][j].c = dir.find(c);
}
}
for (int i = 0; i < n; i++) {
int st = -1;
for (int j = 0; j < m; j++)
if (B[i][j].c != Maxd) {
B[i][j].to[0] = make_pair(i, st);
st = j;
}
st = -1;
for (int j = m - 1; j >= 0; j--)
if (B[i][j].c != Maxd) {
B[i][j].to[2] = make_pair(i, st);
st = j;
}
}
for (int j = 0; j < m; j++) {
int st = -1;
for (int i = 0; i < n; i++)
if (B[i][j].c != Maxd) {
B[i][j].to[1] = make_pair(st, j);
st = i;
}
st = -1;
for (int i = n - 1; i >= 0; i--)
if (B[i][j].c != Maxd) {
B[i][j].to[3] = make_pair(st, j);
st = i;
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (B[i][j].c != Maxd) Try(i, j, 0);
cout << res << " " << cnt << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
typedef unsigned long long uint64;
const double pi = acos(-1.0);
const double eps = 1e-11;
template <class T>
inline void checkmin(T &a, T b) {
if (b < a) a = b;
}
template <class T>
inline void checkmax(T &a, T b) {
if (b > a) a = b;
}
template <class T>
inline T sqr(T x) {
return x * x;
}
typedef pair<int, int> ipair;
template <class T>
inline T lowbit(T n) {
return (n ^ (n - 1)) & n;
}
template <class T>
inline int countbit(T n) {
return (n == 0) ? 0 : (1 + countbit(n & (n - 1)));
}
template <class T>
inline T gcd(T a, T b) {
if (a < 0) return gcd(-a, b);
if (b < 0) return gcd(a, -b);
return (b == 0) ? a : gcd(b, a % b);
}
template <class T>
inline T lcm(T a, T b) {
if (a < 0) return lcm(-a, b);
if (b < 0) return lcm(a, -b);
return a * (b / gcd(a, b));
}
template <class T>
inline T euclide(T a, T b, T &x, T &y) {
if (a < 0) {
T d = euclide(-a, b, x, y);
x = -x;
return d;
}
if (b < 0) {
T d = euclide(a, -b, x, y);
y = -y;
return d;
}
if (b == 0) {
x = 1;
y = 0;
return a;
} else {
T d = euclide(b, a % b, x, y);
T t = x;
x = y;
y = t - (a / b) * y;
return d;
}
}
template <class T>
inline vector<pair<T, int> > factorize(T n) {
vector<pair<T, int> > R;
for (T i = 2; n > 1;) {
if (n % i == 0) {
int C = 0;
for (; n % i == 0; C++, n /= i)
;
R.push_back(make_pair(i, C));
}
i++;
if (i > n / i) i = n;
}
if (n > 1) R.push_back(make_pair(n, 1));
return R;
}
template <class T>
inline bool isPrimeNumber(T n) {
if (n <= 1) return false;
for (T i = 2; i * i <= n; i++)
if (n % i == 0) return false;
return true;
}
template <class T>
inline T eularFunction(T n) {
vector<pair<T, int> > R = factorize(n);
T r = n;
for (int i = 0; i < R.size(); i++) r = r / R[i].first * (R[i].first - 1);
return r;
}
const int MaxMatrixSize = 40;
template <class T>
inline void showMatrix(int n, T A[MaxMatrixSize][MaxMatrixSize]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) cout << A[i][j];
cout << endl;
}
}
template <class T>
inline T checkMod(T n, T m) {
return (n % m + m) % m;
}
template <class T>
inline void identityMatrix(int n, T A[MaxMatrixSize][MaxMatrixSize]) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) A[i][j] = (i == j) ? 1 : 0;
}
template <class T>
inline void addMatrix(int n, T C[MaxMatrixSize][MaxMatrixSize],
T A[MaxMatrixSize][MaxMatrixSize],
T B[MaxMatrixSize][MaxMatrixSize]) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) C[i][j] = A[i][j] + B[i][j];
}
template <class T>
inline void subMatrix(int n, T C[MaxMatrixSize][MaxMatrixSize],
T A[MaxMatrixSize][MaxMatrixSize],
T B[MaxMatrixSize][MaxMatrixSize]) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) C[i][j] = A[i][j] - B[i][j];
}
template <class T>
inline void mulMatrix(int n, T C[MaxMatrixSize][MaxMatrixSize],
T _A[MaxMatrixSize][MaxMatrixSize],
T _B[MaxMatrixSize][MaxMatrixSize]) {
T A[MaxMatrixSize][MaxMatrixSize], B[MaxMatrixSize][MaxMatrixSize];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
A[i][j] = _A[i][j], B[i][j] = _B[i][j], C[i][j] = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++) C[i][j] += A[i][k] * B[k][j];
}
template <class T>
inline void addModMatrix(int n, T m, T C[MaxMatrixSize][MaxMatrixSize],
T A[MaxMatrixSize][MaxMatrixSize],
T B[MaxMatrixSize][MaxMatrixSize]) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) C[i][j] = checkMod(A[i][j] + B[i][j], m);
}
template <class T>
inline void subModMatrix(int n, T m, T C[MaxMatrixSize][MaxMatrixSize],
T A[MaxMatrixSize][MaxMatrixSize],
T B[MaxMatrixSize][MaxMatrixSize]) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) C[i][j] = checkMod(A[i][j] - B[i][j], m);
}
template <class T>
inline T multiplyMod(T a, T b, T m) {
return (T)((((int64)(a) * (int64)(b) % (int64)(m)) + (int64)(m)) %
(int64)(m));
}
template <class T>
inline void mulModMatrix(int n, T m, T C[MaxMatrixSize][MaxMatrixSize],
T _A[MaxMatrixSize][MaxMatrixSize],
T _B[MaxMatrixSize][MaxMatrixSize]) {
T A[MaxMatrixSize][MaxMatrixSize], B[MaxMatrixSize][MaxMatrixSize];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
A[i][j] = _A[i][j], B[i][j] = _B[i][j], C[i][j] = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
C[i][j] = (C[i][j] + multiplyMod(A[i][k], B[k][j], m)) % m;
}
template <class T>
inline T powerMod(T p, int e, T m) {
if (e == 0)
return 1 % m;
else if (e % 2 == 0) {
T t = powerMod(p, e / 2, m);
return multiplyMod(t, t, m);
} else
return multiplyMod(powerMod(p, e - 1, m), p, m);
}
double dist(double x1, double y1, double x2, double y2) {
return sqrt(sqr(x1 - x2) + sqr(y1 - y2));
}
double distR(double x1, double y1, double x2, double y2) {
return sqr(x1 - x2) + sqr(y1 - y2);
}
template <class T>
T cross(T x0, T y0, T x1, T y1, T x2, T y2) {
return (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);
}
int crossOper(double x0, double y0, double x1, double y1, double x2,
double y2) {
double t = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);
if (fabs(t) <= eps) return 0;
return (t < 0) ? -1 : 1;
}
bool isIntersect(double x1, double y1, double x2, double y2, double x3,
double y3, double x4, double y4) {
return crossOper(x1, y1, x2, y2, x3, y3) * crossOper(x1, y1, x2, y2, x4, y4) <
0 &&
crossOper(x3, y3, x4, y4, x1, y1) * crossOper(x3, y3, x4, y4, x2, y2) <
0;
}
bool isMiddle(double s, double m, double t) {
return fabs(s - m) <= eps || fabs(t - m) <= eps || (s < m) != (t < m);
}
bool isUpperCase(char c) { return c >= 'A' && c <= 'Z'; }
bool isLowerCase(char c) { return c >= 'a' && c <= 'z'; }
bool isLetter(char c) { return c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z'; }
bool isDigit(char c) { return c >= '0' && c <= '9'; }
char toLowerCase(char c) { return (isUpperCase(c)) ? (c + 32) : c; }
char toUpperCase(char c) { return (isLowerCase(c)) ? (c - 32) : c; }
template <class T>
string toString(T n) {
ostringstream ost;
ost << n;
ost.flush();
return ost.str();
}
int toInt(string s) {
int r = 0;
istringstream sin(s);
sin >> r;
return r;
}
int64 toInt64(string s) {
int64 r = 0;
istringstream sin(s);
sin >> r;
return r;
}
double toDouble(string s) {
double r = 0;
istringstream sin(s);
sin >> r;
return r;
}
template <class T>
void stoa(string s, int &n, T A[]) {
n = 0;
istringstream sin(s);
for (T v; sin >> v; A[n++] = v)
;
}
template <class T>
void atos(int n, T A[], string &s) {
ostringstream sout;
for (int i = 0; i < n; i++) {
if (i > 0) sout << ' ';
sout << A[i];
}
s = sout.str();
}
template <class T>
void atov(int n, T A[], vector<T> &vi) {
vi.clear();
for (int i = 0; i < n; i++) vi.push_back(A[i]);
}
template <class T>
void vtoa(vector<T> vi, int &n, T A[]) {
n = vi.size();
for (int i = 0; i < n; i++) A[i] = vi[i];
}
template <class T>
void stov(string s, vector<T> &vi) {
vi.clear();
istringstream sin(s);
for (T v; sin >> v; vi.push_bakc(v))
;
}
template <class T>
void vtos(vector<T> vi, string &s) {
ostringstream sout;
for (int i = 0; i < vi.size(); i++) {
if (i > 0) sout << ' ';
sout << vi[i];
}
s = sout.str();
}
template <class T>
struct Fraction {
T a, b;
Fraction(T a = 0, T b = 1);
string toString();
};
template <class T>
Fraction<T>::Fraction(T a, T b) {
T d = gcd(a, b);
a /= d;
b /= d;
if (b < 0) a = -a, b = -b;
this->a = a;
this->b = b;
}
template <class T>
string Fraction<T>::toString() {
ostringstream sout;
sout << a << "/" << b;
return sout.str();
}
template <class T>
Fraction<T> operator+(Fraction<T> p, Fraction<T> q) {
return Fraction<T>(p.a * q.b + q.a * p.b, p.b * q.b);
}
template <class T>
Fraction<T> operator-(Fraction<T> p, Fraction<T> q) {
return Fraction<T>(p.a * q.b - q.a * p.b, p.b * q.b);
}
template <class T>
Fraction<T> operator*(Fraction<T> p, Fraction<T> q) {
return Fraction<T>(p.a * q.a, p.b * q.b);
}
template <class T>
Fraction<T> operator/(Fraction<T> p, Fraction<T> q) {
return Fraction<T>(p.a * q.b, p.b * q.a);
}
const int maxsize = 5000 + 5;
int n, m;
char a[maxsize][maxsize], t[maxsize][maxsize];
int father[maxsize * 4];
int last;
int ID(int x, int y, int d) { return (x * m + y) * 4 + d; }
int getfather(int p) {
return (father[p] < 0) ? p : (father[p] = getfather(father[p]));
}
void add(int x, int y) {
father[ID(x, y, 0)] = (x + 1 < n) ? ID(x + 1, y, 0) : last;
father[ID(x, y, 1)] = (x - 1 >= 0) ? ID(x - 1, y, 1) : last;
father[ID(x, y, 2)] = (y - 1 >= 0) ? ID(x, y - 1, 2) : last;
father[ID(x, y, 3)] = (y + 1 < m) ? ID(x, y + 1, 3) : last;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%s", t[i]);
int R1 = 0, R2 = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (t[i][j] == '.') continue;
for (int u = 0; u < n; u++)
for (int v = 0; v < m; v++) a[u][v] = t[u][v];
memset(father, 255, sizeof(father));
last = n * m * 4;
for (int u = 0; u < n; u++)
for (int v = 0; v < m; v++) {
if (a[u][v] != '.') continue;
add(u, v);
}
int S = 0, x = i, y = j;
while (1) {
S++;
int dir = -1;
if (a[x][y] == 'D') dir = 0;
if (a[x][y] == 'U') dir = 1;
if (a[x][y] == 'L') dir = 2;
if (a[x][y] == 'R') dir = 3;
a[x][y] = '.';
add(x, y);
int p = getfather(ID(x, y, dir));
if (p == last) break;
x = p / 4 / m;
y = p / 4 % m;
}
if (S > R1)
R1 = S, R2 = 1;
else if (S == R1)
R2++;
}
printf("%d %d\n", R1, R2);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 5010;
int n, m;
vector<string> grid;
vector<vector<int> > l, r, u, d;
bool active[N];
int getpar(vector<vector<int> > &arr, int x, int y) {
int nxt = arr[x][y];
if (nxt == N - 1 || active[nxt]) return nxt;
return arr[x][y] = getpar(arr, nxt / m, nxt % m);
}
int solve(int x, int y) {
fill(active, active + N - 1, true);
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
if (grid[i][j] == '.') active[i * m + j] = false;
l[i][j] = i * m + j - 1;
r[i][j] = i * m + j + 1;
u[i][j] = i * m + j - m;
d[i][j] = i * m + j + m;
if (i == 0) u[i][j] = N - 1;
if (i == n - 1) d[i][j] = N - 1;
if (j == 0) l[i][j] = N - 1;
if (j == m - 1) r[i][j] = N - 1;
}
int ans = 1;
while (true) {
int nxt = N - 1;
if (grid[x][y] == 'L') {
nxt = getpar(l, x, y);
}
if (grid[x][y] == 'R') {
nxt = getpar(r, x, y);
}
if (grid[x][y] == 'U') {
nxt = getpar(u, x, y);
}
if (grid[x][y] == 'D') {
nxt = getpar(d, x, y);
}
if (nxt == N - 1) return ans;
active[x * m + y] = false;
x = nxt / m;
y = nxt % m;
++ans;
}
}
int main() {
cin >> n >> m;
l = vector<vector<int> >(n, vector<int>(m));
r = vector<vector<int> >(n, vector<int>(m));
u = vector<vector<int> >(n, vector<int>(m));
d = vector<vector<int> >(n, vector<int>(m));
grid.resize(n);
for (int i = 0; i < n; ++i) cin >> grid[i];
vector<int> res;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
if (grid[i][j] != '.') res.push_back(solve(i, j));
}
sort(res.rbegin(), res.rend());
int i = 0;
while (res[i] == res[0]) ++i;
cout << res[0] << ' ' << i << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
typedef struct pointers_ {
int U, D, R, L;
} pointers;
int r, c;
char m[5010][5010];
pointers* p[5010];
pointers memory[5010];
int pp;
pointers* pmalloc() {
pointers* point = memory + pp;
pp += c;
return point;
}
int run(int i, int j) {
char old = m[i][j];
int x = i;
int y = j;
switch (m[i][j]) {
case 'U':
x = p[x][y].U;
break;
case 'D':
x = p[x][y].D;
break;
case 'R':
y = p[x][y].R;
break;
case 'L':
y = p[x][y].L;
break;
}
m[i][j] = '.';
int run2 = 0;
if (x >= 0 and x < r and y >= 0 and y < c) {
if (p[i][j].L >= 0 and p[i][j].L < c) p[i][p[i][j].L].R = p[i][j].R;
if (p[i][j].R >= 0 and p[i][j].R < c) p[i][p[i][j].R].L = p[i][j].L;
if (p[i][j].D >= 0 and p[i][j].D < r) p[p[i][j].D][j].U = p[i][j].U;
if (p[i][j].U >= 0 and p[i][j].U < r) p[p[i][j].U][j].D = p[i][j].D;
run2 = run(x, y);
if (p[i][j].L >= 0 and p[i][j].L < c) p[i][p[i][j].L].R = j;
if (p[i][j].R >= 0 and p[i][j].R < c) p[i][p[i][j].R].L = j;
if (p[i][j].D >= 0 and p[i][j].D < r) p[p[i][j].D][j].U = i;
if (p[i][j].U >= 0 and p[i][j].U < r) p[p[i][j].U][j].D = i;
}
m[i][j] = old;
return 1 + run2;
}
void populate(int i, int j) {
if (m[i][j] == '.') return;
int k;
for (k = i + 1; k < r; k++)
if (m[k][j] != '.') {
p[i][j].D = k;
break;
}
if (k == r) p[i][j].D = 0x3f3f3f3f;
for (k = i - 1; k >= 0; k--)
if (m[k][j] != '.') {
p[i][j].U = k;
break;
}
if (k < 0) p[i][j].U = -1;
for (k = j + 1; k < c; k++)
if (m[i][k] != '.') {
p[i][j].R = k;
break;
}
if (k == c) p[i][j].R = 0x3f3f3f3f;
for (k = j - 1; k >= 0; k--)
if (m[i][k] != '.') {
p[i][j].L = k;
break;
}
if (k < 0) p[i][j].L = -1;
return;
}
int main() {
scanf("%d %d", &r, &c);
for (int i = 0; i < r; i++) scanf("%s", m[i]);
pp = 0;
for (int i = 0; i < r; i++) p[i] = pmalloc();
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++) populate(i, j);
int maior = 0;
int qnts = 0;
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
if (m[i][j] != '.') {
int res = run(i, j);
if (res > maior) {
maior = res;
qnts = 1;
} else if (res == maior)
qnts++;
}
printf("%d %d\n", maior, qnts);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct values {
long long L, R, D, U;
values() {
L = -1;
R = -1;
D = -1;
U = -1;
}
void debug() { cout << L << " " << R << " " << D << " " << U << "\n"; }
};
const long long N = 5005;
vector<vector<values> > _next;
vector<string> s;
long long helper(long long i, long long j) {
if (i == -1 or j == -1) return 0;
if (s[i][j] == '.') assert(0);
long long l = _next[i][j].L, r = _next[i][j].R, d = _next[i][j].D,
u = _next[i][j].U;
if (l != -1) {
_next[i][l].R = _next[i][j].R;
}
if (r != -1) {
_next[i][r].L = _next[i][j].L;
}
if (d != -1) {
_next[d][j].U = _next[i][j].U;
}
if (u != -1) {
_next[u][j].D = _next[i][j].D;
}
long long _i = i, _j = j;
if (s[i][j] == 'U') {
_i = u;
} else if (s[i][j] == 'D') {
_i = d;
} else if (s[i][j] == 'R') {
_j = r;
} else if (s[i][j] == 'L') {
_j = l;
}
return 1 + helper(_i, _j);
}
void solve() {
long long n, m;
cin >> n >> m;
s.resize(n);
for (auto &x : s) cin >> x;
vector<vector<values> > nearest(n, vector<values>(m));
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < m; ++j) {
if (i != 0) {
if (s[i - 1][j] != '.') {
nearest[i][j].U = i - 1;
} else {
nearest[i][j].U = nearest[i - 1][j].U;
}
}
if (j != 0) {
if (s[i][j - 1] != '.') {
nearest[i][j].L = j - 1;
} else {
nearest[i][j].L = nearest[i][j - 1].L;
}
}
}
}
for (long long i = n - 1; i >= 0; --i) {
for (long long j = m - 1; j >= 0; --j) {
if (i != n - 1) {
if (s[i + 1][j] != '.') {
nearest[i][j].D = i + 1;
} else {
nearest[i][j].D = nearest[i + 1][j].D;
}
}
if (j != m - 1) {
if (s[i][j + 1] != '.') {
nearest[i][j].R = j + 1;
} else {
nearest[i][j].R = nearest[i][j + 1].R;
}
}
}
}
long long mx = 0, cnt = 0;
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < m; ++j) {
if (s[i][j] == '.') {
continue;
}
_next = nearest;
long long ans = helper(i, j);
if (ans == mx) ++cnt;
if (ans > mx) mx = ans, cnt = 1;
}
}
cout << mx << " " << cnt << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t = 1;
for (long long i = 1; i <= t; ++i) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int M = 5000 + 10;
vector<char> board[M];
string dir = "LRUD";
int per[4] = {1, 0, 3, 2};
map<char, int> rev;
struct NODE {
char n_dir;
pair<int, int> point[4];
};
vector<struct NODE> node[M];
int n, m;
void Ini() {
for (int row = 0; row < n; row++) {
int nrow = -1;
int ncol = -1;
for (int col = 0; col < m; col++) {
if (board[row][col] != '.') {
node[row][col].point[0] = make_pair(nrow, ncol);
nrow = row;
ncol = col;
}
}
nrow = ncol = -1;
for (int col = m - 1; col >= 0; col--) {
if (board[row][col] != '.') {
node[row][col].point[1] = make_pair(nrow, ncol);
nrow = row;
ncol = col;
}
}
}
for (int col = 0; col < m; col++) {
int nrow = -1;
int ncol = -1;
for (int row = 0; row < n; row++) {
if (board[row][col] != '.') {
node[row][col].point[2] = make_pair(nrow, ncol);
nrow = row;
ncol = col;
}
}
nrow = ncol = -1;
for (int row = n - 1; row >= 0; row--) {
if (board[row][col] != '.') {
node[row][col].point[3] = make_pair(nrow, ncol);
nrow = row;
ncol = col;
}
}
}
return;
}
void DFS(int row, int col, int &res) {
if (row == -1 && col == -1) {
return;
} else {
for (int i = 0; i < 4; i++) {
int nrow = node[row][col].point[i].first;
int ncol = node[row][col].point[i].second;
if (nrow != -1 && ncol != -1) {
int irow = node[row][col].point[per[i]].first;
int icol = node[row][col].point[per[i]].second;
node[nrow][ncol].point[per[i]] = make_pair(irow, icol);
}
}
int nextp = rev[node[row][col].n_dir];
int nrow = node[row][col].point[nextp].first;
int ncol = node[row][col].point[nextp].second;
res++;
DFS(nrow, ncol, res);
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < 4; i++) {
rev[dir[i]] = i;
}
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (int j = 0; j < m; j++) {
board[i].push_back(s[j]);
NODE inode;
inode.n_dir = '.';
if (s[j] != '.') {
inode.n_dir = s[j];
}
node[i].push_back(inode);
}
}
Ini();
int num_pos = 1;
int max_ans = 0;
for (int row = 0; row < n; row++) {
for (int col = 0; col < m; col++) {
if (board[row][col] != '.') {
int res = 0;
DFS(row, col, res);
if (res > max_ans) {
max_ans = res;
num_pos = 1;
} else {
if (res == max_ans) {
num_pos++;
}
}
Ini();
}
}
}
cout << max_ans << " " << num_pos << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int> > > roww;
vector<vector<pair<int, int> > > colw;
vector<string> board;
int R, C;
int cnt = 0;
void processRow() {
for (int i = 0; i < R; i++) {
int priv = -1;
for (int j = 0; j < C; j++) {
if (board[i][j] == '.') continue;
if (priv != -1) {
roww[i][j].first = priv;
roww[i][priv].second = j;
priv = j;
} else {
priv = j;
}
}
}
}
void processCol() {
for (int j = 0; j < C; j++) {
int priv = -1;
for (int i = 0; i < R; i++) {
if (board[i][j] == '.') continue;
if (priv != -1) {
colw[i][j].first = priv;
colw[priv][j].second = i;
priv = i;
} else {
priv = i;
}
}
}
}
void dfs(int r, int c) {
int tham;
cnt++;
if (board[r][c] == 'U') {
if (colw[r][c].first == -1) return;
int nxt = colw[r][c].first;
colw[nxt][c].second = colw[r][c].second;
if (colw[r][c].second != -1) {
int priv = colw[r][c].second;
colw[priv][c].first = nxt;
}
if (roww[r][c].first != -1) {
int baam = roww[r][c].first;
roww[r][baam].second = roww[r][c].second;
}
if (roww[r][c].second != -1) {
int daan = roww[r][c].second;
roww[r][daan].first = roww[r][c].first;
}
dfs(nxt, c);
colw[nxt][c].second = r;
if (colw[r][c].second != -1) {
int priv = colw[r][c].second;
colw[priv][c].first = r;
}
if (roww[r][c].first != -1) {
int baam = roww[r][c].first;
roww[r][baam].second = c;
}
if (roww[r][c].second != -1) {
int daan = roww[r][c].second;
roww[r][daan].first = c;
}
}
if (board[r][c] == 'D') {
if (colw[r][c].second == -1) return;
int nxt = colw[r][c].second;
colw[nxt][c].first = colw[r][c].first;
if (colw[r][c].first != -1) {
int priv = colw[r][c].first;
colw[priv][c].second = nxt;
}
if (roww[r][c].first != -1) {
int baam = roww[r][c].first;
roww[r][baam].second = roww[r][c].second;
}
if (roww[r][c].second != -1) {
int daan = roww[r][c].second;
roww[r][daan].first = roww[r][c].first;
}
dfs(nxt, c);
colw[nxt][c].first = r;
if (colw[r][c].first != -1) {
int priv = colw[r][c].first;
colw[priv][c].second = r;
}
if (roww[r][c].first != -1) {
int baam = roww[r][c].first;
roww[r][baam].second = c;
}
if (roww[r][c].second != -1) {
int daan = roww[r][c].second;
roww[r][daan].first = c;
}
}
if (board[r][c] == 'R') {
if (roww[r][c].second == -1) return;
int nxt = roww[r][c].second;
roww[r][nxt].first = roww[r][c].first;
if (roww[r][c].first != -1) {
int priv = roww[r][c].first;
roww[r][priv].second = nxt;
}
if (colw[r][c].first != -1) {
int upor = colw[r][c].first;
colw[upor][c].second = colw[r][c].second;
}
if (colw[r][c].second != -1) {
int nich = colw[r][c].second;
colw[nich][c].first = colw[r][c].first;
}
dfs(r, nxt);
roww[r][nxt].first = c;
if (roww[r][c].first != -1) {
int priv = roww[r][c].first;
roww[r][priv].second = c;
}
if (colw[r][c].first != -1) {
int upor = colw[r][c].first;
colw[upor][c].second = r;
}
if (colw[r][c].second != -1) {
int nich = colw[r][c].second;
colw[nich][c].first = r;
}
}
if (board[r][c] == 'L') {
if (roww[r][c].first == -1) return;
int nxt = roww[r][c].first;
roww[r][nxt].second = roww[r][c].second;
if (roww[r][c].second != -1) {
int priv = roww[r][c].second;
roww[r][priv].first = nxt;
}
if (colw[r][c].first != -1) {
int upor = colw[r][c].first;
colw[upor][c].second = colw[r][c].second;
}
if (colw[r][c].second != -1) {
int nich = colw[r][c].second;
colw[nich][c].first = colw[r][c].first;
}
dfs(r, nxt);
roww[r][nxt].second = c;
if (roww[r][c].second != -1) {
int priv = roww[r][c].second;
roww[r][priv].first = c;
}
if (colw[r][c].first != -1) {
int upor = colw[r][c].first;
colw[upor][c].second = r;
}
if (colw[r][c].second != -1) {
int nich = colw[r][c].second;
colw[nich][c].first = r;
}
}
}
int main() {
cin >> R >> C;
string line = "";
vector<pair<int, int> > cc;
pair<int, int> p;
p.first = -1;
p.second = -1;
for (int i = 0; i < C; i++) {
cc.push_back(p);
}
for (int i = 0; i < R; i++) {
roww.push_back(cc);
colw.push_back(cc);
}
for (int i = 0; i < R; i++) {
cin >> line;
board.push_back(line);
}
processRow();
processCol();
int mx = 0;
int oc = 0;
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
if (board[i][j] == '.') continue;
cnt = 0;
dfs(i, j);
if (cnt == mx)
oc++;
else if (cnt > mx) {
mx = cnt;
oc = 1;
}
}
}
cout << mx << " " << oc << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5005;
int N, M;
string grid[MAXN];
struct udlr {
int u = -1, d = -1, l = -1, r = -1;
};
udlr orig[MAXN];
udlr adj[MAXN];
int main() {
cin.tie(0);
cin.sync_with_stdio(0);
cin >> N >> M;
for (int a = 0; a < N; a++) {
cin >> grid[a];
}
for (int a = 0; a < N; a++) {
for (int b = 0; b < M; b++) {
if (grid[a][b] == '.') {
continue;
}
for (int c = a - 1; c >= 0; c--) {
if (grid[c][b] != '.') {
orig[a * M + b].u = c * M + b;
break;
}
}
for (int c = a + 1; c < N; c++) {
if (grid[c][b] != '.') {
orig[a * M + b].d = c * M + b;
break;
}
}
for (int c = b - 1; c >= 0; c--) {
if (grid[a][c] != '.') {
orig[a * M + b].l = a * M + c;
break;
}
}
for (int c = b + 1; c < M; c++) {
if (grid[a][c] != '.') {
orig[a * M + b].r = a * M + c;
break;
}
}
}
}
int best = 0, bestcnt = 0;
for (int a = 0; a < N; a++) {
for (int b = 0; b < M; b++) {
if (grid[a][b] == '.') {
continue;
}
copy(orig, orig + N * M, adj);
int step = 0;
int cur = a * M + b;
while (cur != -1) {
int nxt;
char ch = grid[cur / M][cur % M];
if (ch == 'U') {
nxt = adj[cur].u;
} else if (ch == 'D') {
nxt = adj[cur].d;
} else if (ch == 'L') {
nxt = adj[cur].l;
} else if (ch == 'R') {
nxt = adj[cur].r;
}
if (adj[cur].u != -1) {
adj[adj[cur].u].d = adj[cur].d;
}
if (adj[cur].d != -1) {
adj[adj[cur].d].u = adj[cur].u;
}
if (adj[cur].l != -1) {
adj[adj[cur].l].r = adj[cur].r;
}
if (adj[cur].r != -1) {
adj[adj[cur].r].l = adj[cur].l;
}
step++;
cur = nxt;
}
if (step > best) {
best = step;
bestcnt = 1;
} else if (best == step) {
bestcnt++;
}
}
}
cout << best << " " << bestcnt << "\n";
}
|
#include <bits/stdc++.h>
char ch[6000];
int up[6000], down[6000], left[6000], right[6000], vis[6000], st[6000];
char op[6000];
inline void add(int t) {
if (up[t] >= 0) down[up[t]] = t;
if (down[t] >= 0) up[down[t]] = t;
if (left[t] >= 0) right[left[t]] = t;
if (right[t] >= 0) left[right[t]] = t;
}
inline void del(int t) {
if (up[t] >= 0) down[up[t]] = down[t];
if (down[t] >= 0) up[down[t]] = up[t];
if (left[t] >= 0) right[left[t]] = right[t];
if (right[t] >= 0) left[right[t]] = left[t];
}
int dfs(int t) {
int next;
if (ch[t] == 'U') next = up[t];
if (ch[t] == 'D') next = down[t];
if (ch[t] == 'L') next = left[t];
if (ch[t] == 'R') next = right[t];
if (next < 0) return 1;
del(t);
next = dfs(next) + 1;
add(t);
return next;
}
int main() {
int n, m, num = 0, *q = st;
scanf("%d %d", &n, &m);
getchar();
char *p = ch;
for (int i = 0; i < m; i++) vis[i] = -1;
for (int i = 0; i < n * m; i++) up[i] = down[i] = right[i] = left[i] = -1;
for (int i = 0; i < n; i++) {
gets(p);
for (int j = 0, pre = -1; j < m; j++)
if (*(p++) != '.') {
int cur = i * m + j;
if (vis[j] >= 0) up[cur] = vis[j] * m + j, down[vis[j] * m + j] = cur;
vis[j] = i;
if (pre >= 0) left[cur] = i * m + pre, right[i * m + pre] = cur;
pre = j;
*(q++) = cur;
num++;
}
}
int ans = 0, ans_n = 0;
for (int i = 0; i < num; i++) {
int tmp = dfs(st[i]);
if (tmp > ans)
ans = tmp, ans_n = 1;
else if (tmp == ans)
ans_n++;
}
printf("%d %d\n", ans, ans_n);
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long maxi = 0, res = 1;
long long n, m;
cin >> n >> m;
vector<string> mp(n);
for (long long i = 0; i < ((long long)n); i++) cin >> mp[i];
long long all = n * m;
for (long long start = 0; start < ((long long)all); start++)
if (mp[start / m][start % m] != '.') {
vector<int> up(all), down(all), left(all), right(all);
for (long long i = 0; i < ((long long)all); i++) up[i] = max(-1ll, i - m);
for (long long i = 0; i < ((long long)all); i++)
down[i] = (all <= i + m) ? -1 : (i + m);
for (long long i = 0; i < ((long long)all); i++)
left[i] = (i % m == 0) ? -1 : (i - 1);
for (long long i = 0; i < ((long long)all); i++)
right[i] = ((i % m) + 1 == m) ? -1 : (i + 1);
long long pos = start, cnt = 0, dir = -1;
while (pos != -1) {
char c = mp[pos / m][pos % m];
if (c == 'D')
cnt++, dir = 0;
else if (c == 'R')
cnt++, dir = 1;
else if (c == 'U')
cnt++, dir = 2;
else if (c == 'L')
cnt++, dir = 3;
if (up[pos] != -1) down[up[pos]] = down[pos];
if (down[pos] != -1) up[down[pos]] = up[pos];
if (left[pos] != -1) right[left[pos]] = right[pos];
if (right[pos] != -1) left[right[pos]] = left[pos];
if (dir == 0)
pos = down[pos];
else if (dir == 1)
pos = right[pos];
else if (dir == 2)
pos = up[pos];
else if (dir == 3)
pos = left[pos];
}
if (maxi == cnt) res++;
if (maxi < cnt) {
maxi = cnt;
res = 1;
}
}
cout << maxi << " " << res << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
char g[5005][5005];
vector<vector<pair<int, int> > > L, R, U, D;
vector<vector<pair<int, int> > > LL, RR, UU, DD;
int n, m;
void remove_node(int i, int j) {
int ui = UU[i][j].first, uj = UU[i][j].second;
int di = DD[i][j].first, dj = DD[i][j].second;
int li = LL[i][j].first, lj = LL[i][j].second;
int ri = RR[i][j].first, rj = RR[i][j].second;
DD[ui][uj] = DD[i][j];
UU[di][dj] = UU[i][j];
LL[ri][rj] = LL[i][j];
RR[li][lj] = RR[i][j];
}
int search(int i, int j) {
int ret = 1;
while (true) {
int new_i, new_j;
if (g[i][j] == 'L') {
if (LL[i][j].first == 0) {
return ret;
}
new_i = LL[i][j].first;
new_j = LL[i][j].second;
} else if (g[i][j] == 'R') {
if (RR[i][j].first == 0) {
return ret;
}
new_i = RR[i][j].first;
new_j = RR[i][j].second;
} else if (g[i][j] == 'U') {
if (UU[i][j].first == 0) {
return ret;
}
new_i = UU[i][j].first;
new_j = UU[i][j].second;
} else {
if (DD[i][j].first == 0) {
return ret;
}
new_i = DD[i][j].first;
new_j = DD[i][j].second;
}
remove_node(i, j);
i = new_i;
j = new_j;
++ret;
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
scanf(" %c", &g[i][j]);
}
}
L = R = U = D =
vector<vector<pair<int, int> > >(n + 1, vector<pair<int, int> >(m + 1));
for (int i = 1; i <= n; ++i) {
int last = 0;
for (int j = 1; j <= m; ++j) {
if (g[i][j] != '.') {
if (last != 0) {
L[i][j] = make_pair(i, last);
}
last = j;
}
}
}
for (int i = 1; i <= n; ++i) {
int last = 0;
for (int j = m; j >= 1; --j) {
if (g[i][j] != '.') {
if (last != 0) {
R[i][j] = make_pair(i, last);
}
last = j;
}
}
}
for (int j = 1; j <= m; ++j) {
int last = 0;
for (int i = 1; i <= n; ++i) {
if (g[i][j] != '.') {
if (last != 0) {
U[i][j] = make_pair(last, j);
}
last = i;
}
}
}
for (int j = 1; j <= m; ++j) {
int last = 0;
for (int i = n; i >= 1; --i) {
if (g[i][j] != '.') {
if (last != 0) {
D[i][j] = make_pair(last, j);
}
last = i;
}
}
}
int ans = 0, count = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (g[i][j] != '.') {
LL = L;
RR = R;
UU = U;
DD = D;
int cur = search(i, j);
if (cur == ans) {
++count;
} else if (cur > ans) {
ans = cur;
count = 1;
}
}
}
}
printf("%d %d\n", ans, count);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:64000000")
inline pair<int, int> dxdy(char c) {
if (c == 'U') return pair<int, int>(-1, 0);
if (c == 'D') return pair<int, int>(+1, 0);
if (c == 'L') return pair<int, int>(0, -1);
return pair<int, int>(0, +1);
}
int n, m;
vector<string> a;
vector<vector<pair<int, int> > > b;
string s;
int FLAG;
vector<vector<int> > flag;
vector<vector<int> > was;
vector<vector<int> > T;
inline int check(int x, int y) { return (x >= 0 && y >= 0 && x < n && y < m); }
inline void init() {
cin >> n >> m;
a.resize(n);
flag.resize(n);
T.resize(n);
was.resize(n);
for (int i = 0; i < n; ++i) {
cin >> s;
a[i] = s;
T[i].resize(m);
flag[i].assign(m, -1);
was[i].assign(m, -1);
}
}
inline int solve(int x, int y) {
if (a[x][y] == '.') return 0;
++FLAG;
int res = 0;
for (;;) {
++res;
was[x][y] = FLAG;
pair<int, int> cur = dxdy(a[x][y]);
int dx = cur.first;
int dy = cur.second;
if (flag[x][y] != FLAG) {
T[x][y] = 1;
flag[x][y] = FLAG;
}
int nx = x + T[x][y] * dx;
int ny = y + T[x][y] * dy;
int ok = 0;
while (check(nx, ny)) {
++T[x][y];
if (a[nx][ny] != '.' && was[nx][ny] != FLAG) {
ok = 1;
x = nx;
y = ny;
break;
}
if (a[nx][ny] == a[x][y]) {
int TT = T[nx][ny] - 1;
T[x][y] += TT;
nx += (TT + 1) * dx;
ny += (TT + 1) * dy;
continue;
}
nx += dx;
ny += dy;
}
if (!ok) return res;
}
return res;
}
int main() {
init();
FLAG = 0;
int cnt = 0;
int best = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
int cur = solve(i, j);
if (cur > best) {
best = cur;
cnt = 0;
}
if (cur == best) {
++cnt;
}
}
printf("%d %d\n", best, cnt);
return 0;
}
|
#include <bits/stdc++.h>
int a[5010], d[5010][4];
int n, m, w;
int ans, num;
int used[5010];
int main() {
int i, j, k;
int s;
char c;
scanf("%d%d", &n, &m);
while (scanf("%c", &c) != EOF) {
if (c == 'U')
a[w++] = 0;
else if (c == 'R')
a[w++] = 1;
else if (c == 'D')
a[w++] = 2;
else if (c == 'L')
a[w++] = 3;
else if (c == '.')
a[w++] = 4;
}
for (i = 0; i < w; i++) {
memset(used, 0, sizeof(used));
memset(d, 0, sizeof(d));
if (a[i] > 3) continue;
s = 1;
used[i] = 1;
j = i;
while (1) {
if (a[j] == 0) {
k = d[j][0] + m;
while (j - k >= 0) {
if (!used[j - k] && a[j - k] < 4) break;
k = k + m + d[j - k][0];
}
if (j - k < 0) break;
d[j][0] = k;
j = j - k;
used[j] = 1;
s++;
} else if (a[j] == 1) {
k = d[j][1] + 1;
while ((j % m) + k < m) {
if (!used[j + k] && a[j + k] < 4) break;
k = k + 1 + d[j + k][1];
}
if ((j % m) + k >= m) break;
d[j][1] = k;
j = j + k;
used[j] = 1;
s++;
} else if (a[j] == 2) {
k = d[j][2] + m;
while (j + k < w) {
if (!used[j + k] && a[j + k] < 4) break;
k = k + m + d[j + k][2];
}
if (j + k >= w) break;
d[j][2] = k;
j = j + k;
used[j] = 1;
s++;
} else if (a[j] == 3) {
k = d[j][3] + 1;
while ((j % m) - k >= 0) {
if (!used[j - k] && a[j - k] < 4) break;
k = k + 1 + d[j - k][3];
}
if ((j % m) - k < 0) break;
d[j][3] = k;
j = j - k;
used[j] = 1;
s++;
}
}
if (s > ans) {
ans = s;
num = 1;
} else if (s == ans)
num++;
}
printf("%d %d\n", ans, num);
return 0;
}
|
#include <bits/stdc++.h>
const int maxi = 2000000000;
const int maxq = 1000000000;
const double eps = 1e-10;
using namespace std;
short n, m, ni[5009][5009], nj[5009][5009], pi[5009][5009], pj[5009][5009];
int sum, xx, yy;
char a[5009][5009];
int main() {
cin >> n >> m;
scanf("\n");
int _max = 0;
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) scanf("%c", &a[i][j]);
scanf("\n");
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] != '.') {
for (int i = 1; i <= n; i++) {
int nomj = 0;
for (int j = 1; j <= m; j++)
if (a[i][j] != '.') {
pj[i][j] = nomj;
nj[i][nomj] = j;
nomj = j;
}
nj[i][nomj] = 0;
}
for (int j = 1; j <= m; j++) {
int nomi = 0;
for (int i = 1; i <= n; i++)
if (a[i][j] != '.') {
pi[i][j] = nomi;
ni[nomi][j] = i;
nomi = i;
}
ni[nomi][j] = 0;
}
int x = i;
int y = j;
sum = 0;
while (a[x][y] == 'L' || a[x][y] == 'R' || a[x][y] == 'U' ||
a[x][y] == 'D') {
if (a[x][y] == 'L') {
yy = pj[x][y];
pj[x][nj[x][y]] = yy;
nj[x][pj[x][y]] = nj[x][y];
pi[ni[x][y]][y] = pi[x][y];
ni[pi[x][y]][y] = ni[x][y];
y = yy;
if (y == maxi || y == 0) break;
sum++;
continue;
}
if (a[x][y] == 'R') {
yy = nj[x][y];
nj[x][pj[x][y]] = yy;
pj[x][nj[x][y]] = pj[x][y];
pi[ni[x][y]][y] = pi[x][y];
ni[pi[x][y]][y] = ni[x][y];
y = yy;
if (y == maxi || y == 0) break;
sum++;
continue;
}
if (a[x][y] == 'U') {
xx = pi[x][y];
pi[ni[x][y]][y] = xx;
ni[pi[x][y]][y] = ni[x][y];
pj[x][nj[x][y]] = pj[x][y];
nj[x][pj[x][y]] = nj[x][y];
x = xx;
if (xx == maxi || xx == 0) break;
sum++;
continue;
}
if (a[x][y] == 'D') {
xx = ni[x][y];
ni[pi[x][y]][y] = xx;
pi[ni[x][y]][y] = pi[x][y];
pj[x][nj[x][y]] = pj[x][y];
nj[x][pj[x][y]] = nj[x][y];
x = xx;
if (x == maxi || x == 0) break;
sum++;
continue;
}
}
if (sum > _max) {
_max = sum;
ans = 0;
}
if (sum == _max) ans++;
}
cout << _max + 1 << ' ' << ans << endl;
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000000000")
using namespace std;
const long long int INF = 2e18;
char mode[5002 / 2][5002];
short int cup[4][5002 / 2][5002];
short int up[4][5002 / 2][5002];
int best = 0, cnt = 0;
inline int geti(int type, int x, int y) {
if (up[type][x][y] == -1 || (type >= 2 && up[type][x][y] == x) ||
(type < 2 && up[type][x][y] == y))
return up[type][x][y];
int nex = up[type][x][y];
if (type >= 2) return up[type][x][y] = geti(type, nex, y);
return up[type][x][y] = geti(type, x, nex);
}
inline void upd(int x, int y) {
for (int i = 0; i < 2; i++) {
int plus = (!i ? -1 : 1);
up[i][x][y] = y + plus;
}
for (int i = 0; i < 2; i++) {
int plus = (!i ? -1 : 1);
up[i + 2][x][y] = x + plus;
}
}
int n, m;
int dfs(int i, int j) {
char c = mode[i][j];
upd(i, j);
int out = 1;
if (c == 'L') {
int nex = geti(0, i, j);
if (nex != -1 && nex < m) out = dfs(i, nex) + 1;
}
if (c == 'R') {
int nex = geti(1, i, j);
if (nex != -1 && nex < m) out = dfs(i, nex) + 1;
}
if (c == 'U') {
int nex = geti(2, i, j);
if (nex != -1 && nex < n) out = dfs(nex, j) + 1;
}
if (c == 'D') {
int nex = geti(3, i, j);
if (nex != -1 && nex < n) out = dfs(nex, j) + 1;
}
if (out > best) {
best = out;
cnt = 0;
}
if (out == best) cnt++;
return out;
}
int main() {
cin >> n >> m;
char c;
scanf("\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%c", &c);
if (n < m)
mode[i][j] = c;
else {
if (c == 'U')
c = 'R';
else if (c == 'R')
c = 'D';
else if (c == 'D')
c = 'L';
else if (c != '.')
c = 'U';
mode[j][n - 1 - i] = c;
}
if (c == '.') continue;
}
scanf("\n");
}
if (n >= m) swap(n, m);
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
cup[1][i][j] = m;
cup[3][i][j] = n;
}
}
vector<int> pre(m, -1);
for (int i = 0; i < n; i++) {
int pp = -1;
for (int j = 0; j < m; j++) {
if (mode[i][j] != '.') {
pre[j] = i;
pp = j;
}
cup[0][i][j] = pp;
cup[2][i][j] = pre[j];
}
}
pre.assign(m, n);
for (int i = n - 1; i >= 0; i--) {
int pp = m;
for (int j = m - 1; j >= 0; j--) {
if (mode[i][j] != '.') {
pre[j] = i;
pp = j;
}
cup[1][i][j] = pp;
cup[3][i][j] = pre[j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < 4; k++) {
for (int i2 = 0; i2 <= n; i2++) {
for (int j2 = 0; j2 <= m; j2++) {
up[k][i2][j2] = cup[k][i2][j2];
}
}
}
if (mode[i][j] != '.') dfs(i, j);
}
}
cout << best << " " << cnt;
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
int sz = 0;
int n, m;
struct item {
int l, r, u, d;
char c;
item() {
l = r = u = d = -1;
c = '.';
}
};
item a[5010], b[5010];
int up[5010];
int calc(int num) {
int help = num, kol = 0;
while (help != -1) {
a[a[help].l].r = a[help].r;
a[a[help].r].l = a[help].l;
a[a[help].u].d = a[help].d;
a[a[help].d].u = a[help].u;
if (a[help].c == 'L')
help = a[help].l;
else if (a[help].c == 'R')
help = a[help].r;
else if (a[help].c == 'U')
help = a[help].u;
else
help = a[help].d;
kol++;
}
return kol;
}
int main() {
scanf("%d %d\n", &n, &m);
memset(up, -1, sizeof(up));
for (int i = 0; i < int(n); i++) {
int tek = -1;
for (int j = 0; j < int(m); j++) {
char c;
scanf("%c", &c);
if (c == '.') continue;
a[sz].l = tek;
a[sz].u = up[j];
a[sz].c = c;
if (up[j] != -1) a[up[j]].d = sz;
if (tek != -1) a[tek].r = sz;
tek = sz;
up[j] = sz++;
}
scanf("\n");
}
for (int i = 0; i < int(sz); i++) b[i] = a[i];
int maxx = 0, maxkol = 0;
for (int i = 0; i < int(sz); i++) {
int k = calc(i);
if (k > maxx) {
maxx = k;
maxkol = 1;
} else if (k == maxx)
maxkol++;
for (int j = 0; j < int(sz); j++) a[j] = b[j];
}
cout << maxx << ' ' << maxkol << endl;
return 0;
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000000000")
using namespace std;
const long long int INF = 2e18;
char mode[100][5002];
short int cup[4][100][5002];
short int up[4][100][5002];
int best = 0, cnt = 0;
inline int geti(int type, int x, int y) {
if (up[type][x][y] == -1 || (type >= 2 && up[type][x][y] == x) ||
(type < 2 && up[type][x][y] == y))
return up[type][x][y];
int nex = up[type][x][y];
if (type >= 2) return up[type][x][y] = geti(type, nex, y);
return up[type][x][y] = geti(type, x, nex);
}
inline void upd(int x, int y) {
for (int i = 0; i < 2; i++) {
int plus = (!i ? -1 : 1);
up[i][x][y] = y + plus;
}
for (int i = 0; i < 2; i++) {
int plus = (!i ? -1 : 1);
up[i + 2][x][y] = x + plus;
}
}
int n, m;
int dfs(int i, int j) {
char c = mode[i][j];
upd(i, j);
int out = 1;
if (c == 'L') {
int nex = geti(0, i, j);
if (nex != -1 && nex < m) out = dfs(i, nex) + 1;
}
if (c == 'R') {
int nex = geti(1, i, j);
if (nex != -1 && nex < m) out = dfs(i, nex) + 1;
}
if (c == 'U') {
int nex = geti(2, i, j);
if (nex != -1 && nex < n) out = dfs(nex, j) + 1;
}
if (c == 'D') {
int nex = geti(3, i, j);
if (nex != -1 && nex < n) out = dfs(nex, j) + 1;
}
if (out > best) {
best = out;
cnt = 0;
}
if (out == best) cnt++;
return out;
}
int main() {
cin >> n >> m;
char c;
scanf("\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%c", &c);
if (n < m)
mode[i][j] = c;
else {
if (c == 'U')
c = 'R';
else if (c == 'R')
c = 'D';
else if (c == 'D')
c = 'L';
else if (c != '.')
c = 'U';
mode[j][n - 1 - i] = c;
}
if (c == '.') continue;
}
scanf("\n");
}
if (n >= m) swap(n, m);
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
cup[1][i][j] = m;
cup[3][i][j] = n;
}
}
vector<int> pre(m, -1);
for (int i = 0; i < n; i++) {
int pp = -1;
for (int j = 0; j < m; j++) {
if (mode[i][j] != '.') {
pre[j] = i;
pp = j;
}
cup[0][i][j] = pp;
cup[2][i][j] = pre[j];
}
}
pre.assign(m, n);
for (int i = n - 1; i >= 0; i--) {
int pp = m;
for (int j = m - 1; j >= 0; j--) {
if (mode[i][j] != '.') {
pre[j] = i;
pp = j;
}
cup[1][i][j] = pp;
cup[3][i][j] = pre[j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < 4; k++) {
for (int i2 = 0; i2 <= n; i2++) {
for (int j2 = 0; j2 <= m; j2++) {
up[k][i2][j2] = cup[k][i2][j2];
}
}
}
if (mode[i][j] != '.') dfs(i, j);
}
}
cout << best << " " << cnt;
}
|
#include <bits/stdc++.h>
using namespace std;
int N, M, best, curr, cnt;
char A[5005][5005], B[5005][5005];
int P[4][4 * 5005], up, dp, lp, rp;
int pos(int x, int y) {
if (x < 1 || x > N || y < 1 || y > M) return 0;
return (x - 1) * M + y;
}
int X(int p) {
p--;
return (p / M) + 1;
}
int Y(int p) {
int ret = (p % M);
if (ret == 0) ret = M;
return ret;
}
void init() {
for (int I = 1; I <= N; I++)
for (int K = 1; K <= M; K++) B[I][K] = A[I][K];
for (int I = 1; I <= N; I++)
for (int K = 1; K <= M; K++) {
int p = pos(I, K);
P[0][p] = pos(I - 1, K);
P[1][p] = pos(I + 1, K);
P[2][p] = pos(I, K - 1);
P[3][p] = pos(I, K + 1);
}
}
int setof(int p, int id) {
if (p == 0 || (B[X(p)][Y(p)] != '.')) return p;
return P[id][p] = setof(P[id][p], id);
}
int DFS(int x, int y) {
int p = pos(x, y), np = -1;
char tmp = B[x][y];
B[x][y] = '.';
if (tmp == 'U')
np = setof(p, 0);
else if (tmp == 'D')
np = setof(p, 1);
else if (tmp == 'L')
np = setof(p, 2);
else if (tmp == 'R')
np = setof(p, 3);
else
assert(false);
assert(np != -1);
if (np == 0) return 1;
return 1 + DFS(X(np), Y(np));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M;
for (int I = 1; I <= N; I++) cin >> A[I] + 1;
for (int I = 1; I <= N; I++)
for (int K = 1; K <= M; K++) {
int p = pos(I, K);
}
for (int I = 1; I <= N; I++)
for (int K = 1; K <= M; K++)
if (A[I][K] != '.') {
init();
curr = DFS(I, K);
if (curr > best) {
best = curr;
cnt = 1;
} else if (curr == best) {
cnt++;
}
}
cout << best << " " << cnt << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
void ga(int N, int *A) {
for (int i(0); i < N; i++) scanf("%d", A + i);
}
short H[1 << 17];
int W;
struct DSU {
short *C, *X, *Y;
int gc(int a) { return C[a] = (a == C[a] ? a : gc(C[a])); }
bool con(int a, int b) {
if (gc(a) == gc(b)) return 0;
X[gc(a)] = min(X[gc(a)], X[gc(b)]);
Y[gc(a)] = max(Y[gc(a)], Y[gc(b)]);
C[gc(b)] = gc(a);
return 1;
}
void ini(int N) {
C = H + W, W += N + 1, X = H + W, W += N + 1, Y = H + W, W += N + 1;
}
void CLR(int N) { iota(C, C + N, 0), iota(X, X + N, 0), iota(Y, Y + N, 0); }
int mn(int a) { return X[gc(a)]; }
int mx(int a) { return Y[gc(a)]; }
} R[5005], C[5005];
bool V[5005][5005];
char s[5005][5005];
int N, M, B, I;
void go(int x, int y) {
for (int i(0); i < N; i++)
for (int j(0); j < M; j++) V[i][j] = 0;
for (int i(0); i < N; i++) R[i].CLR(M);
for (int i(0); i < M; i++) C[i].CLR(N);
int S = 0, a;
while (666) {
V[x][y]++, ++S;
if (s[x][y] == 'R')
while (666) {
a = R[x].mx(y) + 1;
if (a == M) goto https;
if (V[x][a] || s[x][a] == 46) {
R[x].con(y, a);
continue;
}
R[x].con(y, a), y = a;
break;
}
else if (s[x][y] == 'L')
while (666) {
a = R[x].mn(y) - 1;
if (!~a) goto https;
if (V[x][a] || s[x][a] == 46) {
R[x].con(y, a);
continue;
}
R[x].con(y, a), y = a;
break;
}
else if (s[x][y] == 'D')
while (666) {
a = C[y].mx(x) + 1;
if (a == N) goto https;
if (V[a][y] || s[a][y] == 46) {
C[y].con(x, a);
continue;
}
C[y].con(x, a), x = a;
break;
}
else {
while (666) {
a = C[y].mn(x) - 1;
if (!~a) goto https;
if (V[a][y] || s[a][y] == 46) {
C[y].con(x, a);
continue;
}
C[y].con(x, a), x = a;
break;
}
}
}
https:
if (S > B) B = S, I = 0;
I += S == B;
}
int main(void) {
scanf("%d%d", &N, &M);
for (int i(0); i < N; i++) R[i].ini(M);
for (int i(0); i < M; i++) C[i].ini(N);
for (int i(0); i < N; i++) scanf("%s", s[i]);
for (int i(0); i < N; i++)
for (int j(0); j < M; j++)
if (s[i][j] ^ 46) go(i, j);
printf("%d %d\n", B, I);
return 0;
}
|
#include <bits/stdc++.h>
const int N = 5010;
const int inf = 0x3f3f3f3f;
using namespace std;
int n, m;
char s[N];
int c[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int check(char c) {
if (c == 'U') return 0;
if (c == 'D') return 1;
if (c == 'L') return 2;
if (c == 'R') return 3;
return -1;
}
bool judge(int x, int y) {
if (x < 0 || x >= n || y < 0 || y >= m) return 0;
return 1;
}
int dp[N][4], vis[N], cnt[N];
int work(int f, int t, int d) {
int &ret = dp[t][d];
int x = ret / m, y = ret % m;
if (s[ret] == '.' || vis[ret] == f) {
x += c[d][0], y += c[d][1];
if (!judge(x, y)) return ret = -1;
ret = work(f, x * m + y, d);
}
return ret;
}
int dfs(int f, int t, int d) {
int x = t / m, y = t % m;
int ret = work(f, t, d);
if (ret == -1) return 0;
vis[ret] = f;
dfs(f, ret, check(s[ret]));
return 0;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) cin >> s[i * m + j];
memset(vis, -1, sizeof(vis));
int ret = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
for (int ii = 0; ii < n; ii++)
for (int jj = 0; jj < m; jj++)
for (int k = 0; k < 4; k++) dp[ii * m + jj][k] = ii * m + jj;
if (s[i * m + j] != '.')
vis[i * m + j] = i * m + j,
dfs(i * m + j, i * m + j, check(s[i * m + j]));
for (int ii = 0; ii < n; ii++)
for (int jj = 0; jj < m; jj++)
if (s[ii * m + jj] != '.' && vis[ii * m + jj] == i * m + j)
cnt[i * m + j]++;
ret = max(ret, cnt[i * m + j]);
}
int c = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (ret == cnt[i * m + j]) c++;
cout << ret << " " << c << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 5050;
char s[N];
int l[N], r[N], u[N], d[N], n, m, x[N], y[N];
int ID(int i, int j) { return (i - 1) * m + j; }
void Del(int id) {
int L = l[id], R = r[id], U = u[id], D = d[id];
if (L) r[L] = R;
if (R) l[R] = L;
if (U) d[U] = D;
if (D) u[D] = U;
}
void init() {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
int id = ID(i, j);
if (i > 1) u[id] = ID(i - 1, j);
if (i < n) d[id] = ID(i + 1, j);
if (j > 1) l[id] = ID(i, j - 1);
if (j < m) r[id] = ID(i, j + 1);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
int id = ID(i, j);
if (s[id] == '.') Del(id);
}
}
int Calc(int id) {
if (s[id] == '.') return 0;
init();
int ans = 0;
while (id) {
Del(id);
if (s[id] == 'U')
id = u[id];
else if (s[id] == 'D')
id = d[id];
else if (s[id] == 'L')
id = l[id];
else if (s[id] == 'R')
id = r[id];
ans++;
}
return ans;
}
char Read() {
char c = getchar();
while (c != '.' && c != 'U' && c != 'D' && c != 'L' && c != 'R')
c = getchar();
return c;
}
int main() {
scanf("%i %i", &n, &m);
for (int i = 1; i <= n * m; i++) s[i] = Read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) x[ID(i, j)] = i, y[ID(i, j)] = j;
int sol = 0, cnt = 0;
for (int i = 1; i <= n * m; i++) {
int ans = Calc(i);
if (ans > sol) sol = ans, cnt = 0;
if (ans == sol) cnt++;
}
printf("%i %i\n", sol, cnt);
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.