Advertisement
GerONSo

poor euler tour tree(near to work)

Nov 6th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 KB | None | 0 0
  1. /*
  2.  
  3. ∧_∧
  4. ( ・ω・。)つ━☆・*。
  5. ⊂  ノ    ・゜
  6. しーJ   Accepted
  7.  
  8. */
  9.  
  10.  
  11.  
  12. // #pragma GCC optimize("O3")
  13. // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  14.  
  15. #include <bits/stdc++.h>
  16. #include <ext/pb_ds/assoc_container.hpp>
  17. #include <ext/pb_ds/tree_policy.hpp>
  18.  
  19. #define ll long long
  20. #define all(x) begin(x), end(x)
  21. #define x first
  22. #define y second
  23. #define int long long
  24.  
  25. using namespace std;
  26. using namespace __gnu_pbds;
  27.  
  28. typedef long double ld;
  29. template<typename T>
  30. using kawaii_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  31.  
  32. const ld PI = atan2(0, -1);
  33.  
  34. void seriy() {
  35. ios::sync_with_stdio(0);
  36. cin.tie(0);
  37. cout.tie(0);
  38. cout << fixed << setprecision(14);
  39. #ifdef _offline
  40. freopen("input.txt", "r", stdin);
  41. freopen("output.txt", "w", stdout);
  42. #else
  43. freopen("river.in", "r", stdin);
  44. freopen("river.out", "w", stdout);
  45. #endif
  46. }
  47.  
  48. const int MAXN = 1e6 + 10;
  49. const int MAXM = 600;
  50. const int INF = 1e18;
  51. const int BASE = 47;
  52. const int MOD = 1e9 + 7;
  53. const int MAXLOG = 21;
  54.  
  55. mt19937 rr(time(0));
  56.  
  57. struct treap {
  58. treap *left = nullptr;
  59. treap *right = nullptr;
  60. treap *parent = nullptr;
  61. int prior, sz, val;
  62. bool reversed = 0, cycle = 0, is_right = 0;
  63.  
  64. treap(int val) {
  65. this->val = val;
  66. this->prior = rr();
  67. this->sz = 1;
  68. }
  69. };
  70.  
  71. vector<vector<int>> g;
  72. vector<treap*> links;
  73.  
  74.  
  75. int get_size(treap *&root) {
  76. return ((root == nullptr) ? 0 : root->sz);
  77. }
  78.  
  79. int get_val(treap *&root) {
  80. return ((root == nullptr) ? 0 : root->val);
  81. }
  82.  
  83. void print(treap *a) {
  84. if(a->left != nullptr) print(a->left);
  85. cerr << get_val(a) << " ";
  86. if(a->right != nullptr) print(a->right);
  87. }
  88.  
  89. void update(treap *&root) {
  90. if(root == nullptr) return;
  91. root->sz = get_size(root->left) + get_size(root->right) + 1;
  92. if(root->left != nullptr) {
  93. root->left->parent = root;
  94. root->left->is_right = 0;
  95. }
  96. if(root->right != nullptr) {
  97. root->right->parent = root;
  98. root->right->is_right = 1;
  99. }
  100. }
  101.  
  102. void push(treap *&root) {
  103. if(root == nullptr || root->reversed == 0) {
  104. return;
  105. }
  106. if(root->left != nullptr) {
  107. root->left->reversed ^= true;
  108. }
  109. if(root->right != nullptr) {
  110. root->right->reversed ^= true;
  111. }
  112. swap(root->left, root->right);
  113. root->reversed = 0;
  114. }
  115.  
  116. pair<treap*, treap*> split(treap *&root, int key) {
  117. push(root);
  118. if(root == nullptr) {
  119. return {nullptr, nullptr};
  120. }
  121. int root_key = get_size(root->left);
  122. // cerr << root->sz << " " << root_key << '\n';
  123. if(key > root_key) {
  124. pair<treap*, treap*> splitted = split(root->right, key - get_size(root->left) - 1);
  125. root->right = splitted.x;
  126. update(root);
  127. return {root, splitted.y};
  128. }
  129. else {
  130. pair<treap*, treap*> splitted = split(root->left, key);
  131. root->left = splitted.y;
  132. update(root);
  133. return {splitted.x, root};
  134. }
  135. }
  136.  
  137. treap* merge(treap *&left, treap *&right) {
  138. push(left);
  139. push(right);
  140. if(left == nullptr) {
  141. return right;
  142. }
  143. if(right == nullptr) {
  144. return left;
  145. }
  146. if(left->prior > right->prior) {
  147. left->right = merge(left->right, right);
  148. update(left);
  149. return left;
  150. }
  151. else {
  152. right->left = merge(left, right->left);
  153. update(right);
  154. return right;
  155. }
  156. }
  157.  
  158. int get(treap *&root, int pos) {
  159. if(pos < get_size(root->left)) {
  160. return get(root->left, pos);
  161. }
  162. else if(pos > get_size(root->left)) {
  163. return get(root->right, pos - get_size(root->left) - 1);
  164. }
  165. else {
  166. return get_val(root);
  167. }
  168. }
  169.  
  170. void reverse(treap *&root, int l, int r) {
  171. pair<treap*, treap*> a = split(root, r);
  172. pair<treap*, treap*> b = split(a.x, l);
  173. b.y->reversed = 1;
  174. b.x = merge(b.x, b.y);
  175. root = merge(b.x, a.y);
  176. }
  177.  
  178. void insert(treap *&root, int pos, int val) {
  179. pair<treap*, treap*> splitted = split(root, pos);
  180. treap *nw = new treap(val);
  181. splitted.x = merge(splitted.x, nw);
  182. update(splitted.x);
  183. splitted.x = merge(splitted.x, splitted.y);
  184. update(splitted.x);
  185. root = splitted.x;
  186. }
  187.  
  188. vector<vector<int>> bamboos, cycles;
  189. vector<bool> used;
  190. vector<int> cur;
  191. bool is_cyc = 0;
  192.  
  193. void dfs(int u) {
  194. used[u] = 1;
  195. cur.push_back(u);
  196. for(auto v : g[u]) {
  197. if(!used[v]) {
  198. dfs(v);
  199. }
  200. else {
  201. is_cyc = 1;
  202. }
  203. }
  204. }
  205.  
  206. treap* get_root(treap *&a) {
  207. if(a->parent == nullptr) {
  208. return a;
  209. }
  210. return get_root(a->parent);
  211. }
  212.  
  213. int get_pos(treap *&a, bool from_right) {
  214. if(a == nullptr) {
  215. return 0;
  216. }
  217. // cerr << a->val << " ";
  218. int ans = 0;
  219. if(from_right) {
  220. ans += get_size(a->left);
  221. }
  222. return ans + get_pos(a->parent, a->is_right);
  223. }
  224.  
  225. signed main() {
  226. seriy();
  227. int n, m, q;
  228. cin >> n >> m >> q;
  229. links.resize(n);
  230. g.resize(n);
  231. used.resize(n);
  232. for(int i = 0; i < m; i++) {
  233. int u, v;
  234. cin >> u >> v;
  235. u--;
  236. v--;
  237. g[u].push_back(v);
  238. g[v].push_back(u);
  239. }
  240. for(int i = 0; i < n; i++) {
  241. if(!used[i] && g[i].size() <= 1) {
  242. cur.clear();
  243. dfs(i);
  244. bamboos.push_back(cur);
  245. }
  246. }
  247. for(int i = 0; i < n; i++) {
  248. if(!used[i]) {
  249. cur.clear();
  250. dfs(i);
  251. cycles.push_back(cur);
  252. }
  253. }
  254. for(int i = 0; i < n; i++) {
  255. links[i] = new treap(i);
  256. }
  257. for(int i = 0; i < bamboos.size(); i++) {
  258. treap *start = links[bamboos[i][0]];
  259. for(int j = 1; j < bamboos[i].size(); j++) {
  260. int cur = bamboos[i][j];
  261. insert(start, j, cur);
  262. }
  263. }
  264. for(int i = 0; i < cycles.size(); i++) {
  265. for(int j = 1; j < cycles[i].size(); j++) {
  266. int start = cycles[i][0];
  267. int cur = cycles[i][j];
  268. treap *parent = get_root(links[start]);
  269. links[start] = merge(parent, links[cur]);
  270. cerr << cur << " " << links[cur]->val << '\n';
  271. }
  272. treap *root = get_root(links[cycles[i][0]]);
  273. root->cycle = 1;
  274. }
  275. cerr << '\n';
  276. while(q--) {
  277. char c;
  278. cin >> c;
  279. if(c == '?') {
  280. int a, b;
  281. cin >> a >> b;
  282. a--;
  283. b--;
  284. if(get_root(links[a]) == get_root(links[b])) {
  285. int p1 = get_pos(links[a], 1);
  286. cerr << a << " " << links[a]->val << '\n';
  287. // cerr << '\n';
  288. int p2 = get_pos(links[b], 1);
  289. // cerr << '\n';
  290. // cerr << p1 << " " << p2 << '\n';
  291. }
  292. else {
  293. cout << -1 << '\n';
  294. }
  295. }
  296. else if(c == '+') {
  297.  
  298. }
  299. else if(c == '-') {
  300.  
  301. }
  302. }
  303. return 0;
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement