답안 #500244

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
500244 2021-12-30T14:01:24 Z Mounir Klasika (COCI20_klasika) C++14
33 / 110
1174 ms 524292 KB
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define pb push_back
#define pii pair<int, int>
#define chmin(x, v) x = min(x, v)
#define chmax(x, v) x = max(x, v)
#define x first
#define y second
//#define int long long
using namespace std;

const int OO = 1e9, N = 3e5, T = 30;
bitset<T> AXOR = (1 << 30) - 1;

//Plus petit dans le plus grand

struct Trie {
      vector<vector<int>> arbre;
      vector<int> dateMin, dateDico;
      vector<bitset<T>> dico;

      void init(){
            for (int i = 0; i < 2; ++i){
                  arbre.pb({0, 0});
                  dateMin.pb(OO);
            }
      }

      void ajouter(bitset<30> line, int dateAjout){
       //     cout << "ajouter " << line << " " << dateAjout << endl;
            dico.pb(line);
            string str  = line.to_string();
            reverse(all(str));
            line = (bitset<T>)str;

            dateDico.pb(dateAjout);
            int noeud = 1;
            for (int i = 0; i < T; ++i){
                  int cara = line[i];
               //   cout << noeud << " " << cara << endl;
                  if (arbre[noeud][cara] == 0){
                        arbre[noeud][cara] = sz(arbre);
                        arbre.pb({0, 0});
                        dateMin.pb(OO);
                  }

                  noeud = arbre[noeud][cara];
                  chmin(dateMin[noeud], dateAjout);
            }
      }

      bitset<T> search(bitset<30> cible, int borneSup){
        /*    string str  = cible.to_string();
            reverse(all(str));
            cible = (bitset<T>)str;
*/
            int noeud = 1;
            bitset<T> retour;
         //   cout << "recherche " << (cible^AXOR).to_ulong() << endl;
            for (int i = 0; i < T; ++i){
                  int cara = cible[i];
                  if (arbre[noeud][cara] != 0 && dateMin[arbre[noeud][cara]] <= borneSup){
                        noeud = arbre[noeud][cara];
                        retour[i] = cara;
                  }
                  else {
                        noeud = arbre[noeud][1 - cara];
                        retour[i] = 1 - cara;
                  }
           //       cout << retour[i];
            }
         //   cout << endl;
            return retour;
      }

      void fusionner(Trie autre){
            for (int i = 0; i < sz(autre.dico); ++i)
                  ajouter(autre.dico[i], autre.dateDico[i]);
      }
};

vector<Trie> parcours;
int fusion(int pere, int fils){
      if (sz(parcours[pere].dico) < sz(parcours[fils].dico))
            return fusion(fils, pere);
      parcours[pere].fusionner(parcours[fils]);
      return pere;
}

vector<int> enfants[N];
int dateAjout[N];
bitset<T> xorPref[N];

struct Req {
      int iReq;
      bitset<T> cste;
};

vector<Req> reqs[N];
bitset<T> ans[N];
vector<bitset<T>> poids[N];

void calcPref(int noeud, bitset<T> cur){
    //  cout << "calc pref" << noeud << " " << cur << endl;
      xorPref[noeud] = cur;
      for (int i = 0; i < sz(enfants[noeud]); ++i)
            calcPref(enfants[noeud][i], cur^poids[noeud][i]);
}

int dfs(int noeud){
     // cout << "dfs " << noeud << endl;
      int id = sz(parcours);
      parcours.pb({});
      parcours[id].init();
      for (int enfant : enfants[noeud])
            id = fusion(id, dfs(enfant));
      //cout << "ppf" << noeud << endl;
      parcours[id].ajouter(xorPref[noeud], dateAjout[noeud]);
     // cout << "pf " << noeud << endl;

      for (Req req : reqs[noeud]){
            ans[req.iReq] = req.cste^parcours[id].search(AXOR^req.cste, req.iReq);
            string str  = ans[req.iReq].to_string();
            reverse(all(str));
            ans[req.iReq] = (bitset<T>)str;
      }
      return id;
}


bool estReq[N];
int deb[N], daron[N];

signed main(){
      ios::sync_with_stdio(false);
      cin.tie(nullptr);
      cout.tie(nullptr);

      int nReqs; cin >> nReqs;
      int nNoeuds = 2;
      for (int iReq = 0; iReq < nReqs; ++iReq){
            string nature;
            int x, y; cin >> nature >> x >> y;
            
            if (nature == "Add"){
                  dateAjout[nNoeuds] = iReq;
                  enfants[x].pb(nNoeuds++);
                  poids[x].pb(y);
            }
            else {
                  estReq[iReq] = true;
                  deb[iReq] = x;
                  daron[iReq] = y;
            }
      }

      //cout << "bonsoir" << endl;
    //  cout << AXOR << endl;

      calcPref(1, 0);
      for (int iReq = 0; iReq < nReqs; ++iReq){
            if (!estReq[iReq]) continue;
            bitset<T> cste = xorPref[deb[iReq]];
            string str  = cste.to_string();
            reverse(all(str));
            cste = (bitset<T>)str;
            reqs[daron[iReq]].pb({iReq, cste});
      }

     // cout << "salut" << endl;

      dfs(1);
   //   for (int noeud = 1; noeud <= nNoeuds; ++noeud)
     //       cout << "xor " << noeud << " : " << xorPref[noeud] << endl;

      for (int iReq = 0; iReq < nReqs; ++iReq){
            if (estReq[iReq])
                  cout << ans[iReq].to_ulong() << endl;
      }
      return 0;   
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 21580 KB Output is correct
2 Correct 11 ms 21596 KB Output is correct
3 Correct 11 ms 21708 KB Output is correct
4 Correct 11 ms 21708 KB Output is correct
5 Correct 12 ms 21608 KB Output is correct
6 Correct 12 ms 21708 KB Output is correct
7 Correct 12 ms 21964 KB Output is correct
8 Correct 12 ms 22256 KB Output is correct
9 Correct 11 ms 21592 KB Output is correct
10 Correct 12 ms 21836 KB Output is correct
11 Correct 11 ms 21964 KB Output is correct
12 Correct 12 ms 22084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 21580 KB Output is correct
2 Correct 11 ms 21596 KB Output is correct
3 Correct 11 ms 21708 KB Output is correct
4 Correct 11 ms 21708 KB Output is correct
5 Correct 12 ms 21608 KB Output is correct
6 Correct 12 ms 21708 KB Output is correct
7 Correct 12 ms 21964 KB Output is correct
8 Correct 12 ms 22256 KB Output is correct
9 Correct 11 ms 21592 KB Output is correct
10 Correct 12 ms 21836 KB Output is correct
11 Correct 11 ms 21964 KB Output is correct
12 Correct 12 ms 22084 KB Output is correct
13 Correct 16 ms 22484 KB Output is correct
14 Correct 16 ms 23308 KB Output is correct
15 Correct 16 ms 23812 KB Output is correct
16 Correct 21 ms 25120 KB Output is correct
17 Correct 24 ms 23848 KB Output is correct
18 Correct 27 ms 26248 KB Output is correct
19 Correct 30 ms 29120 KB Output is correct
20 Correct 32 ms 30704 KB Output is correct
21 Correct 21 ms 23320 KB Output is correct
22 Correct 20 ms 24940 KB Output is correct
23 Correct 23 ms 26228 KB Output is correct
24 Correct 25 ms 27624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 660 ms 92752 KB Output is correct
2 Correct 681 ms 159892 KB Output is correct
3 Correct 726 ms 198832 KB Output is correct
4 Correct 744 ms 292908 KB Output is correct
5 Correct 1174 ms 323676 KB Output is correct
6 Runtime error 1138 ms 524292 KB Execution killed with signal 9
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 21580 KB Output is correct
2 Correct 11 ms 21596 KB Output is correct
3 Correct 11 ms 21708 KB Output is correct
4 Correct 11 ms 21708 KB Output is correct
5 Correct 12 ms 21608 KB Output is correct
6 Correct 12 ms 21708 KB Output is correct
7 Correct 12 ms 21964 KB Output is correct
8 Correct 12 ms 22256 KB Output is correct
9 Correct 11 ms 21592 KB Output is correct
10 Correct 12 ms 21836 KB Output is correct
11 Correct 11 ms 21964 KB Output is correct
12 Correct 12 ms 22084 KB Output is correct
13 Correct 16 ms 22484 KB Output is correct
14 Correct 16 ms 23308 KB Output is correct
15 Correct 16 ms 23812 KB Output is correct
16 Correct 21 ms 25120 KB Output is correct
17 Correct 24 ms 23848 KB Output is correct
18 Correct 27 ms 26248 KB Output is correct
19 Correct 30 ms 29120 KB Output is correct
20 Correct 32 ms 30704 KB Output is correct
21 Correct 21 ms 23320 KB Output is correct
22 Correct 20 ms 24940 KB Output is correct
23 Correct 23 ms 26228 KB Output is correct
24 Correct 25 ms 27624 KB Output is correct
25 Correct 660 ms 92752 KB Output is correct
26 Correct 681 ms 159892 KB Output is correct
27 Correct 726 ms 198832 KB Output is correct
28 Correct 744 ms 292908 KB Output is correct
29 Correct 1174 ms 323676 KB Output is correct
30 Runtime error 1138 ms 524292 KB Execution killed with signal 9
31 Halted 0 ms 0 KB -