結果

問題 No.3591 I Love Graph
コンテスト
ユーザー GOTKAKO
提出日時 2026-07-17 21:57:13
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
RE  
実行時間 -
コード長 6,666 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,476 ms
コンパイル使用メモリ 226,992 KB
実行使用メモリ 5,888 KB
平均クエリ数 4053.89
最終ジャッジ日時 2026-07-17 21:57:33
合計ジャッジ時間 10,632 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 2 WA * 13 RE * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

using SS = int;
class SegmentTree{
    public:
    int siz = -1,n = -1;
    vector<SS> dat;
 
    SS op(SS a, SS b){return a+b;}
    SS e(){return 0;}
    void renew (SS &a,SS x){
        a = op(a,x);
        //a = x; //set(pos,x)で可能.
        //その他.
    }
 
    SegmentTree(int N){init(N);}
    SegmentTree(const vector<SS> &A){//長さ配列サイズに合わせる.
        siz = 1; n = A.size();
        while(siz < n) siz *= 2;
        dat.resize(siz*2,e());
        for(int i=0; i<n; i++) dat.at(i+siz) = A.at(i);
        for(int i=siz-1; i>0; i--) dat.at(i) = op(dat.at(i*2),dat.at(i*2+1));
    }
    void init(int N){
        //全要素単位元に初期化.
        siz = 1; n = N;
        while(siz < n) siz *= 2;
        dat.assign(siz*2,e());
    }
    void init(const vector<SS> &A){//長さ配列サイズに合わせる.
        siz = 1; n = A.size();
        while(siz < n) siz *= 2;
        dat.resize(siz*2,e());
        for(int i=0; i<n; i++) dat.at(i+siz) = A.at(i);
        for(int i=siz-1; i>0; i--) dat.at(i) = op(dat.at(i*2),dat.at(i*2+1));
    }
    void set(int pos,SS x){
        pos = pos+siz;
        dat.at(pos) = x;
        while(pos != 1){
            pos = pos/2;
            dat.at(pos) = op(dat.at(pos*2),dat.at(pos*2+1));
        }
    }
    void update(int pos,SS x){
        pos = pos+siz;
        renew(dat.at(pos),x);
        while(pos != 1){
            pos = pos/2;
            dat.at(pos) = op(dat.at(pos*2),dat.at(pos*2+1));
        }
    } 
    SS findans(int l, int r){
        SS retl = e(),retr = e();
        l += siz,r += siz;
        while(l < r){
            if(l&1) retl = op(retl,dat.at(l++));
            if(r&1) retr = op(dat.at(--r),retr);
            l >>= 1; r >>= 1;
        }
        return op(retl,retr);
    }
    SS get(int pos){return dat.at(pos+siz);}
    SS rangeans(int l, int r){return findans(l,r);}
    SS allrange(){return dat.at(1);}
 
    //rightは) leftは[で 渡す&返す. 
    int maxright(const function<bool(SS)> f,int l = 0){
        //fを満たさない最小の箇所を返す なければn.
        l += siz; int r = n+siz;
        vector<int> ls,rs;
        while(l < r){
            if(l&1) ls.push_back(l++);
            if(r&1) rs.push_back(--r);
            l >>= 1; r >>= 1; 
        }
        SS okl = e();
        for(int i=0; i<ls.size(); i++){
            l = ls.at(i);
            SS now = op(okl,dat.at(l));
            if(!f(now)){
                while(l < siz){
                    l <<= 1;
                    now = op(okl,dat.at(l));
                    if(f(now)){okl = now; l++;}
                }
                return l-siz;
            } 
            okl = now;
        }
        for(int i=rs.size()-1; i>=0; i--){
            l = rs.at(i);
            SS now = op(okl,dat.at(l));
            if(!f(now)){
                while(l < siz){
                    l <<= 1;
                    now = op(okl,dat.at(l));
                    if(f(now)){okl = now; l++;}
                }
                return l-siz;
            } 
            okl = now;
        }
        return n;
    }
    int minleft(const function<bool(SS)> f,int r = -1){
        //fを満たす最小の箇所を返す なければ0.
        if(r == -1) r = n;
        int l = siz; r += siz;
        vector<int> ls,rs;
        while(l < r){
            if(l&1) ls.push_back(l++);
            if(r&1) rs.push_back(--r);
            l >>= 1; r >>= 1; 
        }
        SS okr = e();
        for(int i=0; i<rs.size(); i++){
            r = rs.at(i);
            SS now = op(dat.at(r),okr);
            if(!f(now)){
                while(r < siz){
                    r <<= 1; r++;
                    now = op(dat.at(r),okr);
                    if(f(now)){okr = now; r--;}
                }
                return r+1-siz;
            }
            okr = now;
        }
        for(int i=ls.size()-1; i>=0; i--){
            r = ls.at(i);
            SS now = op(dat.at(r),okr);
            if(!f(now)){
                while(r < siz){
                    r <<= 1; r++;
                    now = op(dat.at(r),okr);
                    if(f(now)){okr = now; r--;}
                }
                return r+1-siz;
            }
            okr = now;
        }
        return 0;
    }
};

random_device rnd;
mt19937 mt(rnd());

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N; cin >> N;
    int query = 4500;
    vector<pair<int,int>> edge;
    auto ask = [&](vector<int> A,vector<int> B) -> long long {
        if(A.size() == 0 || B.size() == 0 || edge.size() == 400) return 0; 
        sort(A.begin(),A.end()),sort(B.begin(),B.end());
        query--;
        assert(query >= 0);
        cout << "? " << A.size() << " " << B.size();
        for(auto a : A) cout << " " << a+1;
        for(auto a : B) cout << " " << a+1;
        cout << endl;
        long long ret; cin >> ret;
        if(ret == -1) exit(0);
        return ret;
    };
    auto ans = [&]() -> void {
        cout << "! " << edge.size() << endl;
        for(auto [u,v] : edge) cout << min(u,v)+1 << " " << max(u,v)+1 << endl;
        exit(0);
    };

    auto f = [&](vector<int> A,vector<int> B) -> void {
        if(A.size() == 0 || B.size() == 0) return;
        int l = 0,n = A.size();
        while(true){
            vector<int> X;
            for(int i=l; i<n; i++) X.push_back(A.at(i));
            if(ask(X,B) == 0) break;
            
            int low = l-1,high = n-1;
            while(high-low > 1){
                int mid = (high+low)/2;
                X.clear();
                for(int i=l; i<=mid; i++) X.push_back(A.at(i));
                if(ask(X,B)) high = mid;
                else low = mid;
            }
            X = {A.at(high)},l = high+1;

            int l2 = 0,m = B.size();
            while(true){
                vector<int> Y;
                for(int i=l2; i<m; i++) Y.push_back(B.at(i));
                if(l2 && ask(X,Y) == 0) break;
                low = l2-1,high = m-1;
                while(high-low > 1){
                    int mid = (high+low)/2;
                    Y.clear();
                    for(int i=l2; i<=mid; i++) Y.push_back(B.at(i));
                    if(ask(X,Y)) high = mid;
                    else low = mid;
                }
                Y = {B.at(high)},l2 = high+1;
                edge.push_back({X.at(0),Y.at(0)});
            }
        }
    };
    for(int d=0; d<10; d++){
        vector<int> A,B;
        for(int i=0; i<N; i++){
            if((i>>d)&1) B.push_back(i);
            else A.push_back(i);
        }
        f(A,B);
    }
    ans();


}
0