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
record
1
...
record
r
q
query
1
...
query
q
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.
t
s
t
e
m
It represents "Usage of the student
m
between
t
s
and
t
e
".
t
s
,
t
e
and
m
satisfy the following.
540 ≤
t
s
<
t
e
≤ 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
7
...(truncated)

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;


int main(){
	int N,M;
	int r;
	while( cin >> N >> M && N ){
		cin >> r;
		vector<int> imos[M];
		for(int i = 0 ; i < M ; i++) imos[i].resize(60*24+1);
		for(int i = 0 ; i < r ; i++){
			int t,n,m,s;
			cin >> t >> n >> m >> s;
			--m;
			imos[m][t+1] += (s==1?1:-1);
		}
		for(int i = 0 ; i < M ; i++)
			for(int j = 1 ; j <= 24 * 60 ; j++)
				imos[i][j] += imos[i][j-1];
		for(int i = 0 ; i < M ; i++)
			for(int j = 0 ; j <= 24 * 60 ; j++){
				imos[i][j] = imos[i][j] > 0;
			}
		for(int i = 0 ; i < M ; i++)
			for(int j = 1 ; j <= 24 * 60 ; j++)
				imos[i][j] += imos[i][j-1];
		int q;
		cin >> q;
		for(int i = 0 ; i < q ; i++){
			int a,b,c;
			cin >> a >> b >> c;
			--c;
			//cout << imos[0][24*60] << endl;
			
			cout << imos[c][b] - imos[c][a] << endl;
		}
		
	}
}
accuracy: 1
time_ms: 610
memory_kb: 57848
final_rank: 17

code_snippet: #include <bits/stdc++.h>

#define all(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> Pair;
typedef complex<double> Complex;

const double PI = M_PI;
const int INF = 1e9;

int t[10001][1261];

int main() {
    int n, m, tt, nn , mm, ss;
    int r;
    while(cin >> n >> m) {
        if(n == 0) break;
        cin >> r;
        memset(t, 0, sizeof t);

        for(int i = 0; i < r; i++) {
            cin >> tt >> nn >> mm >> ss;
            mm--;
            if(ss) {
                t[mm][tt] += 1;
            } else {
                t[mm][tt] += -1;
            }
        }

        for(int i = 0; i < m; i++) {
            for(int j = 0; j <= 1261; j++) {
                t[i][j] += t[i][j-1];
            }
        }

        cin >> r;
        for(int i = 0; i < r; i++) {
            cin >> tt >> nn >> mm;
            mm--;
            int ans = 0;
            for(int j = tt; j < nn; j++) {
                ans += bool(t[mm][j]);
            }
            cout << ans << endl;
        }
    }
}
accuracy: 1
time_ms: 170
memory_kb: 52580
final_rank: 6

code_snippet: #include <bits/stdc++.h>

#define all(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> Pair;
typedef complex<double> Complex;

const double PI = M_PI;
const int INF = 1e9;

int t[10001][1261];

int main() {
    int n, m, tt, nn , mm, ss;
    int r;
    while(cin >> n >> m) {
        if(n == 0) break;
        cin >> r;
        memset(t, 0, sizeof t);

        for(int i = 0; i < r; i++) {
            cin >> tt >> nn >> mm >> ss;
            mm--;
            if(ss) {
                t[mm][tt] += 1;
            } else {
                t[mm][tt] += -1;
            }
        }

        for(int i = 0; i < m; i++) {
            for(int j = 0; j <= 1261; j++) {
                t[i][j] += t[i][j-1];
//                cout << t[i][j];
            }
//            cout << endl;
        }

        cin >> r;
        for(int i = 0; i < r; i++) {
            cin >> tt >> nn >> mm;
            mm--;
            int ans = 0;
            for(int j = tt; j < nn; j++) {
                ans += bool(t[mm][j]);
            }
            cout << ans << endl;
        }
    }
}
accuracy: 1
time_ms: 170
memory_kb: 52608
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i,l,r) for(ll i=l;i<r;i++)
#define REP(i,n) FOR(i,0,n)
int main(){
    while(1){
        ll N,M;cin>>N>>M;
        if(!N)return 0;
        ll r;cin>>r;
        vector<vector<int>>A(M,vector<int>(2000));
        while(r--){
            ll t,n,m,s;cin>>t>>n>>m>>s;m--;
            if(s)A[m][t]++;
            else A[m][t]--;
        }
        REP(i,M)REP(j,1999)A[i][j+1]+=A[i][j];
        REP(i,M)REP(j,2000)A[i][j]=min(1,A[i][j]);
        REP(i,M)REP(j,1999)A[i][j+1]+=A[i][j];
        ll Q;cin>>Q;
        while(Q--){
            ll s,e,i;cin>>s>>e>>i;i--;
            cout<<A[i][e-1]-A[i][s-1]<<endl;
        }
    }
}
accuracy: 1
time_ms: 430
memory_kb: 79116
final_rank: 16

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

int main() {
while(1) {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0) return 0;
    int MAXS = 721;
    vector<vector<int>> V(M,vector<int>(MAXS,0));
    int R;
    cin >> R;
    rep(i,0,R) {
        int t,n,m,s;
        cin >> t >> n >> m >> s;
        t-=540;
        m--;
        if (s == 1) V[m][t]++;
        else V[m][t]--;
    }
    rep(i,0,M) {
        rep(j,0,720) V[i][j+1] += V[i][j];
    }
    int Q;
    cin >> Q;
    rep(i,0,Q) {
        int s, t, m;
        cin >> s >> t >> m;
        m--;
        s-=540,t-=540;
        int ANS = 0;
        rep(j,s,t) {
            if (V[m][j]>0) ANS++;
        }
        cout << ANS << endl;
    }
}
}
accuracy: 1
time_ms: 80
memory_kb: 30816
final_rank: 4

code_snippet: #include <iostream>
#include <utility>
#include <map>
#include <vector>
 
using namespace std;
 
#define rep(i, n) REP(i, 0, n)
#define REP(i, a, n) for(int i = a ; i < (int)n ; i++)
#define pb push_back
 
typedef pair<int, int> P;
 
signed main(){
  int N,M;
  int r, q;
  cin.tie(0);
  ios::sync_with_stdio(false);
   
  while(cin >> N >> M, N || M){
    cin >> r;
    int t, n, m, s;
    map<P, vector<P> > data;

    rep(i, r){
      cin >> t >> n >> m >> s;      
      data[P(n,m)].pb(P(t, s));
    }
    cin >> q;
    rep(i, q){
      int ts, te, qm;
      cin >> ts >> te >> qm;
      bool flag[1261]; 
      fill(flag, flag+1260, false);
       
      REP(j, 1, N+1){
        for(int k = 0 ; k < (int)data[P(j, qm)].size() ; k+=2){
          int start = max(data[P(j, qm)][k].first, ts);
          int end = min(data[P(j, qm)][k+1].first, te);
          REP(l, start, end+1){
            flag[l] = true;
          }
        }
      }
 
      int ans = 0;
      REP(j, ts, te){
        if(flag[j]){
          int start = j;
          while(flag[j]) j++;     
          ans+=(j-start-1);
        }
      }
 
      cout << ans << endl;
    }
  }
}
accuracy: 1
time_ms: 20
memory_kb: 4760
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;


int main()
{
  std::cin.tie(0)->sync_with_stdio(0);
  while(true){
    int n,m;
    cin >> n >> m;
    if(n==0 and m==0)break;
    int r;
    cin >> r;
    // touch[i][j]:= 学生iが時間jにログインしてるパソコンの数
    vector<vector<int>> touch(m+1,vector<int>(2000,0));
    for(int i=0;i<r;i++){
      int t,pcn,who,s;
      cin >> t >> pcn >> who >> s;
      if(s==0){
        touch[who][t+1]--;
      }else{
        touch[who][t+1]++;
      }
    }
    for(int i=0;i<=m;i++){
      for(int j=1;j<2000;j++){
        touch[i][j]=touch[i][j-1]+touch[i][j];
        if(touch[i][j-1]>0)touch[i][j-1]=1;
      }
    }
    for(int i=1;i<=m;i++){
      for(int j=1;j<2000;j++){
        touch[i][j]=touch[i][j-1]+touch[i][j];
        //cout << i << " " << j << endl;
        //cout << touch[i][j] << endl;
      }
    }
    int q;
    cin >> q;
    for(int i=0;i<q;i++){
      int s,e,who;
      cin >> s >> e >> who;
      cout << touch[who][e]-touch[who][s] << endl;
    }

  }
}
accuracy: 1
time_ms: 660
memory_kb: 79176
final_rank: 20

code_snippet: #include <bits/stdc++.h>

//使用したライブラリ
//https://github.com/atcoder/ac-library/tree/fe9b6fca9ab4e1be946ea23a4e6a2a751cf4aaa2
//https://github.com/ei1333/library/tree/7da7fca8cebc1d17a048c9483f07e39c8e465cdf
//https://github.com/zawa-tin/cp-documentation/tree/3842eb973aace809be0bcf1649e2fe85e99098e3
//https://github.com/luckylat/library/tree/8e2a2a6a58bef4644c9442998a1c9c1890672cd3


const int TIME{1261};

bool solve() {
    int N, M;
    std::cin >> N >> M;
    if (N == 0 and M == 0) return false;
    int R;
    std::cin >> R;
    std::vector<std::vector<std::tuple<int, int, int>>> input(M);
    for (int i{} ; i < R ; i++) {
        int t, n, m, s;
        std::cin >> t >> n >> m >> s;
        m--; n--;
        input[m].emplace_back(t, n, s);
    }
    std::vector<std::array<bool, TIME>> range(M);
    for (int i{} ; i < M ; i++) {
        std::vector<std::pair<int, int>> cur;
        std::vector<int> left(N, -1);
        for (auto [t, n, s] : input[i]) {
            if (s == 1) { // login
                assert(left[n] == -1);
                left[n] = t;
            }
            else { // logout
                assert(left[n] != -1);
                cur.emplace_back(left[n], t);
                left[n] = -1;
            }
        }
        range[i].fill(false);
        for (auto [L, R] : cur) {
            for (int j{L} ; j < R ; j++) {
                range[i][j] = true;
            }
        }
    }
    int Q;
    std::cin >> Q;
    while (Q--) {
        int ts, te, m;
        std::cin >> ts >> te >> m;
        m--;
        int ans{};
        for (int i{ts} ; i < te ; i++) {
            ans += range[m][i];
        }
        std::cout << ans << '\n';
    }
    return true;
}

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    while (solve()) ;
}
accuracy: 1
time_ms: 60
memory_kb: 22672
final_rank: 3

code_snippet: #include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define all(x) x.begin(), x.end()
template <class A, class B>
inline bool chmin(A &a, const B &b) {
    return b < a && (a = b, true);
}
template <class A, class B>
inline bool chmax(A &a, const B &b) {
    return b > a && (a = b, true);
}

int solve() {
    int n, m;
    cin >> n >> m;
    if (n == 0) return 1;
    const int N = 1300;
    vector a(m, vector<int>(N));
    int r;
    cin >> r;
    rep(i, r) {
        int t, x, y, s;
        cin >> t >> x >> y >> s;
        y--;
        if (s == 0) {
            a[y][t]--;
        } else {
            a[y][t]++;
        }
    }
    rep(i, m) {
        rep(j, N - 1) {
            a[i][j + 1] += a[i][j];
        }
    }
    int q;
    cin >> q;
    while (q--) {
        int s, t, i;
        cin >> s >> t >> i;
        i--;
        int ans = 0;
        for (int j = s; j < t; j++) ans += (a[i][j] > 0);
        cout << ans << endl;
    }
    return 0;
}

int main() {
    while (!solve());
}
accuracy: 1
time_ms: 190
memory_kb: 52704
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vpii = vector<pair<int, int>>;
using vpll = vector<pair<long long, long long>>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<long long>>;
using vvc = vector<vector<char>>;
using vvvi = vector<vector<vector<int>>>;
using pii = pair<int, int>;
using vvb = vector<vector<bool>>;
using Graph = vector<vector<ll>>;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 998422353;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep3(i, m, n) for (int i = (m); i < (int)(n); i++)
#define rrep(i, m, n) for (int i = (m); i >= (int)(n); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

vi dx4 = {1, 0, -1, 0}, dy4 = {0, 1, 0, -1};
vi dx8 = {1, 1, 1, 0, 0, -1, -1, -1}, dy8 = {1, 0, -1, 1, -1, 1, 0, -1};

int main()
{
    while (1)
    {
        // 宣言・入力の受け取り
        int n, m;
        cin >> n >> m;
        // 終了条件
        if (n == 0 && m == 0)
        {
            break;
        }
        // 宣言・入力の受け取り
        vvb use(1440, vb(m, false));
        int r;
        cin >> r;
        vi t(r), N(r), M(r), s(r);
        rep(i, r)
        {
            cin >> t[i] >> N[i] >> M[i] >> s[i];
            N[i]--, M[i]--;
        }
        vi online(m, 0);
        rep3(i, 540, 1265)
        {
            rep(j, r)
            {
                if (i == t[j])
                {
                    if (s[j] == 1)
                    {
                        online[M[j]]++;
                    }
                    else if (s[j] == 0)
                    {
                        online[M[j]]--;
                    }
                }
            }
            rep(k, m)
            {
                if (online[k] > 0)
                {
                    use[i][k] = true;
                }
            }
        }
        // solve
        int q;
        cin >> q;
        rep(i, q)
        {
            int t_s, t_e, x;
            cin >> t_s >> t_e >> x;
            x--;
            int ans = 0;
            rep3(j, t_s, t_e)
            {
                if (use[j][x])
                {
                    ans++;
                }
            }
            cout << ans << '\n';
        }
        // 出力
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 4924
final_rank: 2

code_snippet: #include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define SHOW(n) cerr << #n << ":" << n << endl;


ofstream fout("out.txt");

void solve() {
	int pcs, stu;
	cin >> pcs >> stu;
	if (pcs == 0 and stu == 0) {
		exit(0);
	}
	vector<vector<int>> time(stu, vector<int>(1300));

	int r;
	cin >> r;
	while (r--) {
		int t, n, m, s;
		cin >> t >> n >> m >> s;
		time[m - 1][t] += (s == 1 ? 1 : -1);
	}

	for (auto& v : time) {
		for (int i = 540; i < 1300; ++i) {
			v[i] += v[i - 1];
		}
	}

	int q;
	cin >> q;
	
	while (q--) {
		int s, t, m;
		cin >> s >> t >> m;
		--m;
		int res = 0;
		for (int i = s; i < t; ++i) {
			if (time[m][i] > 0) {
				res += 1;
			}
		}
		cout << res << endl;
		fout << res << endl;
	}
	
}

int main() {

	
	
	while (true) {
		solve();
	}

	

	return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 52572
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main () {
    while (1) {
        int n, m;
        cin >> n >> m;
        
        if(n == 0 and m == 0) break;
        
        vector<vector<int>> v(m+1, vector<int>(1300));
        
        int r;
        cin >> r;
        for (int i = 0; i < r; i++) {
            int a, b, c, d;
            cin >> a >> b >> c >> d;
            
            if (d == 0) v[c][a]--;
            else if (d == 1) v[c][a]++;
        }
        
        for (int i = 0; i < m+1; i++) {
            for (int j = 1; j < 1300; j++) 
            v[i][j] += v[i][j-1];
        }
        
        int q;
        cin >> q;
        for (int i = 0; i < q; i++) {
            int x, y, z;
            cin >> x >> y >> z;
            
            int ans = 0;
            for (int j = x; j < y; j++) {
                if (v[z][j] != 0) ans++;
            }
            cout << ans << endl;
        }
    }
    
    return 0;
}
accuracy: 1
time_ms: 270
memory_kb: 52760
final_rank: 13

code_snippet: #include <bits/stdc++.h>
#define LL_INF 9223372036854775807
#define INT_INF 2147483647
using namespace std;
using ll = long long;

int main()
{
	int n,m;
	while(cin >> n >> m,n){
		vector<vector<pair<int,int>>> v(m+1,vector<pair<int,int>>(1300,{0,0}));
		int r;
		cin >> r;
		for(int i=0;i<r;i++){
			int t,l,m,s;
			cin >> t >> l >> m >> s;
			if(s==0)v[m][t+1].first--;
			if(s==1)v[m][t+1].first++;
		}
		//累積和とって加算して質問出力
		for(int i=0;i<m+1;i++){
			for(int j=0;j<1299;j++){
				v[i][j+1].first+=v[i][j].first;
			}
		}
		for(int i=0;i<m+1;i++){
			for(int j=0;j<1299;j++){
				v[i][j+1].second=v[i][j].second;
				if(v[i][j+1].first!=0) v[i][j+1].second++;
				
			}
		}

		int q;
		cin >> q;
		for(int i=0;i<q;i++){
			int ts,te,m;
			cin >> ts >> te >> m;
			cout << v[m][te].second-v[m][ts].second << endl;
		}
	}	
}
accuracy: 1
time_ms: 490
memory_kb: 101780
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(long long i = 0;i < (long long)(n);i++)

int main(){
  ll siz = 780;
  while(true){
    ll N,M;cin >> N >> M;
    if(N == 0 && M == 0){
      break;
    }
    ll r;cin >> r;
    vector<vector<ll>> time(M,vector<ll>(siz));//M人
    rep(z,r){
      ll t,n,m,s;cin >> t >> n >> m >> s;
      t-=500;
      m--;
      if(s == 0){
        time[m][t+1]--;
      }else{
        time[m][t+1]++;
      }
    }

    //imos
    rep(i,M){
      rep(j,siz-1){
        time[i][j+1]+= time[i][j];
      }
    }
    //debug
    // rep(i,M){
    //   cout << time[i][1999] << endl;
    // }

    //ruiseki
    rep(i,M){
      rep(j,siz-1){
        if(time[i][j+1] != 0){
          time[i][j+1] = time[i][j] + 1;
        }else{
          time[i][j+1] = time[i][j];
        }
      }
    }

    ll q;cin >> q;
    rep(z,q){
      ll a,b,m;cin >> a >> b >> m;
      a-=500;
      b-=500;
      m--;
      cout << time[m][b] - time[m][a] << endl;
    }
  }
}
accuracy: 1
time_ms: 250
memory_kb: 62324
final_rank: 14

code_snippet: #include <bits/stdc++.h>

using namespace std;

int main() {
	int n, m;
	while(cin >> n >> m && n) {
		int r, q;
		cin >> r;
		int time, pc, student, stat;
		vector<vector<int> > person(m, vector<int>(1260-540+1+5, 0));
		vector<vector<int> > imos(m, vector<int>(1260-540+1, 0));
		for(int i=0;i<r;i++) {
			cin >> time >> pc >> student >> stat;
			if(stat) {
				person[student-1][time-540] += 1;
			} else {
				person[student-1][time-540] -= 1;
			}
		}
		for(int i=0;i<m;i++) { 
			int current = 0;
			for(int j=0;j<1260-540;j++) {
				current += person[i][j];
				if(current) {
					imos[i][j+1] = imos[i][j] + 1;
				} else {
					imos[i][j+1] = imos[i][j];
				}
			}
		}
		cin >> q;
		int t1, t2;
		for(int i=0;i<q;i++) {
			cin >> t1 >> t2 >> student;
			cout << imos[student-1][t2-540]-imos[student-1][t1-540] << endl;
		}	
	}
	return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 58400
final_rank: 12

code_snippet: #line 2 "template.hpp"
#include <bits/stdc++.h>
using namespace std;
#define square(x) ((x)*(x))
#define endl '\n'
#define debug(val); cerr << #val << ": " << val << endl;

// 型エイリアス
using ll = long long int;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using uint = unsigned int;
using T3 = tuple<ll, int, int>;
// 定数
constexpr int INT_INF = 1e9 + 1;
constexpr ll INF = 1LL << 60;
// constexpr ll MOD = (long long)1e9 + 7;
constexpr ll MOD = 998244353;

// 配列での方向
// 上下左右（右から時計回り）
constexpr int dx[4] = { 1, 0, -1, 0 };
constexpr int dy[4] = { 0, 1, 0, -1 };
// 斜め4方向（右上から時計回り）
//constexpr int dx[4] = { 1, 1, -1, -1 };
//constexpr int dy[4] = { -1, 1, 1, -1 };
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

void Yes(bool yes)
{
    if (yes) cout << "Yes" << '\n';
    else cout << "No" << '\n';
}

// pii
ostream& operator<<(ostream& os, pii p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
// vector<T>
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    for (const T& val : v)
    {
        os << val << ' ';
    }

    return os;
}
// vector<vector<T>>
template <typename T>
ostream& operator<<(ostream& os, const vector<vector<T>>& v)
{
    for (const vector<T>& line : v)
    {
        os << endl;
        for (const T& val : line)
        {
            os << val << ' ';
        }
    }

    return os;
}
// set<T>
template <typename T>
ostream& operator<<(ostream& os, const set<T>& s)
{
    for (auto itr = s.begin(); itr != s.end(); ++itr)
    {
        os << *itr << ' ';
    }
    return os;
}
#line 2 "a.cpp"

int main(void) {
	while (true) {
		int N, M;
		int r, q;
		cin >> N >> M;
		if (N == 0 && M == 0) break;
		cin >> r;
		vector <vector<int>> student(M, vector<int>(1261, 0));
		for (int i = 0; i < r; ++i) {
			int t, n, m, s;
			cin >> t >> n >> m >> s;

			int add = (s ? 1 : -1);
			student[m - 1][t] += add;
		}

		for (int i = 0; i < M; ++i) {
			for (int j = 1; j <= 1260; ++j) {
				student[i][j] += student[i][j - 1];
				if (student[i][j] < 0) exit(-1);
			}
			for (int j = 1; j <= 1260; ++j) {
				student[i][j] = (student[i][j] > 0);
			}
			for (int j = 1; j <= 1260; ++j) {
				student[i][j] += student[i][j - 1];
			}
		}

		cin >> q;
		for (int i = 0; i < q; ++i) {
			int ts, te, m;
			cin >> ts >> te >> m;
			cout << (student[m - 1][te - 1] - student[m - 1][ts - 1]) << endl;
		}
	}
}
accuracy: 1
time_ms: 440
memory_kb: 51084
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;

void solve(int n, int m){
    // n num of pc
    // m num of user

    int r;  // r num of records
    cin>>r;

    vector<vector<int>> time(m+1, vector<int>(1300));
    for(int i=0;i<r;i++){
        int tt,nn,mm,ss;
        cin>>tt>>nn>>mm>>ss;
        if(ss==1){ // login
            time[mm][tt]++;
        }
        else{ // logout
            time[mm][tt]--;
        }
    }
    for(int i=0;i<=m;i++){
        for(int j=0;j<1300-1;j++){
            time[i][j+1]+=time[i][j];
        }
    }

    int q; // num of questions
    cin>>q;
    for(int i=0;i<q;i++){
        int start,end,user;
        cin>>start>>end>>user;
        int ans=0;
        for(int j=start;j<end;j++){
            if(time[user][j]>=1) ans++;
        }
        cout<<ans<<endl;
    }
}

int main(){
    while(true){
        int n,m;
        cin>>n>>m;
        if(n==0 && m==0) break;
        solve(n,m);
    }
    return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 52616
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i = 0;i < n;i++)

void main_(){
	int N,M;
	cin >> N >> M;

	if(!N) exit(0);

	vector<vector<int>> imos(1261,vector<int>(M,0)) ;
	vector<int> st(M,0);

	int r;
	cin >> r;

	rep(i,r){
		int t,n,m,s;
		cin >> t >> n >> m >> s;
		m--;

		if(s == 1){
			imos.at(t).at(m) += 1;
		}
		else {
			imos.at(t).at(m) -= 1;
		}
	}

	int q;
	cin >> q;

	rep(_,q){
		int s,e,m;
		cin >> s >> e >> m;
		m--;
		int cnt = 0;
		int ans = 0;
		for(int i = 540;i <= 1260;i++){

			cnt += imos.at(i).at(m);

			// if(cnt != 0){
			// 	cout << i << " " << cnt << endl;
			// }



			if(cnt >= 1 && s <= i && i < e){
				ans++;
			}
		}

		cout << ans << endl;
	}
}

signed main(){
	while(1) main_();
}
accuracy: 1
time_ms: 90
memory_kb: 50584
final_rank: 5

code_snippet: #include <bits/stdc++.h>
#ifdef MY_DEBUG
  #include "dprint.cpp"
  # define dprint(a, ...);\
      do {\
          printf("line : %d, func : %s\n",\
                __LINE__, __func__);\
          dprint(#a,a,##__VA_ARGS__ );\
      } while (0)
#else
  #define dprint(a,...) 
#endif
using namespace std;
#define P pair<int,int>
template<typename T> inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); }
template<typename T> inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); }
bool solve(){
  int N,M;
  cin >>N>>M;
  if(N==0&&M==0)return false;
  int r;
  cin >>r;

  vector<vector<int>> PCs(M,vector<int>(1263,0));
  for (int i = 0; i < r; i++){
    int t,n;
    cin >>t>>n;
    int m,s;
    cin >>m>>s;m--;
    if(s==0){
      PCs[m][t]--;
    }else{
      PCs[m][t]++;
    }
  } 

for (int m = 0; m < M; m++){
 for (int t = 0; t < 1262; t++){
  PCs[m][t+1]+=PCs[m][t];
 } 
}
  int q;
  cin >>q;

  for (int i = 0; i < q; i++){
    int s,e,m;
    cin >>s>>e>>m;m--;

    int ans=0;
    for (int t = s; t < e; t++){
      if(PCs[m][t]>0)ans++;
    }
    cout << ans << endl;
  } 
  
  return true;

}
int main() {
  while (true){
    if(solve()==false)break;
  }
  

return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 51304
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
//#define MOD 998244353
#define INF 1e18 + 10
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout<<x<<endl;
#define repi(i,x,n) for(int i=x;i<n;i++)
#define rep(i,n) repi(i,0,n)
#define lp(i,n) repi(i,0,n)
#define repn(i,n) for(int i=n;i>=0;i--)
#define int long long
#define endl "\n"

typedef pair<int,int> PII;
typedef pair<int,string> PIS;
typedef pair<string,int> PSI;

template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b; 
    return true;
  }
  return false;
}

template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

signed main(){
  cin.tie(0);	
  ios::sync_with_stdio(false);
  while(1){
    int N,M;
    cin>>N>>M;
    if(N==0 && M==0) break;
    vector<vector<int> > times(M,vector<int>(1300,0) );
    int r;
    
    cin>>r;
    rep(i,r){
      int t,n,m1,S;
      cin>>t>>n>>m1>>S;
      m1--;
      if(S) times[m1][t]++;
      else times[m1][t]--;
    }
    rep(i,M){
      for(int j=1;j<1300;j++) times[i][j]+=times[i][j-1];
    }
    int q;
    cin>>q;
    while(q--){
      int cnt=0;
      int s,e,m2;
      cin>>s>>e>>m2;
      m2--;
      for(int i=s;i<e;i++){
	if(times[m2][i]) cnt++;
      }
      cout<<cnt<<endl;
    }
  }

  return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 101736
final_rank: 18