Spider's Web (Codeforces 216D)
Tags: Others, two pointers

Paw the Spider is making a web. Web-making is a real art, Paw has been learning to do it his whole life. Let's consider the structure of the web.  There are n main threads going from the center of the web. All main threads are located in one plane and divide it into n equal infinite sectors. The sectors are indexed from 1 to n in the clockwise direction. Sectors i and i + 1 are adjacent for every i, 1 ≤ i < n. In addition, sectors 1 and n are also adjacent.Some sectors have bridge threads. Each bridge connects the two main threads that make up this sector. The points at which the bridge is attached to the main threads will be called attachment points. Both attachment points of a bridge are at the same distance from the center of the web. At each attachment point exactly one bridge is attached. The bridges are adjacent if they are in the same sector, and there are no other bridges between them.A cell of the web is a trapezoid, which is located in one of the sectors and is bounded by two main threads and two adjacent bridges. You can see that the sides of the cell may have the attachment points of bridges from adjacent sectors. If the number of attachment points on one side of the cell is not equal to the number of attachment points on the other side, it creates an imbalance of pulling forces on this cell and this may eventually destroy the entire web. We'll call such a cell unstable. The perfect web does not contain unstable cells.Unstable cells are marked red in the figure. Stable cells are marked green.Paw the Spider isn't a skillful webmaker yet, he is only learning to make perfect webs. Help Paw to determine the number of unstable cells in the web he has just spun.

Input: The first line contains integer n (3 ≤ n ≤ 1000) — the number of main threads.The i-th of following n lines describe the bridges located in the i-th sector: first it contains integer ki (1 ≤ ki ≤ 105) equal to the number of bridges in the given sector. Then follow ki different integers pij (1 ≤ pij ≤ 105; 1 ≤ j ≤ ki). Number pij equals the distance from the attachment points of the j-th bridge of the i-th sector to the center of the web.It is guaranteed that any two bridges between adjacent sectors are attached at a different distance from the center of the web. It is guaranteed that the total number of the bridges doesn't exceed 105.

Output: Print a single integer — the number of unstable cells in Paw the Spider's web.


Sample Inputs:
7
3 1 6 7
4 3 5 2 9
2 8 1
4 3 7 6 4
3 2 5 9
3 6 3 8
3 4 2 9

Sample Outputs:
6

Solution (Python):
import bisect

def get_iterator(container, value):
    return bisect.bisect_left(container, value)

def main():
    nodes = int(input())
    N = 1 << 10
    path = [[] for _ in range(N)]
    connector = [[] for _ in range(N)]
    for i in range(nodes):
        input_line = input().split()
        links = int(input_line[0])
        values = map(int, input_line[1:])
        for value in values:
            path[i].append(value)
            path[(i + 1) % nodes].append(value)
            connector[i].append(value)
    for i in range(nodes):
        path[i].sort()
        connector[i].sort()
    result = 0
    for i in range(nodes):
        for j in range(len(connector[i]) - 1):
            left = get_iterator(path[i], connector[i][j + 1]) - get_iterator(path[i], connector[i][j])
            right = get_iterator(path[(i + 1) % nodes], connector[i][j + 1]) - get_iterator(path[(i + 1) % nodes], connector[i][j])
            if left != right:
                result += 1
    print(result)
if __name__ == '__main__':
    main()

Solution (Python):
import bisect

def get_iterator(container, value):
    return bisect.bisect_left(container, value)

def main():
    nodes = int(input())
    N = 1 << 10
    path = [[] for _ in range(N)]
    connector = [[] for _ in range(N)]
    for i in range(nodes):
        input_line = input().split()
        links = int(input_line[0])
        values = map(int, input_line[1:])
        for value in values:
            path[i].append(value)
            path[(i + 1) % nodes].append(value)
            connector[i].append(value)
    for i in range(nodes):
        path[i].sort()
        connector[i].sort()
    result = 0
    for i in range(nodes):
        for j in range(len(connector[i]) - 1):
            left = get_iterator(path[i], connector[i][j + 1]) - get_iterator(path[i], connector[i][j])
            right = get_iterator(path[(i + 1) % nodes], connector[i][j + 1]) - get_iterator(path[(i + 1) % nodes], connector[i][j])
            if left != right:
                result += 1
    print(result)
if __name__ == '__main__':
    main()

Solution (Python):
import collections
import heapq
import sys
import math
import itertools
import bisect
import os

def values():
    return tuple(map(int, sys.stdin.readline().split()))

def inplst():
    return [int(i) for i in sys.stdin.readline().split()]

def inp():
    return int(sys.stdin.readline())

def inpstr():
    return sys.stdin.readline().strip()

def words():
    return sys.stdin.readline().split()

def chars():
    return list(sys.stdin.readline().strip())

def solve():
    N = inp()
    sectors = []
    for i in range(N):
        sector_bridges = values()[1:]
        sectors.append(sorted(sector_bridges))
    ans = 0
    for i in range(N):
        (pi, ni) = (0, 0)
        (ps, ns) = (sectors[(i - 1 + N) % N], sectors[(i + 1) % N])
        s = sectors[i]
        for j in range(len(s) - 1):
            (p, n) = (0, 0)
            while pi < len(ps) and ps[pi] < s[j + 1]:
                if s[j] <= ps[pi] <= s[j + 1]:
                    p += 1
                pi += 1
            if pi < len(ps) and s[j] <= ps[pi] <= s[j + 1]:
                p += 1
            while ni < len(ns) and ns[ni] < s[j + 1]:
                if s[j] <= ns[ni] <= s[j + 1]:
                    n += 1
                ni += 1
            if ni < len(ns) and s[j] <= ns[ni] <= s[j + 1]:
                n += 1
            if n != p:
                ans += 1
    print(ans)
if __name__ == '__main__':
    if os.path.exists('/home/ms/myp/problem-solving/input'):
        sys.stdin = open('/home/ms/myp/problem-solving/input', 'r')
    sys.setrecursionlimit(10000000)
    solve()