QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#374794 | #8316. Random Permutation | PetroTarnavskyi# | WA | 3ms | 13456kb | C++20 | 3.1kb | 2024-04-02 18:19:17 | 2024-04-02 18:19:19 |
Judging History
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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 13456kb
input:
2
output:
result:
wrong output format Unexpected end of file - double expected