QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#245627 | #7520. Monster Generator | kkio | WA | 1ms | 3452kb | C++17 | 10.2kb | 2023-11-10 07:13:33 | 2023-11-10 07:13:33 |
Judging History
answer
#include <bits/stdc++.h>
//#define Kachang 1
#ifdef Kachang
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2")
#pragma GCC optimize("Ofast","unroll-loops","inline","no-stack-protector")
#else
#pragma GCC optmize("2")
#endif
using namespace std;
namespace Def{
#define fir first
#define sec second
#define lson (tr[i].ls)
#define rson (tr[i].rs)
#define FIO(file) freopen(file".in","r",stdin), freopen(file".out","w",stdout)
#define Untie() ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef __int128_t i128;
typedef __uint128_t u128;
}
using namespace Def;
namespace FastIO {
struct IO {
char ibuf[(1 << 20) + 1], *iS, *iT, obuf[(1 << 20) + 1], *oS;
IO() : iS(ibuf), iT(ibuf), oS(obuf) {} ~IO() { fwrite(obuf, 1, oS - obuf, stdout); }
#if ONLINE_JUDGE
#define gh() (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20) + 1, stdin), (iS == iT ? EOF : *iS++) : *iS++)
#else
#define gh() getchar()
#endif
inline bool eof (const char &ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == 't' || ch == EOF; }
inline long long read() {
char ch = gh();
long long x = 0;
bool t = 0;
while (ch < '0' || ch > '9') t |= ch == '-', ch = gh();
while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gh();
return t ? ~(x - 1) : x;
}
inline void read (char *s) {
char ch = gh(); int l = 0;
while (eof(ch)) ch = gh();
while (!eof(ch)) s[l++] = ch, ch = gh();
s[l] = 0;
}
inline void read (double &x) {
char ch = gh(); bool t = 0;
while (ch < '0' || ch > '9') t |= ch == '-', ch = gh();
while (ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = gh();
if (ch != '.') return t && (x = -x), void(); ch = gh();
for (double cf = 0.1; '0' <= ch && ch <= '9'; ch = gh(), cf *= 0.1) x += cf * (ch ^ 48);
t && (x = -x);
}
inline void pc (char ch) {
#ifdef ONLINE_JUDGE
if (oS == obuf + (1 << 20) + 1) fwrite(obuf, 1, oS - obuf, stdout), oS = obuf;
*oS++ = ch;
#else
putchar(ch);
#endif
}
inline void write (char *s)
{
int len = strlen(s);
for(int i = 0; i < len; i++)pc(s[i]);
}
template<typename _Tp>
inline void write (_Tp x) {
static char stk[64], *tp = stk;
if (x < 0) x = ~(x - 1), pc('-');
do *tp++ = x % 10, x /= 10;
while (x);
while (tp != stk) pc((*--tp) | 48);
}
inline void puts(const char *s){
int len = strlen(s);
for (int i = 0; i < len; i++)pc(s[i]);
}
} io;
inline long long read () { return io.read(); }
template<typename Tp>
inline void read (Tp &x) { io.read(x); }
template<typename _Tp>
inline void write (_Tp x) { io.write(x); }
}
using namespace FastIO;
namespace misc{
constexpr int infi=1e9;
constexpr int minfi=0x3f3f3f3f;
constexpr ll infl=1e18;
constexpr ll minfl=0x3f3f3f3f3f3f3f3f;
constexpr int MOD=998244353;
constexpr int inv2=(MOD+1)/2;
constexpr int inv3=(MOD+1)/3;
constexpr double eps=1e-6;
mt19937_64 rnd(0x3408532);
template<typename T,typename E>
inline T ksm(T b,E p){T ret=1;while(p){if(p&1)ret=1ll*ret*b%MOD;b=1ll*b*b%MOD;p>>=1;}return ret;}
template<typename T,typename E,typename R>
inline T ksm(T b,E p,R mod){T ret=1;while(p){if(p&1)ret=1ll*ret*b%mod;b=1ll*b*b%mod;p>>=1;}return ret;}
template<typename T>
inline T ginv(T v){return ksm(v,MOD-2);}
template<typename T,typename E>
inline void cmax(T &a,E b){a<b?(a=b,1):0;}
template<typename T,typename E>
inline void cmin(T &a,E b){a>b?(a=b,1):0;}
template<typename T,typename E>
inline void cadd(T &a,E b){(a+=b)>=MOD?(a-=MOD):0;}
template<typename T,typename E>
inline void csub(T &a,E b){(a-=b)<0?(a+=MOD):0;}
template<typename T,typename E>
inline void cmul(T &a,E b){a=1ll*a*b%MOD;}
template<typename T,typename E>
inline T madd(T a,E b){return (a+=b)>=MOD?(a-MOD):a;}
template<typename T,typename E>
inline T msub(T a,E b){return (a-=b)<0?(a+MOD):a;}
template<typename T,typename E>
inline T mmul(T a,E b){return 1ll*a*b%MOD;}
template<typename T>
struct dseg{T *first,*last;dseg(T* _l,T* _r):first(_l),last(_r){}};
inline void debug(void){cerr<<'\n';}
template<typename T,typename... arg>
inline void debug(T x,arg... r){cerr<<x<<' ';debug(r...);}
template<typename T,typename... arg>
inline void debug(dseg<T> A,arg... v){cerr<<"[ ";for(T* i=A.first;i!=A.last;++i)cerr<<*i<<' ';cerr<<"] ";debug(v...);}
template<typename T>
inline T randseg(T l,T r){assert(l<=r);return rnd()%(r-l+1)+l;}
template<typename T>
inline bool gbit(T v,int bit){return v>>bit&1;}
template<typename T>
inline void FWTXor(T *a,int n){for(int i=2;i<=n;i<<=1)for(int p=i>>1,j=0;j<n;j+=i)for(int k=j;k<j+p;k++){T x=a[k],y=a[k+p];a[k]=madd(x+y),a[k+p]=msub(x-y);}}
template<typename T>
inline void iFWTXor(T *a,int n){for(int i=2;i<=n;i<<=1)for(int p=i>>1,j=0;j<n;j+=i)for(int k=j;k<j+p;k++){T x=a[k],y=a[k+p];a[k]=mmul(madd(x+y),inv2),a[k+p]=mmul(msub(x-y),inv2);}}
inline ll gcd(ll a,ll b){if(!b||!a) return a+b;ll az=__builtin_ctz(a),bz=__builtin_ctz(b),z=(az>bz)?bz:az,t;b>>=bz;while(a) a>>=az,t=a-b,az=__builtin_ctz(t),b=a<b?a:b,a=t<0?-t:t;return b<<z;}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1,y=0;return a;}ll g=exgcd(b,a%b,y,x);y-=x*(a/b);return g;}
inline ll Sum1(ll n){return n*(n+1)/2;}
inline ll Sum2(ll n){return n*(n+1)*(2*n+1)/6;}
inline ll Sqr(ll n){return n*n;}
#define binom(n,m) (n<0||m<0||n<m?0:1ll*fac[n]*ifac[m]%mod*ifac[n-m]%mod)
#define likely(x) (__builtin_expect(!!(x),1))
#define unlikely(x) (__builtin_expect(!!(x),0))
}
using namespace misc;
namespace Barret
{
class reduction
{
private:
__uint128_t brt;
int mod;
public:
reduction(){};
reduction(int __mod):brt(((__uint128_t)1<<64)/__mod),mod(__mod){}
inline void setmod(int __mod){brt=((__uint128_t)1<<64)/__mod,mod=__mod;}
template<typename T> inline void fix(T& val){val-=mod*(brt*val>>64);while(val>=mod)val-=mod;}
template<typename T> inline int fixv(T val){val-=mod*(brt*val>>64);return val>=mod?val-mod:val;}
};
}
using namespace Barret;
const int maxn=20005;
int n;ll m,a[maxn],b[maxn],dta[maxn],dtb[maxn];
struct Seg{
i128 k,b;
}S[maxn],q[maxn];
int tail;
ll sc[maxn];
int tot=0;
i128 na[maxn],nb[maxn],p[maxn],sda[maxn],sdb[maxn],sa[maxn],sb[maxn];
bool comp(Seg a,Seg b){
if(a.k!=b.k)return a.k<b.k;
else return a.b<b.b;
}
inline ll cdiv(ll a,ll b)
{
if(b<0)a*=-1,b*=-1;
if(a<0)return -(-a)/b;
else return (a+b-1)/b;
}
i128 Sec(Seg a,Seg b)
{
if(a.k==b.k)return 0;
i128 k1=a.k,k2=b.k,b1=a.b,b2=b.b;
return cdiv(b2-b1,k1-k2);
}
ll t[maxn],len;
int main()
{
n=read(),m=read();
for(int i=1;i<=n;i++)a[i]=read(),dta[i]=read(),b[i]=read(),dtb[i]=read();
for(int i=1;i<=n;i++)
if(dtb[i]!=dta[i])
{
ll tm=cdiv((b[i]-a[i]),(dta[i]-dtb[i]))+1;
if(tm>0&&tm<=m)t[++len]=tm;
tm=cdiv((b[i]-a[i]),(dta[i]-dtb[i]));
if(tm>0&&tm<=m)t[++len]=tm;
cout<<tm<<'\n';
}
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
{
if(dta[i]!=dta[j])
{
ll tm=cdiv((a[j]-a[i]),(dta[i]-dta[j]));
if(tm>0&&tm<=m)t[++len]=tm;
tm++;
if(tm>0&&tm<=m)t[++len]=tm;
}
if(dtb[i]!=dtb[j])
{
ll tm=cdiv((b[j]-b[i]),(dtb[i]-dtb[j]));
if(tm>0&&tm<=m)t[++len]=tm;
tm++;
if(tm>0&&tm<=m)t[++len]=tm;
}
}
t[++len]=0,t[++len]=m+1;
sort(t+1,t+1+len);len=unique(t+1,t+1+len)-t-1;
ull ans=0;
for(int z=1;z<len;z++)
{
ll Lt=t[z],Rt=t[z+1]-1;
//cout<<"Time:"<<Lt<<' '<<Rt<<'\n';
for(int i=1;i<=n;i++)na[i]=a[i]+dta[i]*Lt,nb[i]=b[i]+dtb[i]*Lt;
for(int i=1;i<=n;i++)p[i]=i;
sort(p+1,p+1+n,[&](int x,int y){
bool flx=na[x]<=nb[x],fly=na[y]<=nb[y];
if(flx^fly)return flx>fly;
else if(flx)return na[x]<na[y];
else return nb[x]>nb[y];
});
tot=0;
for(int i=1;i<=n;i++)
sda[i]=sda[i-1]+dta[p[i]],sdb[i]=sdb[i-1]+dtb[p[i]],sa[i]=sa[i-1]+a[p[i]],sb[i]=sb[i-1]+b[p[i]];
if(Lt==Rt)
{
i128 Ans=0;
for(int i=1;i<=n;i++)Ans=max((i128)sa[i]+(i128)sda[i]*Lt-sb[i-1]-(i128)sdb[i-1]*Lt,Ans);
ans+=Ans;
continue;
}
for(int i=1;i<=n;i++)
S[++tot]=(Seg){sda[i]-sdb[i-1],sa[i]-sb[i-1]};
sort(S+1,S+1+tot,comp);
tail=0;
for(int i=1;i<=tot;i++)
{
while(tail)
{
i128 secp=Sec(q[tail],S[i]);
secp=max(secp,(i128)0);
if(secp<=sc[tail])tail--;
else break;
}
if(tail)
{
i128 secp=Sec(q[tail],S[i]);
++tail;q[tail]=S[i],sc[tail]=secp;
}
else
++tail,q[tail]=S[i],sc[tail]=0;
}
ll tr=Rt+1;
for(int i=tail;i>=1;i--)
{
ll nowl=max(Lt,sc[i]);
ll nowr=min(tr-1,Rt);
if(nowl>nowr)continue;
//cout<<nowl<<' '<<nowr<<'\n';
if(nowl<=nowr)ans+=1llu*((i128)(nowr+nowl)*(nowr-nowl+1)/2)*q[i].k+1llu*(nowr-nowl+1)*q[i].b;
tr=nowl;
}
}
write(ans),io.pc('\n');
}
/*
5 100000000000000
3 4 1 2
7 2 5 3
7 8 1 2
8 10 2 10
1 9 2 5
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3452kb
input:
3 5 3 1 5 2 4 2 1 3 1 9 100 1
output:
-2 3 13 113
result:
wrong answer 1st lines differ - expected: '113', found: '-2'