Submission #74668210


Source Code Expand

// Problem: F - Interval Inversion Count
// Contest: AtCoder - AtCoder Beginner Contest 452
// URL: https://atcoder.jp/contests/abc452/tasks/abc452_f
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#pragma GCC optimize("Ofast,inline,unroll-loops")
#ifdef GTRAKIOI
#define _GLIBCXX_DEBUG //交题前记得注释掉不然容易T。
#endif
#include<bits/stdc++.h>
// #include<stdio.h>
#define File(s) freopen(#s".in","r",stdin),freopen(#s".out","w",stdout)
#ifdef GTRAKIOI
#include"C:/code/deb_20.cpp"
#define defrog(...) fprintf(stderr,__VA_ARGS__)
#define deb(x) (std::cerr<<#x<<"@"<<__LINE__<<"="<<(x)<<'\n')
#else
#define defrog(...) 1
#define deb(x) 1
#define debug(...) 1
#define debugArr(...) 1
#endif
#define defrogf(...) defrog(__VA_ARGS__)
#define Tp template<typename T>
#define Tl template<typename T
#define Tr >
#define IS(cond) ,std::enable_if_t<(cond), int> = 0
#if __cplusplus>=201703L
#define register
#endif

#ifdef _MSC_VER
#if __has_include(<__msvc_int128.hpp>)
#include <__msvc_int128.hpp> // https://stackoverflow.com/a/76440171
#define __int128 std::_Signed128
#define __int128_t std::_Signed128
#define __uint128_t std::_Unsigned128
#define __SIZEOF_INT128__ 16
#endif
#endif
using ll=long long;
// #define int ll
using ull=unsigned long long;
#ifdef __SIZEOF_INT128__
using lll=__int128;
// using ulll=unsigned __int128;
#endif
using db=double;
using ld=long double;
#define INT_ALIAS(w) using i##w=std::int##w##_t;using u##w=std::uint##w##_t;
INT_ALIAS(8) INT_ALIAS(16) INT_ALIAS(32) INT_ALIAS(64)
#ifdef __SIZEOF_INT128__
using i128=__int128_t;
using u128=__uint128_t;
using i7=__int128_t;
using u7=__uint128_t;
template <class T>
using to_unsigned = typename std::conditional<
    std::is_same<T, __int128_t>::value ||
        std::is_same<T, __int128>::value,
    std::common_type<__uint128_t>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;
#else
template <class T>
using to_unsigned = std::make_unsigned<T>;
#endif
template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

template<typename T>using vv=std::vector<T>;
template<typename T>using V=std::vector<T>;
using pii=std::pair<int,int>;
using vi=V<int>;
using vll=V<ll>;
using vpii=V<pii>;
using vvi=V<vi>;
template<typename T>using pq=std::priority_queue<T>;
template<typename T>using pqg=std::priority_queue<T,std::vector<T>,std::greater<>>;
#define pb push_back
#define eb emplace_back
#define pob pop_back
#define all(cont) std::begin(cont),std::end(cont)
#define rall(cont) std::rbegin(cont),std::rend(cont)

#define G90 1
#ifdef G90
char ibuf[1<<15],*p1,*p2;
#define getchar() (p1==p2&&(p2=(p1=ibuf)+fread(ibuf,1,1<<15,stdin),p1==p2)?EOF:*p1++)
#endif
struct FastIO{
	Tl IS(!std::numeric_limits<T>::is_signed) Tr inline void oint(T x){
		T y=1;
		while(y<=x/10)y*=10;
		do putchar(int(x/y)|48),x%=y,y/=10;while(y);
	}
	Tl IS(std::numeric_limits<T>::is_signed) Tr inline void oint(const T&x){
		if(x<0){
			putchar('-');
			oint<to_unsigned_t<T>>(-x);
		}else oint<to_unsigned_t<T>>(x);
	}
	#ifdef G90
	Tl=int IS(std::numeric_limits<T>::is_integer) Tr inline T rint(){register char c,f=0;while((c=getchar())<48||c>57)f|=c=='-';to_unsigned_t<T> a=c&15;while((c=getchar())>=48&&c<=57)a=a*10+(c&15);return f?~a+1:a;}
	// inline ll rll(){rg char c,f=0;while((c=getchar())<48||c>57)f|=c=='-';rg ull a=c&15;while((c=getchar())>=48&&c<=57)a=a*10+(c&15);return f?~a+1:a;}
//	inline operator int(){return rint();}
	// inline operator ll(){return rll();}
	Tl IS(std::numeric_limits<T>::is_integer) Tr inline operator T(){return rint<T>();}
	inline char rchar(){register char c;while(!isgraph(c=getchar()));return c;}
	inline int rstr(char*s){register char c;while(!isgraph(c=getchar()));int cnt=-1;do s[++cnt]=c;while(isgraph(c=getchar()));s[++cnt]=0;return cnt;}
	inline std::string rs(){register char c;while(!isgraph(c=getchar()));std::string s;do s+=c;while(isgraph(c=getchar()));return s;}
	#else
	Tp requires requires(std::istream&is,T&x){is>>x;} inline operator T(){T a;std::cin>>a;return a;}
	inline char rchar(){return char(*this);}
	inline int rstr(char*s){register char c=-1;while(!isgraph(c=char(std::cin.get())));int cnt=-1;do s[++cnt]=c;while(isgraph(c=char(std::cin.get())));s[++cnt]=0;return cnt;}
	inline std::string rs(){return *this;}
	#endif
	Tl IS(std::numeric_limits<T>::is_integer) Tr inline void print(const T&x){oint(x);}
	inline void print(const char&x){putchar(x);}
	inline void print(const char*const&x){for(int i=0;x[i];++i)putchar(x[i]);}
	#if __cplusplus >= 202002L
	Tp requires std::ranges::range<T> inline void print(const T&c){
		bool first=true;
		for(const auto&x:c){
			if(!first)putchar(' ');
			first=false;
			print(x);
		}
	}
	#endif
	inline void print(const std::string&x){for(int i=0;x[i];++i)putchar(x[i]);}

	// print with separators
	// inline void prints(){putchar('\n');}
	// inline void prints(const auto&x,const auto&...rst){print(x),putchar(' '),prints(rst...);}
	inline void prints(const auto&...x){((print(x),putchar(' ')),...);putchar('\n');}
}g90;
inline void YON(const bool&x){puts(x?"YES":"NO");}
inline void Yon(const bool&x){puts(x?"Yes":"No");}
inline void yon(const bool&x){puts(x?"yes":"no");}

template<typename T=int>std::vector<T>rvec(std::size_t n,std::size_t start=0) {
	std::vector<T>res(start+n);
	for(std::size_t i=start;i<start+n;++i)res[i]=g90;
	return res;
}

std::mt19937_64 rng(u32(std::chrono::high_resolution_clock::now().time_since_epoch().count()));
Tl IS(std::is_floating_point<T>::value) Tr inline T rnd(const T&a,const T&b){
	return std::uniform_real_distribution<T>(a,b)(rng);
}
Tl IS(std::numeric_limits<T>::is_integer) Tr inline T rnd(const T&a,const T&b){
	return std::uniform_int_distribution<T>(a,b)(rng);
}
namespace MY_STD{
	Tp inline T abs(const T&a){return a<0?-a:a;}
}
#if __cplusplus >= 202002L
namespace all{
	using namespace std::ranges;
	using namespace std::views;
	
	//ambiguous ones
	using std::views::iota;
	using std::views::empty;
	using std::views::reverse;
	inline constexpr auto&R=std::views::reverse;
}
#else
#define ssize(a) int((a).size())
#endif
struct DSU{//unweighted
	using key_type=int;

	std::vector<key_type>fa,size;
	inline DSU(key_type n):fa(n),size(n,1){std::iota(fa.begin(),fa.end(),0);}
	inline key_type& getFa(key_type x){
		while(x^fa[x])x=fa[x]=fa[fa[x]];
		return fa[x];
	}
	inline key_type& operator[](const key_type&x){return getFa(x);}
	inline auto canMerge(const key_type&u,const key_type&v){return getFa(u)!=getFa(v);}
	inline bool merge(key_type u,key_type v){
		u=getFa(u),v=getFa(v);
		return (u)!=(v)&&(size[u]<size[v]&&(std::swap(u,v),1),fa[v]=u,size[u]+=size[v],size[v]=0,true);
	}

};

template<typename Compare=std::less<>>inline bool ckmax(auto& a,const auto& b,const Compare&comp={}){return comp(a,b)?(a=b,true):false;}
template<typename Compare=std::less<>>inline bool ckmin(auto& a,const auto& b,const Compare&comp={}){return comp(b,a)?(a=b,true):false;}

inline auto divf(const auto&a,const auto&b){//assume b>0
	return a<0?(a+1)/b-1:a/b;
}
inline auto divc(const auto&a,const auto&b){//assume b>0
	return a>0?(a-1)/b+1:a/b;
}

#define fi first
#define se second
// #define x first
// #define y second

constexpr int N=-2026,M=1;//1000000007;
// using mint = atcoder::static_modint<M>;
inline int qpow(ll a,auto b){int res=1;for(;b;a=a*a%M,b>>=1)if(b&1)res=res*a%M;return res;}
// #define pow qpow

int n=g90;
ll k=g90;
vll a=rvec<ll>(n);
signed main(){
	a.eb(1);
	using std::cin,std::cout,std::cerr;
	//std::ios::sync_with_stdio(0);std::cin.tie(0);std::cout.tie(0);
	struct DS{
		ll k,r=0;
		vi fwt=vi(n+2);
		auto add(int i,int x=1){
			for(;i<ssize(fwt);i+=i&-i)fwt[i]+=x;
		};
		auto sum(int i){
			int x=0;
			for(;i;i-=i&-i)x+=fwt[i];
			return x;
		};
		void push(){
			k-=sum(n)-sum(a[r]);
			add(a[r++],1);
		}
		void pop(int x){
			add(x,-1);
			k+=sum(x);
		}
	}ds(k),ds1(k-1);
	ll ans=0;
	for(int l=0;l<n;++l){
		while(ds.r<=n&&ds.k>=0)ds.push();
		while(ds1.r<=n&&ds1.k>=0)ds1.push();
		debug(l,ds.r,ds1.r);
		if(k)ans+=ds.r-ds1.r;
		else ans+=ds.r-l-1;
		ds.pop(a[l]),ds1.pop(a[l]);
	}
	printf("%lld\n",ans);
}//main()

Submission Info

Submission Time
Task F - Interval Inversion Count
User fission
Language C++23 (GCC 15.2.0)
Score 500
Code Size 8499 Byte
Status AC
Exec Time 64 ms
Memory 11508 KiB

Compile Error

./Main.cpp: In function 'int main()':
./Main.cpp:23:20: warning: statement has no effect [-Wunused-value]
   23 | #define debug(...) 1
      |                    ^
./Main.cpp:244:17: note: in expansion of macro 'debug'
  244 |                 debug(l,ds.r,ds1.r);
      |                 ^~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 55
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt, 01_random_53.txt, 01_random_54.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3776 KiB
00_sample_01.txt AC 1 ms 3708 KiB
00_sample_02.txt AC 1 ms 3708 KiB
01_random_03.txt AC 36 ms 8316 KiB
01_random_04.txt AC 38 ms 8644 KiB
01_random_05.txt AC 20 ms 6292 KiB
01_random_06.txt AC 18 ms 6284 KiB
01_random_07.txt AC 29 ms 7472 KiB
01_random_08.txt AC 21 ms 6524 KiB
01_random_09.txt AC 22 ms 6468 KiB
01_random_10.txt AC 64 ms 11436 KiB
01_random_11.txt AC 62 ms 11284 KiB
01_random_12.txt AC 63 ms 11436 KiB
01_random_13.txt AC 61 ms 11284 KiB
01_random_14.txt AC 60 ms 11416 KiB
01_random_15.txt AC 63 ms 11448 KiB
01_random_16.txt AC 63 ms 11284 KiB
01_random_17.txt AC 62 ms 11300 KiB
01_random_18.txt AC 59 ms 11388 KiB
01_random_19.txt AC 64 ms 11296 KiB
01_random_20.txt AC 58 ms 11300 KiB
01_random_21.txt AC 59 ms 11388 KiB
01_random_22.txt AC 58 ms 11368 KiB
01_random_23.txt AC 64 ms 11444 KiB
01_random_24.txt AC 23 ms 11444 KiB
01_random_25.txt AC 33 ms 11368 KiB
01_random_26.txt AC 33 ms 11340 KiB
01_random_27.txt AC 30 ms 11300 KiB
01_random_28.txt AC 32 ms 11368 KiB
01_random_29.txt AC 33 ms 11396 KiB
01_random_30.txt AC 23 ms 11508 KiB
01_random_31.txt AC 47 ms 11368 KiB
01_random_32.txt AC 47 ms 11408 KiB
01_random_33.txt AC 47 ms 11396 KiB
01_random_34.txt AC 47 ms 11440 KiB
01_random_35.txt AC 47 ms 11288 KiB
01_random_36.txt AC 1 ms 3860 KiB
01_random_37.txt AC 29 ms 11308 KiB
01_random_38.txt AC 30 ms 11496 KiB
01_random_39.txt AC 29 ms 11372 KiB
01_random_40.txt AC 30 ms 11312 KiB
01_random_41.txt AC 29 ms 11456 KiB
01_random_42.txt AC 29 ms 11300 KiB
01_random_43.txt AC 29 ms 11424 KiB
01_random_44.txt AC 29 ms 11440 KiB
01_random_45.txt AC 30 ms 11308 KiB
01_random_46.txt AC 30 ms 11308 KiB
01_random_47.txt AC 29 ms 11408 KiB
01_random_48.txt AC 30 ms 11292 KiB
01_random_49.txt AC 30 ms 11384 KiB
01_random_50.txt AC 26 ms 10028 KiB
01_random_51.txt AC 26 ms 10152 KiB
01_random_52.txt AC 25 ms 9804 KiB
01_random_53.txt AC 26 ms 10032 KiB
01_random_54.txt AC 29 ms 10524 KiB