提出 #74665795
ソースコード 拡げる
#include<bits/stdc++.h>
#define int long long
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
bool st;
using namespace std;
/*Ctrl+Alt+G F*/
namespace AC{
namespace Fread{const int SIZE=1<<16;char buf[SIZE],*S,*T;inline char getchar(){if(S==T){T=(S=buf)+fread(buf,1,SIZE,stdin);if(S==T)return'\n';}return *S++;}}namespace Fwrite{const int SIZE=1<<16;char buf[SIZE],*S=buf,*T=buf+SIZE;inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}inline void putchar(char c){*S++=c;if(S==T)flush();}struct NTR{~NTR(){flush();}}ztr;}
#define getchar Fread::getchar
#define putchar Fwrite::putchar
#define Setprecision 15
#define between ' '
template<typename T>struct is_char{static constexpr bool value=(std::is_same<T,char>::value||std::is_same<T,signed char>::value||std::is_same<T,unsigned char>::value);};template<typename T>struct is_integral_ex{static constexpr bool value=(std::is_integral<T>::value||std::is_same<T,__int128>::value)&&!is_char<T>::value;};template<typename T>struct is_floating_point_ex{static constexpr bool value=std::is_floating_point<T>::value||std::is_same<T,__float128>::value;};namespace Fastio{struct Reader{template<typename T>typename std::enable_if_t<std::is_class<T>::value,Reader&>operator>>(T&x){for(auto &y:x)*this>>y;return *this;}template<typename T>typename std::enable_if_t<is_integral_ex<T>::value,Reader&>operator>>(T&x){char c=getchar();short f=1;while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}x=0;while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}x*=f;return *this;}template<typename T>typename std::enable_if_t<is_floating_point_ex<T>::value,Reader&>operator>>(T&x){char c=getchar();short f=1,s=0;x=0;T t=0;while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else return x*=f,*this;while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}template<typename T>typename std::enable_if_t<is_char<T>::value,Reader&>operator>>(T&c){c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();return *this;}Reader&operator>>(char*str){int len=0;char c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();while(c!='\n'&&c!=' '&&c!='\r')str[len++]=c,c=getchar();str[len]='\0';return*this;}Reader&operator>>(std::string&str){str.clear();char c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();while(c!='\n'&&c!=' '&&c!='\r')str.push_back(c),c=getchar();return*this;}Reader(){}}cin;const char endl='\n';struct Writer{typedef __int128 mxdouble;template<typename T>typename std::enable_if_t<std::is_class<T>::value,Writer&>operator<<(T x){for(auto &y:x)*this<<y<<between;*this<<'\n';return *this;}template<typename T>typename std::enable_if_t<is_integral_ex<T>::value,Writer&>operator<<(T x){if(x==0)return putchar('0'),*this;if(x<0)putchar('-'),x=-x;static int sta[45];int top=0;while(x)sta[++top]=x%10,x/=10;while(top)putchar(sta[top]+'0'),--top;return*this;}template<typename T>typename std::enable_if_t<is_floating_point_ex<T>::value,Writer&>operator<<(T x){if(x<0)putchar('-'),x=-x;x+=pow(10,-Setprecision)/2;mxdouble _=x;x-=(T)_;static int sta[45];int top=0;while(_)sta[++top]=_%10,_/=10;if(!top)putchar('0');while(top)putchar(sta[top]+'0'),--top;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top)putchar(sta[top]+'0'),--top;return*this;}template<typename T>typename std::enable_if_t<is_char<T>::value,Writer&>operator<<(T c){putchar(c);return*this;}Writer&operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return *this;}Writer&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}Writer&operator<<(std::string str){int st=0,ed=str.size();while(st<ed)putchar(str[st++]);return*this;}Writer(){}void flush(){Fwrite::flush();}}cout;}
#undef Setprecision
using Fastio::cin;
using Fastio::cout;
using Fastio::endl;
using istream=Fastio::Reader;
using ostream=Fastio::Writer;
#include<iostream>
#include<vector>
using namespace std;
struct FenwickTree{
vector<int>tr;
int n;
FenwickTree(int _n):n(_n),tr(_n+2,0){}
void add(int x,int y){
for(;x<=n;x+=x&-x)tr[x]+=y;
}
int sum(int x){
int res=0;
for(;x>0;x-=x&-x)res+=tr[x];
return res;
}
};
int calc(int n,vector<int>&p,int x){
if(x<0)return 0;
FenwickTree ft(n);
int ans=0,inv=0;
int l=1;
for(int r=1;r<=n;r++){
int v=p[r];
ft.add(v,1);
int cnt=ft.sum(n)-ft.sum(v);
inv+=cnt;
while(inv>x){
int u=p[l];
int dec=ft.sum(u-1);
inv-=dec;
ft.add(u,-1);
l++;
}
ans+=r-l+1;
}
return ans;
}
void solve(){
int n,k;
cin>>n>>k;
vector<int>p(n+1);
for(int i=1;i<=n;i++)cin>>p[i];
cout<<calc(n,p,k)-calc(n,p,k-1)<<endl;
}
}
bool ed;
signed main(){
#ifdef debug
cerr<<"----------------------------------- START -----------------------------------\n";
#endif
int t=1;
//cin>>t;
while(t--)AC::solve();
#ifdef debug
cerr<<"\n--------------------------------- INPUT END ---------------------------------\n";
AC::cout.flush();
cerr<<"\n------------------------------------ END ------------------------------------\n";
cerr<<setprecision(3)<<fixed<<"Total Memory : "<<left<<setw(10)<<(&ed-&st)/1048576.0<<" \tMB\n"<<" "<<left<<setw(10)<<(&ed-&st)/1024.0<<" \tKB\n"<<" "<<left<<setw(10)<<(&ed-&st)<<" \tByte\n";
#endif
}
提出情報
コンパイルエラー
./Main.cpp: In constructor 'AC::FenwickTree::FenwickTree(long long int)':
./Main.cpp:26:13: warning: 'AC::FenwickTree::n' will be initialized after [-Wreorder]
26 | int n;
| ^
./Main.cpp:25:20: warning: 'std::vector<long long int> AC::FenwickTree::tr' [-Wreorder]
25 | vector<int>tr;
| ^~
./Main.cpp:27:9: warning: when initialized here [-Wreorder]
27 | FenwickTree(int _n):n(_n),tr(_n+2,0){}
| ^~~~~~~~~~~
ジャッジ結果
| セット名 |
Sample |
All |
| 得点 / 配点 |
0 / 0 |
500 / 500 |
| 結果 |
|
|
| セット名 |
テストケース |
| 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 |
| ケース名 |
結果 |
実行時間 |
メモリ |
| 00_sample_00.txt |
AC |
1 ms |
3456 KiB |
| 00_sample_01.txt |
AC |
1 ms |
3620 KiB |
| 00_sample_02.txt |
AC |
1 ms |
3552 KiB |
| 01_random_03.txt |
AC |
30 ms |
8088 KiB |
| 01_random_04.txt |
AC |
27 ms |
8448 KiB |
| 01_random_05.txt |
AC |
19 ms |
5980 KiB |
| 01_random_06.txt |
AC |
15 ms |
6060 KiB |
| 01_random_07.txt |
AC |
22 ms |
7220 KiB |
| 01_random_08.txt |
AC |
16 ms |
6212 KiB |
| 01_random_09.txt |
AC |
21 ms |
6324 KiB |
| 01_random_10.txt |
AC |
75 ms |
11124 KiB |
| 01_random_11.txt |
AC |
64 ms |
11204 KiB |
| 01_random_12.txt |
AC |
74 ms |
11076 KiB |
| 01_random_13.txt |
AC |
59 ms |
11204 KiB |
| 01_random_14.txt |
AC |
53 ms |
11136 KiB |
| 01_random_15.txt |
AC |
71 ms |
11056 KiB |
| 01_random_16.txt |
AC |
76 ms |
11160 KiB |
| 01_random_17.txt |
AC |
66 ms |
11248 KiB |
| 01_random_18.txt |
AC |
50 ms |
11056 KiB |
| 01_random_19.txt |
AC |
80 ms |
11080 KiB |
| 01_random_20.txt |
AC |
46 ms |
11076 KiB |
| 01_random_21.txt |
AC |
50 ms |
11096 KiB |
| 01_random_22.txt |
AC |
51 ms |
11252 KiB |
| 01_random_23.txt |
AC |
78 ms |
11148 KiB |
| 01_random_24.txt |
AC |
12 ms |
11204 KiB |
| 01_random_25.txt |
AC |
27 ms |
11088 KiB |
| 01_random_26.txt |
AC |
29 ms |
11136 KiB |
| 01_random_27.txt |
AC |
21 ms |
11060 KiB |
| 01_random_28.txt |
AC |
27 ms |
11076 KiB |
| 01_random_29.txt |
AC |
29 ms |
11060 KiB |
| 01_random_30.txt |
AC |
12 ms |
11204 KiB |
| 01_random_31.txt |
AC |
38 ms |
11080 KiB |
| 01_random_32.txt |
AC |
38 ms |
11252 KiB |
| 01_random_33.txt |
AC |
38 ms |
11204 KiB |
| 01_random_34.txt |
AC |
38 ms |
11208 KiB |
| 01_random_35.txt |
AC |
37 ms |
11196 KiB |
| 01_random_36.txt |
AC |
1 ms |
3476 KiB |
| 01_random_37.txt |
AC |
23 ms |
11208 KiB |
| 01_random_38.txt |
AC |
25 ms |
11252 KiB |
| 01_random_39.txt |
AC |
21 ms |
11080 KiB |
| 01_random_40.txt |
AC |
25 ms |
11148 KiB |
| 01_random_41.txt |
AC |
23 ms |
11088 KiB |
| 01_random_42.txt |
AC |
21 ms |
11076 KiB |
| 01_random_43.txt |
AC |
20 ms |
11080 KiB |
| 01_random_44.txt |
AC |
22 ms |
11148 KiB |
| 01_random_45.txt |
AC |
27 ms |
11192 KiB |
| 01_random_46.txt |
AC |
25 ms |
11080 KiB |
| 01_random_47.txt |
AC |
26 ms |
11204 KiB |
| 01_random_48.txt |
AC |
26 ms |
11136 KiB |
| 01_random_49.txt |
AC |
26 ms |
11204 KiB |
| 01_random_50.txt |
AC |
23 ms |
9724 KiB |
| 01_random_51.txt |
AC |
18 ms |
9868 KiB |
| 01_random_52.txt |
AC |
22 ms |
9400 KiB |
| 01_random_53.txt |
AC |
19 ms |
9848 KiB |
| 01_random_54.txt |
AC |
25 ms |
10352 KiB |