QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#748222#9731. Fuzzy RankinginfCraftTL 1563ms138728kbC++174.4kb2024-11-14 19:44:412024-11-14 19:44:42

Judging History

你现在查看的是最新测评结果

  • [2024-11-25 12:28:53]
  • hack成功,自动添加数据
  • (/hack/1257)
  • [2024-11-14 19:44:42]
  • 评测
  • 测评结果:TL
  • 用时:1563ms
  • 内存:138728kb
  • [2024-11-14 19:44:41]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back

#define Yes cout << "Yes\n"
#define No cout << "No\n"
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define ff first
#define ss second
#define fori(x,y) for(int i=x;i<=(int)(y);++i)
#define forj(x,y) for(int j=x;j<=(int)(y);++j)
#define fork(x,y) for(int k=x;k<=(int)(y);++k)

#define debug(x) cerr << #x << " = " << x << endl

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

ll MOD = 998244353;
ll qpow(ll a,ll p) {ll res=1; while(p) {if (p&1) {res=res*a%MOD;} a=a*a%MOD; p>>=1;} return res;}

struct SegTree {
    int n;
    vector<int> arr;
    vector<map<int,ll>> tree;
    SegTree(int n): n(n) {  // 开4倍空间
        n *= 4;
        tree = vector<map<int,ll>>(n+1);
        arr = vector<int>(n+1);
    }
    inline int ls(int p) { return p<<1; }  // 线段树左孩子 
    inline int rs(int p) { return p<<1|1; }  // 线段树右孩子

    void push_up(int p) {  // 由下向上传递区间值 
        if (tree[ls(p)].size()>=tree[rs(p)].size()) {
            tree[p] = tree[ls(p)];
            for (auto [x, y]: tree[rs(p)]) tree[p][x] += y;
        }
        else {
            tree[p] = tree[rs(p)];
            for (auto [x, y]: tree[ls(p)]) tree[p][x] += y;
        }
    }
    
    void build(int p, int pl, int pr) {
        if (pl == pr) {
            tree[p][arr[pl]]++;
            return;
        }
        int mid = (pl+pr)>>1;
        build(ls(p), pl, mid);
        build(rs(p), mid+1, pr);
        push_up(p);
    }

    map<int,ll> query(int L, int R, int p, int pl, int pr) {
        if (L<=pl&&R>=pr) return tree[p];  // [L,R]完全覆盖区间[pl, pr],直接返回
        map<int,ll> res;  // 存储结果
        int mid = (pl+pr)>>1;
        if (L<=mid) {
            map<int,ll> tmp = query(L, R, ls(p), pl, mid);
            if (tmp.size()>res.size()) swap(tmp, res);
            for (auto [x, y]: tmp) res[x] += y;
        }
        if (R>mid) {
            map<int,ll> tmp = query(L, R, rs(p), mid+1, pr);  // 递归求下面的子树
            if (tmp.size()>res.size()) swap(tmp, res);
            for (auto [x, y]: tmp) res[x] += y;
        }
        return res;
    }
};

void solve() {
	int n, m, q;
    cin >> n >> m >> q;
    vector<vector<int>> g(n+1, vector<int>());
    vector<vector<int>> rnk(m+1, vector<int>(n+1, 0));
    fori(1, m) {
        forj(1, n) cin >> rnk[i][j];
        forj(1, n-1) g[rnk[i][j]].push_back(rnk[i][j+1]);
    }

    vector<int> scc(n+1, 0), low(n+1, 0), num(n+1, 0);
    int dfn = 0, cnt = 0;  // dfn为访问序号,cnt记录强连通分量的个数和编号
    stack<int> st;  // 用于保存属于同一个强连通分量的点

    auto dfs = [&](auto self, int u) -> void {
        st.push(u);
        low[u] = num[u] = ++dfn;  // 初始化
        fori(0, g[u].size()-1) {
            int v = g[u][i];
            if (!num[v]) {  // 如果没有访问过
                self(self, v);
                low[u] = min(low[u], low[v]);  // 递归返回子节点的low
            }
            else if (!scc[v]) {  // 如果子节点v访问过且不属于某一个强连通分量(好像直接else不加判断也可以)
                low[u] = min(low[u], num[v]);  // 该点的序号
            }
        }
        if (low[u] == num[u]) {  // 栈底:scc祖先
            cnt++;  // 这是一个新的强连通分量
            while (1) {
                int v = st.top();
                st.pop();
                scc[v] = cnt;  // 记录编号
                if (v == u) break;  // 栈底为该强连通分量的祖先节点
            }
        }
    };
    fori(1, n) if (!num[i]) dfs(dfs, i);

    vector<SegTree> vec;
    fori(1, m) {
        vec.push_back(SegTree(n));
        forj(1, n) vec.back().arr[j] = scc[rnk[i][j]];
        vec.back().build(1, 1, n);
    }

    ll lastans = 0;
    while (q--) {
        int id, l, r;
        cin >> id >> l >> r;
        id = (id+lastans)%m+1;
        l = (l+lastans)%n+1;
        r = (r+lastans)%n+1;

        map<int,ll> maps = vec[id-1].query(l, r, 1, 1, n);
        ll ans = 0;
        for (auto [x, y]: maps) ans += y*(y-1)/2;
        cout << ans << endl;
        lastans = ans;
    }

}

signed main() {
	IOS;
	int t;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3628kb

input:

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

output:

3
10
1
1
2

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 58ms
memory: 3612kb

input:

2000
10 10 10
4 5 3 6 8 9 2 1 7 10
4 5 6 3 8 9 1 2 10 7
5 4 3 6 8 9 1 2 7 10
4 5 6 3 8 9 1 2 7 10
4 5 3 6 8 9 2 1 10 7
4 5 6 3 8 9 1 2 10 7
5 4 6 3 8 9 1 2 7 10
5 4 6 3 8 9 1 2 10 7
4 5 6 3 8 9 2 1 7 10
5 4 3 6 8 9 2 1 10 7
3 1 6
5 7 8
0 2 3
7 9 9
2 1 9
6 1 6
7 2 3
0 0 4
1 8 1
1 8 7
10 10 10
9 10 5 ...

output:

1
1
0
0
3
2
0
2
2
4
1
0
1
1
1
1
2
4
4
3
1
6
28
0
0
10
10
6
6
15
0
3
10
6
16
6
11
1
2
1
1
6
3
3
0
4
5
3
4
1
1
3
2
4
0
3
4
4
4
4
0
0
1
1
2
0
0
1
2
1
1
0
0
1
4
3
0
4
4
1
3
6
16
16
0
11
16
1
4
15
1
4
2
0
0
2
0
1
2
4
0
0
0
0
0
0
0
0
0
0
1
0
0
6
3
0
3
4
0
0
0
0
0
0
0
0
0
0
0
0
3
0
0
1
3
1
0
0
3
3
9
1
9
4
...

result:

ok 20000 lines

Test #3:

score: 0
Accepted
time: 53ms
memory: 3844kb

input:

8000
5 5 10
3 5 2 4 1
3 2 5 4 1
3 5 2 4 1
3 5 2 4 1
3 5 2 4 1
1 1 1
4 1 3
1 0 3
4 2 3
1 0 1
3 2 3
1 2 3
3 0 2
1 1 3
1 1 2
5 5 10
5 3 1 2 4
3 5 1 2 4
5 3 1 2 4
3 5 1 2 4
5 3 1 2 4
0 0 1
2 0 1
4 1 2
1 3 4
2 0 1
4 3 3
1 4 4
1 3 4
0 0 4
0 0 3
5 5 10
2 3 4 1 5
5 1 4 3 2
1 3 4 2 5
2 3 4 1 5
5 1 3 4 2
1 2 ...

output:

0
1
1
0
0
0
0
1
0
1
1
0
0
0
1
0
0
0
1
0
3
0
3
1
0
3
1
6
1
0
0
0
0
0
0
0
0
0
0
0
1
1
2
1
0
3
0
0
3
0
1
0
0
0
0
0
0
1
0
0
6
1
0
6
0
3
3
3
0
0
3
3
6
1
10
1
3
0
1
0
3
1
0
0
1
0
1
1
1
2
0
0
0
0
0
0
0
0
0
0
0
0
3
1
3
3
1
3
1
0
0
0
0
0
0
0
0
0
0
0
0
1
1
0
0
1
6
0
6
6
1
1
1
0
1
1
0
0
1
0
0
0
3
0
1
1
0
2
3
3...

result:

ok 80000 lines

Test #4:

score: 0
Accepted
time: 42ms
memory: 3780kb

input:

8000
5 5 5
1 3 5 2 4
5 3 2 1 4
5 2 1 3 4
3 1 2 5 4
1 3 2 5 4
1 1 2
1 4 0
1 4 1
2 2 2
4 1 3
5 5 5
2 3 4 1 5
2 3 4 1 5
2 3 4 5 1
2 3 4 1 5
2 3 4 5 1
2 0 4
0 0 0
4 1 3
3 0 1
4 4 4
5 5 5
2 5 4 1 3
2 5 4 1 3
2 5 4 1 3
2 5 4 1 3
2 5 4 1 3
3 1 3
2 0 4
0 1 3
4 0 2
3 4 4
5 5 5
1 2 4 5 3
1 2 4 5 3
1 2 4 5 3
1...

output:

1
1
3
0
3
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
3
0
1
0
0
0
1
0
0
1
1
1
1
3
0
3
0
0
0
0
0
10
3
1
3
1
2
1
1
1
0
3
3
1
0
1
6
3
6
6
1
0
0
0
0
0
0
2
1
2
0
3
1
1
1
3
1
3
1
3
3
6
3
6
0
1
1
0
6
0
3
1
1
1
1
0
0
0
0
0
0
6
0
0
10
1
0
0
0
1
2
1
1
0
0
0
1
1
1
0
0
1
0
1
1
0
1
3
0
0
0
3
1
0
10
0
4
0
0
2...

result:

ok 40000 lines

Test #5:

score: 0
Accepted
time: 64ms
memory: 3596kb

input:

2000
1 100 100
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
25 0 0
9 0 0
80 0 0
37 0 0
18 0 0
24 0 0
5 0 0
87 0 0
50 0 0
63 0 0
53 0 0
62 0 0
37 ...

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
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
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 200000 lines

Test #6:

score: 0
Accepted
time: 58ms
memory: 14788kb

input:

10
20 1000 2000
2 5 1 3 12 16 14 15 7 4 19 13 18 10 20 9 11 8 6 17
5 2 12 1 16 3 19 4 7 15 14 18 13 10 9 20 11 8 6 17
2 5 1 16 12 3 7 14 4 19 15 13 18 10 20 9 11 17 6 8
2 5 1 16 3 12 15 7 4 14 19 18 13 10 9 20 11 8 6 17
5 2 3 12 1 16 14 7 4 15 19 18 13 10 20 9 11 6 17 8
2 5 3 1 12 16 19 15 7 4 14 18...

output:

11
0
11
10
1
11
5
10
10
5
13
2
0
18
2
14
2
18
10
13
1
1
0
1
4
7
6
0
15
11
1
4
6
19
3
9
3
1
0
20
2
16
1
0
3
2
3
0
18
1
17
1
1
8
1
5
17
1
3
6
1
2
15
15
2
1
0
3
18
22
0
1
2
15
13
12
20
3
0
9
15
1
4
17
22
16
8
6
13
0
7
11
15
19
15
6
7
10
17
12
9
1
11
1
0
4
6
17
1
17
6
5
1
1
16
14
3
1
12
18
1
0
18
12
17
...

result:

ok 20000 lines

Test #7:

score: 0
Accepted
time: 48ms
memory: 6164kb

input:

33
3 2000 2000
1 2 3
2 1 3
2 1 3
2 1 3
1 2 3
2 1 3
2 1 3
1 2 3
2 1 3
1 2 3
2 1 3
2 1 3
2 1 3
1 2 3
2 1 3
2 1 3
2 1 3
1 2 3
2 1 3
2 1 3
1 2 3
1 2 3
2 1 3
1 2 3
2 1 3
1 2 3
2 1 3
1 2 3
2 1 3
2 1 3
1 2 3
2 1 3
2 1 3
2 1 3
1 2 3
2 1 3
1 2 3
1 2 3
2 1 3
1 2 3
1 2 3
1 2 3
1 2 3
2 1 3
2 1 3
1 2 3
2 1 3
2 1...

output:

1
1
0
1
0
0
1
0
0
0
0
0
0
1
1
0
0
0
1
1
0
0
0
0
1
0
1
0
0
0
0
1
0
0
1
0
1
0
0
0
1
0
1
0
0
0
0
1
0
0
1
1
0
1
0
0
1
0
1
1
0
0
0
1
1
0
1
1
0
1
0
1
0
0
0
1
0
0
0
1
1
1
1
0
0
0
1
0
1
0
0
0
0
1
1
0
0
1
1
1
0
1
1
0
0
1
0
0
0
0
1
0
0
0
0
0
0
0
1
1
1
0
0
0
0
1
0
1
0
1
0
0
0
1
1
1
0
0
0
0
0
0
0
0
0
0
1
1
0
0
...

result:

ok 66000 lines

Test #8:

score: 0
Accepted
time: 302ms
memory: 17388kb

input:

10
100 200 20000
33 77 70 12 88 3 29 59 63 41 75 18 42 83 96 44 67 73 79 48 92 54 78 93 21 14 24 72 91 25 71 97 2 89 76 20 37 95 68 87 39 31 5 100 9 23 74 80 7 27 50 69 52 82 81 85 56 84 34 32 17 36 11 16 8 57 49 30 60 86 62 99 13 19 98 43 6 4 46 58 64 45 51 53 10 28 90 26 40 94 35 22 61 15 47 1 55 ...

output:

1
4
2
3
2
4
0
2
1
1
1
0
0
4
0
4
3
2
2
1
4
0
4
0
2
2
4
2
2
0
2
2
1
0
0
1
3
2
2
0
0
0
3
2
0
1
4
1
2
2
3
1
3
0
2
1
1
4
2
1
0
0
3
0
0
2
0
2
4
3
0
4
0
0
3
0
2
2
0
4
3
4
2
3
2
0
2
0
1
1
2
1
2
1
4
0
1
0
1
2
0
2
4
1
0
2
4
2
3
1
4
2
0
2
0
2
0
2
2
0
1
2
1
2
0
3
2
1
2
1
1
4
1
1
1
2
1
2
2
1
0
2
0
1
0
1
1
1
0
2
...

result:

ok 200000 lines

Test #9:

score: 0
Accepted
time: 1563ms
memory: 138728kb

input:

1
316 632 200000
36 93 100 184 246 134 89 157 219 9 18 29 8 109 159 3 255 66 257 27 290 286 283 132 216 175 167 208 238 31 220 296 271 113 269 127 156 300 121 267 99 122 90 288 64 210 28 144 262 60 53 6 96 268 276 279 13 174 287 78 295 72 143 155 146 197 107 35 24 88 187 110 163 204 2 25 226 119 164...

output:

7
87
10
47
74
55
79
64
19
2
104
21
54
1
4
72
81
57
12
74
28
77
82
77
7
22
56
6
81
4
28
24
56
19
25
7
7
4
64
68
51
16
95
23
67
31
33
45
72
69
65
7
24
11
85
25
0
1
0
20
72
43
56
12
16
76
98
91
50
84
23
71
8
14
22
49
64
3
24
2
15
5
90
11
26
40
3
12
4
23
88
65
34
42
25
4
80
3
6
20
41
83
12
10
43
24
3
77...

result:

ok 200000 lines

Test #10:

score: -100
Time Limit Exceeded

input:

1
632 316 200000
625 142 323 331 85 521 376 51 411 31 418 11 16 66 112 611 459 622 148 247 122 587 249 141 20 88 439 334 233 474 305 381 203 559 228 595 326 535 128 449 138 28 86 56 109 102 428 194 612 256 466 598 189 539 237 59 225 528 200 330 490 560 133 57 590 632 422 213 340 310 49 195 41 217 19...

output:

60
34
45
304
31
71
52
36
123
38
76
28
200
223
55
58
11
275
202
76
285
30
154
113
40
48
355
332
358
33
240
46
29
91
147
75
318
62
162
200
171
124
228
31
285
59
16
206
114
44
163
87
53
65
221
298
23
172
123
91
298
14
57
6
8
71
1
84
3
2
58
20
6
111
244
220
25
137
86
269
134
254
68
64
129
14
65
108
43
3...

result: