QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#560681#6409. Classical Data Structure ProblemMysterious_CatWA 0ms11876kbC++232.8kb2024-09-12 17:20:182024-09-12 17:20:18

Judging History

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

  • [2024-09-12 17:20:18]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:11876kb
  • [2024-09-12 17:20:18]
  • 提交

answer

#pragma GCC optimize(2)

#include <bits/stdc++.h>
using namespace std;

const int M = 5e5 + 5;

int n, m, ans;

int rt, tot, fa[M], son[M][2], cnt[M], sz[M], val[M];
unsigned int a[M], b[M], A[M], B[M];

void clear(int x) {
    fa[x] = son[x][0] = son[x][1] = cnt[x] = sz[x] = val[x] = 0;
}

void pushup(int x) {
    sz[x] = sz[son[x][0]] + sz[son[x][1]] + cnt[x];
    a[x] = a[son[x][0]] + a[son[x][1]] + A[x];
    b[x] = b[son[x][0]] + b[son[x][1]] + B[x];
}

bool get(int x) {
    return x == son[fa[x]][1];
}

void rotate(int x) {
    int y = fa[x];
    int z = fa[y];
    bool c = get(x);
    son[y][c] = son[x][c ^ 1];
    if (son[x][c ^ 1]) {
        fa[son[x][c ^ 1]] = y;
    }
    son[x][c ^ 1] = y;
    fa[y] = x;
    fa[x] = z;
    if (z) {
        son[z][y == son[z][1]] = x;
    }
    pushup(y);
}

void splay(int x, int t) {
    for (int f = fa[x]; f != t; rotate(x), f = fa[x]) {
        if (fa[f] != t) {
            rotate(get(f) == get(x) ? f : x);
        }
    }
    if (t == 0) {
    	rt = x;
    }
    pushup(x);
}

void insert(int k, unsigned int v) {
    if (!rt) {
        val[++tot] = k;
        A[tot] = v;
        B[tot] += (unsigned)k * v;
        cnt[tot]++;
        rt = tot;
        pushup(rt);
        return;
    }
    int cur = rt, f = 0;
    while (1) {
        if (val[cur] == k) {
            cnt[cur]++;
            A[cur] += v;
            B[cur] += (unsigned)k * v;
            pushup(cur);
            pushup(f);
            splay(cur, 0);
            break;
        }
        f = cur;
        cur = son[cur][k > val[cur]];
        if (!cur) {
            val[++tot] = k;
            cnt[tot]++;
            A[tot] = v;
            B[tot] = (unsigned)k * v;
            fa[tot] = f;
            son[f][k > val[f]] = tot;
            pushup(tot);
            pushup(f);
            splay(tot, 0);
            break;
        }
    }
}

int find(int k) {
    int cur = rt;
    while (cur) {
        if (k == val[cur]) {
        	return cur;
        } else if (k < val[cur]) {
        	cur = son[cur][0];
        } else {
        	cur = son[cur][1];
        }
    }
    return cur;
}

signed main() {
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		int l, r;
		cin >> l >> r;
		l = (l + ans) & (1 << m) - 1;
		r = (r + ans) & (1 << m) - 1;
		if (l > r) {
			swap(l, r);
		}
		insert(l, i);
		if (r + 1 < 1 << m) {
			insert(r + 1, 0u - (unsigned)i);
		}
        int v = find(r + 1);
        splay(v, 0);
        int u = find(l);
        splay(u, v);
        ans += (a[u] - a[son[u][0]]) * (unsigned)(r + 1) - (b[u] - b[son[u][0]]) + a[son[u][0]] * (unsigned)(r - l + 1);
	}
	cout << (ans & (1 << 30) - 1) << '\n';

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 11876kb

input:

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

output:

51

result:

wrong answer 1st numbers differ - expected: '87', found: '51'