2017-09-29校训练题题解

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值