Problem: arc192_c
Difficulty: easy
Categories: data structures, math
Time limit: 2000 ms, memory limit: 1024 MB

### Problem Statement

**This problem is interactive.**

You are given a positive integer $N$.

Snuke secretly holds a permutation $P=(P_1,P_2,\dots,P_N)$ of $(1,2,\dots,N)$ and a sequence $A=(A_1,A_2,\dots,A_N)$ of positive integers of length $N$. **It is guaranteed that $P_1<P_2$.**

You may send up to $2N$ queries to Snuke. A query is of the following form:

* Specify two **distinct** positive integers $s$ and $t$ with $1\leq s,t\leq N$, and you will be given the value of $\displaystyle \sum_{i=\min(P_s,P_t)}^{\max(P_s,P_t)}A_i$.

Determine $P$ and $A$.

### Constraints

* $3\leq N\leq 5000$
* $1\leq P_i\leq N$ $(1\leq i\leq N)$
* $P_i\ne P_j$ for $i\ne j$.
* $P_1<P_2$
* $1\leq A_i\leq 10^9$ $(1\leq i\leq N)$
* $N$, $P_i$, and $A_i$ are integers.
* $P$ and $A$ are fixed before the interaction with the judge.

### Input/Output

This problem is interactive.

First, $N$ is given from Standard Input:

```
$N$
```

Next, you may send at most $2N$ queries to Snuke. When sending a query by specifying two **distinct** positive integers $s,t$, output in the following format. Do not forget to include a newline at the end.

```
? $s$ $t$
```

After sending a query, you will receive a response from Snuke in the following format:

```
$X$
```

Here, $X$ is an integer:

* If $X\ne -1$, then $X$ is the value of $\displaystyle \sum_{i=\min(P_s,P_t)}^{\max(P_s,P_t)}A_i$.
* If $X=-1$, then either $s,t$ do not satisfy the constraints or you have sent more than $2N$ queries.
  + In this case, your program is judged as incorrect and must terminate immediately.

Once you have determined $P$ and $A$, output your answer in the following format. This output does not count as a query.

```
! $P_1$ $P_2$ $\dots$ $P_N$ $A_1$ $A_2$ $\dots$ $A_N$
```

**Note that $P_1<P_2$.** After this output, terminate your program immediately.

If you output anything that does not follow the above formats, `-1` will be given as input.

```
-1
```

In that case, your program is judged as incorrect and must terminate immediately.

### Notes

* **After every output, be sure to end with a newline and flush the standard output. Failure to do so may result in a TLE.**
* When you output your answer, or receive `-1` from standard input, terminate your program immediately. Otherwise, the outcome is indeterminate.
* Note that extra newlines may be considered as an output format error.
* **The judge system for this problem is not adaptive.** That is, $P$ and $A$ are determined before the interaction with the judge and will not be changed at any point.

### Sample Interaction

For $N=6$, $P=(2,4,6,5,3,1)$, and $A=(1,9,2,25,2,9)$, here is an example of an interaction.

| Input | Output | Explanation |
| --- | --- | --- |
| `6` |  | First, the integer $N$ is given from standard input. |
|  | `? 1 2` | A query is sent to Snuke with $s=1$, $t=2$. |
| `36` |  | Since the query satisfies the constraints, the value $36$, which is equal to $\displaystyle \sum_{i=\min(P_1,P_2)}^{\max(P_1,P_2)}A_i$, is returned. |
|  | `? 2 5` | A query is sent to Snuke with $s=2$, $t=5$. |
| `27` |  | Since the query satisfies the constraints, the value $27$, which is equal to $\displaystyle \sum_{i=\min(P_2,P_5)}^{\max(P_2,P_5)}A_i$, is returned. |
|  | `! 2 4 6 5 3 1 1 9 2 25 2 9` | You report that $P$ and $A$ have been determined. After this output, the program should terminate immediately, and it will be judged as correct. |

Note that this is just one example of an interaction. In particular, it is not guaranteed that $P$ and $A$ can be uniquely determined from the queries and responses shown above.

Non-adaptive interactor (C++):
#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;


static long long queries = 0;
static long long query_limit = 0;



static void log_metrics() {
    if (query_limit == 0) return; 
    try {
        tout << "queries=" << queries << endl;
        tout << "query_limit=" << query_limit << endl;
    } catch (...) {
        
    }
}



static void finish(TResult verdict, const string& msg) {
    log_metrics();
    quitf(verdict, "%s", msg.c_str());
}

int main(int argc, char** argv) {
    
    registerInteraction(argc, argv);
    
    
    atexit(log_metrics);

    
    int n = inf.readInt();
    
    
    vector<int> p(n);
    for(int i = 0; i < n; ++i) {
        p[i] = inf.readInt();
    }
    
    
    vector<long long> a(n);
    for(int i = 0; i < n; ++i) {
        a[i] = inf.readLong();
    }

    
    
    vector<long long> pref(n + 1, 0);
    for(int i = 0; i < n; ++i) {
        pref[i+1] = pref[i] + a[i];
    }

    
    query_limit = 2LL * n;
    
    
    log_metrics();

    
    cout << n << endl;

    
    
    long long hard_cap = max(20000LL, 10 * query_limit);

    while (true) {
        string token;
        
        if (!(cin >> token)) {
            finish(_wa, "Unexpected EOF (contestant terminated without answer)");
        }

        if (token == "!") {
            
            vector<int> user_p(n);
            vector<long long> user_a(n);
            
            
            for(int i = 0; i < n; ++i) {
                string s;
                if (!(cin >> s)) finish(_wa, "EOF reading answer P");
                try {
                    user_p[i] = stoi(s);
                } catch (...) {
                    finish(_wa, "Invalid integer format in answer P");
                }
            }

            
            for(int i = 0; i < n; ++i) {
                string s;
                if (!(cin >> s)) finish(_wa, "EOF reading answer A");
                try {
                    user_a[i] = stoll(s);
                } catch (...) {
                    finish(_wa, "Invalid integer format in answer A");
                }
            }
            
            
            bool ok = true;
            for(int i = 0; i < n; ++i) {
                if (user_p[i] != p[i]) {
                    ok = false;
                    break;
                }
            }
            
            
            if (ok) {
                for(int i = 0; i < n; ++i) {
                    if (user_a[i] != a[i]) {
                        ok = false;
                        break;
                    }
                }
            }
            
            if (ok) {
                finish(_ok, "Correct answer");
            } else {
                finish(_wa, "Wrong answer (mismatch in P or A)");
            }

        } else {
            
            int s, t;
            
            
            if (token == "?") {
                if (!(cin >> s >> t)) finish(_wa, "EOF reading query parameters");
            } else {
                
                try {
                    s = stoi(token);
                } catch (...) {
                    finish(_wa, "Invalid query format (expected '?' or integer index)");
                }
                if (!(cin >> t)) finish(_wa, "EOF reading query parameter t");
            }

            
            if (s < 1 || s > n || t < 1 || t > n || s == t) {
                cout << -1 << endl;
                finish(_wa, "Invalid query parameters (indices out of bounds or s==t)");
            }

            
            queries++;
            
            
            if (queries % 100 == 0) log_metrics();

            
            if (queries > query_limit) {
                
                cout << -1 << endl;
                finish(_wa, "Query limit exceeded");
            }
            
            if (queries > hard_cap) {
                cout << -1 << endl;
                finish(_pe, "Hard query limit exceeded (potential infinite loop)");
            }

            
            
            int val_s = p[s - 1];
            int val_t = p[t - 1];
            
            int range_min = min(val_s, val_t);
            int range_max = max(val_s, val_t);
            
            
            
            long long result = pref[range_max] - pref[range_min - 1];
            
            cout << result << endl;
        }
    }

    return 0;
}


Adaptive interactor (C++):

Test generator (C++):
#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;

int main(int argc, char* argv[]) {
    registerGen(argc, argv, 1);

    
    string mode = opt<string>("mode", "non");
    
    
    int min_n = 3;
    int max_n = 5000;
    long long max_val = 1000000000LL;

    
    string seed_str = argv[1];
    int n;

    
    if (seed_str == "1") {
        n = min_n; 
    } else if (seed_str == "2") {
        n = max_n; 
    } else if (seed_str == "3") {
        n = rnd.next(min_n, 20); 
    } else {
        
        if (rnd.next(3) == 0) {
            n = rnd.next(min_n, 100);
        } else {
            n = rnd.next(max_n / 2, max_n);
        }
    }

    
    if (mode == "adp") {
        cout << n << " " << max_val << endl;
        return 0;
    }

    
    
    
    vector<int> p = rnd.perm(n, 1);
    
    
    if (p[0] > p[1]) {
        swap(p[0], p[1]);
    }

    
    vector<long long> a(n);
    int type;

    
    if (seed_str == "1") type = 0;      
    else if (seed_str == "2") type = 1; 
    else if (seed_str == "3") type = 2; 
    else type = rnd.next(5);            

    if (type == 0) {
        
        for (int i = 0; i < n; ++i) a[i] = rnd.next(1LL, 100LL);
    } else if (type == 1) {
        
        for (int i = 0; i < n; ++i) a[i] = rnd.next(max_val - 1000, max_val);
    } else if (type == 2) {
        
        for (int i = 0; i < n; ++i) a[i] = (1LL << rnd.next(0, 29));
    } else if (type == 3) {
        
        for (int i = 0; i < n; ++i) a[i] = rnd.next(1, 2);
    } else {
        
        for (int i = 0; i < n; ++i) a[i] = rnd.next(1LL, max_val);
    }

    
    cout << n << endl;
    println(p); 
    println(a); 

    return 0;
}