Problem:


Problem B: Analyzing Login/Logout Records

You have a computer literacy course in your university. In the computer
system, the login/logout records of all PCs in a day are stored in a
file.  Although students may use two or more PCs at a time, no one can
log in to a PC which has been logged in by someone who has not
logged out of that PC yet.

You are asked to write a program that calculates the total time of a
student that he/she used at least one PC in a given time period
(probably in a laboratory class) based on the records in the file.

 The following are example login/logout records.

 The student 1 logged in to the PC 1 at 12:55

 The student 2 logged in to the PC 4 at 13:00

 The student 1 logged in to the PC 2 at 13:10

 The student 1 logged out of the PC 2 at 13:20

 The student 1 logged in to the PC 3 at 13:30

 The student 1 logged out of the PC 1 at 13:40

 The student 1 logged out of the PC 3 at 13:45

 The student 1 logged in to the PC 1 at 14:20

 The student 2 logged out of the PC 4 at 14:30

 The student 1 logged out of the PC 1 at 14:40

For a query such as "Give usage of the student 1 between 13:00 and
14:30", your program should answer "55 minutes", that is, the sum of
45 minutes from 13:00 to 13:45 and 10 minutes from 14:20 to 14:30, as
depicted in the following figure.

Input

The input is a sequence of a number of datasets.
The end of the input is indicated by a line containing two zeros
separated by a space.
The number of datasets never exceeds 10.

Each dataset is formatted as follows.

N M 

r 

record1

... 

recordr 

q 
  
query1

... 

queryq 

The numbers N  and M  in the first line are the numbers of
PCs and the students, respectively. r  is the number of
records.  q  is the number of queries.  These four are integers
satisfying the following.

1 ≤ N  ≤ 1000, 1 ≤ M  ≤ 10000, 2 ≤ r  ≤ 1000, 1 ≤ q  ≤ 50

Each record consists of four integers, delimited by a space, as follows.

t  n  m  s 

s  is 0 or 1.
If s  is 1, this line means that the student m  logged in
to the PC n  at time t .  If s  is 0, it means that
the student m  logged out of the PC n  at time t .
The time is expressed as elapsed minutes from 0:00 of the day.
t , n  and m  satisfy the following.

540 ≤ t  ≤ 1260,
1 ≤ n  ≤ N , 1 ≤ m  ≤ M 

You may assume the following about the records.

  
Records are stored in ascending order of time t. 
  
No two records for the same PC has the same time t. 
  
No PCs are being logged in before the time of the first record
  nor after that of the last record in the file.
  
Login and logout records for one PC appear alternatingly, and each of the login-logout record pairs is for the same student.

Each query consists of three integers delimited by a space, as follows.

ts  te  m 

It represents "Usage of the student m  between 
ts  and te ".
ts , te  and m 
satisfy the following.

540 ≤ ts  < te  ≤ 1260,
1 ≤ m  ≤ M 

Output

For each query, print a line having a decimal integer indicating the
time of usage in minutes.  Output lines should not have any character
other than this number.

Sample Input

4 2
10
775 1 1 1
780 4 2 1
790 2 1 1
800 2 1 0
810 3 1 1
820 1 1 0
825 3 1 0
860 1 1 1
870 4 2 0
880 1 1 0
1
780 870 1
13 15
12
540 12 13 1
600 12 13 0
650 13 15 1
660 12 15 1
665 11 13 1
670 13 15 0
675 11 13 0
680 12 15 0
1000 11 14 1
1060 12 14 1
1060 11 14 0
1080 12 14 0
3
540 700 13
600 1000 15
1000 1200 11
1 1
2
600 1 1 1
700 1 1 0
5
540 600 1
550 650 1
610 620 1
650 750 1
700 800 1
0 0

Output for the Sample Input

55
70
30
0
0
50
10
50
0



Sample input:
4 2
10
775 1 1 1
780 4 2 1
790 2 1 1
800 2 1 0
810 3 1 1
820 1 1 0
825 3 1 0
860 1 1 1
870 4 2 0
880 1 1 0
1
780 870 1
13 15
12
540 12 13 1
600 12 13 0
650 13 15 1
660 12 15 1
665 11 13 1
670 13 15 0
675 11 13 0
680 12 15 0
1000 11 14 1
1060 12 14 1
1060 11 14 0
1080 12 14 0
3
540 700 13
600 1000 15
1000 1200 11
1 1
2
600 1 1 1
700 1 1 0
5
540 600 1
550 650 1
610 620 1
650 750 1
700 800 1
0 0


Sample output:
55
70
30
0
0
50
10
50
0

Submission status: Wrong Answer

Solution code:
# AOJ 1148: Analyzing Login/Logout Records
# Python3 2018.7.17 bal4u

while True:
    n, m = map(int, input().split())
    if n == 0: break
    r = int(input())
    rd = [list(map(int, input().split())) for i in range(r)]
    for i in range(int(input())):
        ts, te, tm = map(int, input().split())
        ans = T = cnt = 0
        for t, pc, m, s in rd:
            if t >= te:
                if T > 0: ans += te - T; break
            if m != tm: continue
            if s:
                cnt += 1
                if T == 0:
                    T = ts if t < ts else t
            else:
                cnt -= 1
                if cnt == 0:
                    if t > ts and T > 0: ans += t - T
                    T = 0
        print(ans)

Submission status: Wrong Answer

Solution code:
#! /usr/bin/python
# -*- coding: utf-8 -*-

from bisect import *

while 1:
    (N, M) = map(int, raw_input().split())
    if N == 0: break
    r = int(raw_input())
    used = [0]*(N+1)
    imos = [[0]*1301 for i in range(M+1)]
    for i in range(r):
        (t, n, m, s) = map(int, raw_input().split())
        if s == 1 and used[n] == 0:
            used[n] = m
            imos[m][t] = 1
        if s == 0 and used[n] == m:
            used[n] = 0
            imos[m][t] = -1

    q = int(raw_input())
    for i in range(q):
        (qs, qe, user) = map(int, raw_input().split())
        tmp = 0
        ans = 0
        for j in range(540, 1261):
            #if imos[user][j] == 1:
            #    tmp += 1
            if tmp > 0 and j > qs and j <= qe:
                ans += 1
            tmp += imos[user][j]
            #if imos[user][j] == -1:
            #    tmp -= 1
        print ans

Submission status: Wrong Answer

Solution code:
#! /usr/bin/python
# -*- coding: utf-8 -*-

from bisect import *

while 1:
    (N, M) = map(int, raw_input().split())
    if N == 0: break
    r = int(raw_input())
    used = [0]*(N+1)
    imos = [[0]*1301 for i in range(M+1)]
    for i in range(r):
        (t, n, m, s) = map(int, raw_input().split())
        if s == 1 and used[n] == 0:
            used[n] = m
            imos[m][t] = 1
        if s == 0 and used[n] == m:
            used[n] = 0
            imos[m][t] -= 1
            #if imos[m][t] > 0:
            #    imos[m][t] -= 1
            #else:
            #    imos[m][t] = -1

    q = int(raw_input())
    for i in range(q):
        (qs, qe, user) = map(int, raw_input().split())
        tmp = 0
        ans = 0
        for j in range(540, 1261):
            #if imos[user][j] == 1:
            #    tmp += 1
            if tmp > 0 and j > qs and j <= qe:
                ans += 1
            tmp += imos[user][j]
            #if imos[user][j] == -1:
            #    tmp -= 1
        print ans

Submission status: Wrong Answer

Solution code:
while True:
    N, M = map(int, input().split())
    if N == 0 and M == 0: break
    r = int(input())
    students = [{'start': [], 'end': [], 'login': []} for _ in range(M)]
    for _ in range(r):
        t, n, m, s = map(int, input().split())
        n -= 1; m -= 1
        if s == 1:
            if len(students[m]['login']) == 0:
                students[m]['start'].append(t)
            students[m]['login'].append(n)
        elif s == 0:
            students[m]['login'].remove(n)
            if len(students[m]['login']) == 0:
                students[m]['end'].append(t)
    q = int(input())
    for _ in range(q):
        ts, te, m = map(int, input().split())
        m -= 1
        ans = 0
        for s, e in zip(students[m]['start'], students[m]['end']):
            if s <= ts and te <= e:
                ans = te - ts
                break
            elif s <= ts and e < te:
                ans += e - ts
            elif ts < s and te <= e:
                ans += te - s
            elif ts < s and e < te:
                ans += e - s
        print(ans)

Submission status: Wrong Answer

Solution code:

while True:
    N, M = [int(s) for s in input().strip().split()]

    if N == 0 and M == 0:
        break

    r = int(input().strip())

    logs = []
    for _ in range(r):
        t, n, m, s = [int(s) for s in input().strip().split()]
        logs.append((t,n,m,s))

    q = int(input().strip())
    for _ in range(q):
        ts, te, m = [int(s) for s in input().strip().split()]

        selected_logs = [log for log in logs if log[2] == m]

        used_time = 0
        login_time = 2000
        is_login_list = [False]*N
        for log in selected_logs:
            lt, ln, lm, ls = log
            ln -= 1

            # ログアウト
            if ls == 0:
                # 観測開始前
                if lt < ts:
                    continue

                # 観測終了前
                if lt < te:
                    # 他のPCにログインしていたらそのPCのログアウトまで待つ
                    other_pc_login = False
                    for i, is_login in enumerate(is_login_list):
                        if i == ln:
                            continue
                        if is_login:
                            other_pc_login = True
                            break
                    if other_pc_login:
                        is_login_list[ln] = False
                        continue

                    # ログイン時刻が観測開始前
                    if login_time < ts:
                        used_time += (lt - ts)
                    # ログイン時刻が観測開始後
                    else:
                        used_time += (lt - login_time)
                        
                    # ログイン状態を解除
                    is_login_list[ln] = False
                    continue
                # 観測終了後
                else:
                    if login_time < ts:
                        used_time += (te - ts)
                    else:
                        used_time += (te - login_time)
                    break

            # ログイン
            else:
                if lt > te:
                    break

                is_login_list[ln] = True
                
                other_pc_login = False
                for i, is_login in enumerate(is_login_list):
                    if i == ln:
                        continue
                    if is_login:
                        other_pc_login = True
                        break

                if not other_pc_login:
                    login_time = lt
                    
        print(used_time)

Submission status: Wrong Answer

Solution code:
l = (1260-540+1)
while True:
	N,M = map(int,input().split(" "))
	if N == 0 and M == 0:
		break
	T = [[0]*l for _ in range(M)]
	r = int(input())
	for i in range(r):
		t,n,m,s = map(int,input().split(" "))
		if s == 1:
			T[m-1][t-540] += 1
		else:
			T[m-1][t-540] -= 1

	q = int(input())
	for i in range(q):
		t1,t2,m = map(int,input().split(" "))
		T_range = []
		flg = True if T[m-1][0] >= 1 else False
		lb = 0
		for j in range(l-1):
			T[m-1][j+1] += T[m-1][j]
			if flg and T[m-1][j+1] == 0:
				T_range.append((lb+ 540,j+1+540))
				flg = False
			elif (not flg) and T[m-1][j+1] >= 1:
				flg = True
				lb = (j+1)
		if flg:
			T_range.append((lb+540,l+540))
		ans = 0
		for a,b in T_range:
			if not (b < t1 or t2 < a):
				ans += (min(t2,b) - max(t1,a))

		print(ans)

Submission status: Wrong Answer

Solution code:
l = (1260-540+1)
while True:
	N,M = map(int,input().split(" "))
	if N == 0 and M == 0:
		break
	T = [[0]*l for _ in range(M)]
	r = int(input())
	for i in range(r):
		t,n,m,s = map(int,input().split(" "))
		if s == 1:
			T[m-1][t-540] += 1
		else:
			T[m-1][t-540] -= 1

	q = int(input())
	for i in range(q):
		t1,t2,m = map(int,input().split(" "))
		T_range = []
		flg = True if T[m-1][0] >= 1 else False
		lb = 0
		for j in range(l-1):
			T[m-1][j+1] += T[m-1][j]
			if flg and T[m-1][j+1] == 0:
				T_range.append((lb+540,j+1+540))
				flg = False
			elif (not flg) and T[m-1][j+1] >= 1:
				flg = True
				lb = (j+1)
		if flg:
			T_range.append((lb+540,l+1+540))
		ans = 0
		for a,b in T_range:
			if not (b < t1 or t2 < a):
				ans += (min(t2,b) - max(t1,a))

		print(ans)

Submission status: Time Limit Exceeded

Solution code:
while True:
    N,M=map(int,input().split())
    if N==M==0: break
    r=int(input())
    history=[]
    for i in range(r):
        history.append(list(map(int,input().split())))

    q=int(input())
    question=[]
    for i in range(q):
        question.append(list(map(int,input().split())))

    timetable=[0 for _ in range(1261)]
    students=[timetable[:] for _ in range(M+1)]

    for i in range(len(history)):
        hist=history[i]
        stdnum=hist[2]
        time=hist[0]
        flag=hist[3]
        if flag==1: students[stdnum][time]+=1
        else: students[stdnum][time]-=1

    for i in range(1,M+1):
        for j in range(540,1261):
            students[i][j]+=students[i][j-1]

    for qst in question:
        start=qst[0]
        end=qst[1]
        stdnum=qst[2]
        time=students[stdnum][start:end]
        count=0
        for t in time:
            if t>0: count+=1
        print(count)

Submission status: Time Limit Exceeded

Solution code:
l = (1260-540+1)
while True:
	N,M = map(int,input().split(" "))
	if N == 0 and M == 0:
		break
	T = [[0]*l for _ in range(M)]
	r = int(input())
	for i in range(r):
		t,n,m,s = map(int,input().split(" "))
		if s == 1:
			T[m-1][t-540] += 1
		else:
			T[m-1][t-540] -= 1

	for i in range(M):
		for j in range(l-1):
			T[i][j+1] += T[i][j]

	q = int(input())
	for i in range(q):
		t1,t2,m = map(int,input().split(" "))
		ans = 0
		for t in range(t1-540,t2-540):
			if T[m-1][t] >= 1:
				ans += 1
		print(ans)

Submission status: Time Limit Exceeded

Solution code:
l = (1260-540+1)
while True:
	N,M = map(int,input().split(" "))
	if N == 0 and M == 0:
		break
	T = [[0]*l for _ in range(M)]
	r = int(input())
	for i in range(r):
		t,n,m,s = map(int,input().split(" "))
		if s == 1:
			T[m-1][t-540] += 1
		else:
			T[m-1][t-540] -= 1

	T_range = [[] for _ in range(M)]
	for i in range(M):
		flg = True if T[i][0] >= 1 else False
		lb = 0
		for j in range(l-1):
			T[i][j+1] += T[i][j]
			if flg and T[i][j+1] == 0:
				T_range[i].append((lb+ 540,j+1+540))
				flg = False
			elif (not flg) and T[i][j+1] >= 1:
				flg = True
				lb = (j+1)
		if flg:
			T_range[i].append((lb+540,l+540))

	q = int(input())
	for i in range(q):
		t1,t2,m = map(int,input().split(" "))
		ans = 0
		for a,b in T_range[m-1]:
			if not (b < t1 or t2 < a):
				ans += (min(t2,b) - max(t1,a))

		print(ans)

Submission status: Time Limit Exceeded

Solution code:
while 1:
	N,M = map(int,raw_input().split())
	if N == 0: break
	T = [[0]*1261 for i in range(M)]
	r = input()
	tnms = sorted([map(int,raw_input().split()) for i in range(r)], key = lambda x:x[1])
	for i in range(0,r,2):
		t1,n,m,s = tnms[i]
		t2,n,m,s = tnms[i+1]
		T[m-1][t1:t2] = [1 for j in range(t1,t2)]
	for m in range(M):
		c = 0
		for i in range(1260):
			if T[m-1][i]:
				c += 1
			T[m-1][i] = c - T[m-1][i+1]
	for i in range(input()):
		ts,te,m = map(int,raw_input().split())
		print T[m-1][te] - T[m-1][ts]

Submission status: Time Limit Exceeded

Solution code:
while 1:
	N,M = map(int,raw_input().split())
	if N == 0: break
	T = [[0]*1261 for i in range(M)]
	r = input()
	tnms = sorted([map(int,raw_input().split()) for i in range(r)], key = lambda x:x[1])
	for i in range(0,r,2):
		t1,n,m,s = tnms[i]
		t2,n,m,s = tnms[i+1]
		T[m-1][t1:t2] = [1 for j in range(t1,t2)]
	for m in range(M):
		c = 0
		for i in range(540,1260):
			if T[m-1][i]:
				c += 1
			T[m-1][i] = c - T[m-1][i+1]
	for i in range(input()):
		ts,te,m = map(int,raw_input().split())
		print T[m-1][te] - T[m-1][ts]

Submission status: Accepted

Solution code:
# AOJ 1148: Analyzing Login/Logout Records
# Python3 2018.7.17 bal4u

while True:
    n, m = map(int, input().split())
    if n == 0: break
    r = int(input())
    rd = [list(map(int, input().split())) for i in range(r)]
    for i in range(int(input())):
        ts, te, tm = map(int, input().split())
        ans = T = cnt = 0
        for t, pc, m, s in rd:
            if t >= te:
                if T > 0: ans += te-T
                break
            if m != tm: continue
            if s:
                cnt += 1
                if T == 0: T = ts if t < ts else t
            else:
                cnt -= 1
                if cnt == 0:
                    if t > ts and T > 0: ans += t - T
                    T = 0
        print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

def solve(N, M):
    login = [0] * (M+1)
    student = [[] for i in range(M+1)]
    r = int(input())
    for i in range(r):
        t, n, m, s = map(int, input().split())
        if s == 1:
            if login[m] == 0:
                student[m].append(t)
            login[m] += 1
        else:
            login[m] -= 1
            if login[m] == 0:
                student[m].append(t)
    q = int(input())
    for i in range(q):
        s, e, m = map(int, input().split())
        ans = 0
        for j in range(0, len(student[m]), 2):
            s2 = student[m][j]
            e2 = student[m][j+1]
            l = max(s, s2)
            r = min(e, e2)
            if r - l > 0:
                ans += r - l
        print(ans)
            
while 1:
    N, M = map(int, input().split())
    if N == 0 and M == 0:
        break
    solve(N, M)

Submission status: Accepted

Solution code:
while 1:
	N, M = map(int,raw_input().split())
	if (N,M) == (0,0):
		break
	r = int(raw_input())
	log = [[0,[]] for i in range(M)]
	time_log = [[] for i in range(M)]
	for i in range(r):
		t, n, m, s = map(int,raw_input().split())
		if s == 1:
			log[m-1][1].append(t)
			log[m-1][0] += 1
		else :
			tmp = log[m-1][1].pop()
			if log[m-1][0] == 1:
				time_log[m-1].append((tmp,t))
			log[m-1][0] -= 1
	q = int(raw_input())
	for i in range(q):
		t_s, t_e, m = map(int,raw_input().split())
		ans = 0
		for tl in time_log[m-1]:
			tmp = min(tl[1],t_e)- max(tl[0],t_s)
			ans += tmp if tmp > 0 else 0
		print ans

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

while True:
    N,M = map(int,input().split())
    if N == 0 and M == 0:
        break
    r = int(input())
    user = [[0 for i in range(1260+1)] for j in range(M+1)]
    for i in range(r):
        t,n,m,s = map(int,input().split())
        if s == 1:
            user[m][t] += 1
        else:
            user[m][t] -= 1

    q = int(input())
    for i in range(q):
        ts,te,m = map(int,input().split())
        if user[m][0] == 0:
            for j in range(540,1261):
                user[m][j] += user[m][j-1]
            user[m][0] = 1
        print(sum([1 for j in range(ts,te) if user[m][j] > 0]))

Submission status: Accepted

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        while True:
            N, M = map(int, raw_input().split())
            if N == M == 0:
                break
            record = [[0] * 2000 for i in range(M+1)]
            r = input()        
            for i in range(r):
                t, n, m, s = map(int, raw_input().split())
                if s == 1:
                    record[m][t] += 1
                else:
                    record[m][t] -= 1
            # for row in record:
            #     print row[540:1000]
            for i in range(1, M+1):
                for j in range(540, 1261):
                    record[i][j] += record[i][j-1]
            # for row in record:
            #     print row[540:1000]                    
            q = input()
            for i in range(q):
                ts, te, m = map(int, raw_input().split())
                ans = 0
                for j in range(ts, te):
                    if record[m][j] >= 1:
                        ans += 1
                print ans
        return None

if __name__ == '__main__':
    m = Main()
    m.solve()

Submission status: Accepted

Solution code:
#! /usr/bin/python
# -*- coding: utf-8 -*-

from bisect import *

while 1:
    (N, M) = map(int, raw_input().split())
    if N == 0: break
    r = int(raw_input())
    used = [0]*(N+1)
    imos = [[0]*1301 for i in range(M+1)]
    for i in range(r):
        (t, n, m, s) = map(int, raw_input().split())
        if s == 1 and used[n] == 0:
            used[n] = m
            imos[m][t] += 1
        if s == 0 and used[n] == m:
            used[n] = 0
            imos[m][t] -= 1
            #if imos[m][t] > 0:
            #    imos[m][t] -= 1
            #else:
            #    imos[m][t] = -1

    q = int(raw_input())
    for i in range(q):
        (qs, qe, user) = map(int, raw_input().split())
        tmp = 0
        ans = 0
        for j in range(540, 1261):
            #if imos[user][j] == 1:
            #    tmp += 1
            if tmp > 0 and j > qs and j <= qe:
                ans += 1
            tmp += imos[user][j]
            #if imos[user][j] == -1:
            #    tmp -= 1
        print ans

Submission status: Accepted

Solution code:
while True:
    n, m = map(int, input().split())
    if n == 0: break
    r = int(input())
    records = []
    for i in range(r):
        records.append(list(map(int, input().split())))
    q = int(input())
    queries = []
    for i in range(q):
        queries.append(list(map(int, input().split())))
    #print(records,queries)
    for q in queries:
        st = q[2]
        rs = list(filter(lambda x: x[2] == st, records))
        pcs = [[0,0] for i in range(n)]
        ls = []
        for r in rs:
            pcs[r[1]-1][r[3]] = r[0]
            if r[3] == 0:
                ls.append(pcs[r[1]-1])
                pcs[r[1]-1] = [0,0]
        if not ls: 
            print(0)
            continue
        ls.sort(key=lambda x:x[1])
        uses = []
        start, end = ls[0][1], ls[0][0]
        for v in ls[1:]:
            if v[0] < end: # subset
                continue
            elif v[1] <= end:
                end = v[0]
            elif v[1] > end:
                uses.append((start,end))
                start, end = v[1], v[0]
        uses.append((start,end))
        ans = 0
        for u in uses:
            for i in range(u[0], u[1]):
                if i >= q[0] and i < q[1]:
                    ans += 1
        print(ans)

Submission status: Accepted

Solution code:
while True:
    N, M = map(int, input().split())
    if N == 0 and M == 0: break
    r = int(input())
    students = [{'start': [], 'end': [], 'login': []} for _ in range(M)]
    for _ in range(r):
        t, n, m, s = map(int, input().split())
        n -= 1; m -= 1
        if s == 1:
            if len(students[m]['login']) == 0:
                students[m]['start'].append(t)
            students[m]['login'].append(n)
        elif s == 0:
            students[m]['login'].remove(n)
            if len(students[m]['login']) == 0:
                students[m]['end'].append(t)
    q = int(input())
    for _ in range(q):
        ts, te, m = map(int, input().split())
        m -= 1
        ans = 0
        for s, e in zip(students[m]['start'], students[m]['end']):
            if s <= ts < te <= e:
                ans = te - ts
                break
            elif s <= ts <= e < te:
                ans += e - ts
            elif ts < s <= te <= e:
                ans += te - s
            elif ts < s < e < te:
                ans += e - s
        print(ans)

Submission status: Accepted

Solution code:
while True:
    n, m = map(int, input().split())
    if n == m == 0:
        break
    l = int(input())
    v = [[] for i in range(m + 1)]
    for _ in range(l):
        t, nn, m, s = map(int, input().split())
        s = 1 if s else -1
        v[m].append((t, s))
    q = int(input())
    for _ in range(q):
        s, e, m = map(int, input().split())
        v[m].sort()
        arr = [0] * 1261
        for i, j in v[m]:
            arr[i] += j
        for i in range(540, len(arr) - 1):
            arr[i + 1] += arr[i]
        res = 0
        for i in range(s, e):
            if arr[i] > 0:
                res += 1
        print(res)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    while True:
        n,m = LI()
        if n == 0 and m == 0:
            break

        r = I()
        a = [LI() for _ in range(r)]
        q = I()
        b = [LI() for _ in range(q)]
        for s,e,c in b:
            t = 0
            ins = -1
            inc = 0
            for tt,nn,mm,ss in a:
                if mm != c:
                    continue
                if ss == 1:
                    inc += 1
                    if ins < 0:
                        ins = max(tt,s)
                else:
                    inc -= 1
                    if inc == 0:
                        t += max(0, min(e,tt)-ins)
                        ins = -1

            rr.append(t)

    return '\n'.join(map(str, rr))


print(main())

Submission status: Accepted

Solution code:
l = (1260-540)+1
while True:
	N,M = map(int,input().split(" "))
	if N == 0 and M == 0:
		break
	T = [[0]*l for _ in range(M)]
	r = int(input())
	for i in range(r):
		t,n,m,s = map(int,input().split(" "))
		if s == 1:
			T[m-1][t-540] += 1
		else:
			T[m-1][t-540] -= 1

	q = int(input())
	used = {}
	for i in range(q):
		t1,t2,m = map(int,input().split(" "))
		if not (m-1) in used:
			T_range = []
			flg = True if T[m-1][0] >= 1 else False
			lb = 0
			for j in range(l-1):
				T[m-1][j+1] += T[m-1][j]
				if flg and T[m-1][j+1] == 0:
					T_range.append((lb+540,j+1+540))
					flg = False
				elif (not flg) and T[m-1][j+1] >= 1:
					flg = True
					lb = (j+1)
			if flg:
				T_range.append((lb+540,1261))
			used[m-1] = T_range

		ans = 0
		for a,b in used[m-1]:
			if not (b < t1 or t2 < a):
				ans += (min(t2,b) - max(t1,a))

		print(ans)

Submission status: Accepted

Solution code:
import sys
import collections

while True:
    pc, student = map(int, sys.stdin.readline().split())
    if pc == student == 0:
        break
    
    record = collections.defaultdict(list)
    r = int(raw_input())
    for i in xrange(r):
        t, n, m, s = map(int, sys.stdin.readline().split())
        record[m].append((t, n, s))
    q = int(raw_input())
    for i in xrange(q):
        start, end, q_student = map(int, sys.stdin.readline().split())
        temp_start = start
        time = 0
        f_open = set()
        for i_t, i_n, i_s in record[q_student]:
            if start <= i_t <= end:
                if i_s == 1:
                    if len(f_open) == 0:
                        temp_start = i_t
                    f_open.add(i_n)
                else:
                    f_open.remove(i_n)
                    if len(f_open) == 0:
                        time += i_t - temp_start
                        temp_start = -1
            elif i_t < start:
                if i_s == 1:
                    f_open.add(i_n)
                else:
                    f_open.remove(i_n)
        if len(f_open) > 0:
            time += end - temp_start
        print time

Submission status: Accepted

Solution code:
while True:
    N,M=map(int,raw_input().split())
    if N==M==0:break
    r=input()
    L=[[False]*1261 for i in range(10001)]
    P=[[] for i in range(10001)]
    for i in range(r):
        t,n,m,s=map(int,raw_input().split())
        if s:
            P[m].append(t)
        else:
            temp=P[m].pop()
            for j in range(temp,t):
		L[m][j]=True
    q=input()
    for	i in range(q):
        ts,te,m=map(int,raw_input().split())
	cnt=0
	for j in range(ts,te):
            if L[m][j]:
		cnt+=1
        print cnt

Submission status: Accepted

Solution code:
while 1:
	N,M = map(int,raw_input().split())
	if N == 0: break
	T = [[0]*1261 for i in range(N)]
	for i in range(input()):
		t,n,m,s = map(int,raw_input().split())
		for ti in range(t,1261):
			T[n-1][ti] = s*m
	for i in range(input()):
		ts,te,m = map(int,raw_input().split())
		print sum(1 if any(T[n][t] == m for n in range(N)) else 0 for t in range(ts,te))

Submission status: Accepted

Solution code:
while 1:
	N,M = map(int,raw_input().split())
	if N == 0: break
	T = [[0]*1261 for i in range(N)]
	r = input()
	tnms = sorted([map(int,raw_input().split()) for i in range(r)], key = lambda x:x[1])
	for i in range(0,r,2):
		t1,n,m,s = tnms[i]
		t2,n,m,s = tnms[i+1]
		T[n-1][t1:t2] = [m for j in range(t1,t2)]
	for i in range(input()):
		ts,te,m = map(int,raw_input().split())
		print sum(1 if any(T[n][t] == m for n in range(N)) else 0 for t in range(ts,te))

Submission status: Accepted

Solution code:
while 1:
	N,M = map(int,raw_input().split())
	if N == 0: break
	T = [[0]*1261 for i in range(M)]
	r = input()
	tnms = sorted([map(int,raw_input().split()) for i in range(r)], key = lambda x:x[1])
	for i in range(0,r,2):
		t1,n,m,s = tnms[i]
		t2,n,m,s = tnms[i+1]
		T[m-1][t1:t2] = [1 for j in range(t1,t2)]
	for i in range(input()):
		ts,te,m = map(int,raw_input().split())
		print sum(T[m-1][ts:te])

Submission status: Accepted

Solution code:
def main(N, M):
    member = [[0] * 721 for i in range(M)]
    r = int(input())
    for _ in range(r):
        t, _, m, s = map(int, input().split())
        t -= 540
        if s == 0:
            member[m - 1][t] -= 1
        else:
            member[m - 1][t] += 1
    for i in range(M):
        for t in range(1,721):
            member[i][t] += member[i][t - 1]
    
    q = int(input())
    for i in range(q):
        ts, te, m = map(int, input().split())
        ts -= 540
        te -= 540
        ans = 0
        #print(member[m-1][ts:te])
        for t in range(ts, te):
            ans += (member[m-1][t] > 0)
        print(ans)

while 1:
    n, m = map(int, input().split())
    if n == m == 0:
        break
    main(n,m)

Submission status: Accepted

Solution code:
while True :

    N, M = map(int, input().split())
    if N == 0 and M == 0 :
        break
    
    r = int(input())
    comp_use = [[0]*720 for b in range(M)]
    for i in range(r) :
        t, n, m, s = map(int, input().split())
        if s == 1 :
            for j in range(t, 1260) :
                comp_use[m-1][j-540] += 1

        elif s == 0 :
            for j in range(t, 1260) :
                comp_use[m-1][j-540] -= 1


    q = int(input())
    for i in range(q) :
        t_s, t_e, m = map(int, input().split())
        count = 0
        for j in range(t_s-540, t_e-540) :
            if comp_use[m-1][j] >= 1 :
                count = count + 1

        print(count)