Advertisement
Guest User

Untitled

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