Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

用SBT暴做的

Posted by lz1 at 2012-02-26 21:24:33 on Problem 2892
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;

#define MAX_N 51000
#define MAX_M 101000

typedef int mint[MAX_M];

class SBT
{
      public :
      mint key, l, r, s;
      int tot, rt, ts;
      void lr(int &x)
      {
           int y = r[x];
           r[x] = l[y], l[y] = x;
           s[y] = s[x], s[x] = s[l[x]] + s[r[x]] + 1;
           x = y;
      }
      void rr(int &x)
      {
           int y = l[x];
           l[x] = r[y], r[y] = x;
           s[y] = s[x], s[x] = s[l[x]] + s[r[x]] + 1;
           x = y;
      }
      void maintain(int &x, bool w)
      {
           if (!w)
           {
              if (s[l[l[x]]] > s[r[x]]) rr(x);
              else if (s[r[l[x]]] > s[r[x]]) lr(l[x]), rr(x);
              else return ;
           }
           else
           {
               if (s[r[r[x]]] > s[l[x]]) lr(x);
               else if (s[l[r[x]]] > s[l[x]]) rr(r[x]), lr(x);
               else return ;
           }
           maintain (l[x], false);
           maintain (r[x], true);
           maintain (x, false);
           maintain (x, true);
      }
      void insert(int &x, int w)
      {
           if (!x)
           {
              x = ++tot, l[x] = r[x] = 0;
              key[x] = w, s[x] = 1;
              return ;
           }
           s[x]++;
           if (w < key[x]) insert (l[x], w);
           else insert (r[x], w);
           maintain (x, w >= key[x]);
      }
      int del(int &x, int w)
      {
          s[x]--;
          if (w == key[x] || (w < key[x] && !l[x]) || (w > key[x] && !r[x]))
          {
             int t = key[x];
             if (!l[x] || !r[x]) x = l[x] + r[x];
             else key[x] = del (l[x], t + 1);
             return t;
          }
          if (w < key[x]) return del (l[x], w);
          else return del (r[x], w);
      }
      void findmin(int x, int w)
      {
           if (w >= key[x])
           {
              ts = key[x];
              if (key[x] != w && r[x]) findmin (r[x], w);
           }
           else if (l[x]) findmin (l[x], w);
          
      }
      void findmax(int x, int w)
      {
           if (w <= key[x])
           {
              ts = key[x];
              if (key[x] != w && l[x]) findmax (l[x], w);
           }
           else if (r[x]) findmax (r[x], w);
          
      }
      int findmin(int w) { findmin (rt, w); return ts; }
      int findmax(int w) { findmax (rt, w); return ts; }
      void insert(int w) { insert (rt, w); }
      void del(int w) { del (rt, w); }
};

SBT p;
int st[MAX_N];
char str[10];
int n, m, top;

int main(void)
{
    scanf ("%d%d", &n, &m);
    p.insert (0), p.insert (n + 1);
    for (int i = 1, x, s; i <= m; i++)
    {
        scanf ("%s", str);
        int t1, t2;
        switch (str[0])
        {
               case 'D':
               scanf ("%d", &x), p.insert(x), st[++top] = x; break;
               case 'Q':
               scanf ("%d", &x);
               s = p.findmax (x) - p.findmin (x) - 1; 
               printf ("%d\n", max (s, 0));
               break;
               case 'R':
               p.del (st[top--]);
               break;
        }
    }
    return 0;
}



Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator