Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. const int q = 1000000000;
  9.          
  10. struct tree {
  11.   int x,y;
  12.   int v[11],s[11];
  13.   tree *l,*r;
  14. };
  15.  
  16. typedef tree * link;
  17.  
  18. tree data[100000];
  19. int cnt = 0;
  20. link root;
  21.  
  22. void print(tree *root)
  23. {
  24.   if (root == NULL)
  25.   {
  26.     printf("NULL\n");
  27.     return;
  28.   }
  29.   printf("X %d Y %d\n",root->x,root->y);
  30.   for (int i = 1; i < 11;i++)
  31.     printf("V[%d] = %d ",i,root->v[i]);
  32.   printf("\n");
  33.   for (int i = 1; i < 11;i++)
  34.     printf("S[%d] = %d ",i,root->s[i]);
  35.   printf("\n");
  36.   printf("%d's LEFT\n",root->x);
  37.   print(root->l);
  38.   printf("%d's RIGHT\n",root->x);
  39.   print(root->r);
  40.   printf("X %d Y %d END \n",root->x,root->y);
  41. }
  42.  
  43. int sum(link root,int k)
  44. {
  45.   return !root ? 0 : root->s[k];
  46. }
  47. void update(link root)
  48. {
  49.   if (!root)
  50.     return;
  51.   for (int i = 1; i < 11;i++)
  52.     root->s[i] = ((sum(root->l,i) + sum(root->r,i)) %q + root->v[i]) % q;
  53. }
  54. void split(link root, link &l, link &r, int x)
  55. {
  56.   if (!root)
  57.     l = r = NULL;
  58.   else if (root->x > x)
  59.   {
  60.     split(root->l,l,root->l,x);
  61.     r = root;
  62.   }
  63.   else
  64.   {
  65.     split(root->r,root->r,r,x);
  66.     l = root;
  67.   }
  68.   update(l);
  69.   update(r);
  70. }
  71. void merge(link &root, link l, link r)
  72. {
  73.   if (!l || !r)
  74.     root = l ? l : r;
  75.   else if (l->y < r->y)
  76.   {
  77.     merge(l->r,l->r,r);
  78.     root = l;
  79.   }
  80.   else
  81.   {
  82.     merge(r->l,l,r->l);
  83.     root = r;
  84.   }
  85.   update(root);
  86. }
  87. link make(int x,int y )
  88. {
  89.   link a;
  90. //  a = new tree;
  91.   a = &data[cnt++];
  92.   a->x = x;
  93.   a->l = a->r = NULL;
  94.   a->y = y;
  95.   a->v[1] = a->s[1] = 1;
  96.   for (int i = 2;i < 11;i++)
  97.     a->v[i] = a->s[i] = 0;
  98.   return a;
  99. }
  100. void add(link &root, link a  )
  101. {
  102.   if (root == NULL)
  103.     root = a;
  104.   else if (root->y > a->y)
  105.   {
  106.     link l,r;
  107.     split(root,l,r,a->x);
  108.     a->l = l;
  109.     a->r = r;
  110.     root = a;
  111.   }
  112.   else if (root->x > a->x)
  113.     add(root->l,a);
  114.   else
  115.     add(root->r,a);
  116.   update(root);
  117. }
  118. void tadd(int x)
  119. {
  120.  
  121.   link a = make(x,rand() << 16 + rand());
  122.   link l,r;
  123.   split(root,l,r,x);
  124.   for (int i = 2; i < 11;i++)
  125.     a->v[i] = a->s[i] = (a->v[i] + sum(r,i - 1)) % q;
  126.   //merge(l,l,a);
  127.   merge(root,l,r);
  128.   add(root,a);    
  129. }
  130. int height(link root,int h)
  131. {
  132.   if (!root)
  133.     return h;
  134.   else
  135.     return max(h,max(height(root->l,h + 1),height(root->r,h + 1)));
  136. }
  137. int main()
  138. {
  139.   #ifndef ONLINE_JUDGE
  140.     freopen("input.txt","rt",stdin);
  141.     freopen("output.txt","wt",stdout);
  142.   #endif
  143.   srand(time(NULL));
  144.   root = NULL;
  145.   int n,k;            
  146.   scanf("%d %d",&n,&k);
  147.   for (int i = 0;i < n  ;i++)
  148.   {
  149.  
  150.     int a;
  151.     scanf("%d",&a);
  152.     tadd(a);
  153.  
  154.   }
  155.   printf("%d\n",sum(root,k));
  156.   return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement