Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cassert>
  4. #include <cstdio>
  5. #include <algorithm>
  6. #include <set>
  7. #include <math.h>
  8. #include <string>
  9. #include <map>
  10. #include <unordered_map>
  11. #include <unordered_set>
  12. #include <deque>
  13. #include <iomanip>
  14. #pragma GCC optimize(-Ofast)
  15.  
  16. #define pr pair
  17. #define mp make_pair
  18. #define ft first
  19. #define sd second
  20. #define ll long long
  21. #define ld long double
  22.  
  23. using namespace std;
  24.  
  25. #ifdef FAST_ALLOCATOR_MEMORY
  26. int allocator_pos = 0;
  27. char allocator_memory[(int)FAST_ALLOCATOR_MEMORY];
  28. inline void* operator new (size_t n) {
  29. char* res = allocator_memory + allocator_pos;
  30. allocator_pos += n;
  31. assert(allocator_pos <= (int)FAST_ALLOCATOR_MEMORY);
  32. return (void*)res;
  33. }
  34. inline void operator delete (void*) noexcept { }
  35. //inline void * operator new [] ( size_t ) { assert(0); }
  36. //inline void operator delete [] ( void * ) { assert(0); }
  37. #endif
  38.  
  39. inline int readUInt();
  40. inline int readChar();
  41. inline bool isEof();
  42. inline int getChar();
  43.  
  44. static const int buf_size = 4096;
  45. static unsigned char buf[buf_size];
  46. static int buf_len = 0, buf_pos = 0;
  47.  
  48. inline bool isEof() {
  49. if (buf_pos == buf_len) {
  50. buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
  51. if (buf_pos == buf_len)
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. inline int getChar() {
  57. return isEof() ? -1 : buf[buf_pos++];
  58. }
  59. inline int readChar() {
  60. int c = getChar();
  61. while (c != -1 && c <= 32)
  62. c = getChar();
  63. return c;
  64. }
  65. inline int readUInt() {
  66. int c = readChar(), x = 0;
  67. while ('0' <= c && c <= '9')
  68. x = x * 10 + c - '0', c = getChar();
  69. return x;
  70. }
  71.  
  72.  
  73. inline int superRand() {
  74. if (RAND_MAX == (1 << 15) - 1) {
  75. return (rand() << 15) + rand();
  76. }
  77. return rand();
  78. }
  79.  
  80. struct Node {
  81. int key, prior, sz;
  82. ll sum;
  83. Node* l, * r, * p;
  84. inline Node() {};
  85. inline Node(int key_) :
  86. l(nullptr),
  87. r(nullptr),
  88. prior(superRand()),
  89. sz(1),
  90. sum(key),
  91. key(key_)
  92. {};
  93. };
  94.  
  95. typedef Node* Pnode;
  96.  
  97.  
  98. inline int get_size(Pnode root) {
  99. if (root != nullptr)
  100. return root->sz;
  101. return 0;
  102. }
  103. inline ll get_sum(Pnode root) {
  104. if (root != nullptr)
  105. return root->sum;
  106. else
  107. return 0;
  108. }
  109. inline void update(Pnode root) {
  110. if (root != nullptr) {
  111. root->sz = get_size(root->l) + get_size(root->r) + 1;
  112. root->sum = get_sum(root->l) + get_sum(root->r) + root->key;
  113. }
  114. }
  115. void forceupdate(Pnode root) {
  116. if (root->l != nullptr)
  117. forceupdate(root->l);
  118. if (root->r != nullptr)
  119. forceupdate(root->r);
  120. update(root);
  121. }
  122. void splitbykey(Pnode root, Pnode& l, Pnode& r, int x) {
  123. if (root == nullptr)
  124. return void(l = r = nullptr);
  125. if (root->key < x) {
  126. splitbykey(root->r, root->r, r, x);
  127. l = root;
  128. }
  129. else {
  130. splitbykey(root->l, l, root->l, x);
  131. r = root;
  132. }
  133. update(r);
  134. update(l);
  135. }
  136. void splitbysz(Pnode root, Pnode& l, Pnode& r, int k) {
  137. if (root == nullptr)
  138. return void(l = r = 0);
  139. if (get_size(root->l) >= k) {
  140. splitbysz(root->l, l, root->l, k);
  141. r = root;
  142. }
  143. else {
  144. splitbysz(root->r, root->r, r, k - get_size(root->l) - 1);
  145. l = root;
  146. }
  147. update(root);
  148. }
  149. void mymerge(Pnode& root, Pnode l, Pnode r) {
  150. if (!l || !r)
  151. return void(root = l ? l : r);
  152. if (l->prior > r->prior) {
  153. mymerge(l->r, l->r, r);
  154. root = l;
  155. }
  156. else {
  157. mymerge(r->l, l, r->l);
  158. root = r;
  159. }
  160. update(root);
  161. }
  162. void insert(Pnode& root, Pnode ins) {
  163. if (root == nullptr)
  164. root = ins;
  165. else if (root->prior < ins->prior) {
  166. splitbykey(root, ins->l, ins->r, ins->key);
  167. root = ins;
  168. }
  169. else if (root->key < ins->key)
  170. insert(root->r, ins);
  171. else
  172. insert(root->l, ins);
  173. update(root);
  174. }
  175. void erase(Pnode& root, int x) {
  176. if (root->l != nullptr && root->key > x)
  177. erase(root->l, x);
  178. else if (root->r != nullptr && root->key < x)
  179. erase(root->r, x);
  180. else
  181. mymerge(root, root->l, root->r);
  182. update(root);
  183. }
  184.  
  185. int a[100000];
  186. Pnode trees[400228];
  187.  
  188. vector<int> build1(int n, int l, int r) {
  189. vector<int> nodes;
  190. if (r - l == 1)
  191. nodes = { a[l] };
  192. else {
  193. int m = (l + r) / 2;
  194. vector<int> nodes1 = build1(2 * n + 1, l, m);
  195. vector<int> nodes2 = build1(2 * n + 2, m, r);
  196. nodes.resize(r - l);
  197. merge(nodes1.begin(), nodes1.end(), nodes2.begin(), nodes2.end(), nodes.begin());
  198. }
  199. trees[n] = new Node(0);
  200. trees[n]->prior = INT32_MAX;
  201. Pnode cur = trees[n];
  202. for (int i = 0; i < r - l; ++i) {
  203. Pnode newnode = new Node(nodes[i]);
  204. while (cur->prior < newnode->prior)
  205. cur = cur->p;
  206. newnode->l = cur->r;
  207. if (cur->r != nullptr)
  208. cur->r->p = newnode;
  209. cur->r = newnode;
  210. newnode->p = cur;
  211. cur = newnode;
  212. }
  213. forceupdate(trees[n]);
  214. return nodes;
  215. }
  216.  
  217. int pos, oldval, newval;
  218.  
  219. void change(int n, int l, int r) {
  220. erase(trees[n], oldval);
  221. insert(trees[n], new Node(newval));
  222. if (r - l != 1) {
  223. int m = (l + r) / 2;
  224. if (m <= pos)
  225. change(2 * n + 2, m, r);
  226. else
  227. change(2 * n + 1, l, m);
  228. }
  229. }
  230.  
  231. int l1, r1, a1, b1;
  232.  
  233. pair<ll, int> ask(int n, int l, int r) {
  234. if (l >= r1 || r <= l1)
  235. return mp(0, 0);
  236. if (l >= l1 && r <= r1) {
  237. pr<ll, int> ans;
  238. Pnode L, M, R;
  239. splitbykey(trees[n], L, M, a1);
  240. splitbykey(M, M, R, b1 + 1);
  241. ans.first = get_sum(M);
  242. ans.second = get_size(M);
  243. mymerge(M, M, R);
  244. mymerge(trees[n], L, M);
  245. return ans;
  246. }
  247. else {
  248. int m = (l + r) / 2;
  249. pr<ll, int> ans1 = ask(2 * n + 1, l, m), ans2 = ask(2 * n + 2, m, r);
  250. return mp(ans1.ft + ans2.ft, ans1.sd + ans2.sd);
  251. }
  252. }
  253.  
  254. int main()
  255. {
  256. int n;
  257. //n = readUInt();
  258. cin >> n;
  259. for (int i = 0; i < n; i++) {
  260. //a[i] = readUInt();
  261. cin >> a[i];
  262. }
  263. build1(0, 0, n);
  264. int q;
  265. //q = readUInt();
  266. cin >> q;
  267. cout << setprecision(20);
  268. short type;
  269. for (int i = 0; i < q; ++i) {
  270. cin >> type;
  271. //type = readUInt();
  272. if (type == 1) {
  273. /*l1 = readUInt();
  274. r1 = readUInt();
  275. a1 = readUInt();
  276. b1 = readUInt();*/
  277. cin >> l1 >> r1 >> a1 >> b1;
  278. --l1;
  279. auto ans = ask(0, 0, n);
  280. if (ans.second == 0)
  281. cout << 0 << ' ';
  282. else
  283. cout << (ld)ans.first / (ld)ans.second << ' ';
  284. }
  285. else {
  286. /*l1 = readUInt();
  287. r1 = readUInt();*/
  288. cin >> l1 >> r1;
  289. if (a[l1 - 1] != a[r1 - 1]) {
  290. pos = l1 - 1, oldval = a[l1 - 1], newval = a[r1 - 1];
  291. change(0, 0, n);
  292. pos = r1 - 1, oldval = a[r1 - 1], newval = a[l1 - 1];
  293. change(0, 0, n);
  294. swap(a[l1 - 1], a[r1 - 1]);
  295. }
  296. }
  297. }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement