QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#749476#9731. Fuzzy RankingxhytomWA 19ms3676kbC++145.2kb2024-11-15 01:19:312024-11-15 01:19:33

Judging History

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

  • [2024-11-25 12:28:53]
  • hack成功,自动添加数据
  • (/hack/1257)
  • [2024-11-15 01:19:33]
  • 评测
  • 测评结果:WA
  • 用时:19ms
  • 内存:3676kb
  • [2024-11-15 01:19:31]
  • 提交

answer

/*
 
_/      _/    _/      _/    _/      _/   _/_/_/_/_/     _/_/       _/      _/ 
 _/    _/     _/      _/     _/    _/        _/       _/    _/     _/      _/            
  _/  _/      _/      _/      _/  _/         _/      _/      _/    _/_/  _/_/         
   _/_/       _/_/_/_/_/        _/           _/      _/      _/    _/  _/  _/          
  _/  _/      _/      _/        _/           _/      _/      _/    _/      _/          
 _/    _/     _/      _/        _/           _/       _/    _/     _/      _/          
_/      _/    _/      _/        _/           _/         _/_/       _/      _/       
 
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
using i64 = long long;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define multi int _;cin>>_;while(_--)
#define int long long
#define pb push_back
#define eb emplace_back
ll gcd(ll a,ll b){ return b?gcd(b,a%b):a;}
mt19937_64 mrand(chrono::steady_clock().now().time_since_epoch().count());
int rnd(int l,int r){ return mrand() % (r - l + 1) + l;}
#ifdef localfreopen
#define debug(x) cerr << #x << " = " << (x) << endl;
void test() {cerr << "\n";}
template<typename T, typename... Args> 
void test(T x, Args... args) {cerr << x << " ";test(args...);}
#else
#define debug //
#define test //
#endif
const ll MOD = 998244353;
// const ll MOD = 1e9+7;
ll ksm(ll x,ll y){ll ans=1;x%=MOD;while(y){if(y&1)ans=ans*x%MOD;x=x*x%MOD,y/=2;}return ans;}

const int P1 = 972152273, base1 = 809;
const int P2 = 905563261, base2 = 919;
const ll N = 200005;
//head

struct SCC {
    int n;
    std::vector<std::vector<int>> adj;
    std::vector<int> stk;
    std::vector<int> dfn, low, bel;
    int cur, cnt;
    
    SCC() {}
    SCC(int n) {
        init(n);
    }
    
    void init(int n) {
        this->n = n;
        adj.assign(n + 1, {});
        dfn.assign(n + 1, -1);
        low.resize(n + 1);
        bel.assign(n + 1, -1);
        stk.clear();
        cur = cnt = 0;
    }
    
    void addEdge(int u, int v) {
        adj[u].push_back(v);
    }
    
    void dfs(int x) {
        dfn[x] = low[x] = cur++;
        stk.push_back(x);
        
        for (auto y : adj[x]) {
            if (dfn[y] == -1) {
                dfs(y);
                low[x] = std::min(low[x], low[y]);
            } else if (bel[y] == -1) {
                low[x] = std::min(low[x], dfn[y]);
            }
        }
        
        if (dfn[x] == low[x]) {
            int y;
            ++cnt;
            do {
                y = stk.back();
                bel[y] = cnt;
                stk.pop_back();
            } while (y != x);
        }
    }
    
    std::vector<int> work() {
        for (int i = 1; i <= n; i++) {
            if (dfn[i] == -1) {
                dfs(i);
            }
        }
        return bel;
    }
};

struct DSU {
    std::vector<int> f, siz, l, r;
    DSU(int n) : f(n), siz(n, 1), l(n), r(n) {
    	std::iota(f.begin(), f.end(), 0);
    	std::iota(l.begin(), l.end(), 0);
    	std::iota(r.begin(), r.end(), 0);
    }
    int leader(int x) {
        while (x != f[x]) x = f[x] = f[f[x]];
        return x;
    }
    bool same(int x, int y) { return leader(x) == leader(y); }
    bool merge(int x, int y) {
        x = leader(x);
        y = leader(y);
        if (x == y) return false;
        siz[x] += siz[y];
        f[y] = x;
        l[x] = std::min(l[x], l[y]);
        r[x] = std::max(r[x], r[y]);
        return true;
    }
    int size(int x) { return siz[leader(x)]; }
};

void solve() {
	int n, k, q;
	std::cin >> n >> k >> q;
	
	SCC scc(n + 1);
	std::vector<std::vector<int>> a(k + 1, std::vector<int> (n + 1));
	for (int i = 1; i <= k; i++) {
		for (int j = 1; j <= n; j++) {
			int x;
			std::cin >> x;
			a[i][j] = x;
			if (j != 1) {
				scc.addEdge(a[i][j - 1], a[i][j]);
			}
		}
	}
	
	auto bel = scc.work();
	for (int i = 1; i <= n; i++) {
		std::cerr << bel[i] << " \n"[i == n];
	}
	
	DSU dsu(n + 1);
	for (int i = 2; i <= n; i++) {
		if (bel[i - 1] == bel[i]) {
			dsu.merge(i - 1, i);
		}
	}
	
	std::vector<int> pre(n + 1);
	for (int i = 1; i <= n; i++) {
		int u = dsu.leader(i);
		if (dsu.l[u] == i) {
			int sz = dsu.size(i);
			pre[i] = sz * (sz - 1) / 2;
		}
	}
	int ans = 0;
	for (int i = 1; i <= q; i++) {
		int id, l, r;
		std::cin >> id >> l >> r;
		id = (id + ans) % k + 1;
		l = (l + ans) % n + 1;
		r = (r + ans) % n + 1;
		if (dsu.leader(l) == dsu.leader(r)) {
			ans = (r - l + 1) * (r - l) / 2;
		} else {
			int u = dsu.leader(l), v = dsu.leader(r);
			int l1 = dsu.l[u], r1 = dsu.r[u];
			int l2 = dsu.l[v], r2 = dsu.r[v];
			ans = pre[l2 - 1] - pre[r1];
			int sz1 = r1 - l + 1;
			int sz2 = r - l2 + 1;
			ans += sz1 * (sz1 - 1) / 2;
			ans += sz2 * (sz2 - 1) / 2;
		}
		std::cout << ans << "\n";
	
	}
	
}

signed main()
{  
#ifdef localfreopen
    // freopen("1.in","r",stdin);
#endif
    fastio
    std::cout << std::fixed << std::setprecision(10);
    
    int t;
    std::cin >> t;
    while (t--) {
    	solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: -100
Wrong Answer
time: 19ms
memory: 3676kb

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:

0
0
0
0
0
0
0
2
1
0
0
0
0
0
0
0
0
0
0
0
1
6
28
0
0
10
10
6
6
15
0
3
1
1
1
3
0
0
3
1
1
3
0
0
0
1
2
0
1
1
1
0
0
1
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
3
6
10
15
0
6
10
0
15
15
0
0
1
0
1
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
1
0...

result:

wrong answer 1st lines differ - expected: '1', found: '0'