QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#298313 | #7904. Rainbow Subarray | ucup-team1134# | AC ✓ | 1896ms | 139696kb | C++23 | 9.2kb | 2024-01-05 23:37:54 | 2024-01-05 23:37:55 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005;
const ll INF=1LL<<60;
//wavelet matrix topK sum 付き
//Wavelet matrix + range topK sum (small)
// https://kopricky.github.io/code/DataStructure_Advanced/wavelet_matrix.html
struct BitRank {
// block: bit 列を管理, count: block ごとに立っている 1 の数を管理
vector<unsigned long long> block;
vector<unsigned int> count;
BitRank(){}
void resize(const unsigned int num){
block.resize(((num + 1) >> 6) + 1, 0);
count.resize(block.size(), 0);
}
// i ビット目を val(0,1) にセット
void set(const unsigned int i, const unsigned long long val){
block[i >> 6] |= (val << (i & 63));
}
void build(){
for(unsigned int i = 1; i < block.size(); i++){
count[i] = count[i - 1] + __builtin_popcountll(block[i - 1]);
}
}
// [0, i) ビットの 1 の数
unsigned int rank1(const unsigned int i) const {
return count[i >> 6] +
__builtin_popcountll(block[i >> 6] & ((1ULL << (i & 63)) - 1ULL));
}
// [i, j) ビットの 1 の数
unsigned int rank1(const unsigned int i, const unsigned int j) const {
return rank1(j) - rank1(i);
}
// [0, i) ビットの 0 の数
unsigned int rank0(const unsigned int i) const {
return i - rank1(i);
}
// [i, j) ビットの 0 の数
unsigned int rank0(const unsigned int i, const unsigned int j) const {
return rank0(j) - rank0(i);
}
};
class WaveletMatrix
{
private:
unsigned int height;
vector<BitRank> B;
vector<int> pos;
vector<vector<ll>> rui;
public:
WaveletMatrix(){}
WaveletMatrix(vector<int> vec) :
WaveletMatrix(vec, *max_element(vec.begin(), vec.end()) + 1){}
// sigma:文字の種類数
WaveletMatrix(vector<int> vec, const unsigned int sigma){
init(vec, sigma);
}
void init(vector<int>& vec, const unsigned int sigma){
height = (sigma == 1) ? 1 : (64 - __builtin_clzll(sigma - 1));
B.resize(height), pos.resize(height);
vector<int> A = vec;
rui.resize(height+1);
for(unsigned int i = 0; i < height; ++i){
B[i].resize(vec.size());
for(unsigned int j = 0; j < vec.size(); ++j) {
B[i].set(j, get(vec[j], height - i - 1));
}
B[i].build();
auto it = stable_partition(vec.begin(), vec.end(), [&](int c){
return !get(c, height - i - 1);
});
pos[i] = it - vec.begin();
}
for(unsigned int i = 0; i <= height; ++i){
rui[i].resize(si(A) + 1);
for(int j = 1;j <= si(A); j++){
rui[i][j] = rui[i][j-1] + A[j-1];
}
if(i==height) break;
stable_partition(all(A),[&](int c){
return !get(c,height-i-1);
});
}
}
// val の i ビット目の値を返す(0,1)
int get(const int val, const int i){
return val >> i & 1;
}
// [l, r) の間に現れる値 val の数
int rank(const int val, const int l, const int r){
return rank(val, r) - rank(val, l);
}
// [0, i) の間に現れる値 val の数
int rank(int val, int i){
int p = 0;
for(unsigned int j = 0; j < height; ++j){
if(get(val, height - j - 1)){
p = pos[j] + B[j].rank1(p);
i = pos[j] + B[j].rank1(i);
}else{
p = B[j].rank0(p);
i = B[j].rank0(i);
}
}
return i - p;
}
// [l, r) の k(0,1,2...) 番目に小さい値を返す
int quantile(int k, int l, int r){
int res = 0;
for(unsigned int i = 0; i < height; ++i){
const int j = B[i].rank0(l, r);
if(j > k){
l = B[i].rank0(l);
r = B[i].rank0(r);
}else{
l = pos[i] + B[i].rank1(l);
r = pos[i] + B[i].rank1(r);
k -= j;
res |= (1 << (height - i - 1));
}
}
return res;
}
ll topKsum(int k,int l,int r){
if(l==r) return 0LL;
ll res = 0;
int atai = 0;
for(unsigned int i = 0; i < height; ++i){
const int j = B[i].rank0(l, r);
if(j > k){
l = B[i].rank0(l);
r = B[i].rank0(r);
}else{
int l2 = B[i].rank0(l);
int r2 = B[i].rank0(r);
res += rui[i+1][r2] - rui[i+1][l2];
l = pos[i] + B[i].rank1(l);
r = pos[i] + B[i].rank1(r);
k -= j;
atai |= (1 << (height - i - 1));
}
}
res += (ll) atai * k;
return res;
}
int rangefreq(const int i, const int j, const int a, const int b,
const int l, const int r, const int x){
if(i == j || r <= a || b <= l) return 0;
const int mid = (l + r) >> 1;
if(a <= l && r <= b){
return j - i;
}else{
const int left = rangefreq(B[x].rank0(i), B[x].rank0(j), a, b, l, mid, x + 1);
const int right = rangefreq(pos[x] + B[x].rank1(i), pos[x] + B[x].rank1(j),
a, b, mid, r, x + 1);
return left + right;
}
}
// [l,r) で値が [a,b) 内に含まれる数を返す
int rangefreq(const int l, const int r, const int a, const int b){
return rangefreq(l, r, a, b, 0, 1 << height, 0);
}
int rangemin(const int i, const int j, const int a, const int b,
const int l, const int r, const int x, const int val){
if(i == j || r <= a || b <= l) return -1;
if(r - l == 1) return val;
const int mid = (l + r) >> 1;
const int res = rangemin(B[x].rank0(i),B[x].rank0(j),a,b,l,mid,x+1,val);
if(res < 0) return rangemin(pos[x] + B[x].rank1(i), pos[x] + B[x].rank1(j),
a, b, mid, r, x + 1, val + (1 << (height - x - 1)));
else return res;
}
// [l,r) で値が [a,b) 内に最小の数を返す(数が存在しない場合は -1 を返す)
int rangemin(int l, int r, int a, int b){
return rangemin(l, r, a, b, 0, 1 << height, 0, 0);
}
};
template<typename T> class OrthogonalRangeCount
{
private:
using ptt = pair<T, T>;
const int sz;
vector<T> X, Y;
WaveletMatrix wm;
public:
OrthogonalRangeCount(vector<ptt> candidate)
: sz((int)candidate.size()), X(sz), Y(sz){
sort(candidate.begin(), candidate.end());
vector<int> vec(sz);
for(int i = 0; i < sz; ++i){
X[i] = candidate[i].first, Y[i] = candidate[i].second;
}
sort(Y.begin(), Y.end());
Y.erase(unique(Y.begin(), Y.end()), Y.end());
for(int i = 0; i < sz; ++i){
vec[i] = lower_bound(Y.begin(), Y.end(), candidate[i].second) - Y.begin();
}
wm.init(vec, Y.size());
}
// [lx,rx) × [ly, ry) の長方形領域に含まれる点の数を答える
int query(const T lx, const T ly, const T rx, const T ry){
const int lxid = lower_bound(X.begin(), X.end(), lx) - X.begin();
const int rxid = lower_bound(X.begin(), X.end(), rx) - X.begin();
const int lyid = lower_bound(Y.begin(), Y.end(), ly) - Y.begin();
const int ryid = lower_bound(Y.begin(), Y.end(), ry) - Y.begin();
if(lxid >= rxid || lyid >= ryid) return 0;
return wm.rangefreq(lxid, rxid, lyid, ryid);
}
};
int main(){
std::ifstream in("text.txt");
std::cin.rdbuf(in.rdbuf());
cin.tie(0);
ios::sync_with_stdio(false);
int Q;cin>>Q;
while(Q--){
ll N,K;cin>>N>>K;
vector<int> A(N);
vector<ll> rui(N+1);
for(int i=0;i<N;i++){
cin>>A[i];
A[i]+=N;
A[i]-=i;
rui[i+1]=rui[i]+A[i];
}
WaveletMatrix WM(A);
ll left=1,right=N+1;
while(right-left>1){
ll mid=(left+right)/2;
bool ok=false;
for(ll l=0;l+mid<=N;l++){
ll r=l+mid;
ll x=WM.quantile(mid/2,l,r);
ll need=0;
ll Z=WM.topKsum(mid/2,l,r);
need+=x*(mid/2)-Z;
need+=(rui[r]-rui[l]-Z-x)-x*(mid-mid/2-1);
if(need<=K){
ok=true;
break;
}
}
if(ok) left=mid;
else right=mid;
}
cout<<left<<"\n";
}
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3468kb
input:
5 7 5 7 2 5 5 4 11 7 6 0 100 3 4 5 99 100 5 6 1 1 1 1 1 5 50 100 200 300 400 500 1 100 3
output:
4 3 5 1 1
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 677ms
memory: 22416kb
input:
11102 2 167959139 336470888 134074578 5 642802746 273386884 79721198 396628655 3722503 471207868 6 202647942 268792718 46761498 443917727 16843338 125908043 191952768 2 717268783 150414369 193319712 6 519096230 356168102 262263554 174936674 407246545 274667941 279198849 9 527268921 421436316 3613460...
output:
1 4 3 2 6 5 7 2 4 1 4 1 1 3 2 2 7 8 7 7 1 7 6 2 4 3 1 6 7 7 3 4 3 9 3 8 6 6 3 1 6 3 1 2 4 6 4 6 4 1 4 7 1 6 3 5 6 6 1 7 5 3 1 6 4 5 3 2 2 6 2 3 10 1 4 3 2 4 5 1 7 5 5 5 8 5 3 6 3 5 5 8 5 4 5 2 1 5 2 3 3 4 8 1 3 1 2 2 8 3 1 6 8 1 8 4 5 6 6 8 4 8 3 2 8 4 5 6 2 6 2 4 1 5 4 5 3 2 4 1 2 1 4 5 8 3 7 3 3 3...
result:
ok 11102 lines
Test #3:
score: 0
Accepted
time: 1615ms
memory: 135732kb
input:
1 500000 17244641009859 54748096 75475634 204928248 276927808 84875072 103158867 27937890 322595515 186026685 45468307 69240390 139887597 188586447 373764525 121365644 310156469 185188306 60350786 211308832 384695957 370562147 208427221 35937909 267590963 126478310 275357775 55361535 335993561 36696...
output:
172998
result:
ok single line: '172998'
Test #4:
score: 0
Accepted
time: 1896ms
memory: 139696kb
input:
1 500000 43483524125987 923264237 374288891 535590429 751244358 124321145 232930851 266089174 543529670 773363571 319728747 580543238 582720391 468188689 490702144 598813561 138628383 284660056 733781508 155605777 931759705 245485733 723534730 257812292 794937524 596788519 188451996 981010588 144836...
output:
174260
result:
ok single line: '174260'
Test #5:
score: 0
Accepted
time: 97ms
memory: 136792kb
input:
1 499999 62499749999 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081 496813081...
output:
499998
result:
ok single line: '499998'
Extra Test:
score: 0
Extra Test Passed