Guest User

Untitled

a guest
Jan 17th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.math.*;
  4.  
  5. public class Main {
  6. private BufferedReader in;
  7. private PrintWriter out;
  8. private StringTokenizer st;
  9. private Random rnd;
  10.  
  11. class Pair {
  12. int v;
  13. short action;
  14.  
  15. Pair(int v, short action) {
  16. this.v = v;
  17. this.action = action;
  18. }
  19. }
  20.  
  21. int[] pluses = new int[1000010];
  22. int[] when = new int[200010];
  23. int[] v = new int[1000010];
  24. int vp = 0;
  25. ArrayList<Pair>[] actions = new ArrayList[200010];
  26. long[] result = new long[200010];
  27.  
  28. final short plus = (short) 0;
  29. final short add = (short) 1;
  30. final short remove = (short) 2;
  31.  
  32. public void solve() throws IOException {
  33. int n = nextInt(), m = nextInt();
  34.  
  35. for(int i = 0; i < 200010; i++) actions[i] = new ArrayList<Pair>();
  36.  
  37. for(int i = 0; i < m; i++) {
  38. char action = nextToken().charAt(0);
  39.  
  40. if(action == '!') {
  41. int who = nextInt();
  42.  
  43. actions[who].add(new Pair(-1, plus));
  44. } else if(action == '+') {
  45. int u = nextInt(), v = nextInt();
  46.  
  47. actions[u].add(new Pair(v, add));
  48. actions[v].add(new Pair(u, add));
  49. } else {
  50. int u = nextInt(), v = nextInt();
  51.  
  52. actions[u].add(new Pair(v, remove));
  53. actions[v].add(new Pair(u, remove));
  54. }
  55. }
  56.  
  57.  
  58.  
  59. for(int i = 1; i <= n; i++) {
  60. int howMuch = actions[i].size();
  61. pluses[0] = 0;
  62. vp = 0;
  63.  
  64. for(int j = 0; j < howMuch; j++) {
  65. if(j > 0) pluses[j] = pluses[j - 1];
  66.  
  67. Pair curAction = actions[i].get(j);
  68.  
  69. if(curAction.action == add) {
  70. when[curAction.v] = j;
  71. v[vp++] = curAction.v;
  72. } else if(curAction.action == remove) {
  73. result[curAction.v] += pluses[j] - pluses[when[curAction.v]];
  74. when[curAction.v] = -1;
  75. } else {
  76. pluses[j]++;
  77. }
  78. }
  79.  
  80. for(int j = 0; j < vp; j++) {
  81. if(when[v[j]] == -1) continue;
  82. result[v[j]] += pluses[howMuch - 1] - pluses[when[v[j]]];
  83. when[v[j]] = -1;
  84. }
  85. }
  86.  
  87. for(int i = 1; i <= n; i++) {
  88. out.print(result[i]);
  89. out.print(' ');
  90. }
  91. out.println();
  92. }
  93.  
  94. public static void main(String[] args) {
  95. new Main().run();
  96. }
  97.  
  98. public void run() {
  99. try {
  100. in = new BufferedReader(new FileReader("intouch.in"));
  101. out = new PrintWriter(new FileWriter("intouch.out"));
  102.  
  103. //in = new BufferedReader(new InputStreamReader((System.in)));
  104. //out = new PrintWriter(System.out);
  105.  
  106. st = null;
  107. rnd = new Random();
  108.  
  109. solve();
  110.  
  111. out.close();
  112. } catch(IOException e) {
  113. e.printStackTrace();
  114. }
  115. }
  116.  
  117. private String nextToken() throws IOException, NullPointerException {
  118. while(st == null || !st.hasMoreTokens()) {
  119. st = new StringTokenizer(in.readLine());
  120. }
  121.  
  122. return st.nextToken();
  123. }
  124.  
  125. private int nextInt() throws IOException {
  126. return Integer.parseInt(nextToken());
  127. }
  128.  
  129. private long nextLong() throws IOException {
  130. return Long.parseLong(nextToken());
  131. }
  132.  
  133. private double nextDouble() throws IOException {
  134. return Double.parseDouble(nextToken());
  135. }
  136.  
  137. }
Add Comment
Please, Sign In to add comment