t1
傻逼dfs,大暴搜,不贴代码。
t2
ans= 黑点数 − <script type="math/tex" id="MathJax-Element-13">-</script> 边数,二维前缀和。
#include<set>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
inline int read() {
int x = 0, flag = 1; char ch = getchar();
while (ch > '9' || ch < '0') { if (ch == '-') flag = -1; ch = getchar(); }
while (ch <= '9' && ch >= '0') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * flag;
}
inline bool get() {
char ch = getchar();
while (ch != '1' && ch != '0') ch = getchar();
return ch == '1';
}
#define rep(ii, aa, bb) for (int ii = aa; ii <= bb; ii++)
#define ll long long
#define N 2001
bool a[N][N];
int sum[N][N], upe[N][N], lfe[N][N];
int main() {
int n, m, q; scanf("%d%d%d", &n, &m, &q);
rep(i, 1, n) rep(j, 1, m) a[i][j] = get();
rep(i, 1, n) rep(j, 1, m) {
upe[i][j] = upe[i - 1][j] + (a[i][j] & a[i][j - 1]);
lfe[i][j] = lfe[i][j - 1] + (a[i][j] & a[i - 1][j]);
sum[i][j] = a[i][j] - (a[i][j] & a[i][j - 1]) - (a[i][j] & a[i - 1][j]) + sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1];
}
while (q--) {
int x1 = read(), y1 = read(), x2 = read(), y2 = read();
int ans = 0;
ans += sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1];
ans -= upe[x1 - 1][y1] - upe[x2][y1];
ans -= lfe[x1][y1 - 1] - lfe[x1][y2];
printf("%d\n", ans);
}
return 0;
}
t3
卡空间的逆序对,玄学颓公式。
#include<set>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
inline int read() {
int x = 0, flag = 1; char ch = getchar();
while (ch > '9' || ch < '0') { if (ch == '-') flag = -1; ch = getchar(); }
while (ch <= '9' && ch >= '0') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * flag;
}
#define rep(ii, aa, bb) for (int ii = aa; ii <= bb; ii++)
#define ll long long
#define N 100001
int n, x, a, mod, cnt = 0;
struct period {
int base, cnt;
ll operator * (const period &t) {
int st = (base - t.base + a) / a;
int tn = t.cnt - st + 1;
if (tn >= cnt) return (ll)cnt * ((st << 1) + cnt - 1) >> 1;
return ((ll)tn * (st + t.cnt) >> 1) + (ll)t.cnt * (cnt - tn);
}
} p[N];
int main() {
scanf("%d%d%d%d", &n, &x, &a, &mod);
while (true) {
int tmp = (mod - x + a - 1) / a;
if (tmp >= n) {
p[cnt].base = x, p[cnt].cnt = n;
break;
}
p[cnt].base = x, p[cnt++].cnt = tmp;
x += tmp*a - mod;
n -= tmp;
}
ll ans = 0;
rep(i, 0, cnt - 1) rep(j, i + 1, cnt) ans += p[i] * p[j];
printf("%lld\n", ans);
return 0;
}