UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#214454#2718. 8.2t3N201419ms20964kbC++115.1kb2024-11-18 22:17:022024-11-19 08:37:57

answer

//
//  na 2718.cpp
//  Competitive Programming
//
//  Created by m2 on 2024/11/18.
//
#ifndef MAC_OS_VERSION_11_0
#pragma GCC optimize("Ofast")
#endif
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <random>
using namespace std;
typedef long long int ll;
const int maxn = 4, maxs = 16, maxq = 1e5, mod = 998244353;
inline void add(int &a, int b){
    if((a += b) >= mod)
        a -= mod;
}
inline int mul(int a, int b){
    return ll(a) * b % mod;
}
struct Mat{
    int m[maxn][maxn], w, h;
    Mat(int w, int h): w(w), h(h) {
        for(int i = 0; i < h; ++i)
            for(int j = 0; j < w; ++j)
                m[i][j] = 0;
    }
    Mat(){}
    inline Mat operator * (const Mat &other) const{
        Mat res(other.w, h);
        for(int i = 0; i < res.h; ++i)
            for(int j = 0; j < res.w; ++j){
                for(int k = 0; k < w; ++k)
                    add(res.m[i][j], mul(m[i][k], other.m[k][j]));
            }
        return res;
    }
    inline void print(){
        cerr << "[";
        for(int i = 0; i < h; ++i){
            cerr << " ";
            for(int j = 0; j < w; ++j)
                cerr << setw(4) << m[i][j];
            if(i != h - 1)
                cerr << endl;
        }
        cerr << "   ]" << endl;
    }
} states[maxs][maxs], sstate(4, 1), unit;
inline Mat qpow(Mat base, int exp){
    Mat res = unit;
    while(exp){
        if(exp & 1)
            res = res * base;
        base = base * base;
        exp >>= 1;
    }
    return res;
}
int n, m, q, k;
namespace segtree{
struct Node{
    int lt, rt, mid, lstate, rstate;
    Mat res;
    Node *lc, *rc;
    Node(int lt, int rt): lt(lt), rt(rt), mid((lt + rt) >> 1), lc(nullptr), rc(nullptr) {}
    inline void update(){ // non leaf
//        cerr << "update: " << lt << " -> " << rt << endl;
        lstate = lc == nullptr? 0: lc->lstate;
        rstate = rc == nullptr? 0: rc->rstate;
//        cerr << "ls = " << lstate << ", rs = " << rstate << endl;
        res = (lc == nullptr? qpow(states[0][0], mid - lt): lc->res) *
              states[lc == nullptr? 0: lc->rstate][rc == nullptr? 0: rc->lstate] *
              (rc == nullptr? qpow(states[0][0], rt - mid - 1): rc->res);
//        cerr << "res: " << endl;
//        res.print();
    }
};
inline void change(int &x, int &y, Node* cur){
//    cerr << "change: " << cur->lt << " -> " << cur->rt << endl;
    if(cur->lt == cur->rt){
        cur->lstate = cur->rstate = cur->lstate | (1 << y);
        cur->res = unit;
        return;
    }
    if(x <= cur->mid){
        if(cur->lc == nullptr)
            cur->lc = new Node(cur->lt, cur->mid);
        change(x, y, cur->lc);
    }else{
        if(cur->rc == nullptr)
            cur->rc = new Node(cur->mid + 1, cur->rt);
        change(x, y, cur->rc);
    }
    cur->update();
}
}
using segtree::Node; using segtree::change;
inline void qread(int &var){
    var = 0;
    char reg = getchar();
    while(reg - 48 >= 10u)
        reg = getchar();
    do{
        var = (var << 1) + (var << 3) + (reg ^ 48);
        reg = getchar();
    }while(reg - 48 < 10u);
}
namespace qwritesp{
char buf[15];
inline void qwrite(int val){
    int p = 0, tmp;
    while(val){
        tmp = val / 10;
        buf[p++] = (val - tmp * 10) ^ 48;
        val = tmp;
    }
    for(int i = 0; i < p; ++i)
        putchar(buf[i]);
    if(!p)
        putchar(48);
}
}
using qwritesp::qwrite;
#ifdef MAC_OS_VERSION_11_0
#define RNDDATA
#endif
#ifdef RNDDATA
random_device rd;
mt19937 rnd(rd());
#endif
int main(){
    cin.tie(0)->sync_with_stdio(false);
//    cin >> n >> m >> q;
    qread(n); qread(m); qread(q);
#ifdef MAC_OS_VERSION_11_0
    time_t st = clock();
#endif
    unit = Mat(n, n);
    for(int i = 0; i < n; ++i)
        unit.m[i][i] = true;
    k = 1 << n;
    sstate.m[0][0] = true;
    for(int ss = 0; ss < k; ++ss)
        for(int ns = 0; ns < k; ++ns){
            states[ss][ns] = Mat(n, n);
            for(int i = 0; i < n; ++i)
                if((~ss & ~ns) & (1 << i)){
                    if(i && (~ss & (1 << (i - 1))))
                        states[ss][ns].m[i - 1][i] = true;
                    if(i != n - 1 && (~ss & (1 << (i + 1))))
                        states[ss][ns].m[i + 1][i] = true;
                }
        }
    Node *root = new Node(1, m + 1);
    root->update();
//    cerr << "root: " << endl;
//    root->res.print();
//    cerr << "emp: " << (sstate * root->res).m[0][n - 1] << endl;
    int x, y;
    while(q--){
#ifdef RNDDATA
        x = rnd() % m + 1;
        y = rnd() % n + 1;
#else
//        cin >> y >> x;
        qread(y); qread(x);
#endif
//        cerr << x << ", " << y << endl;
        --y;
        change(x, y, root);
//        cerr << "root: " << endl;
//        root->res.print();
#ifndef RNDDATA
//        cout << (sstate * root->res).m[0][n - 1] << endl;
        qwrite((sstate * root->res).m[0][n - 1]);
        putchar('\n');
#endif
    }
#ifdef MAC_OS_VERSION_11_0
    cerr << double(clock() - st) / CLOCKS_PER_SEC * 1000 << endl;
#endif
    fflush(stdout);
    return 0;
}
 

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 1252kb

input:

3 4 5
3 1
1 4
1 2
1 3
2 2

output:

2
2
1
1
0

result:

ok 5 number(s): "2 2 1 1 0"

Test #2:

score: 10
Accepted
time: 220ms
memory: 20952kb

input:

2 99999 100000
2 70567
1 17791
2 77890
2 12623
2 13544
1 18390
2 8888
1 74050
2 67101
1 56764
2 3761...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 100000 numbers

Test #3:

score: 0
Wrong Answer
time: 1ms
memory: 1320kb

input:

3 1000 5
3 805
3 596
3 575
3 58
3 577

output:

166920451
700731675
086091787
043595393
076797691

result:

wrong answer 1st numbers differ - expected: '154029661', found: '166920451'

Test #4:

score: 0
Wrong Answer
time: 3ms
memory: 1512kb

input:

3 1000 1000
1 45
3 898
1 799
1 48
1 847
3 607
1 760
1 802
3 903
3 836
1 526
3 264
1 96
3 244
3 242
3...

output:

166920451
700731675
086091787
043595393
076797691
53889389
495123845
797061472
575202636
464322718
2...

result:

wrong answer 1st numbers differ - expected: '154029661', found: '166920451'

Test #5:

score: 0
Wrong Answer
time: 402ms
memory: 20956kb

input:

3 100000 100000
1 74197
3 11259
3 65940
3 1906
3 40328
1 71201
1 50943
1 98978
1 70015
3 43996
1 114...

output:

035434951
56271797
908089835
185216867
764824388
014638049
502814074
972133437
618782668
809341334
4...

result:

wrong output format Expected integer, but "035434951" found

Test #6:

score: 0
Wrong Answer
time: 406ms
memory: 20964kb

input:

3 100000 100000
1 16644
3 29648
3 77047
1 94435
1 43985
1 89629
1 78236
1 93267
3 25517
1 52560
1 89...

output:

035434951
56271797
908089835
185216867
764824388
014638049
502814074
972133437
618782668
809341334
4...

result:

wrong output format Expected integer, but "035434951" found

Test #7:

score: 0
Wrong Answer
time: 5ms
memory: 1496kb

input:

3 1000000000 100
1 574043575
1 413916829
3 96
1 394696238
1 40
3 38
1 69
1 968879034
1 52
1 62558778...

output:

048745547
029377273
069683681
08439139
04769564
07389232
58194611
967649405
165595157
759919478
5512...

result:

wrong output format Expected integer, but "048745547" found

Test #8:

score: 0
Time Limit Exceeded

input:

3 1000000000 100000
3 15019
1 79221
3 22394
1 89278
3 875067515
1 15404
1 615238057
3 89925
3 271777...

output:

048745547
029377273
069683681
08439139
04769564
07389232
58194611
967649405
165595157
759919478
5512...

result:


Test #9:

score: 0
Wrong Answer
time: 382ms
memory: 18496kb

input:

4 99999 50000
4 66854
4 48295
4 95292
4 86389
4 9961
4 33406
4 96945
4 64418
4 19331
4 71257
4 36656...

output:

173992636
918447512
067725252
797125196
156729433
163366194
9868232
732977345
827297607
238050775
82...

result:

wrong answer 1st numbers differ - expected: '636299371', found: '173992636'

Test #10:

score: 0
Time Limit Exceeded

input:

4 999999999 100000
4 908546081
4 885383980
4 37966517
4 191661556
4 107475378
4 699076844
4 58764448...

output:

204991199
253170675
736908982
240148368
937881899
981699773
709226808
571976633
874004355
611906376
...

result: