제출 #936522

#제출 시각아이디문제언어결과실행 시간메모리
936522dshfjkaSwap Swap Sort (CCO21_day1problem1)C++14
0 / 25
191 ms19152 KiB
#include <bits/stdc++.h> #define LL long long using namespace std; const LL N=1e5+5; LL n,k,q,srt; LL sum[N+5]; void add(LL x) { for(LL a=x;a<=k;a+=(a&-a)) { sum[a]++; // printf("sum[%lld]++\n",a); } } LL query(LL x) { LL tot=0; for(LL a=x;a>0;a-=(a&-a))tot+=sum[a]; return tot; } LL query(LL x, LL y) { return query(y)-query(x-1); } int main() { scanf("%lld %lld %lld",&n,&k,&q); LL arr[n+5],cnt[n+5]; memset(cnt,0,sizeof(cnt)); vector<LL>pos[k+5]; for(LL a=1;a<=n;a++) { scanf("%lld",&arr[a]); pos[arr[a]].push_back(a); } for(LL a=1;a<=n;a++) { cnt[arr[a]]++; } LL now=0; for(LL a=1;a<=n;a++) { now+=query(arr[a]+1,k); add(arr[a]); // printf("now =%lld\n",now); } // printf("now=%lld\n",now); LL srt=sqrt(n); vector<LL>v[n+5]; for(LL a=1;a<=k;a++) { if(cnt[a]>srt) { // printf("MASUK\n"); LL tmp[n+5]; for(LL b=1;b<=n;b++) { tmp[b]=tmp[a-1]; if(arr[b]==a)tmp[b]++; } for(LL b=1;b<=k;b++) { LL tot=0; for(LL x:pos[b])tot+=tmp[x]; v[a].push_back(2*tot-cnt[a]*cnt[b]); } } } LL brr[k+5]; for(LL a=1;a<=k;a++)brr[a]=a; for(LL a=1;a<=q;a++) { LL x; scanf("%lld",&x); if(cnt[brr[x]]>srt || cnt[brr[x+1]]>srt) { if(cnt[brr[x]]>cnt[brr[x+1]])now+=v[brr[x]][brr[x+1]-1]; else now-=v[brr[x+1]][brr[x]-1]; } else{ // printf("a=%lld\n",a); LL left=0,tot=0; // printf("SWAP(%lld %lld)\n",brr[x],brr[x+1]); for(LL b:pos[brr[x]]) { while(left<pos[brr[x+1]].size() && pos[brr[x+1]][left]<b) { // printf("left = %lld\n",left); left++; } tot+=left; } now-=(2*tot-cnt[brr[x]]*cnt[brr[x+1]]); } swap(brr[x],brr[x+1]); printf("%lld\n",now); } }

컴파일 시 표준 에러 (stderr) 메시지

Main.cpp: In function 'int main()':
Main.cpp:88:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |        while(left<pos[brr[x+1]].size() && pos[brr[x+1]][left]<b)
      |              ~~~~^~~~~~~~~~~~~~~~~~~~~
Main.cpp:27:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |     scanf("%lld %lld %lld",&n,&k,&q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:33:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |         scanf("%lld",&arr[a]);
      |         ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:75:11: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |      scanf("%lld",&x);
      |      ~~~~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...