QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#207913 | #7108. Couleur | ammardab3an# | AC ✓ | 1122ms | 67476kb | C++20 | 4.9kb | 2023-10-08 22:57:51 | 2023-10-08 22:57:51 |
Judging History
answer
// By AmmarDab3an
#include "bits/stdc++.h"
using namespace std;
#define int int64_t
#define ll int64_t
// typedef unsigned int uint;
// typedef long long int ll;
// typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> iii;
typedef pair<ll, pll> lll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
#define endl '\n'
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define freopenI freopen("input.txt", "r", stdin);
#define freopenO freopen("output.txt", "w", stdout);
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
const double PI = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int x, int y) {
return uniform_int_distribution<int>(x, y)(rng);
}
int mul(int a, int b){
int ret = (1ll * (a%MOD) * (b%MOD)) % MOD;
return (ret+MOD)%MOD;
}
int add(int a, int b){
int ret = (1ll * (a%MOD) + (b%MOD)) % MOD;
return (ret+MOD)%MOD;
}
int pow_exp(int n, int p){
if(!p) return 1;
if(p&1) return mul(n, pow_exp(n, p-1));
int tmp = pow_exp(n, p/2);
return mul(tmp, tmp);
}
int inv(int x){
return pow_exp(x, MOD-2);
}
const int MAX = 2e5 + 10;
const int NMAX = 2e5 + 10;
const int MMAX = 2e5 + 10;
const int LOG_MAX = ceil(log2(double(NMAX)));
const int BLOCK = ceil(sqrt(double(NMAX)));
struct FenwickTree {
int n;
vector<int> bit; // binary indexed tree
FenwickTree(){}
FenwickTree(int n) {
this->n = n;
bit.assign(n, 0);
}
FenwickTree(vector<int> a) : FenwickTree(a.size()) {
for (size_t i = 0; i < a.size(); i++)
add(i, a[i]);
}
int sum(int r) {
int ret = 0;
for (; r >= 0; r = (r & (r + 1)) - 1)
ret += bit[r];
return ret;
}
int sum(int l, int r) {
return sum(r) - sum(l - 1);
}
void add(int idx, int delta) {
for (; idx < n; idx = idx | (idx + 1))
bit[idx] += delta;
}
};
deque<int> compress(const vi &vec){
vi tmp = vec;
sort(tmp.begin(), tmp.end());
tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());
deque<int> ret(vec.begin(), vec.end());
for(auto &e : ret){
e = lower_bound(tmp.begin(), tmp.end(), e) - tmp.begin();
}
return ret;
}
int32_t main(){
fastIO;
#ifdef LOCAL
freopenI;
freopenO;
#endif
// freopen("name.in", "r", stdin);
// init();
int t; cin >> t; while(t--){
int n;
cin >> n;
vi vec(n);
for(auto &i : vec) cin >> i;
vi per(n);
for(auto &i : per) cin >> i;
struct s_data{
int frq_cnt = 0;
FenwickTree st;
deque<int> elements;
};
vector<s_data> data;
set<pair<pii, int>> st;
multiset<int> mx;
auto calc = [&](int l, int r){
if(l > r){
return;
}
s_data tmp;
int sz = r-l+1;
tmp.st = FenwickTree(sz);
tmp.elements = compress(vi(vec.begin()+l, vec.begin()+r+1));
for(int i = sz-1; i >= 0; i--){
tmp.frq_cnt += tmp.st.sum(0, tmp.elements[i]-1);
tmp.st.add(tmp.elements[i], 1);
}
mx.insert(tmp.frq_cnt);
st.insert({{l, r}, data.size()});
data.emplace_back(tmp);
};
calc(0, n-1);
for(auto p : per){
cout << (*mx.rbegin()) << ' ';
// cout << ' ' << (*mx.rbegin()) << endl;
// for(auto [lr, i] : st){
// auto [l, r] = lr;
// cout << l << ' ' << r << ' ' << data[i].frq_cnt << endl;
// }
// cout << flush;
int i = (p ^ (*mx.rbegin())) - 1;
// cout << i << endl << flush;
auto it = st.upper_bound({{i, INFLL}, INFLL});
assert(it != st.begin());
it--;
auto [l, r] = it->first;
int d_idx = it->second;
st.erase(it);
// pii st = {l, i-1};
// pii nd = {i+1, r};
int sz = r-l+1;
int sz_st = (i-1)-l+1;
int sz_nd = r-(i+1)+1;
if(sz_st <= sz_nd){
auto &d = data[d_idx];
mx.erase(mx.find(d.frq_cnt));
while(sz > sz_nd){
sz--;
d.frq_cnt -= d.st.sum(0, d.elements.front()-1);
d.st.add(d.elements.front(), -1);
d.elements.pop_front();
}
if(i+1 <= r){
mx.insert(d.frq_cnt);
st.insert({{i+1, r}, d_idx});
}
calc(l, i-1);
}
else{
auto &d = data[d_idx];
mx.erase(mx.find(d.frq_cnt));
while(sz > sz_st){
sz--;
d.frq_cnt -= d.st.sum(d.elements.back()+1, d.st.n-1);
d.st.add(d.elements.back(), -1);
d.elements.pop_back();
}
if(l <= i-1){
mx.insert(d.frq_cnt);
st.insert({{l, i-1}, d_idx});
}
calc(i+1, r);
}
}
cout << endl;
}
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3740kb
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: 1122ms
memory: 67476kb
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