Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.03 KB | None | 0 0
  1. #include <map>  
  2. #include <vector>  
  3. #include <cstdio>  
  4. #include <iostream>  
  5. #include <cstdlib>  
  6. #include <algorithm>  
  7. #include <string>  
  8. #include <cstring>  
  9. #include <time.h>  
  10. #include <set>  
  11. #include <cmath>  
  12. using namespace std;
  13.  
  14. #pragma comment (linker, "/STACK:136667216")
  15. struct node
  16. {
  17.     node* Left, *Right;
  18.     int val;
  19.     long long sum;
  20.     long long l, r;
  21.     node(long long x, long long y)
  22.     {
  23.         Left = NULL;
  24.         Right = NULL;
  25.         val = 0;
  26.         sum = 0;
  27.         l = x;
  28.         r = y;
  29.     }
  30. };
  31.  
  32. long long A, B;
  33. long long X;
  34. long long totalsum = 0;
  35.  
  36. void inc(node *i)
  37. {
  38.     if (i->r < A || i->l > B) return;
  39.     if (i->r <= B && i->l >= A)
  40.     {
  41.         if (X == -1) totalsum -= i->sum * i->sum, i->val = 0, i->sum = 0;
  42.         else totalsum -= i->sum * i->sum, i->val = 1, i->sum = X, totalsum += X * X;
  43.         return;
  44.     }
  45.     if (i->Left == NULL)
  46.         i->Left = new node(i->l, (i->l + i->r) / 2);
  47.     inc(i->Left);
  48.     if (i->Right == NULL)
  49.         i->Right = new node((i->l + i->r) / 2 + 1, i->r);
  50.     inc(i->Right);
  51.     i->val = i->Left->val + i->Right->val;
  52. }
  53.  
  54. int get(node* i)
  55. {
  56.     //cerr << i->l << " " << i->r << " " << i->val << endl;  
  57.     if (i->r < A || i->l > B) return 0;
  58.     if (i->r <= B && i->l >= A) { return i->val; }
  59.     if (i->Right == NULL)
  60.         return get(i->Left);
  61.     if (i->Left == NULL)
  62.         return get(i->Right);
  63.     return get(i->Left) + get(i->Right);
  64. }
  65.  
  66. long long k2 = (1LL << 30) - 1LL;
  67. node* root = new node(0, k2);
  68.  
  69. long long gather(int k)
  70. {
  71.     node* cur = root;
  72.     //We guarantee that k is not less than cur->size  
  73.     while (cur->l != cur->r)
  74.     {
  75.         if (cur->Left == NULL)
  76.         {
  77.             cur = cur->Right;
  78.             continue;
  79.         }
  80.         if (cur->Right == NULL)
  81.         {
  82.             cur = cur->Left;
  83.             continue;
  84.         }
  85.         if (cur->Left->val < k)
  86.             k -= cur->Left->val, cur = cur->Right;
  87.         else cur = cur->Left;
  88.     }
  89.     return cur->l;
  90. }
  91.  
  92. long long getsum(node* i)
  93. {
  94.     if ((i->r < A) || (i->l > B)) return 0;
  95.     if ((i->r <= B) && (i->l >= A))
  96.     {
  97.         return i->sum;
  98.     }
  99.     if (i->Right == NULL)
  100.         return getsum(i->Left);
  101.     if (i->Left == NULL)
  102.         return getsum(i->Right);
  103.     return getsum(i->Left) + getsum(i->Right);
  104. }
  105. int main()
  106. {
  107.     freopen("river.in", "r", stdin);
  108.     freopen("river.out", "w", stdout);
  109.     int n, p;
  110.     scanf("%d%d", &n, &p);
  111.     A = 0, B = 0;
  112.     for (int i = 0; i < n; i++)
  113.     {
  114.         cin >> X;
  115.         inc(root);
  116.         A += X;
  117.         B += X;
  118.     }
  119.     int k;
  120.     scanf("%d", &k);
  121.     printf("%I64d\n", totalsum);
  122.     int number = n;
  123.     while (k--)
  124.     {
  125.         int x, y;
  126.         scanf("%d%d", &x, &y);
  127.         long long pos = gather(y);
  128.         if (x == 1)
  129.         {
  130.             long long posl, posr;
  131.             if (y > 1)
  132.                 posl = gather(y - 1);
  133.             else
  134.                 posl = 0;
  135.             if (y < number)
  136.                 posr = gather(y + 1);
  137.             else
  138.                 posr = k2 - 1;
  139.             if (y == 1)
  140.             {
  141.                 A = posr, B = posr;
  142.                 X = -1;
  143.                 long long g = getsum(root);
  144.                 inc(root);
  145.                 A = pos, B = pos;
  146.                 X = getsum(root) + g;
  147.                 inc(root);
  148.                 printf("%I64d\n", totalsum);
  149.                 number--;
  150.                 continue;
  151.             }
  152.             if (posr == k2 - 1)
  153.             {
  154.                 A = pos, B = pos;
  155.                 X = -1;
  156.                 long long g = getsum(root);
  157.                 inc(root);
  158.                 A = posl, B = posl;
  159.                 X = getsum(root) + g;
  160.                 inc(root);
  161.                 printf("%I64d\n", totalsum);
  162.                 number--;
  163.                 continue;
  164.             }
  165.             //Оставшийся случай, когда рушится предприятие в центре  
  166.             {
  167.                 A = posr, B = posr;
  168.                 X = -1;
  169.                 long long gr = getsum(root);
  170.                 inc(root);
  171.                 A = pos, B = pos;
  172.                 long long gm = getsum(root);
  173.                 inc(root);
  174.                 A = posl, B = posl;
  175.                 long long gl = getsum(root);
  176.                 inc(root);
  177.                 X = gl + (gm / 2);
  178.                 inc(root);
  179.                 pos += gm / 2;
  180.                 X = gm - (gm / 2) + gr;
  181.                 A = pos, B = pos;
  182.                 inc(root);
  183.                 printf("%I64d\n", totalsum);
  184.                 number--;
  185.                 continue;
  186.             }
  187.         }
  188.         else
  189.         {
  190.             A = pos, B = pos;
  191.             long long gm = getsum(root);
  192.             X = gm / 2;
  193.             inc(root);
  194.             A += X;
  195.             B += X;
  196.             X = gm - X;
  197.             inc(root);
  198.             printf("%I64d\n", totalsum);
  199.             number++;
  200.             continue;
  201.         }
  202.     }
  203.     return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement