Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef struct nodes{
- struct nodes* l;
- struct nodes* r;
- struct nodes* p;
- int m;
- long long lm;
- long long rm;
- }node;
- int reversePairs(int* nums, int numsSize){
- node* create(){
- node* p=(node*)malloc(sizeof(node));
- p->m=0;p->r=NULL;p->l=NULL;
- return p;
- }
- node * root=create();
- void throw(long long n){
- long long l=-4294967296; long long r=4294967296;
- long long m=(l+r)/2;
- node *ptr=root;
- while(l!=r){
- m=(l+r)/2;
- if((l+r)<0&&(l+r)%2!=0){
- m=m-1;
- }
- //printf("l=%ld,r=%ld,m=%ld\n",l,r,m);
- ptr->m++;
- if(n>m){
- // printf("hi\n");
- if(ptr->r==NULL){
- ptr->r=create();
- ptr->r->p=ptr;
- ptr=ptr->r;
- }else{
- // printf("hi1\n");
- ptr=ptr->r;
- }
- ptr->lm=m+1;ptr->rm=r;
- l=m+1;
- }else{
- if(ptr->l==NULL){
- ptr->l=create();
- ptr->l->p=ptr;
- ptr=ptr->l;
- }else{
- ptr=ptr->l;
- }
- ptr->rm=m; ptr->lm=l;
- r=m;
- }
- }
- ptr->m++;
- return;
- }
- void antithrow(long long n){
- long long l=-4294967296; long long r=4294967296;
- long long m=(l+r)/2;
- node *ptr=root;
- while(ptr){
- m=(l+r)/2;
- if((l+r)<0&&(l+r)%2!=0){
- m=m-1;
- }
- ptr->m--;
- //printf("%d:[%ld,%ld] -1 now %d \n",n,l,r,ptr->m);
- if(n>m){
- ptr=ptr->r;
- l=m+1;
- }else{
- ptr=ptr->l;
- r=m;
- }
- }
- //ptr->m--;
- return;
- }
- int search(int n){
- int ans=0; long long l=-4294967296; long long r=4294967296;
- long long m=(l+r)/2;
- node *ptr=root;
- while(l!=r){
- m=(l+r)/2;
- if((l+r)<0&&(l+r)%2!=0){
- m=m-1;
- }
- //printf(" m=%ld ",m);
- if(n>=m){
- if(ptr->l){
- ans+=ptr->l->m;
- // printf("search=%d,+%d at %d,%d\n",n,ptr->l->m,l,m);
- }
- if(ptr->r==NULL){
- break;
- }
- ptr=ptr->r;
- l=m+1;
- }else{
- if(ptr->l==NULL){
- break;
- }
- ptr=ptr->l;
- r=m;
- }
- }
- return ans;
- }
- int anss=0;
- long long a=0;
- for(int i=0;i<numsSize;i++){
- // printf("hi%d\n",2*nums[i]+1);
- a=(long long)nums[i]+(long long)nums[i]+1;
- throw(a);
- }
- for(int i=0;i<numsSize;i++){
- a=(long long)nums[i]+(long long)nums[i]+1;
- antithrow(a);
- anss+=search(nums[i]);
- // printf("+%d,%d ",search(nums[i]),root->m);
- }
- // antithrow(1);
- //antithrow(1);
- //node *ptr=root->r;
- /* while(ptr->l!=NULL){
- ptr=ptr->l;
- }*/
- /* void trans(node *ptr){
- if(ptr->)
- }*/
- //printf("%d",ptr->p->m);
- /*if(numsSize>10000){
- return anss+1;
- }*/
- return anss;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement