QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#232348 | #7616. Jump Graph | ExplodingKonjac | WA | 2ms | 15792kb | C++20 | 5.2kb | 2023-10-30 11:24:28 | 2023-10-30 11:24:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
// #define OPENIOBUF
namespace FastIO
{
class FastIOBase
{
protected:
#ifdef OPENIOBUF
static const int BUFSIZE=1<<16;
char buf[BUFSIZE+1];
int buf_p=0;
#endif
FILE *target;
FastIOBase(FILE *f): target(f){}
~FastIOBase()=default;
public:
#ifdef OPENIOBUF
virtual void flush()=0;
#endif
};
class FastOutput final: public FastIOBase
{
public:
FastOutput(FILE *f=stdout): FastIOBase(f) {}
#ifdef OPENIOBUF
~FastOutput() { flush(); }
void flush() { fwrite(buf,1,buf_p,target),buf_p=0; }
#endif
void put(char x)
{
#ifdef OPENIOBUF
if(buf[buf_p++]=x,buf_p==BUFSIZE) flush();
#else
putc(x,target);
#endif
}
FastOutput &operator <<(char x)
{ return put(x),*this; }
FastOutput &operator <<(const char *s)
{ for(;*s;put(*(s++)));return *this; }
FastOutput &operator <<(const std::string &s)
{ return (*this)<<s.c_str(); }
template<typename T>
std::enable_if_t<std::is_integral<T>::value,FastOutput&> operator <<(T x)
{
if(x<0) return put('-'),(*this)<<(-x);
char stk[numeric_limits<T>::digits10+1],*top=stk;
do *(top++)=x%10+'0',x/=10; while(x);
while(top!=stk) put(*(--top));
return *this;
}
template<typename ...T>
void writesp(T &&...x)
{ std::initializer_list<int>{((*this)<<(x),put(' '),0)...}; }
template<typename ...T>
void writeln(T &&...x)
{ std::initializer_list<int>{((*this)<<(x),put('\n'),0)...}; }
template<typename Iter>
void writesp(Iter begin,Iter end)
{ while(begin!=end) (*this)<<*(begin++)<<' '; }
template<typename Iter>
void writeln(Iter begin,Iter end)
{ while(begin!=end) (*this)<<*(begin++)<<'\n'; }
}qout;
class FastInput final: public FastIOBase
{
private:
bool __eof;
public:
FastInput(FILE *f=stdin): FastIOBase(f),__eof(false)
#ifdef OPENIOBUF
{ buf_p=BUFSIZE; }
void flush() { buf[fread(buf,1,BUFSIZE,target)]=EOF,buf_p=0; }
bool eof()const { return buf[buf_p]==EOF; }
#else
{}
bool eof()const { return feof(target); }
#endif
char get()
{
if(__eof) return EOF;
#ifdef OPENIOBUF
if(buf_p==BUFSIZE) flush();
char ch=buf[buf_p++];
#else
char ch=getc(target);
#endif
return ~ch?ch:(__eof=true,EOF);
}
void unget(char c)
{
__eof=false;
#ifdef OPENIOBUF
buf[--buf_p]=c;
#else
ungetc(c,target);
#endif
}
explicit operator bool()const { return !__eof; }
FastInput &operator >>(char &x)
{ while(isspace(x=get()));return *this; }
template<typename T>
std::enable_if_t<std::is_integral<T>::value,FastInput&> operator >>(T &x)
{
char ch,sym=0;x=0;
while(isspace(ch=get()));
if(__eof) return *this;
if(ch=='-') sym=1,ch=get();
for(;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=get());
return unget(ch),sym?x=-x:x,*this;
}
FastInput &operator >>(char *s)
{
while(isspace(*s=get()));
if(__eof) return *this;
for(;!isspace(*s) && !__eof;*(++s)=get());
return unget(*s),*s='\0',*this;
}
FastInput &operator >>(std::string &s)
{
char str_buf[(1<<8)+1]={0},*p=str_buf;
char *const buf_end=str_buf+(1<<8);
while(isspace(*p=get()));
if(__eof) return *this;
for(s.clear(),p++;;p=str_buf)
{
for(;p!=buf_end && !isspace(*p=get()) && !__eof;p++);
if(p!=buf_end) break;
s.append(str_buf);
}
unget(*p),*p='\0',s.append(str_buf);
return *this;
}
template<typename ...T>
void read(T &&...x)
{ std::initializer_list<int>{((*this)>>(x),0)...}; }
template<typename Iter>
void read(Iter begin,Iter end)
{ while(begin!=end) (*this)>>*(begin++); }
}qin;
} // namespace FastIO
using FastIO::qin,FastIO::qout;
using LL=long long;
using LD=long double;
using UI=unsigned int;
using ULL=unsigned long long;
#ifndef DADALZY
#define FILEIO(file) freopen(file".in","r",stdin),freopen(file".out","w",stdout)
#define LOG(...) [](auto...){}(__VA_ARGS__)
#else
#define FILEIO(file)
#define LOG(...) fprintf(stderr,__VA_ARGS__)
#endif
int n,p[300005],pos[300005];
LL ans[300005];
struct Data
{
LL sum,cnt0,cnt1,cnt2;
Data operator +=(const Data &rhs)
{ return sum+=rhs.sum,cnt0+=rhs.cnt0,cnt1+=rhs.cnt1,cnt2+=rhs.cnt2,*this; }
Data operator +(const Data &rhs)const
{ return Data(*this)+=rhs; }
Data updl()const
{ return Data{sum+cnt2,cnt2,cnt0+cnt1,0}; }
Data updr()const
{ return Data{sum+cnt1,cnt1,0,cnt0+cnt2}; }
}f[300005];
int lc[300005],rc[300005],L[300005],R[300005];
int build()
{
static int stk[1000005];
int top=0;
for(int i=1;i<=n;i++)
{
int k=top;
while(k && p[stk[k]]<p[i]) k--;
if(k) rc[stk[k]]=i;
if(k<top) lc[i]=stk[k+1];
stk[top=++k]=i;
}
return stk[1];
}
void dfs1(int u)
{
f[u]=Data{1,1,0,0};
if(lc[u]) dfs1(lc[u]),f[u]+=f[lc[u]].updr();
if(rc[u]) dfs1(rc[u]),f[u]+=f[rc[u]].updl();
}
void dfs2(int u,LL sum=0)
{
ans[u]=sum+(n-(R[u]-L[u]-1));
LL sl=f[lc[u]].sum+f[lc[u]].cnt2,
sr=f[rc[u]].sum+f[rc[u]].cnt1;
ans[u]+=sl+sr;
if(lc[u]) dfs2(lc[u],sum+sr);
if(rc[u]) dfs2(rc[u],sum+sl);
}
int main()
{
qin>>n;
qin.read(p+1,p+n+1);
for(int i=1;i<=n;i++)
{
L[i]=i-1;
while(L[i]>=1 && p[L[i]]<p[i])
L[i]=L[L[i]];
}
for(int i=n;i>=1;i--)
{
R[i]=i+1;
while(R[i]<=n && p[R[i]]<p[i])
R[i]=R[R[i]];
}
int rt=build();
dfs1(rt),dfs2(rt);
qout.writesp(ans+1,ans+n+1);
qout<<'\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 15792kb
input:
6 1 6 3 2 5 4
output:
11 7 7 7 6 8
result:
ok single line: '11 7 7 7 6 8 '
Test #2:
score: 0
Accepted
time: 0ms
memory: 13940kb
input:
2 1 2
output:
1 1
result:
ok single line: '1 1 '
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 11796kb
input:
36 9 29 1 3 14 31 24 21 10 18 22 16 8 7 15 12 17 19 25 28 27 34 11 6 32 4 20 13 2 35 23 26 33 36 30 5
output:
96 93 94 94 95 81 76 74 74 74 68 68 67 67 66 68 68 73 77 88 101 90 112 112 108 111 109 109 109 102 131 131 132 112 144 144
result:
wrong answer 1st lines differ - expected: '92 89 90 90 91 78 73 71 71 71 ...110 107 136 136 137 136 168 168', found: '96 93 94 94 95 81 76 74 74 74 ...09 102 131 131 132 112 144 144 '