QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#245457#7520. Monster GeneratorkkioWA 1ms7644kbC++1711.4kb2023-11-09 22:19:372023-11-09 22:19:38

Judging History

你现在查看的是最新测评结果

  • [2023-11-09 22:19:38]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:7644kb
  • [2023-11-09 22:19:37]
  • 提交

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 Point{
	db x,y;
	Point operator + (const Point rhs) const{
		return (Point){x+rhs.x,y+rhs.y};
	}
	Point operator - (const Point rhs) const{
        return (Point){x-rhs.x,y-rhs.y};
    }
};
ll cross(Point A,Point B){return A.x*B.y-A.y*B.x;}
struct Seg{
    ll k,b;
    int id;
}S[maxn],q[maxn];
int tail;
Point sc[maxn];
int tot=0;
ll 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;
}
Point Sec(Seg a,Seg b)
{
    db k1=a.k,k2=b.k,b1=a.b,b2=b.b;
    return (Point){(b2-b1)/(k1-k2),k1/(k1-k2)*(b2-b1)+b1};
}
ll t[maxn],len;
int main()
{
    i128 MX=(i128)1<<64;
    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=ceil(1.0*(b[i]-a[i])/(dta[i]-dtb[i]))+1;
            if(tm>0&&tm<=m)t[++len]=tm;
        }
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
        {
            if(dta[i]!=dta[j])
            {
                ll tm=ceil(1.0*(a[j]-a[i])/(dta[i]-dta[j]));
                if(tm>0&&tm<=m)t[++len]=tm;
            }
            if(dtb[i]!=dtb[j]) 
            {
                ll tm=ceil(1.0*(b[j]-b[i])/(dtb[i]-dtb[j]));
                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;
    i128 ans=0;
    for(int z=1;z<len;z++)
    {
        ll Lt=t[z],Rt=t[z+1]-1;
        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];
        });
        //printf("Time:%lld %lld\n",Lt,Rt);
        //for(int i=1;i<=n;i++)printf("%lld %lld %lld\n",p[i],na[p[i]],nb[p[i]]);
        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)
        {
            ll Ans=0;
            for(int i=1;i<=n;i++)Ans=max(sa[i]+sda[i]*Lt-sb[i-1]-sdb[i-1]*Lt,Ans);
            ans+=Ans;
            ans=min(ans,MX);
            continue;
        }
        for(int i=1;i<=n;i++)
            S[++tot]=(Seg){sda[i]-sdb[i-1],sa[i]-sb[i-1],i};
        sort(S+1,S+1+tot,comp);
       // for(int i=1;i<=tot;i++)
       //     cout<<S[i].k<<' '<<S[i].b<<' '<<S[i].id<<'\n';
        tail=0;
        for(int i=1;i<=tot;i++)
        {
            while(tail)
            {
                Point secp=Sec(q[tail],S[i]);
                
                secp.x=max(secp.x,0.0);
                if(secp.x<sc[tail].x)tail--;
                else break;
            }
            if(tail)
            {
                Point secp=Sec(q[tail],S[i]);   
                ++tail;q[tail]=S[i],sc[tail]=secp;
            }
            else 
                ++tail,q[tail]=S[i],sc[tail]={0,0};
        }
        ll tr=Rt+1;
        //for(int i=1;i<=tail;i++)cout<<sc[i].x<<' '<<sc[i].y<<'\n';
        for(int i=tail;i>=1;i--)
        {
            ll nowl=max(Lt,(ll)ceil(sc[i].x));
            ll nowr=min(tr-1,Rt);
            if(nowl>nowr)continue;
            //printf("%lld %lld %d %lld %lld\n",nowl,nowr,q[i].id,q[i].k,q[i].b);
            if(nowl<=nowr)ans+=(i128)(nowr+nowl)*(nowr-nowl+1)/2*q[i].k+(nowr-nowl+1)*q[i].b;
            ans=min(ans,MX);
            tr=nowl;
        }
    }
    // ll tans=0;
    // for(int t=0;t<=m;t++)
    // {
    //     for(int i=1;i<=n;i++)na[i]=a[i]+dta[i]*t,nb[i]=b[i]+dtb[i]*t;
    //     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];
    //     });
    //     for(int i=1;i<=n;i++)printf("%lld %lld %lld\n",p[i],na[p[i]],nb[p[i]]);
    //     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]];
    //     ll Ans=0,pos=0;
    //     for(int i=1;i<=n;i++)
    //     {
    //         if(sa[i]+sda[i]*t-sb[i-1]-sdb[i-1]*t>Ans)pos=i;
    //         Ans=max(sa[i]+sda[i]*t-sb[i-1]-sdb[i-1]*t,Ans);
    //     }
    //     cout<<"!"<<pos<<'\n';
    //     // cout<<Ans<<'\n';
    //     tans+=Ans;
    //     cout<<Ans<<'\n';
    // }
    // cout<<tans<<'\n';
    write(ans),io.pc('\n');
}
/*
5 10
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: 100
Accepted
time: 1ms
memory: 7644kb

input:

3 5
3 1 5 2
4 2 1 3
1 9 100 1

output:

113

result:

ok single line: '113'

Test #2:

score: 0
Accepted
time: 1ms
memory: 7464kb

input:

3 100000000
3 1 5 2
4 2 1 3
1 9 100 1

output:

35000000549999998

result:

ok single line: '35000000549999998'

Test #3:

score: -100
Wrong Answer
time: 1ms
memory: 7644kb

input:

10 1000000000000000000
776874380544333 197 471391764744275 33
159838820333814 107 677112662750393 41
962335658276824 48 255593531071176 11
127404116579775 209 268525254990127 34
647620110614714 76 897947476313307 13
146196843402516 221 772928712898807 39
637929916804442 2 716937021892338 15
64200226...

output:

18446744073709551616

result:

wrong answer 1st lines differ - expected: '17883317185357051350', found: '18446744073709551616'