Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. struct user {
  7. int prev, rang, exp, diff;
  8. user(int v = 0) {
  9. prev = v;
  10. rang = 0;
  11. exp = 0;
  12. diff = 0;
  13. }
  14. };
  15.  
  16. int n, m;
  17. long long zerg = 0;
  18. const int p = 1000003;
  19.  
  20. vector<user> cur_vec;
  21.  
  22. int get(int x) {
  23. int t = cur_vec[x].prev;
  24. if (cur_vec[x].prev != x) {
  25. cur_vec[x].prev = get(cur_vec[x].prev);
  26. }
  27. cur_vec[x].diff += cur_vec[t].diff;
  28. return cur_vec[x].prev;
  29. }
  30.  
  31. void merge(int x, int y) {
  32. x = get(x);
  33. y = get(y);
  34. if (x == y) {
  35. return;
  36. }
  37. if (cur_vec[x].rang == cur_vec[y].rang) {
  38. cur_vec[x].rang++;
  39. }
  40. if (cur_vec[x].rang < cur_vec[y].rang) {
  41. cur_vec[x].prev = y;
  42. cur_vec[x].diff = cur_vec[x].exp - cur_vec[y].exp;
  43. }
  44. else {
  45. cur_vec[y].prev = x;
  46. cur_vec[y].diff = cur_vec[y].exp - cur_vec[x].exp;
  47. }
  48. }
  49.  
  50. int main() {
  51.  
  52. int n, m;
  53. int x, y;
  54. string s;
  55. cin >> n >> m;
  56.  
  57. cur_vec.resize(n);
  58.  
  59. for (int i = 0; i < n; i++) {
  60. cur_vec[i] = user(i);
  61. }
  62.  
  63. for (int i = 0; i < m; i++) {
  64. cin >> s;
  65. if (s[0] == 'j') {
  66. cin >> x >> y;
  67. merge(x - 1, y - 1);
  68. }
  69. if (s[0] == 'g') {
  70. cin >> x;
  71. cout << cur_vec[get(x - 1)].exp + cur_vec[x - 1].diff << endl;
  72. }
  73. if (s[0] == 'a') {
  74. cin >> x >> y;
  75. cur_vec[get(x - 1)].exp += y;
  76. }
  77. }
  78.  
  79. //system("pause");
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement