Advertisement
jeff69

Untitled

Oct 7th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.90 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define sc(a) scanf("%d",&a)
  3. #define scc(a) scanf(" %c",&a)
  4. #define scs(a) scanf(" %s",a)
  5. #define scll(a) scanf("%lld",&a)
  6. using namespace std;
  7. const int MX = 1e5+6;
  8. typedef long long ll;
  9. typedef unsigned long long ull;
  10. typedef pair<string,int> crap;
  11. int n,ty[MX],Mm[MX],findN[MX];
  12. string codes[MX],codesforid[MX];
  13. char bb[33];
  14. string getnigger()
  15. {
  16.     scanf("%s",bb);
  17.     return (string)bb;
  18. }
  19. map<crap,int> vis;
  20. map<string,int> Ids;
  21. int getId(string& h)
  22. {
  23.     if(Ids.count(h))
  24.         return Ids[h];
  25.     return -1;
  26. }
  27. bool deletecode(string & h)
  28. {
  29.     if(!Ids.count(h))return false;
  30.     vis.erase({h,Ids[h]});
  31.     Ids.erase(h);
  32.     return true;
  33. }
  34. map<pair<int,pair<int,string>>,int> index;
  35. int getindex(int x,int id,string& h)
  36. {
  37.     if(index.count({x,{id,h}}))
  38.     {
  39.        return index[{x,{id,h}}];
  40.     }
  41.     return -1;
  42. }
  43. vector<pair<int,pair<int,string>>> hashy;
  44. int tree[4*MX];
  45. int pos,val=-1;
  46. void insert(int x=1,int l=0,int r=(int)hashy.size()-1)
  47. {
  48.     if(l>pos||r<pos)return;
  49.     int mid = (l+r)/2;
  50.     if(l==r)
  51.     {
  52.         assert(val!=-1);
  53.         tree[x] = val;
  54.         return;
  55.     }
  56.     insert(x*2,l,mid);
  57.     insert(x*2+1,mid+1,r);
  58.     tree[x] = tree[x*2] + tree[x*2+1];
  59. }
  60. int K;
  61. int search(int x=1,int l=0,int r=(int)hashy.size()-1)
  62. {
  63.     int mid = (l+r)/2;
  64.     if(l==r)
  65.         return r;
  66.     if(tree[x*2]>=K)
  67.         return search(2*x,l,mid);
  68.     K-=tree[x*2];
  69.     return search(2*x+1,mid+1,r);
  70. }
  71. int main()
  72. {
  73.  
  74.     cin>>n;
  75.     int lastid = 0;
  76.     for(int i=1; i<=n; i++)
  77.     {
  78.         scanf("%s",bb);
  79.         string h = bb;
  80.         if(h=="ISSUE")ty[i]=1;
  81.         else if (h=="DELETE")ty[i]=2;
  82.         else if (h=="RELIABILITY")ty[i] =3;
  83.         else ty[i] = 4;
  84.         if(ty[i]!=4)
  85.         {
  86.             codes[i] = getnigger();
  87.         }
  88.         else
  89.         {
  90.             int amount;
  91.             scanf("%d",&amount);
  92.             findN[i] =amount;
  93.         }
  94.         if(ty[i]==3)
  95.         {
  96.             int amount;
  97.             scanf("%d",&amount);
  98.             Mm[i] =amount;
  99.         }
  100.         if(ty[i]==1)
  101.         {
  102.             if(getId(codes[i])!=-1)
  103.                 continue;
  104.             Ids[codes[i]]=lastid;
  105.             vis[{codes[i],lastid}] = 0;
  106.             hashy.push_back({0,{lastid,codes[i]}});
  107.             lastid++;
  108.         }
  109.         if(ty[i]==2)
  110.         {
  111.             deletecode(codes[i]);
  112.         }
  113.         if(ty[i]==3)
  114.         {
  115.             int id = getId(codes[i]);
  116.             if(id==-1)continue;
  117.             vis[{codes[i],id}]+=Mm[i];
  118.             hashy.push_back({-vis[{codes[i],id}],{id,codes[i]}});
  119.         }
  120.     }
  121.     sort(hashy.begin(),hashy.end());
  122.     for(int i=0;i<hashy.size();i++)
  123.     {
  124.         index[hashy[i]]=i;
  125.     }
  126.     vis.clear();
  127.     Ids.clear();
  128.     lastid =0;
  129.     for(int i=1;i<=n;i++)
  130.     {
  131.         if(ty[i]==1)
  132.         {
  133.             int Id = getId(codes[i]);
  134.             if(Id!=-1)
  135.             {
  136.                 printf("EXISTS %d %d\n",Id,vis[{codesforid[Id],Id}]);
  137.             continue;
  138.             }
  139.             else
  140.                 printf("CREATED %d 0\n",lastid);
  141.             Ids[codes[i]]=lastid;
  142.             codesforid[lastid]=codes[i];
  143.             vis[ {codes[i],lastid}] = 0;
  144.             pos = index[{0,{lastid,codesforid[lastid]}}];
  145.             val = 1;
  146.             insert();
  147.             lastid++;
  148.         }
  149.         if(ty[i]==2)
  150.         {
  151.             bool exist = Ids.count(codes[i]);
  152.             int id =-1;
  153.             if(exist)
  154.                 id= Ids[codes[i]];
  155.             if(exist)
  156.                 printf("OK %d %d\n",id,vis[{codesforid[id],id}]);
  157.             else
  158.                 printf("BAD REQUEST\n");
  159.             if(exist){
  160.  
  161.             pos = index[{-vis[{codesforid[id],id}],{id,codesforid[id]}}];
  162.             val = 0;
  163.             insert();
  164.             }
  165.             deletecode(codes[i]);
  166.         }
  167.         if(ty[i]==3)
  168.         {
  169.             int id = getId(codes[i]);
  170.             if(id==-1){
  171.                 printf("BAD REQUEST\n");
  172.                 continue;
  173.             }
  174.             pos = index[{-vis[{codes[i],id}],{id,codes[i]}}];
  175.             val = 0;
  176.             insert();
  177.             vis[{codes[i],id}]+=Mm[i];
  178.              pos = index[{-vis[{codes[i],id}],{id,codes[i]}}];
  179.             val = 1;
  180.             insert();
  181.             printf("OK %d %d\n",id,vis[{codes[i],id}]);
  182.         }
  183.         if(ty[i]==4)
  184.         {
  185.             if(tree[1]==0)
  186.             {
  187.                 printf("EMPTY\n");
  188.                 continue;
  189.             }
  190.             int kk;
  191.             K = min(tree[1],1+findN[i]);
  192.             kk=K;
  193.             int id = search();
  194.             id = hashy[id].second.first;
  195.             memset(bb,0,sizeof bb);
  196.             for(int j =0;j<codesforid[id].size();j++)
  197.                 bb[j]=codesforid[id][j];
  198.             printf("OK %s %d %d\n",bb,id,vis[{codesforid[id],id}]);
  199.         }
  200.  
  201.     }
  202.     return 0;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement