lina_os

Untitled

Jun 18th, 2025
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define ll long long
  4. #define ul unsigned long long
  5. #define ld long double
  6. #define vll(v) vector<ll>v
  7. //#define vll(v,n) vector<ll>v(n);
  8. #define mll(m) map<ll,ll>m;
  9. #define sll(s) set<ll>s;
  10. #define iv(v) for(auto &i:v) cin >> i;
  11. #define ov(v) for(auto &i:v) cout << i << " ";
  12. #define all(v) (v.begin(),v.end());
  13. #define Bismillah ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  14.  
  15. using namespace std;
  16.  
  17. string toBin(ll n) {
  18.     string s;
  19.     while (n) {
  20.         s.push_back(n%2+'0');
  21.         n/=2;
  22.     }
  23.     reverse(s.begin(), s.end());
  24.     return s;
  25. }
  26.  
  27. ll mul(ll a, ll b) {
  28.     ll mod=1e9+7;
  29.     return(((a%mod)*(b%mod))%mod);
  30. }
  31.  
  32. ll noz(string s) {
  33.     ll ans=0;
  34.     for (int i=0; i<s.length(); i++) {
  35.         if (s[i]=='0') ans++;
  36.     }
  37.     return ans+(6-s.length());
  38. }
  39.  
  40. ll lw,hg;
  41. bool can(ll i,ll n) {
  42.     if (i*(i+1)/2+n*(n+1)/2 <= hg && i*(i+1)/2+n*(n+1)/2 >= lw) return true;
  43.     return false;
  44. }
  45.  
  46.  
  47. void solve() {
  48.     ll n;
  49.     cin >> n;
  50.     vector<pair<char,ll>>v(n);
  51.     for (int i=0; i<n; i++) {
  52.         cin >> v[i].first >> v[i].second;
  53.     }
  54.  
  55.  
  56.     //a string that has all the bits (10 bits) "xxxxxxxxxx"
  57.     //perform the operations on the bits and replace the x with one or zero if that's the case
  58.     //check if the modified string is the same as the one before the operation
  59.     //combine similar neighbour operators
  60.     //an operation that cancels everything above it? (&0)
  61.     //stack
  62.  
  63.     string s(10,'x');
  64.     stack<pair<char,ll>>l;
  65.     vector<pair<char,ll>>a;
  66.     for (int i=0; i<n; i++) {
  67.         char c=v[i].first;
  68.         string bef=s;
  69.  
  70.         if (!l.empty() && l.top().first==c) {
  71.             if (c=='|') v[i].second|=l.top().second;
  72.             else if (c=='&') v[i].second&=l.top().second;
  73.             else v[i].second^=l.top().second;
  74.             l.pop();
  75.         }
  76.         string x=toBin(v[i].second);
  77.         reverse (x.begin(), x.end());
  78.         for (int j=0; j<x.size(); j++) {
  79.             if (c=='|') {
  80.                 if (x[j]=='1') s[j]='1';
  81.             }
  82.             else if (c=='^') {
  83.                 if (s[j]!='x') {
  84.                     if (s[j]==x[j]) s[j]='0';
  85.                     else s[j]='1';
  86.                 }
  87.             }
  88.             else {
  89.                 if (x[j]=='0') s[j]='0';
  90.             }
  91.         }
  92.         if (c=='&' && v[i].second==0) {
  93.             while (!l.empty()) l.pop();
  94.         }
  95.         if (v[i].second==0 && (v[i].first=='|' || v[i].first=='^')) continue;
  96.         if (s.find(string(10,'x'))!=string::npos ||s!=bef ) l.push({c,v[i].second});
  97.     }
  98.     a.clear();
  99.     while (!l.empty()) {
  100.         a.push_back(l.top());
  101.         l.pop();
  102.     }
  103.     reverse(a.begin(), a.end());
  104.     cout << a.size() << endl;
  105.     for (auto i:a) cout << i.first << ' ' << i.second << endl;
  106.  
  107.  
  108. }
  109.  
  110. int main() {
  111.     Bismillah
  112.     ll t=1;
  113. //    cin >> t;
  114.     while (t--) {
  115.         solve();
  116.     }
  117.     return 0;
  118. }
  119.  
  120.  
  121. //    ll l,r;
  122. //cin >> l >> r;
  123. //lw=l; hg=r;
  124. //r=(-1+(ll)sqrt(1+8*r))/2;
  125. //ll ans=0;
  126. //for (ll i=1; i<=r; i++) {
  127. //ll x=i, y=r, mn, mx;
  128. //while (x<=y) {
  129. //mn=(x+y)/2;
  130. //if (can(i,mn)) {
  131. //if (mn==1 || !can(i,mn-1)) break;
  132. //else y=mn-1;
  133. //}
  134. //else x=mn+1;
  135. //
  136. //}
  137. //if (!can(i,mn)) continue;
  138. //x=i; y=r;
  139. //while (x<=y) {
  140. //mx=(x+y)/2;
  141. //if (can(i,mx)) {
  142. //if (mx==r || !can(i,mx+1)) break;
  143. //else x=mx+1;
  144. //}
  145. //else y=mx-1;
  146. //}
  147. //
  148. //ans+=mx-mn+1;
  149. //}
  150. //cout << ans << endl;
  151.  
  152.  
Advertisement
Add Comment
Please, Sign In to add comment