QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#795039 | #9809. The Grand Contest | ucup-team987# | WA | 145ms | 3872kb | C++20 | 5.7kb | 2024-11-30 17:35:00 | 2024-11-30 17:35:01 |
Judging History
answer
#include<iostream>
#include<vector>
#include<map>
#include<cassert>
#include <algorithm>
#include <cassert>
#include <vector>
#ifdef _MSC_VER
#include <intrin.h>
#endif
namespace atcoder {
namespace internal {
int ceil_pow2(int n) {
int x = 0;
while ((1U << x) < (unsigned int)(n)) x++;
return x;
}
constexpr int bsf_constexpr(unsigned int n) {
int x = 0;
while (!(n & (1 << x))) x++;
return x;
}
int bsf(unsigned int n) {
#ifdef _MSC_VER
unsigned long index;
_BitScanForward(&index, n);
return index;
#else
return __builtin_ctz(n);
#endif
}
} // namespace internal
} // namespace atcoder
namespace atcoder {
template <class S, S (*op)(S, S), S (*e)()> struct segtree {
public:
segtree() : segtree(0) {}
explicit segtree(int n) : segtree(std::vector<S>(n, e())) {}
explicit segtree(const std::vector<S>& v) : _n(int(v.size())) {
log = internal::ceil_pow2(_n);
size = 1 << log;
d = std::vector<S>(2 * size, e());
for (int i = 0; i < _n; i++) d[size + i] = v[i];
for (int i = size - 1; i >= 1; i--) {
update(i);
}
}
void set(int p, S x) {
assert(0 <= p && p < _n);
p += size;
d[p] = x;
for (int i = 1; i <= log; i++) update(p >> i);
}
S get(int p) const {
assert(0 <= p && p < _n);
return d[p + size];
}
S prod(int l, int r) const {
assert(0 <= l && l <= r && r <= _n);
S sml = e(), smr = e();
l += size;
r += size;
while (l < r) {
if (l & 1) sml = op(sml, d[l++]);
if (r & 1) smr = op(d[--r], smr);
l >>= 1;
r >>= 1;
}
return op(sml, smr);
}
S all_prod() const { return d[1]; }
template <bool (*f)(S)> int max_right(int l) const {
return max_right(l, [](S x) { return f(x); });
}
template <class F> int max_right(int l, F f) const {
assert(0 <= l && l <= _n);
assert(f(e()));
if (l == _n) return _n;
l += size;
S sm = e();
do {
while (l % 2 == 0) l >>= 1;
if (!f(op(sm, d[l]))) {
while (l < size) {
l = (2 * l);
if (f(op(sm, d[l]))) {
sm = op(sm, d[l]);
l++;
}
}
return l - size;
}
sm = op(sm, d[l]);
l++;
} while ((l & -l) != l);
return _n;
}
template <bool (*f)(S)> int min_left(int r) const {
return min_left(r, [](S x) { return f(x); });
}
template <class F> int min_left(int r, F f) const {
assert(0 <= r && r <= _n);
assert(f(e()));
if (r == 0) return 0;
r += size;
S sm = e();
do {
r--;
while (r > 1 && (r % 2)) r >>= 1;
if (!f(op(d[r], sm))) {
while (r < size) {
r = (2 * r + 1);
if (f(op(d[r], sm))) {
sm = op(d[r], sm);
r--;
}
}
return r + 1 - size;
}
sm = op(d[r], sm);
} while ((r & -r) != r);
return 0;
}
private:
int _n, size, log;
std::vector<S> d;
void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};
} // namespace atcoder
using namespace std;
long long op(long long a,long long b){return min(a,b);}
long long e(){return(long long)9e18;}
int N;
long long P;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;cin>>T;
for(;T--;)
{
cin>>N>>P;
map<int,int>WA[2];
map<int,long long>AC[2];
long long pn[2]={0LL,0LL};
long long sum[2]={0LL,0LL};
for(int i=0;i<N;i++)
{
int a,b,d;
long long c;
cin>>a>>b>>c>>d;
a--;
if(d==0)WA[a][b]++;
else if(AC[a].find(b)==AC[a].end())
{
AC[a][b]=c;
sum[a]+=c;
pn[a]+=P*WA[a][b];
}
}
if(AC[0].size()!=AC[1].size())
{
cout<<"-1\n";
continue;
}
vector<long long>Ts;Ts.reserve(AC[0].size()+AC[1].size()+1);
Ts.push_back(0);
for(int i=0;i<2;i++)
{
for(auto[_,c]:AC[i])Ts.push_back(c);
}
sort(Ts.begin(),Ts.end());
Ts.erase(unique(Ts.begin(),Ts.end()),Ts.end());
vector<int>A[2];
for(int i=0;i<2;i++)
{
A[i].reserve(AC[i].size());
for(auto[_,c]:AC[i])
{
A[i].push_back(lower_bound(Ts.begin(),Ts.end(),c)-Ts.begin());
}
}
long long need;
if(sum[0]+pn[0]<=sum[1]+pn[1])
{
need=sum[1]+pn[1]-(sum[0]+pn[0])+1;
}
else
{
need=sum[0]+pn[0]-(sum[1]+pn[1]);
swap(A[0],A[1]);
}
vector<int>diff(Ts.size());
for(int i=0;i<2;i++)
{
const int d=i==0?1:-1;
for(int j:A[i])diff[j-1]+=d;
}
for(int i=Ts.size();--i;)diff[i-1]+=diff[i];
atcoder::segtree<long long,op,e>seg(Ts.size()-1);
long long base=0;
long long aL=-1,aR;
for(int i=Ts.size()-1;i--;)
{
seg.set(i,-base);
base+=diff[i]*(Ts[i+1]-Ts[i]);
int r=seg.max_right(i,[&](long long x){return x>-base-need;});
if(r==Ts.size()-1)continue;
long long L=Ts[i];
long long v=i==r?0LL:seg.get(r-1)+base;
assert(v>-need);
long long d=diff[r];
long long len=Ts[r+1]-Ts[r];
assert(d<0);
assert(v+d*len<=-need);
//find minimum x s.t.
//0<x<=len
//v+d*x<=-need <=> v+need<=(-d)*x
long long x=(v+need+(-d)-1)/(-d);
assert(0<x&&x<=len);
long long R=Ts[r]+x;
if(aL==-1)aL=L,aR=R;
else if(aR-aL>=R-L)aL=L,aR=R;
}
if(aL==-1)cout<<"-1\n";
else cout<<aL<<" "<<aR<<"\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3564kb
input:
2 6 20 1 1 60 0 2 2 60 0 2 2 120 1 1 2 180 1 1 2 180 0 2 2 300 1 2 20 1 1 300 1 2 2 300 1
output:
120 160 -1
result:
ok 3 number(s): "120 160 -1"
Test #2:
score: 0
Accepted
time: 145ms
memory: 3872kb
input:
400000 1 1 1 1000000000 1000000000000 1 1 1 2 1000000000 1 0 1 1 2 1 1000000000000 1 1 1 1 1 1000000000000 1 1 1 2 1000000000 1000000000000 0 1 1 2 1 1 0 1 1000000000000 2 1000000000 1 0 1 1000000000000 1 1 1 0 1 1000000000000 1 1 1 1 1 1000000000000 2 1000000000 1000000000000 0 1 1 1 1000000000 1 0...
output:
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
result:
ok 400000 numbers
Test #3:
score: -100
Wrong Answer
time: 126ms
memory: 3864kb
input:
10000 4 542575683220 2 101300788 7308006925 1 1 604560531 257884671293 0 1 46911674 422781533607 0 2 10550533 771273976896 1 116 793781361888 1 819065134 15224463201 1 2 552573547 15992997563 1 2 424217 27032314690 0 2 70252887 41541882886 0 2 274093456 46129251985 0 1 458919850 46344406806 1 1 8416...
output:
-1 -1 -1 -1 -1 66660446970 904724933034 -1 -1 -1 -1 -1 -1 37226106549 311799565893 -1 -1 -1 -1 -1 -1 48301734080 375528816957 -1 -1 -1 459021288402 632610827258 -1 -1 -1 -1 -1 -1 -1 688320095661 898231263806 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 22824143484...
result:
wrong answer 6th numbers differ - expected: '66660446969', found: '66660446970'