data structures divide and conquer *2000

Please click on ads to support us..

Python Code:

import sys
import copy
input = sys.stdin.readline
 
n=int(input())
P=[list(map(int,input().split())) for i in range(n)]
 
SET_X=set()
SET_Y=set()
 
for x,y in P:
    SET_X.add(x)
    SET_Y.add(y)
 
CX=sorted(SET_X)
CY=sorted(SET_Y)
 
LEN=len(CX)
MAX=len(CX)-1
 
DICT_X={x:i for i,x in enumerate(CX)}
DICT_Y={x:i for i,x in enumerate(CY)}
 
for i in range(n):
    P[i]=[DICT_X[P[i][0]],DICT_Y[P[i][1]]]
 
check=[0]*len(CX)
 
 
BIT=[0]*(LEN+1) 
def update(v,w):    while v<=LEN:
        BIT[v]+=w
        v+=(v&(-v)) 
def getvalue(v):    ANS=0
    while v!=0:
        ANS+=BIT[v]
        v-=(v&(-v))    return ANS
 
 
LIST_Y=[[] for i in range(len(CY))]
for x,y in P:
    LIST_Y[y].append(x)
 
for i in range(len(CY)):
    LIST_Y[i].sort()
 
ANS=0
for y in range(len(CY)-1,-1,-1):
    for x in LIST_Y[y]:
                if check[x]==0:
            check[x]=1
            update(x+1,1)
 
    ANS+=getvalue(LIST_Y[y][0]+1)*(getvalue(MAX+1)-getvalue(LIST_Y[y][0]+1)+1)
 
    for i in range(1,len(LIST_Y[y])):
                ANS+=(getvalue(LIST_Y[y][i]+1)-getvalue(LIST_Y[y][i-1]+1))*(getvalue(MAX+1)-getvalue(LIST_Y[y][i]+1)+1)
 
     
print(ANS)
 				 	  	 	  	   	    								


Comments

Submit
0 Comments
More Questions

215B - Olympic Medal
1445A - Array Rearrangment
1351A - A+B (Trial Problem)
935B - Fafa and the Gates
1291A - Even But Not Even
1269A - Equation
441A - Valera and Antique Items
1702C - Train and Queries
816B - Karen and Coffee
838D - Airplane Arrangements
148B - Escape
847G - University Classes
1110A - Parity
1215B - The Number of Products
604C - Alternative Thinking
1204C - Anna Svyatoslav and Maps
322A - Ciel and Dancing
1689B - Mystic Permutation
1711B - Party
1702D - Not a Cheap String
1714F - Build a Tree and That Is It
1703F - Yet Another Problem About Pairs Satisfying an Inequality
610A - Pasha and Stick
1200A - Hotelier
1091A - New Year and the Christmas Ornament
1352B - Same Parity Summands
1102A - Integer Sequence Dividing
630B - Moore's Law
1004A - Sonya and Hotels
1680B - Robots