QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#245382 | #7520. Monster Generator | kkio | WA | 1ms | 8196kb | C++17 | 10.8kb | 2023-11-09 21:15:41 | 2023-11-09 21:15:42 |
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;
#define int long long
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{
Point A,B;
ll k,b;
int id;
}S[maxn],q[maxn];
int head,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){
db t1=atan2(a.B.y-a.A.y,a.B.x-a.A.x);
db t2=atan2(b.B.y-b.B.y,b.B.x-b.B.x);
if(fabs(t1-t2)>eps)return t1<t2;
else return cross(b.A-a.A,b.B-a.A)>eps;
}
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;
i128 MX=(i128)1<<64;
signed 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=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;
// for(int i=1;i<=len;i++)printf("%lld ",t[i]);putchar('\n');
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]];
// for(int i=0;i<=n;i++)printf("%lld %lld %lld %lld\n",sa[i],sda[i],sb[i],sdb[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);
// cout<<Ans<<'\n';
ans+=Ans;
ans=min(ans,MX);
continue;
}
S[++tot]=(Seg){(Point){1.0*Lt,0},(Point){1.0*Rt,0},0,0,0};
for(int i=1;i<=n;i++)
S[++tot]=(Seg){(Point){1.0*Lt,1.0*sa[i]+Lt*sda[i]-sb[i-1]-Lt*sdb[i-1]},(Point){1.0*Rt,1.0*sa[i]+Rt*sda[i]-sb[i-1]-Rt*sdb[i-1]},sda[i]-sdb[i-1],sa[i]-sb[i-1],i};
//for(int i=1;i<=tot;i++)cout<<S[i].A.x<<' '<<S[i].A.y<<' '<<S[i].B.x<<' '<<S[i].B.y<<' '<<S[i].k<<' '<<S[i].b<<'\n';
sort(S+1,S+1+tot,comp);
//for(int i=1;i<=tot;i++)cout<<S[i].id<<' ';cout<<'\n';
tail=0;
for(int i=1;i<=tot;i++)
{
if(i!=1&&fabs(cross(S[i].B-S[i].A,S[i-1].B-S[i-1].A))<eps)continue;
while(tail>1&&cross(S[i].B-sc[tail],S[i].A-sc[tail])>eps)tail--;
q[++tail]=S[i];
if(tail>1)
sc[tail]=Sec(q[tail],q[tail-1]);
}
int head=1;
while(head<tail&&sc[tail].x>=Rt)tail--;
while(head<tail&&sc[head+1].x<Lt)head++;
//cout<<head<<' '<<tail<<'\n';
for(int i=head;i<=tail;i++)
{
ll nowl=i==head?Lt:ceil(sc[i].x)+1;
ll nowr=i==tail?Rt:floor(sc[i+1].x);
if(nowl<=nowr)ans+=(i128)(nowr+nowl)*(nowr-nowl+1)/2*q[i].k+(nowr-nowl+1)*q[i].b;
ans=min(ans,MX);
}
}
write(ans),io.pc('\n');
}
/*
3 8
3 4 1 2
7 2 5 3
1 3 5 1
*/
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 8132kb
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: 8196kb
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: 8084kb
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'