QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#245626 | #7520. Monster Generator | kkio | WA | 41ms | 3748kb | C++17 | 10.1kb | 2023-11-10 07:13:01 | 2023-11-10 07:13:02 |
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)
{
return ((i128)a*b<0?-1:1)*(abs(a)+abs(b)-1)/abs(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: 100
Accepted
time: 1ms
memory: 3496kb
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: 3420kb
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: 0
Accepted
time: 1ms
memory: 3460kb
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:
17883317185357051350
result:
ok single line: '17883317185357051350'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3608kb
input:
10 1000000000000 519946 5 967590 4 367668 9 772494 6 635694 5 932710 1 260259 2 627580 1 84994 3 52124 6 447095 4 705749 6 427312 2 977458 7 540121 1 292993 5 556831 6 321679 4 567919 4 609512 4
output:
1542003553318518337
result:
ok single line: '1542003553318518337'
Test #5:
score: 0
Accepted
time: 1ms
memory: 3364kb
input:
10 1000000000000000000 972703917532605 2 524956306619424 679 644953227221677 4 562488807303931 696 726248880302017 2 678581164692315 811 63290732871341 4 2359762326353 451 355584232678496 3 295959529542702 895 982076563374348 4 315626935294595 161 202583559712801 1 987516708328993 170 26590404960673...
output:
4582284981606185217
result:
ok single line: '4582284981606185217'
Test #6:
score: 0
Accepted
time: 1ms
memory: 3660kb
input:
10 1000000000000000000 915236950983 25 924829121702 314 142125786492 33 125091250839 71 702305171043 11 468800042449 438 449646370235 9 56198959092 472 246955215365 12 950417123809 62 646952653060 4 858914642874 441 693754630072 34 490226765023 91 273330383457 25 749838451697 371 635897703553 24 847...
output:
18304932886689493500
result:
ok single line: '18304932886689493500'
Test #7:
score: 0
Accepted
time: 1ms
memory: 3496kb
input:
100 1000000000000000000 839671173511709 107 620743247548148 134 338569457755976 9 455191878916920 157 56529874788057 33 993208347666256 99 553193266380324 120 589361808163439 126 866467572275902 19 13931460152331 210 630774124991101 56 253227140072409 133 970610042608501 106 332792633317838 252 8813...
output:
2159229278647499039
result:
ok single line: '2159229278647499039'
Test #8:
score: 0
Accepted
time: 1ms
memory: 3568kb
input:
100 1000000000000000000 926447257775795 188 535928580576730 524 773621914798781 805 607314524993852 999 433706296251306 467 260773017334982 276 627420175261216 730 936517336182015 391 944793592281860 143 916701567834795 374 985020926183290 391 155471328385744 343 158052135419112 152 37256868527793 4...
output:
845915142005167939
result:
ok single line: '845915142005167939'
Test #9:
score: 0
Accepted
time: 0ms
memory: 3464kb
input:
5 10000000 82420 1 83004 12 90974 1 5052 16 74853 1 50459 3 40080 1 8547 14 73449 1 29852 11
output:
50401135100561
result:
ok single line: '50401135100561'
Test #10:
score: 0
Accepted
time: 41ms
memory: 3584kb
input:
100 1000000000000000000 9993245793650 4 9241831921583 115 6604842581175 13 7477954917260 107 7956734211252 3 351959292590 21 8744829275263 11 1121812966924 88 4383873864556 10 7802901884633 87 2999374450961 5 7728117026444 119 2606040601922 2 9450726899416 95 463533606932 4 456141627827 113 51628088...
output:
1462626783113250968
result:
ok single line: '1462626783113250968'
Test #11:
score: 0
Accepted
time: 32ms
memory: 3604kb
input:
100 1000000000000000000 5514922686365 63 3893026500867 7 9437390653117 2 2307883657774 37 2266370593545 180 282207773345 54 7413603305531 64 6590374339957 4 2003184336714 205 3334946120451 23 8047937523313 197 6016974069987 57 49327962408 210 95380662767 50 5796501143593 219 4738100059711 11 4403864...
output:
3008948596970395169
result:
ok single line: '3008948596970395169'
Test #12:
score: 0
Accepted
time: 27ms
memory: 3600kb
input:
100 1000000000000000000 9522256146511 22 7648033142717 124 4890738110302 13 5169707386838 200 1692873223867 14 4198546120569 164 5759367352857 61 5467937093692 19 5156572753262 64 2244161860595 92 5800903619823 60 9979656907955 2 9003875069201 21 6933129430226 204 9783187462793 49 5298708535013 190 ...
output:
5391002818765040268
result:
ok single line: '5391002818765040268'
Test #13:
score: 0
Accepted
time: 29ms
memory: 3712kb
input:
100 1000000000000000000 3121743641635 206 2774880557656 124 4341273709336 48 3868201748314 58 4898184402079 75 8962585631533 152 6469059809450 191 7785613783588 151 5901037828713 109 1086985069519 120 1872215875662 118 8935389074175 187 1080302564361 60 1962889993669 35 8218338957964 95 230441706635...
output:
4006532618546541597
result:
ok single line: '4006532618546541597'
Test #14:
score: 0
Accepted
time: 1ms
memory: 3496kb
input:
100 1000000000000000000 121103125248530 208 233527234872397 54 842070374171965 178 514148681666092 126 93136745067938 256 36717885771563 224 120511269258919 654 299962020993680 15 426864331284465 625 581173275854814 257 618912001992036 511 767521635123932 235 937058562049830 716 650234846124942 322 ...
output:
7367765353666615058
result:
ok single line: '7367765353666615058'
Test #15:
score: 0
Accepted
time: 1ms
memory: 3496kb
input:
100 1000000000000000000 23284392897817 126 30977511034534 38 841495940790312 583 926499138731470 57 527560260544041 179 627459187144397 29 820340813703299 663 925690360693482 6 547369350978777 777 788007180251049 43 981250793921808 349 194382279990992 63 741370530598735 297 931419589514850 56 687866...
output:
7438179643288006845
result:
ok single line: '7438179643288006845'
Test #16:
score: 0
Accepted
time: 1ms
memory: 3560kb
input:
100 1000000000000000000 872423180427906 171 928660139134452 60 173464176166260 441 30242191203867 679 291627746220343 270 316446546573556 224 433904419794557 267 453622827613070 936 322487075039542 337 273582158548540 770 442190946439316 194 402704746493069 558 224382362705672 382 660906097188180 94...
output:
1676623952079872857
result:
ok single line: '1676623952079872857'
Test #17:
score: 0
Accepted
time: 0ms
memory: 3544kb
input:
100 1000000000000000000 447083504726390 41 217084089403816 104 450487653728833 43 576979382901346 56 589969051420591 8 411176483725392 60 733710648527183 60 208793807198690 80 819246579883243 5 634295240911810 19 120573670504083 31 567245341012191 97 244593463425171 8 74367118641765 87 2790961612563...
output:
3612430687325323033
result:
ok single line: '3612430687325323033'
Test #18:
score: 0
Accepted
time: 1ms
memory: 3572kb
input:
100 1000000000000000000 432410439270777 235 116344272313715 953 18833991674565 325 492411023067340 741 993487833166770 28 8834705038993 320 998878412553368 248 570799530850940 437 194089961995026 163 273965500040581 107 128666129189367 293 279847256835557 716 653181797300576 456 136293503069973 897 ...
output:
12939147014964558709
result:
ok single line: '12939147014964558709'
Test #19:
score: 0
Accepted
time: 1ms
memory: 3748kb
input:
100 1000000000000000000 210114066313907 370 401244816929180 176 711657896227396 361 581112854286866 77 812417204182776 118 837215341723765 82 204282266930923 166 422636641427393 170 944666030439036 69 563942622285108 132 76718175341639 481 806791853518922 76 256410213740841 71 15055409072833 175 732...
output:
9598469605999375590
result:
ok single line: '9598469605999375590'
Test #20:
score: 0
Accepted
time: 1ms
memory: 3568kb
input:
100 1000000000000000000 182752800360801 328 547408164765978 31 557477952347166 161 753441955886985 21 514414548285144 180 993819825241906 10 757393185241412 421 294635656072618 4 29471967015767 474 745284417822919 6 187879293744454 503 379955926601373 22 630787488112518 226 998151791416706 26 312195...
output:
15132802571592726490
result:
ok single line: '15132802571592726490'
Test #21:
score: 0
Accepted
time: 1ms
memory: 3584kb
input:
100 1000000000000000000 249608 2571692352 1254347665 19271613868028 256888 7951928041 1774397545 78315915184249 150686 3237143848 1281449179 100522891111286 32617 3269902298 1554446514 109005033573806 104197 3693458716 1245405691 14792892061217 261343 2560573771 1382558981 61037905159690 180389 3689...
output:
8150417481066096321
result:
ok single line: '8150417481066096321'
Test #22:
score: -100
Wrong Answer
time: 31ms
memory: 3556kb
input:
100 1000000000000000000 116429503963352 16 51197563628 125673 72077114688235 6 39953310726 85648 82787991723257 9 43715545842 239107 89180287090370 10 19316156881 16914 127874136211272 9 62101758469 93904 19228879359180 8 60860048430 33677 5062624378063 8 44235669549 160474 119724750396433 9 3751989...
output:
11164290899805971637
result:
wrong answer 1st lines differ - expected: '10024419831831103889', found: '11164290899805971637'