BZOJ3671 [Noi2014]随机数生成器

[Solution]

I thought of size-balanced tree at first, the got only 60 at the exams. In fact, we can just modify two arrays ml[i] and mr[i] meaning the left and the right pos that the number can be that row. Because there are many queries but few changes, it can get AC. What a pity that I didn't come up with that while examing.


[Code]

#define PROC "random"
#include <cstdio>
#include <cctype>
#include <memory.h>
#include <algorithm>
 
using namespace std;
 
int nextInt() {
    int d, s = 0;
    do
        d = getchar();
    while (!isdigit(d));
    do
        s = s * 10 + d - 48, d = getchar();
    while (isdigit(d));
    return s;
}
 
#define _l (long long int)
#define mkword(a,b) (((a)<<16)|(b))
#define hiword(a) ((a)>>16)
#define loword(a) ((a)&65535)
 
const int maxn = 5009;
const int inf = 0x3f3f3f3f;
 
int n, m, t, a[maxn][maxn], r[maxn * maxn], ans[maxn * 2], ml[maxn], mr[maxn];
 
void init() {
    int x, a, b, c, d, q;
    x = nextInt();
    a = nextInt();
    b = nextInt();
    c = nextInt();
    d = nextInt();
    n = nextInt();
    m = nextInt();
    q = nextInt();
    t = n * m;
    for (int i = 1; i <= t; i ++)
        r[i] = i;
    for (int i = 1; i <= t; i ++) {
        x = (_l a * (_l x * x % d) % d + _l b * x % d + c) % d;
        swap(r[i], r[x % i + 1]);
    }
    for (int i = 0; i < q; i ++) {
        int u = nextInt();
        int v = nextInt();
        swap(r[u], r[v]);
    }
    for (int i = 0; i < n; i ++)
        for (int j = 0; j < m; j ++)
            ::a[i][j] = r[i * m + j + 1];
    for (int i = 0; i < n; i ++)
        for (int j = 0; j < m; j ++)
            r[::a[i][j]] = mkword(i + 1, j + 1);
}
 
void solve() {
    for (int i = 1; i <= n; i ++)
        ml[i] = 1, mr[i] = m;
    for (int i = 0, p = 1; i < m + n - 1; i ++) {
        while (ml[hiword(r[p])] > loword(r[p]) || mr[hiword(r[p])] < loword(r[p]))
            p ++;
        for (int j = 1; j < hiword(r[p]); j ++)
            mr[j] = min(mr[j], loword(r[p]));
        for (int j = hiword(r[p]) + 1; j <= n; j ++)
            ml[j] = max(ml[j], loword(r[p]));
        ans[i] = p ++;
    }
}
 
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
 
    init();
//  debugPrint();
    solve();
    for (int i = 0; i < (m + n - 1); i ++)
        printf("%d%c", ans[i], ((i < (n + m - 2)) ? 32 : 10));
//  putchar(10);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值