#include <iostream>
#include <vector>
#include <map>

using namespace std;
typedef long long ll;
ll INF = 1000000000000000;
struct segment_tree{
    int n; vector<ll> seg;
    segment_tree(vector<ll> v){
        n = v.size(); seg.resize(2*n);
        for(int i=0;i<n;i++) seg[i + n] = v[i];
        for(int i=n - 1;i>0;i--) seg[i] = seg[i<<1] + seg[i<<1|1];
    }
    void update(int p,ll val){
        for(seg[p += n] += val;p>1;p>>=1) seg[p>>1] = seg[p] + seg[p^1];
    }
    ll query(int l,int r){
        ll res = 0;
        for(l += n,r += n; l<r;l>>=1,r>>=1){
            if(l&1) res += seg[l++];
            if(r&1) res += seg[--r];
        }
        return res;
    }
    void clear(){for(int i=0;i<2*n;i++) seg[i] = 0;}
};

int t[200010],x[200010],y[200010];
int main(){
    int i,n,m,q; cin >> n >> m >> q;
    vector<int> a(n),b(m);
    map<int,int> mp;
    mp[0]++;
    for(i=0;i<q;i++){
        cin >> t[i] >> x[i] >> y[i];
        t[i]--; x[i]--; mp[y[i]]++;
    }
    vector<ll> v,u;
    for(auto x:mp) v.push_back(x.first);
    for(i=0;i<v.size();i++) mp[v[i]] = i;
    int k = v.size(); u.resize(k);
    vector<segment_tree> seg(2,segment_tree(u)),seg_num(2,segment_tree(u));
    for(i=0;i<n;i++) seg[0].update(mp[a[i]],a[i]),seg_num[0].update(mp[a[i]],1);
    for(i=0;i<m;i++) seg[1].update(mp[b[i]],b[i]),seg_num[1].update(mp[b[i]],1);
    ll ans = 0;
    for(i=0;i<q;i++){
        ll now;
        int j = x[i];
        if(t[i]==0) now = a[j];
        else now = b[j];
        ans -= seg_num[1 - t[i]].query(0,mp[now])*now + seg[1 - t[i]].query(mp[now],k);
        if(t[i]==0){
            seg[0].update(mp[a[j]],-a[j]); seg_num[0].update(mp[a[j]],-1); a[j] = y[i];
        }else{
            seg[1].update(mp[b[j]],-b[j]); seg_num[1].update(mp[b[j]],-1); b[j] = y[i];
        }
        seg[t[i]].update(mp[y[i]],y[i]); seg_num[t[i]].update(mp[y[i]],1);
        ans += seg_num[1 - t[i]].query(0,mp[y[i]])*y[i] + seg[1 - t[i]].query(mp[y[i]],k);
        cout << ans << "\n";
    }
}
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fo(i,a,b) for(int i = a; i<b ; i++)
#define FIO ios_base::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);
#define pb push_back
#define M 1000000007
int mod(int x){
return ((x%M + M)%M);
}
int add(int a,int b){
return mod(mod(a)+mod(b));
}
int mul(int a,int b){
return mod(mod(a)*mod(b));
}
// ****************************************************************************
int n,m;
map<int,vector<pair<int,int>>>mp;
int dp[1<<(19)];
int rec(int mask){
    if(mask==0)
    return 1;
    // now check the validity
    if(dp[mask]!=-1)
    return dp[mask];
    int pref = n-__builtin_popcount(mask);
    for(auto it : mp[pref]){
        int val = it.first;
        int maxnum = it.second;
        int temp = 0;
        for(int j=1;j<=n;j++){
            if(!(mask&(1<<j)) && j<=val)
            temp++;
        }
        if(temp>maxnum)
        return 0;
    }
    // now try to put every value
    int ans = 0;
    for(int i=1;i<=n;i++){
        if((mask&(1<<i))){
            ans+=rec(mask^(1<<i));
        }
    }
    return dp[mask] = ans;
}
void solve(){
    cin>>n>>m;
    while(m--){
        int x,y,z;
        cin>>x>>y>>z;
        mp[x].pb({y,z});
    }
    memset(dp,-1,sizeof dp);
    int mask = 1<<(n+1);
    mask--;
    mask^=1;
    cout<<rec(mask);
}
signed main()
{
FIO
int t;
t=1;
// cin>>t;
while(t--)
{
solve();
}
}