halfo

UVA 12003

Jul 29th, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.59 KB | None | 0 0
  1. //{ ************[       Template       ]************
  2. using namespace std;
  3. //{ ************[      C headers       ]************
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <cmath>
  7. #include <cstring>
  8. #include <climits>
  9. #include <cfloat>
  10. #include <cctype>
  11. #include <cassert>
  12. #include <ctime>
  13. //}
  14. //{ ************[     C++ headers      ]************
  15. #include <iostream>
  16. #include <iomanip>
  17. #include <sstream>
  18. #include <algorithm>
  19. #include <numeric>
  20. #include <utility>
  21. #include <string>
  22. #include <stack>
  23. #include <queue>
  24. #include <deque>
  25. #include <vector>
  26. #include <set>
  27. #include <map>
  28. #include <list>
  29. #include <complex>
  30. //{ ************[     Test Report 1    ]************
  31. // #include <tr1/regex>
  32. // #include <tr1/unordered_set>
  33. // #include <tr1/unordered_map>
  34. //}
  35. //}
  36. //{ ************[        Loops         ]************
  37. #define forab(i, a, b)  for (__typeof(b) i = (a); i <= (b); ++i)
  38. #define rep(i, n)       forab (i, 0, (n) - 1)
  39. #define For(i, n)       forab (i, 1, n)
  40. #define rofba(i, a, b)  for (__typeof(b) i = (b); i >= (a); --i)
  41. #define per(i, n)       rofba (i, 0, (n) - 1)
  42. #define rof(i, n)       rofba (i, 1, n)
  43. #define forstl(i, s)    for (__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i)
  44. //}
  45. //{ ************[   Floating points    ]************
  46. #define EPS             DBL_EPSILON
  47. #define abs(x)          (((x) < 0) ? - (x) : (x))
  48. #define zero(x)         (abs (x) < EPS)
  49. #define equal(a,b)      (zero ((a) - (b)))
  50. #define PI              2 * acos (0.0)
  51. //}
  52. //{ ************[        Macros        ]************
  53. #define mem(a, i)       memset ((a), i, sizeof ((a)))
  54. #define clr(a)          mem ((a), 0)
  55. #define all(a)          (a).begin (), (a).end ()
  56. #define pb              push_back
  57.  
  58. template <class T, class U> inline T max (T &a, U &b) { return a > b ? a : b; }
  59. template <class T, class U> inline T min (T &a, U &b) { return a < b ? a : b; }
  60. static struct _ { ios_base :: Init Init; _ () { cin.sync_with_stdio (false); cin.tie (false); } } _;
  61. //}
  62. //{ ************[  Typedefs && Consts  ]************
  63. typedef long long int64;
  64. typedef unsigned long long int64u;
  65.  
  66. const int MAX_N = int (4e5) + 7;
  67. const int MOD   = int (1e9) + 7;
  68. //}
  69. //}
  70.  
  71. struct node {
  72.     int key, rnk, cnt;
  73.     node *le, *ri;
  74.  
  75.     node (int key = 0, int rnk = rand ()) : key (key), rnk (rnk), cnt (1), le (0), ri (0) {}
  76. };
  77.  
  78. class treap {
  79.     typedef node* pNode;
  80.     pNode root;
  81.  
  82.     int count (pNode temp) { return temp ? temp -> cnt : 0; }
  83.     void updateCnt (pNode temp) { if (temp) temp -> cnt = 1 + count (temp -> le) + count (temp -> ri); }
  84.  
  85.     void split (pNode temp, pNode &le, pNode &ri, int key) {
  86.         if (!temp) { le = ri = 0; return; }
  87.         if (key < temp -> key ) split (temp -> le, le, temp -> le, key), ri = temp;
  88.         else split (temp -> ri, temp -> ri, ri, key), le = temp;
  89.         updateCnt (temp);
  90.     }
  91.  
  92.     void merge (pNode &temp, pNode le, pNode ri) {
  93.         if (!le || !ri) temp = le ? le : ri;
  94.         else if (le -> rnk > ri -> rnk) merge (le -> ri, le -> ri, ri), temp = le;
  95.         else merge (ri -> le, le, ri -> le), temp = ri;
  96.         updateCnt (temp);
  97.     }
  98.  
  99.     void insert (pNode &temp, pNode it) {
  100.         if (!temp) temp = it;
  101.         else if (it -> rnk > temp -> rnk) split (temp, it -> le, it -> ri, it -> key), temp = it;
  102.         else insert ((it -> key < temp -> key) ? temp -> le : temp -> ri, it);
  103.         updateCnt (temp);
  104.     }
  105.  
  106.     void erase (pNode &temp, int key, int rnk) {
  107.         if (!temp) return;
  108.         if (temp -> key == key) {
  109.             if (rnk == -1) rnk = temp -> rnk;
  110.             if (rnk == temp -> rnk) merge (temp, temp -> le, temp -> ri);
  111.             else erase (temp -> ri, key, rnk);
  112.         } else erase ((key < temp -> key) ? temp -> le : temp -> ri, key, rnk);
  113.         updateCnt (temp);
  114.     }
  115.  
  116.     int countLess (pNode temp, int key) {
  117.         if (!temp) return 0;
  118.         if (key <= temp -> key) return countLess (temp -> le, key);
  119.         return 1 + count (temp -> le) + countLess (temp -> ri, key);
  120.     }
  121.  
  122.     void clear (pNode cur) {
  123.         if (cur -> le) free (cur -> le);
  124.         if (cur -> ri) free (cur -> ri);
  125.         free (cur);
  126.     }
  127.  
  128. public:
  129.     treap () { root = 0; }
  130.     void insert (int key, int rnk = rand ()) { insert (root, new node (key, rnk)); }
  131.     void erase (int key, int rnk = -1) { erase (root, key, rnk); }
  132.     void clear () { clear (root); root = 0; }
  133.     int countLess (int key) { return countLess (root, key); }
  134. } treap [MAX_N];
  135.  
  136. int n, m, u, a[MAX_N];
  137.  
  138. void add (int x, int c) { for (int i = x; i <= n; i += i & (-i)) treap [i].insert (c); }
  139. void del (int x, int c) { for (int i = x; i <= n; i += i & (-i)) treap [i].erase  (c); }
  140. int  sum (int x, int v) { int y = 0; for (int i = x; i; i -= i & (-i)) y += treap [i].countLess (v); return y; }
  141.  
  142. inline int nextInt (){
  143.     int x = 0;
  144.     register int c = getc (stdin);
  145.     int sign = 0;
  146.     for (; ((c < 48 || c > 57) && c != '-'); c = getc (stdin));
  147.     if (c == '-') {
  148.         sign = 1;
  149.         c = getc (stdin);
  150.     }
  151.     for (; c > 47 && c < 58; c = getc (stdin)) x = (x << 1) + (x << 3) + c - 48;
  152.     if (sign) x = -x;
  153.     return x;
  154. }
  155.  
  156. int main() {
  157. #ifdef Local
  158.     freopen ("input.txt", "r", stdin);
  159.     // freopen ("output.txt", "w", stdout);
  160. #endif
  161.     while (scanf("%d %d %d",&n, &m, &u) != EOF) {
  162.         srand (time (0));
  163.         For (i, n) { scanf ("%d", a + i); add (i, a [i]); }
  164.         rep (i, m) {
  165.             int l = nextInt ();
  166.             int r = nextInt ();
  167.             int v = nextInt ();
  168.             int p = nextInt ();
  169.             int ans = sum (r, v) - sum (l - 1, v);
  170.             del (p, a [p]);
  171.             int64 x = u;
  172.             x *= (int64) ans;
  173.             x /= (r - l + 1);
  174.             a [p] = (int) x;
  175.             add (p, a [p]);
  176.         }
  177.  
  178.         For (i, n) printf ("%d\n", a [i]);
  179.         For (i, n) treap [i].clear ();
  180.     }
  181.     return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment