2020ccpc Weihai H Message Bomb (differential)

H Message Bomb

While we enjoy chatting with friends on the internet, it is always annoying that we are overwhelmed by lots of messages in various chat groups. A great majority of these messages are actually not interesting to us, but we may miss some important notices if we silence these groups. How many messages do we receive from all online chat groups? Nobody has ever seriously gone into this question.

As an assistant researcher in the school of informatics, you are required to investigate the number of online messages we receive every day. We have already sampled n groups and m students. Every group contains a subset of the m students, which is possibly empty. Also, the members of the groups are constantly evolving; old members may quit, and new members may join in a chat group. Members can send messages in the group; the message is broadcast to all other members currently in the same group.

Now we have collected the log of these chat groups. The log is a sequence of events, which may be a student joining in a group, quitting a group, or sending a message in a group. Your task is to compute the total number of messages received by every student.

Input Specification:

The first line of the input contains three integers n,m,s (1≤n≤100000,1≤m≤200000,1≤s≤1000000), denoting the number of groups, the number of students and the number of events in the log.

The next s lines give the events in the log in chronological order. Each of them contains three integers t,x,y (t∈{1,2,3},1≤x≤m,1≤y≤n) specifying an event, which may fall into one of the following three categories:

  • If t=1, it means that the x-th student joined in the y-th group. It is guaranteed that the student was not in the group before.
  • If t=2, it means that the x-th student quitted the y-th group. It is guaranteed that the student was currently in the group.
  • If t=3, it means that the x-th student sent a message in the y-th group. It is guaranteed that the student was in the group now.

Initially, all groups were empty.

Output Specification:

Output m lines. The i-th line contains an integer, denoting the total number of messages the i-th student received.

Sample Input 1:

3 3 10
1 3 2
1 3 1
1 1 2
1 2 1
3 1 2
2 3 1
3 3 2
3 2 1
3 3 2
3 2 1

Sample Output 1:

2
0
1

Sample Input 2:

2 5 10
1 1 2
3 1 2
2 1 2
1 3 2
1 1 2
3 1 2
3 3 2
1 4 2
3 3 2
1 5 1

Sample Output 2:

2
0
1
1
0

 Question: n group chats, m individuals, q operations, there are three operations

(1) The xth person joined the yth group chat

(2) The xth person left the yth group chat

(3) The xth person sent a message in the yth group chat

Ask the number of messages received by each person at the end (one person can join multiple group chats

Idea: cnt[i] Store the current number of messages in each group chat, s[i] store the number of messages received by the i-th person, and set[i] record the group chat where the i-th person is located. The idea of ​​difference: when a person joins a group chat, subtract the number of messages before the group chat, add the current number of messages in the group chat when exiting the group chat, and finally add the number of group chat messages that have not exited

Why didn't you think of it?

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;

int cnt[N], s[N];
set<int>st[N];

int main() {
    int n, m, q, x, y, op;
    scanf("%d%d%d", &n, &m, &q);
    for(int i = 1; i <= q; ++i) {
        scanf("%d%d%d", &op, &x, &y);
        if(op == 1) st[x].insert(y), s[x] -= cnt[y];
        else if(op == 2) st[x].erase(y), s[x] += cnt[y];
        else cnt[y]++, s[x]--;
    }
    for(int i = 1; i <= m; ++i) {
        for(set<int>::iterator it = st[i].begin(); it != st[i].end(); ++it)
            s[i] += cnt[*it];
        printf("%d\n", s[i]);
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_43871207/article/details/109355996