Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.math.*;
- public class Main {
- private BufferedReader in;
- private PrintWriter out;
- private StringTokenizer st;
- private Random rnd;
- class Pair {
- int v;
- short action;
- Pair(int v, short action) {
- this.v = v;
- this.action = action;
- }
- }
- int[] pluses = new int[1000010];
- int[] when = new int[200010];
- int[] v = new int[1000010];
- int vp = 0;
- ArrayList<Pair>[] actions = new ArrayList[200010];
- long[] result = new long[200010];
- final short plus = (short) 0;
- final short add = (short) 1;
- final short remove = (short) 2;
- public void solve() throws IOException {
- int n = nextInt(), m = nextInt();
- for(int i = 0; i < 200010; i++) actions[i] = new ArrayList<Pair>();
- for(int i = 0; i < m; i++) {
- char action = nextToken().charAt(0);
- if(action == '!') {
- int who = nextInt();
- actions[who].add(new Pair(-1, plus));
- } else if(action == '+') {
- int u = nextInt(), v = nextInt();
- actions[u].add(new Pair(v, add));
- actions[v].add(new Pair(u, add));
- } else {
- int u = nextInt(), v = nextInt();
- actions[u].add(new Pair(v, remove));
- actions[v].add(new Pair(u, remove));
- }
- }
- for(int i = 1; i <= n; i++) {
- int howMuch = actions[i].size();
- pluses[0] = 0;
- vp = 0;
- for(int j = 0; j < howMuch; j++) {
- if(j > 0) pluses[j] = pluses[j - 1];
- Pair curAction = actions[i].get(j);
- if(curAction.action == add) {
- when[curAction.v] = j;
- v[vp++] = curAction.v;
- } else if(curAction.action == remove) {
- result[curAction.v] += pluses[j] - pluses[when[curAction.v]];
- when[curAction.v] = -1;
- } else {
- pluses[j]++;
- }
- }
- for(int j = 0; j < vp; j++) {
- if(when[v[j]] == -1) continue;
- result[v[j]] += pluses[howMuch - 1] - pluses[when[v[j]]];
- when[v[j]] = -1;
- }
- }
- for(int i = 1; i <= n; i++) {
- out.print(result[i]);
- out.print(' ');
- }
- out.println();
- }
- public static void main(String[] args) {
- new Main().run();
- }
- public void run() {
- try {
- in = new BufferedReader(new FileReader("intouch.in"));
- out = new PrintWriter(new FileWriter("intouch.out"));
- //in = new BufferedReader(new InputStreamReader((System.in)));
- //out = new PrintWriter(System.out);
- st = null;
- rnd = new Random();
- solve();
- out.close();
- } catch(IOException e) {
- e.printStackTrace();
- }
- }
- private String nextToken() throws IOException, NullPointerException {
- while(st == null || !st.hasMoreTokens()) {
- st = new StringTokenizer(in.readLine());
- }
- return st.nextToken();
- }
- private int nextInt() throws IOException {
- return Integer.parseInt(nextToken());
- }
- private long nextLong() throws IOException {
- return Long.parseLong(nextToken());
- }
- private double nextDouble() throws IOException {
- return Double.parseDouble(nextToken());
- }
- }
Add Comment
Please, Sign In to add comment