Untitled

 avatar
unknown
plain_text
10 days ago
2.7 kB
3
Indexable
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define in(v) for (auto &i : v) cin >> i;
#define out(v) for (auto i : v) cout << i << ' ';
#define ot(vp) for(auto it:vp)cout<<it.first<<' '<<it.second<<endl;
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define br cout << endl;
const int mod = 1e9 + 7;

// :)

int power(int a, int n) {
    int res = 1;
    while (n > 0) {
        if (n % 2) res *= a;
        a *= a;
        n /= 2;
    }
    return res;
}

void CutiePie() {
    int n;
    cin >> n;
    int q;
    cin >> q;
    while (q--) {
        string s;
        cin >> s;
        if (s == "->") {
            int x, y;
            cin >> x >> y;
            int n1 = n;
            int ans = 0;
            while (n1 > 0) {
                int k = power(2, n1);
                if (x > k / 2 && y > k / 2) {
                    x -= k / 2;
                    y -= k / 2;
                    ans += (k * k) / 4;
                }
                else if (x <= k / 2 && y > k / 2) {
                    y -= k / 2;
                    ans += 3 * (k * k) / 4;
                }
                else if (x > k / 2 && y <= k / 2) {
                    x -= k / 2;
                    ans += (k * k) / 2;                
                }
                n1--;
            }
            if (x == 1 && y == 1) ans += 1;
            if (x == 1 && y == 2) ans += 4;
            if (x == 2 && y == 1) ans += 3;
            if (x == 2 && y == 2) ans += 2;
            cout << ans << endl;    
        }
        else {
            int d;
            cin >> d;
            d--;
            int n1 = n;
            int x = 0, y = 0;
            while (n1 > 0) {
                int k = power(2, n1);
                if ((4 * d) / (k * k) == 2) {
                    x += k / 2;
                }
                else if ((4 * d) / (k * k) == 3) {
                    y += k / 2;
                }
                else if ((4 * d) / (k * k) == 1) {
                    x += k / 2;
                    y += k / 2;                
                }
                n1--;
                d %= ((k * k) / 4);
            }
            
            if (d == 2) {
                x++; y++;
            }
            if (d == 3) x++;
            if (d == 4) y++;
            cout << x + 1 << ' ' << y + 1 << endl; 
        }
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int QT = 1;
    cin >> QT;
    for (;QT--;) CutiePie();
}
Editor is loading...
Leave a Comment