QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#96726#6303. InversionjoesmittyTL 0ms0kbC++206.0kb2023-04-15 08:56:072023-04-15 08:56:09

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-15 08:56:09]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2023-04-15 08:56:07]
  • 提交

answer

#include <iostream>
#include <tuple>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <random>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <algorithm>
#include <vector>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <cctype>
#include <climits>
#include <chrono>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector< vector <int> > vvi;
typedef pair<int, int> pii;
typedef pair < pair < int, int >, int > piii;
typedef pair < pair <int, int > , pair <int, int> > piiii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
 
#define FOR(i,a,b) for(int i = a; i < b; i ++)
#define RFOR(i,a,b) for(int i = a-1; i >= b; i --)
#define all(a) a.begin(), a.end()
#define endl '\n';
#define sz(x) (int)(x).size()
 
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
 
template <typename T>
void pr(vector<T> &v) {
    FOR(i, 0, sz(v)) cout << v[i] << " ";
    cout << endl;
}
template <typename T>
void pr(vector<vector<T> > &v) {
    FOR(i, 0, sz(v)) { pr(v[i]); }
}
template <typename T>
void re(T &x) {
    cin >> x;
}
template <typename T>
void re(vector<T> &a) {
    FOR(i, 0, sz(a)) re(a[i]);
}
template <class Arg, class... Args>
void re(Arg &first, Args &... rest) {
    re(first);
    re(rest...);
}
template <typename T>
void pr(T x) {
    cout << x << endl;
}
template <class Arg, class... Args>
void pr(const Arg &first, const Args &... rest) {
    cout << first << " ";
    pr(rest...);
    cout << endl;
}
void ps() { cout << endl; }
template<class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
    cout << t; if (sizeof...(ts)) cout << " "; ps(ts...);
}
 
const ll MOD  =  998244353;
#define inf 1e18;
#define INF INT_MAX

long double PI = 4*atan(1);
long double eps = 1e-16;

template<class T>
class FenwickTree {
    private:
        vector<T> ft;
    public:
        FenwickTree(int m) {
            ft.assign(m + 5, 0);
        }
        
        int LSOne(int x) {
            return x & (-x);
        }
        
        
        T RSQ(int j) {
            // this allows array to be 0 indexed while ft is 1 indexed
            j++;
            ll sum = 0;
            for(; j > 0; j -= LSOne(j)) {
                sum += ft[j];
            }
            return sum;
        }
        
        void update(int i, T v) {
            i++;
            for(; i < ft.size(); i += LSOne(i)) {
                ft[i] += v;
            }
        }
    
};

vector<int> ans(1);
vi test;
int numq = 0;

// int calcinv(int l, int r) {
//     int ans = 0;
//     FOR(i,l,r) {
//         FOR(j,i+1, r+1) {
//             if(test[i] > test[j]) {
//                 //cout << i << " " << j << " " << test[i] << " " << test[j] << endl;
//                 ans++;
//             }
//         }
//     }
//     //cout << "inv: " << l << " " << r << " " << ans << endl;
//     return ans % 2;
// }


void add(int p) {
    vector<int> loc(p);
    FOR(i,0,p) loc[ans[i]] = i;

    FenwickTree<int> ft(p+1);

    vi inv(p+1);

    RFOR(i,p,0) {
        inv[i] = (inv[i+1] + ft.RSQ(ans[i]))%2;
        ft.update(ans[i], 1);
    }

    int lo = 0;
    int hi = p;


    while(lo < hi) {
        
        int mid = (lo + hi)/2;
        //cout << lo << " " << hi << " " << mid << " " << loc[mid] << endl;
        
        int pos = loc[mid];
        int res = 0;
        if(pos + 1 == p) {
           cout << "? " << pos + 1 << " " << p + 1 << endl;
            cin >> res;
           // res = calcinv(pos, p);
            numq += 1;
        } else {
            int r1,r2,r3,r4;
           cout << "? " << pos + 1 << " " << p + 1 << endl;
            cin >> r1;
            //r1 = calcinv(pos, p);
           cout << "? " << pos + 2 << " " << p + 1 << endl;
            cin >> r2;
            //r2 = calcinv(pos+1, p);
        //    cout << "? " << pos + 1 << " " << p  << endl;
            //cin >> r3;
            r3 = inv[pos];
        //    cout << "? " << pos + 2  << " " << p << endl;
            //cin >> r4;
            r4 = inv[pos+1];
            numq += 2;
            res = (4 + r1 - r2 - r3 + r4) % 2;
        }
        
        if(res == 1) {
            hi = mid;
        } else {
            lo = mid + 1;
        }
    }
    
    int newpos = lo;
   // cout << newpos << endl;

    vector<int> nans(p+1);
    for(int i = 0; i < p; i++) {
        if(ans[i] < newpos) {
            nans[i] = ans[i];
        } else {
            nans[i] = ans[i] + 1;
        }
    }
    nans[p] = newpos;
    //cout << numq << endl;
    swap(ans, nans);
}


int main() {
    auto start = chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);cin.tie(0);
    //ofstream cout("output.txt");
    //ifstream cin("input.txt");
    #ifdef DEBUG
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
    #endif  
    
    int n; cin >> n;
    // test.resize(n);
    // FOR(i,0,n) test[i] = i;

    // default_random_engine g{1};
    // shuffle(test.begin(), test.end(), g);

    for(int i = 1; i < n; i++) {
        add(i);
    }
    // bool good =true;
    // FOR(i,0,n) {
    //     cout << test[i] << " " << ans[i] << endl;
    //     if(test[i] != ans[i]) {
    //         good =false;
    //     }
    // }
    // if(!good) {
    //     cout << "BAD" << endl;
    // }
    // cout << numq << endl;


    cout << "! ";
    for(auto x : ans) {
        cout << x + 1 << " ";
    }
    cout << endl;


    
    


    // auto stop = chrono::high_resolution_clock::now();
    // auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
    // cout << duration.count() << endl;
    //cin.close();
    //cout.close();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

3

output:


result: