Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- #define ul unsigned long long
- #define ld long double
- #define vll(v) vector<ll>v
- //#define vll(v,n) vector<ll>v(n);
- #define mll(m) map<ll,ll>m;
- #define sll(s) set<ll>s;
- #define iv(v) for(auto &i:v) cin >> i;
- #define ov(v) for(auto &i:v) cout << i << " ";
- #define all(v) (v.begin(),v.end());
- #define Bismillah ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- using namespace std;
- string toBin(ll n) {
- string s;
- while (n) {
- s.push_back(n%2+'0');
- n/=2;
- }
- reverse(s.begin(), s.end());
- return s;
- }
- ll mul(ll a, ll b) {
- ll mod=1e9+7;
- return(((a%mod)*(b%mod))%mod);
- }
- ll noz(string s) {
- ll ans=0;
- for (int i=0; i<s.length(); i++) {
- if (s[i]=='0') ans++;
- }
- return ans+(6-s.length());
- }
- ll lw,hg;
- bool can(ll i,ll n) {
- if (i*(i+1)/2+n*(n+1)/2 <= hg && i*(i+1)/2+n*(n+1)/2 >= lw) return true;
- return false;
- }
- void solve() {
- ll n;
- cin >> n;
- vector<pair<char,ll>>v(n);
- for (int i=0; i<n; i++) {
- cin >> v[i].first >> v[i].second;
- }
- //a string that has all the bits (10 bits) "xxxxxxxxxx"
- //perform the operations on the bits and replace the x with one or zero if that's the case
- //check if the modified string is the same as the one before the operation
- //combine similar neighbour operators
- //an operation that cancels everything above it? (&0)
- //stack
- string s(10,'x');
- stack<pair<char,ll>>l;
- vector<pair<char,ll>>a;
- string pre=s;
- for (int i=0; i<n; i++) {
- char c=v[i].first;
- string bef=s;
- if (!l.empty() && l.top().first==c) {
- bef=pre;
- if (c=='|') v[i].second|=l.top().second;
- else if (c=='&') v[i].second&=l.top().second;
- else v[i].second^=l.top().second;
- l.pop();
- }
- string x=toBin(v[i].second);
- reverse (x.begin(), x.end());
- for (int j=0; j<x.size(); j++) {
- if (c=='|') {
- if (x[j]=='1') s[j]='1';
- }
- else if (c=='^') {
- if (s[j]!='x') {
- if (s[j]==x[j]) s[j]='0';
- else s[j]='1';
- }
- }
- else {
- if (x[j]=='0') s[j]='0';
- }
- }
- if (c=='&') {
- for (int j=x.size(); j<10; j++) {
- s[j]='0';
- }
- }
- if (c=='&' && v[i].second==0) {
- while (!l.empty()) l.pop();
- }
- if (v[i].second==0 && (v[i].first=='|' || v[i].first=='^')) continue;
- if (s.find(string(10,'x'))!=string::npos ||s!=bef ) l.push({c,v[i].second});
- pre=bef;
- }
- a.clear();
- while (!l.empty()) {
- a.push_back(l.top());
- l.pop();
- }
- reverse(a.begin(), a.end());
- cout << a.size() << endl;
- for (auto i:a) cout << i.first << ' ' << i.second << endl;
- }
- int main() {
- Bismillah
- ll t=1;
- // cin >> t;
- while (t--) {
- solve();
- }
- return 0;
- }
- // ll l,r;
- //cin >> l >> r;
- //lw=l; hg=r;
- //r=(-1+(ll)sqrt(1+8*r))/2;
- //ll ans=0;
- //for (ll i=1; i<=r; i++) {
- //ll x=i, y=r, mn, mx;
- //while (x<=y) {
- //mn=(x+y)/2;
- //if (can(i,mn)) {
- //if (mn==1 || !can(i,mn-1)) break;
- //else y=mn-1;
- //}
- //else x=mn+1;
- //
- //}
- //if (!can(i,mn)) continue;
- //x=i; y=r;
- //while (x<=y) {
- //mx=(x+y)/2;
- //if (can(i,mx)) {
- //if (mx==r || !can(i,mx+1)) break;
- //else x=mx+1;
- //}
- //else y=mx-1;
- //}
- //
- //ans+=mx-mn+1;
- //}
- //cout << ans << endl;
Advertisement
Add Comment
Please, Sign In to add comment