QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#763059#9731. Fuzzy RankingSSAABBEERRML 20ms22188kbC++203.9kb2024-11-19 18:01:452024-11-19 18:01:56

Judging History

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

  • [2024-11-25 12:28:53]
  • hack成功,自动添加数据
  • (/hack/1257)
  • [2024-11-19 18:01:56]
  • 评测
  • 测评结果:ML
  • 用时:20ms
  • 内存:22188kb
  • [2024-11-19 18:01:45]
  • 提交

answer

#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define rep(i, a, b) for(int i = a; i <= b; i ++ )
#define pre(i, a, b) for(int i = a; i >= b; i -- )
using namespace std;
const int N = 2e5 + 10;
int n, m, k, q;
int stk[N], instk[N], top, cnt, id[N], dfn[N], low[N], times, sz[N], idx, dd[N];
vector<int>p[N], pp[N], ppp[N], res[N];
void tarjan(int u)
{
    dfn[u] = low[u] = ++times;
    stk[++top] = u, instk[u] = 1;
    for(auto j : p[u])
    {
        if(!dfn[j]) tarjan(j), low[u] = min(low[u], low[j]);
        else if(instk[j]) low[u] = min(low[u], dfn[j]);
    }
    if(dfn[u] == low[u])
    {
        cnt ++ ;
        int y;
        do
        {
            y = stk[top -- ];
            id[y] = cnt;
            sz[cnt] ++ ;
            instk[y] = 0;
        }while(y != u);
    }
}
int f[N], num[N], in[N], sum[N], w[N];
void solve()
{
    idx = 0;
    cin >> n >> k >> q;
    rep(i, 1, n) p[i].clear(), id[i] = 0, sz[i] = 0, in[i] = 0, f[i] = 0, ppp[i].clear(), res[i].clear(), sum[i] = 0, w[i] = 0, num[i] = 0, pp[i].clear(), stk[i] = 0, instk[i] = 0, dfn[i] = 0, low[i] = 0, dd[i] = 0;
    times = 0, cnt = 0, top = 0;
    rep(i, 1, k)
    {
        int last = 0;
        rep(j, 1, n)
        {
            int x;
            cin >> x;
            pp[i].push_back(x);
            if(last) p[last].push_back(x);
            last = x;
        }
    }
    rep(i, 1, n) if(!dfn[i]) tarjan(i);
    rep(i, 1, n) ppp[id[i]].push_back(i);
    rep(i, 1, n)
    {
        for(auto j : p[i])
        {
            if(id[i] != id[j])
            {
                f[id[j]] = id[i], in[id[i]] ++ ;
            }
        }
    }
    rep(i, 1, cnt)
    {
        if(!in[i])
        {
            while(i)
            {
                num[++idx] = i;
                i = f[i];
            }
            break;
        }
    }
    rep(i, 1, idx / 2) swap(num[i], num[idx - i + 1]);
    rep(i, 1, idx)
    {
        dd[num[i]] = i;
        // cout<<num[i]<<" ";
        sum[i] = sum[i - 1] + (sz[num[i]] * (sz[num[i]] - 1)) / 2;
        for(auto it : ppp[num[i]])
        {
            w[it] = i;
        }
    }
    rep(i, 1, k)
    {
        for(auto it : pp[i])
        {
            res[i].push_back(w[it]);
        }
    }
    int last = 0, op, l, r;
    // for(auto it : res[4])cout<<it<<" ";
    // cout<<cnt;
    // cout<<id[3]<<" "<<id[6];
    while(q -- )
    {
        cin >> op >> l >> r;
        op = (op + last) % k + 1;
        l = (l + last) % n + 1;
        r = (r + last) % n + 1;
        // cout<<op<<" "<<l<<" "<<r<<endl;
        if(id[pp[op][l - 1]] == id[pp[op][r - 1]])
        {
            int z = r - l + 1;
            cout << (z * (z - 1)) / 2 << endl;
            last = (z * (z - 1)) / 2;
        }
        else
        {
            int ans = 0;
            int tl = upper_bound(res[op].begin(), res[op].end(), w[pp[op][l - 1]]) - res[op].begin() + 1;
            tl -- ;
            ans += (tl - l + 1) * (tl - l) / 2;
            int tr = lower_bound(res[op].begin(), res[op].end(), w[pp[op][r - 1]]) - res[op].begin() + 1;
            ans += (r - tr + 1) * (r - tr) / 2;
            // cout<<w[pp[op][l - 1]];
            if(w[pp[op][r - 1]] - w[pp[op][l - 1]] > 1)
            {
                // cout<<w[pp[op][r - 1]] - 1;
                ans += (sum[w[pp[op][r - 1]] - 1] - sum[w[pp[op][l - 1]]]);
            }
            cout << ans << endl;
            last = ans;
        }
    }
    rep(i, 1, n) p[i].clear(), id[i] = 0, sz[i] = 0, in[i] = 0, f[i] = 0, ppp[i].clear(), res[i].clear(), sum[i] = 0, w[i] = 0, num[i] = 0, pp[i].clear(), stk[i] = 0, instk[i] = 0, dfn[i] = 0, low[i] = 0, dd[i] = 0;
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int _;
    _ = 1;
    cin >> _;
    while(_ -- )
    {
        solve();
    }
    return 0;
}

详细

Test #1:

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

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: 14ms
memory: 20008kb

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: 20ms
memory: 22188kb

input:

8000
5 5 10
3 5 2 4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