Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save overnew/f35a9f68e272f078597d468a33db9e4d to your computer and use it in GitHub Desktop.
Save overnew/f35a9f68e272f078597d468a33db9e4d to your computer and use it in GitHub Desktop.
[AlgoSpot]_MEASURETIME_삽입 정렬 시간 재기
#include<vector>
#include<cstdio>
using namespace std;
typedef long long ll;
class FenwickTree{
public:
vector<ll> tree = vector<ll>(1000001);
ll Sum(int pos){
ll ret = 0;
while(pos>0){
ret += tree[pos];
pos &= (pos -1);
}
return ret;
}
void Add(int pos){
while(pos <= tree.size()){
tree[pos] += 1;
pos += (pos & -pos);
}
}
};
int main(){
int test_num,arr_size;
scanf("%d", &test_num);
while(test_num--){
scanf("%d", &arr_size);
FenwickTree fen_tree;
int num;
long long result =0;
for(int i =0; i<arr_size ; ++i){
scanf("%d", &num);
++num;
result += fen_tree.Sum(1000000) - fen_tree.Sum(num);
fen_tree.Add(num);
}
printf("%lld\n", result);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment