QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#158860 | #7108. Couleur | ucup-team191# | AC ✓ | 2422ms | 43408kb | C++17 | 3.6kb | 2023-09-02 17:07:08 | 2023-09-02 17:07:09 |
Judging History
answer
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#define X first
#define Y second
#define PB push_back
using namespace std;
typedef long long ll;
typedef pair < int, int > pii;
typedef vector < int > vi;
const int N = 3e5 + 500;
const int M = 2e6 + 500;
const int LOG = 20;
const int OFF = (1 << 20);
const int INF = 0x3f3f3f3f;
const ll LINF = (ll)1e18;
const int MOD = 1e9 + 7; // 998244353
inline int sub(int A, int B) {
if(A - B < 0)
return A - B + MOD;
return A - B;
}
inline int mul(int A, int B) {
return (ll)A * B % MOD;
}
inline int pot(int A, int B) {
int ret = 1, bs = A;
for(;B;B >>= 1) {
if(B & 1) ret = mul(ret, bs);
bs = mul(bs, bs);
}
return bs;
}
int L[M], R[M], val[M], new_node = 1;
void update(int &x, int a, int b, int i) {
L[new_node] = L[x], R[new_node] = R[x], val[new_node] = val[x];
x = new_node++;
if(a == b) {
val[x]++; return;
}
if(i <= (a + b) / 2) update(L[x], a, (a + b) / 2, i);
else update(R[x], (a + b) / 2 + 1, b, i);
val[x] = val[L[x]] + val[R[x]];
}
int query(int &x, int a, int b, int lo, int hi) {
if(!x || a > hi || b < lo) return 0;
if(lo <= a && b <= hi) return val[x];
return query(L[x], a, (a + b) / 2, lo, hi) +
query(R[x], (a + b) / 2 + 1, b, lo, hi);
}
int n, a[N], p[N], off, loga[N];
int T[N];
void add(int x, int y) {
for(x++; x < n + 5; x += x & -x)
loga[x] += y;
}
int query(int x) {
int ret = 0;
for(x++; x ; x -= x & -x)
ret += loga[x];
return ret;
}
ll calc_inv(int l, int r){
if(l > r) return 0;
ll ans = 0;
for(int i = 0;i < r - l + 1;i++) {
ans += i - query(a[l + i]);
add(a[l + i], 1);
}
for(int i = 0;i < r - l + 1;i++) {
add(a[l + i], -1);
}
return ans;
}
inline int get_smaller(int x, int l, int r){
if(!x || l > r) return 0;
int ret = query(T[r], 0, off - 1, 0, x - 1);
if(l)
ret -= query(T[l - 1], 0, off - 1, 0, x - 1);
return ret;
}
inline int get_bigger(int x, int l, int r){
if(x == n - 1 || l > r) return 0;
int ret = query(T[r], 0, off - 1, x + 1, n - 1);
if(l)
ret -= query(T[l - 1], 0, off - 1, x + 1, n - 1);
return ret;
}
ll odg[N];
multiset < pair < ll, int > > fin;
void solve(){
fin.clear(); new_node = 1;
scanf("%d", &n);
for(off = 1;off < n;off <<= 1);
for(int i = 0;i < n;i++) {
scanf("%d", a + i); a[i]--;
T[i] = i ? T[i - 1] : 0;
update(T[i], 0, off - 1, a[i]);
}
odg[0] = calc_inv(0, n - 1);
set < int > rez;
rez.insert(-1); rez.insert(n);
fin.insert({odg[0], 0});
for(int i = 0;i < n;i++) {
ll ans = fin.rbegin() -> X;
printf("%lld ", fin.rbegin() -> X);
ll tx; scanf("%lld", &tx);
int x = (int)(tx ^ ans) - 1;
int l = *(--rez.lower_bound(x));
int r = *rez.lower_bound(x);
fin.erase({odg[l + 1], l + 1});
ll sve = odg[l + 1];
if(x - l < r - x) {
odg[l + 1] = calc_inv(l + 1, x - 1);
odg[x + 1] = sve - odg[l + 1] - get_bigger(a[x], l + 1, x - 1);
for(int j = l + 1;j <= x;j++)
odg[x + 1] -= get_smaller(a[j], x + 1, r - 1);
} else {
odg[x + 1] = calc_inv(x + 1, r - 1);
odg[l + 1] = sve - odg[x + 1] - get_smaller(a[x], x + 1, r - 1);
for(int j = x;j < r;j++)
odg[l + 1] -= get_bigger(a[j], l + 1, x - 1);
}
fin.insert({odg[l + 1], l + 1});
fin.insert({odg[x + 1], x + 1});
rez.insert(x);
}
printf("\n");
}
int main(){
//ios_base::sync_with_stdio(false);
//cin.tie(0);
//int T = 1;
int T; scanf("%d", &T);
for(;T--;) solve();
return 0;
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 9980kb
input:
3 5 4 3 1 1 1 5 4 5 3 1 10 9 7 1 4 7 8 5 7 4 8 21 8 15 5 9 2 4 5 10 6 15 4 8 8 1 12 1 10 14 7 14 2 9 13 10 3 37 19 23 15 7 2 10 15 2 13 4 5 8 7 10
output:
7 0 0 0 0 20 11 7 2 0 0 0 0 0 0 42 31 21 14 14 4 1 1 1 0 0 0 0 0 0
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 2422ms
memory: 43408kb
input:
11116 10 10 5 10 3 6 4 8 5 9 8 31 27 24 11 12 3 0 2 3 1 10 8 2 7 2 8 10 1 10 9 10 6 5 2 13 2 1 0 1 3 1 10 7 10 7 6 1 3 10 6 7 9 21 18 10 1 6 5 4 8 9 10 10 2 10 4 8 8 5 7 2 6 7 20 10 9 1 15 0 4 2 9 7 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 10 1 2 3 4 5 6 7 8 9 10 6 3 5 2 7 10 9 1 4 8 10 1 10 1 3...
output:
21 18 16 12 10 6 4 1 1 0 12 12 10 10 4 4 4 2 1 0 20 16 9 5 3 3 3 0 0 0 22 14 8 8 5 5 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 12 7 4 4 2 2 1 0 0 20 18 8 3 1 1 0 0 0 0 45 21 21 10 3 3 3 0 0 0 17 11 8 2 1 1 1 0 0 0 13 4 1 0 0 0 0 0 0 0 29 27 22 15 9 7 4 3 1 0 26 16 9 2 1 1 1 1 1 ...
result:
ok 11116 lines
Extra Test:
score: 0
Extra Test Passed