QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#374794#8316. Random PermutationPetroTarnavskyi#WA 3ms13456kbC++203.1kb2024-04-02 18:19:172024-04-02 18:19:19

Judging History

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

  • [2024-04-02 18:19:19]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:13456kb
  • [2024-04-02 18:19:17]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second 

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

const int INF = 1e9 + 47;

struct Segtree
{
	int n;
	vector<PII> t;
	
	void init(int _n)
	{
		n = 1;
		while (n < _n) n*= 2;
		t.assign(2 * n - 1, MP(-1, -1));
	}
	
	void upd(int v, int tl, int tr, int pos, int val)
	{
		if (tl + 1 == tr)
		{
			t[v] = {val, pos};
			return;
		}
		int tm = (tl + tr) / 2;
		if (pos < tm)
			upd(v * 2 + 1, tl, tm, pos, val);
		else
			upd(v * 2 + 2, tm, tr, pos, val);
		t[v] = max(t[v * 2 + 1], t[v * 2 + 2]);
	}
	
	void upd(int pos, int val)
	{
		upd(0, 0, n, pos, val);
	}
	
	int query(int v, int tl, int tr, int l, int k)
	{
		if (tl + 1 == tr)
			return t[v].S;
		if (l >= tr)
			return -1;
		int tm = (tl + tr) / 2;
		if (l <= tl) // all in [l, n]
		{
			if (t[v].F < k)
				return -1;
		}
		int res = query(v * 2 + 1, tl, tm, l, k);
		if (res == -1)
			res = query(v * 2 + 2, tm, tr, l, k);
		return res;
	}
	
	int query(int l, int k)
	{
		return query(0, 0, n, l, k);
	}
	
} st;

struct Fenwick
{
	int n;
	vector<LL> t;
	
	void init(int _n)
	{
		n = _n;
		t.clear();
		t.assign(n, 0);
	}
	
	void upd(int i, int x)
	{
		for (; i < n; i |= i + 1)
			t[i] += x;
	}
	
	LL query(int i)
	{
		LL ans = 0;
		for (; i >= 0; i = (i & (i + 1)) - 1)
			ans += t[i];
		return ans;
	}
} fn;

const int N = 200'447;

set<int> s[N];
int c[N];
int v[N];
int f[N];

void add(int pos)
{
	int idx = c[pos];
	auto it = s[idx].lower_bound(pos);
	if (it != s[idx].end())
	{
		st.upd(*it, pos);
		f[*it] = pos;
	}
	int prev = -1;
	if (it != s[idx].begin())
	{
		it--;
		prev = *it;
	}
	st.upd(pos, prev);
	f[pos] = prev;
	s[idx].insert(pos);
	fn.upd(pos, v[pos]);
}

void erase(int pos)
{
	int idx = c[pos];
	s[idx].erase(pos);
	auto it = s[idx].lower_bound(pos);
	int pr = -1;
	if (it != s[idx].begin())
		pr = *prev(it);
	if (it != s[idx].end())
	{
		st.upd(*it, pr);
		f[*it] = pr;
	}
	fn.upd(pos, -v[pos]);
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int n, m;
	cin >> n >> m;
	st.init(n + 1);
	st.upd(n, INF);
	fn.init(n);
	FOR (i, 0, n)
	{
		cin >> c[i] >> v[i];
		add(i);
	}
	FOR (i, 0, m)
	{
		int t;
		cin >> t;
		if (t == 1)
		{
			int x, C, V;
			cin >> x >> C >> V;
			x--;
			erase(x);
			c[x] = C;
			v[x] = V;
			add(x);
		}
		else
		{
			int si, k;
			cin >> si >> k;
			si--;
			LL ans = 0;
			int idx = si;
			FOR (it, 0, k + 1)
			{
				if (idx == n)
					break;
				int j = st.query(idx, si);
				ans += fn.query(j - 1) - fn.query(idx - 1);
				if (i == k || j == n)
					break;
				if (f[j] != -1 && v[j] > v[f[j]])
					ans += v[j] - v[f[j]];
				idx = j + 1;
			}
			cout << ans << '\n';
		}
	}
	
	return 0;
}

详细

Test #1:

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

input:

2

output:


result:

wrong output format Unexpected end of file - double expected