Problem:
Disc
A mysterious device was discovered in an ancient ruin. The device consists of a disc with integers inscribed on both sides, a bundle of cards with an integer written on each of them and a pedestal within which the cards are placed. The disc rotates when the bundle of cards is put into the pedestal. Only one side of the disc is exposed to view at any one time.
The disc is radially segmented in equal angles into $K$ sections, and an integer, from $1$ to $K$ in increasing order, is inscribed clockwise on each section on the front side. The sections on the back side share the same boundaries with the front side, and a negative counterpart of that in the section on the front side is inscribed, i.e., if $X$ is inscribed in the front side section, $-X$ is inscribed on the counterpart section on the back side. There is a needle on the periphery of the disc, and it always points to the middle point of the arc that belongs to a section. Hereafter, we say "the disc is set to $X$" if the front side section just below the needle contains an integer $X$.
Fig.1 Disc with 5 sections (i.e., $K = 5$): When the disc is set to $1$ on the front side, it is set to $-1$ on the back side.
Investigation of the behavior of the device revealed the following:
When the bundle of cards is placed in the pedestal, the device sets the disc to 1.
Then, the device reads the cards slice by slice from top downward and performs one of the following actions according to the integer $A$ on the card
If $A$ is a positive number, it rotates the disc clockwise by $|A|$ sections.
If $A$ is zero, it turns the disc back around the vertical line defined by the needle and the center of the disc.
If $A$ is a negative number, it rotates the disc counterclockwise by $|A|$ sections.
It is provided that $|A|$ is the absolute value of $A$. The final state of the device (i.e., to what section the needle is pointing) varies depending on the initial stacking sequence of the cards. To further investigate the behavior of the device, we swapped two arbitrary cards in the stack and placed the bundle in the pedestal to check what number the disc is set to. We performed this procedure over again and again.
Given the information on the device and several commands to swap two cards, make a program to determine the number to which the device is set at the completion of all actions. Note that the stacking sequence of the cards continues to change as a new trial is made.
Input
The input is given in the following format.
$K$ $N$ $Q$
$A_1$ $A_2$ ... $A_N$
$L_1$ $R_1$
$L_2$ $R_2$
$...$
$L_Q$ $R_Q$
The first line provides the number of sections $K$ ($2 \leq K \leq 10^9$), the slices of cards $N$ ($2 \leq N \leq 100,000$), and the number of commands $Q$ ($1 \leq Q \leq 100,000$). The second line provides an array of $N$ integers inscribed on the sections of the disc. $A_i$ ($-10^9 \leq A_i \leq 10^9$) represents the number written on the $i$-th card from the top. Each of the subsequent $Q$ lines defines the $i$-th
...(truncated)

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long
int K;
template < typename NodeT, typename LazyT>
struct SEG{
    using F = function<NodeT(const NodeT&, const NodeT&)>;
    using G = function<NodeT(const NodeT&, const LazyT&,int)>;
    using H = function<LazyT(const LazyT&, const LazyT&)>;
    int n;
    vector<NodeT> node;
    vector<LazyT> lazy;
    F f;
    G g;
    H h;
    NodeT e;
    LazyT id;
    SEG(int N, F f, G g, H h, NodeT e, LazyT id) : f(f), g(g), h(h), e(e), id(id)
    {
        n = 1;
        while(n < N) n <<= 1;
        node.assign(2*n,e);
        lazy.assign(2*n,id);
    }
    void eval(int k, int l, int r){
        if(lazy[k].ml != id.ml || lazy[k].ad != id.ad){
            node[k] = g(node[k],lazy[k],r-l);
            if(r-l > 1){
                lazy[2*k] = h(lazy[2*k],lazy[k]);
                lazy[2*k+1] = h(lazy[2*k+1],lazy[k]);
            }
            lazy[k] = id;
        }
    }
    void update(int a, int b, LazyT x, int k=1, int l=0, int r=-1){
        if(r == -1) r = n;
        eval(k,l,r);
        if(b <= l || r <= a) return;
        if(a <= l && r <= b){
            lazy[k] = h(lazy[k],x);
            eval(k,l,r);
        }
        else{
            int m = (l+r)/2;
            update(a,b,x,2*k,l,m);
            update(a,b,x,2*k+1,m,r);
            node[k] = f(node[2*k+1],node[2*k]);
        }
    }
    NodeT query(int a, int b, int k = 1, int l = 0, int r = -1){
        if(r == -1) r = n;
        eval(k,l,r);
        if(b <= l || r <= a) return e;
        if(a <= l && r <= b) return node[k];
        int m = (l+r)/2;
        return f(query(a,b,2*k,l,m), query(a,b,2*k+1,m,r));
    }
};
struct SEGt{
private:
    int n;
    vector<int> node;
public:
    SEGt(int N){
        n = 1;
        while(n < N) n *= 2;
        node.resize(2*n-1);
    }
    void update(int i, int x){
        i += n-1;
        node[i] = x;
        while(i){
            i = (i-1)/2;
            node[i] = node[2*i+1]+node[2*i+2];
        }
    }
    int f(int a, int b, int k = 0, int l = 0, int r = -1){
        if(r < 0) r = n;
        if(a <= l && r <= b) return node[k];
        if(b <= l || r <= a) return 0;
        return f(a,b,2*k+1,l,(l+r)/2)+f(a,b,2*k+2,(l+r)/2,r);
    }
};
signed main(){
    int N,Q;
    struct Node{int val;};
    struct Lazy{int ml; int ad;};
    auto f = [](Node a, Node b){return Node{(a.val+b.val)%K};};
    auto g = [](Node a, Lazy b, int len){return Node{(a.val*b.ml+b.ad*len)%K};};
    auto h = [](Lazy a, Lazy b){return Lazy{(a.ml*b.ml)%K,(b.ml*a.ad+b.ad)%K};};
    Node e = Node{0};
    Lazy id = Lazy{1,0};
    cin>>K>>N>>Q;
    SEG<Node,Lazy> seg(N,f,g,h,e,id);
    SEGt segt(N);
    vector<int> A(N);
    for(int i=0;i<N;i++){
        int a;
        cin>>a;
        A[i] = a;
        seg.update(i,i+1,{(segt.f(0,i)%2 ? 1 : -1),(segt.f(0,i)%2 ? 1 : -1)*a});
        if(a == 0) segt.update(i,1);
    }
    while(Q--){
        int a,b;
        cin>>a>>b;
        a--;
        b--;
        if((A[a]==0)^(A[b]==0)){
            seg.update(a+1,b,{-1,0});
            if(A[a] == 0){
                segt.update(a,0);
                segt.update(b,1);
                seg.update(a,a+1,{(segt.f(0,a)%2 ? 1 : -1),(segt.f(0,a)%2 ? 1 : -1)*A[b]});
                seg.update(b,b+1,{0,0});
            }
            else{
                segt.update(a,1);
                segt.update(b,0);
                seg.update(a,a+1,{0,0});
                seg.update(b,b+1,{(segt.f(0,b)%2 ? 1 : -1),(segt.f(0,b)%2 ? 1 : -1)*A[a]});
            }
        }
        else if(A[a] != 0 && A[b] != 0){
            if(segt.f(0,a)%2 == 1){
                seg.update(a,a+1,{1,A[b]-A[a]});
            }
            else{
                seg.update(a,a+1,{1,-(A[b]-A[a])});
            }
            if(segt.f(0,b)%2 == 1){
                seg.update(b,b+1,{1,A[a]-A[b]});
            }
            else{
                seg.update(b,b+1,{1,-(A[a]-A[b])});
            }
        }
        swap(A[a],A[b]);
        int t = seg.query(0,N).val;
        int ans = (t%K+K)%K;
        ans++;
        if(segt.f(0,N)%2) ans = -ans;
        cout<<ans<<endl;
    }
}
accuracy: 1
time_ms: 400
memory_kb: 12016
final_rank: 19

code_snippet: #include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)

#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)

using namespace std;

using ll = long long;
using PLL = pair<ll, ll>;

template<class T, class U> bool chmin(T& a, const U& b) { return a > b ? a = b, true : false; }
template<class T, class U> bool chmax(T& a, const U& b) { return a < b ? a = b, true : false; }

constexpr ll inf = 1e18;
constexpr int MOD = 1000000007;

template<class A>
class LazySegmentTree {
    using M = typename A::M;
    using E = typename A::E;
    using T = typename M::T;
    using U = typename E::T;

    int n, h;
    vector<T> dat;
    vector<U> lazy;

    void all_apply(int k, U x) {
        dat[k] = A::op(x, dat[k]);
        if (k < n) lazy[k] = E::op(lazy[k], x);
    }
    void eval(int k) {
        if (lazy[k] == E::id()) return;
        all_apply(k * 2 + 0, lazy[k]);
        all_apply(k * 2 + 1, lazy[k]);
        lazy[k] = E::id();
    }
    void calc(int k) {
        dat[k] = M::op(dat[k * 2 + 0], dat[k * 2 + 1]);
    }

public:
    LazySegmentTree(vector<T> a) {
        h = 0;
        while ((1 << h) < (int)(a.size())) ++h;
        n = 1 << h;
        dat.assign(n * 2, M::id());
        lazy.assign(n, E::id());
        rep (i, a.size()) dat[i + n] = a[i];
        rrep2 (i, 1, n) calc(i);
    }
    T all_prod() { return dat[1]; }
    T prod(int l, int r) {
        l += n; r += n;
        rrep2 (i, 1, h + 1) {
            if (((l >> i) << i) != l) eval(l >> i);
            if (((r >> i) << i) != r) eval((r - 1) >> i);
        }
        T lsm = M::id(), rsm = M::id();
        while (l < r) {
            if (l & 1) lsm = M::op(lsm, dat[l++]);
            if (r & 1) rsm = M::op(dat[--r], rsm);
            l /= 2; r /= 2;
        }
        return M::op(lsm, rsm);
    }
    T get(int k) {
        k += n;
        rrep2 (i, 1, h + 1) eval(k >> i);
        return dat[k];
    }
    void set(int k, T x) {
        k += n;
        rrep2 (i, 1, h + 1) eval(k >> i);
        dat[k] = x;
        rep2 (i, 1, h + 1) calc(k >> i);
    }
    void apply(int l, int r, U x) {
        l += n; r += n;
        rrep2 (i, 1, h + 1) {
            if (((l >> i) << i) != l) eval(l >> i);
            if (((r >> i) << i) != r) eval((r - 1) >> i);
        }
        int l2 = l, r2 = r;
        while (l2 < r2) {
            if (l2 & 1) all_apply(l2++, x);
            if (r2 & 1) all_apply(--r2, x);
            l2 /= 2; r2 /= 2;
        }
        rep2 (i, 1, h + 1) {
            if (((l >> i) << i) != l) calc(l >> i);
            if (((r >> i) << i) != r) calc((r - 1) >> i);
        }
    }
};

struct Act {
    struct M {
        using T = ll;
        static T id() { return 0; }
        static T op(T a, T b) { return a + b; }
    };
    struct E {
        using T = bool;
        static T id() { return false; }
        static T op(T a, T b) { return b ? !a : a; }
    };
    static ll op(bool a, ll b) { return a ? -b : b; }
};

class BinaryIndexedTree {
    int n;
    vector<ll> dat;

public:
    BinaryIndexedTree(int n_) : n(n_), dat(n_ + 1) {}
    void add(int k, ll x) {
        ++k;
        while (k <= n) {
            dat[k] += x;
            k += k & -k;
        }
    }
    ll sum(int k) const {
        ll res = 0;
        while (k > 0) {
            res += dat[k];
            k -= k & -k;
        }
        return res;
    }
    ll sum(int l, int r) const { return sum(r) - sum(l); }
};

int main() {
    ll K, N, Q; scanf("%lld%lld%lld", &K, &N, &Q);
    vector<ll> A(N);
    rep (i, N) scanf("%lld", &A[i]);
    rep (i, N) A[i] = -A[i];
    BinaryIndexedTree bit(N);
    LazySegmentTree<Act> seg(A);
    rep (i, N) if (A[i] == 0) {
        seg.apply(i, N, true);
        bit.add(i, 1);
    }
    int cnt = count(all(A), 0);
    rep (_, Q) {
        int a, b; scanf("%d%d", &a, &b);
        --a; --b;
        if (A[a] == 0) seg.apply(a, N, true), bit.add(a, -1);
        if (A[b] == 0) seg.apply(b, N, true), bit.add(b, -1);
        swap(A[a], A[b]);
        if (A[a] == 0) seg.apply(a, N, true), bit.add(a, 1);
        if (A[b] == 0) seg.apply(b, N, true), bit.add(b, 1);
        seg.set(a, A[a] * (bit.sum(0, a) % 2 == 1 ? -1 : 1));
        seg.set(b, A[b] * (bit.sum(0, b) % 2 == 1 ? -1 : 1));
        ll res = seg.all_prod();
        res = (res % K + K) % K + 1;
        printf("%lld\n", res * (cnt % 2 == 1 ? -1 : 1));
    }
}
accuracy: 1
time_ms: 80
memory_kb: 7576
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ld=long double;
#define fi first
#define se second
#define i128 __int128_t
#define all(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define si(a) (long)(a.size())
template<typename T>bool chmin(T&a,T b){return b<a?(a=b,1):0;}
template<typename T>bool chmax(T&a,T b){return b>a?(a=b,1):0;}
const int INF=1e9+100;
const long INFL=3e18+100;
struct _ {
   _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); }
} __;


using U = uint64_t;
template<class S, S (*op)(S, S), S (*e)(), class F, S (*mpp)(F, S), F (*cmpo)(F, F), F (*id)()> struct lazy_segtree {
    lazy_segtree() : lazy_segtree(0) {}
    explicit lazy_segtree(int n) : lazy_segtree(vector<S>(n, e())) {}
    explicit lazy_segtree(const vector<S>& v) : n(si(v)) {
        s = __bit_ceil(U(n));
        log = __countr_zero(U(s));
        d = vector<S>(2 * s, e());
        lz = vector<F>(s, id());
        rep(i, n) d[s + i] = v[i];
        for(int i=s; --i;) update(i);
    }
    void set(int p, S x) {
        p += s;
        PUSH(p);
        d[p] = x;
        for(int i=1;i<=log;i++) update(p >> i);
    }
    S get(int p) {
        p += s;
        PUSH(p);
        return d[p];
    }
    S prod(int l, int r) {
        if(l == r) return e();
        l += s, r += s;
        for(int i=log+1;--i;) {
            if(((l >> i) << i) != l) push(l >> i);
            if(((r >> i) << i) != r) push((r - 1) >> i);
        }
        S sml = e(), smr = e();
        while(l < r) {
            if(l & 1) sml = op(sml, d[l++]);
            if(r & 1) smr = op(d[--r], smr);
            l >>= 1, r >>= 1;
        }
        return op(sml, smr);
    }
    S all_prod() {
        return d[1];
    }
    void apply(int p, F f) {
        // assert(0 <= p && p < n);
        p += s;
        PUSH(p);
        d[p] = mpp(f, d[p]);
        for(int i=1;i<=log;i++) update(p >> i);
    }
    void apply(int l, int r, F f) {
        // assert(0 <= l && l <= r && r <= _n);
        if(l == r) return;
        l += s, r += s;

        for(int i=log+1;--i;) {
            if(((l >> i) << i) != l) push(l >> i);
            if(((r >> i) << i) != r) push((r - 1) >> i);
        }
        int ml = l, mr = r;
        while(l < r) {
            if(l & 1) all_apply(l++, f);
            if(r & 1) all_apply(--r, f);
            l >>= 1, r >>= 1;
        }
        l = ml, r = mr;
        for(int i=1;i<=log;i++) {
            if(((l >> i) << i) != l) update(l >> i);
            if(((r >> i) << i) != r) update((r - 1) >> i);
        }
    }
    S operator[](int k) {
        return get(k);
    }
    int len() {
        return n;
    }

private:
    int n, s, log;
    vector<S> d;
    vector<F> lz;
    void update(int k) {
        d[k] = op(d[2 * k], d[2 * k + 1]);
    }
    void all_apply(int k, F f) {
        d[k] = mpp(f, d[k]);
        if(k < s) lz[k] = cmpo(f, lz[k]);
    }
    void push(int k) {
        all_apply(2 * k, lz[k]);
        all_apply(2 * k + 1, lz[k]);
        lz[k] = id();
    }
    void PUSH(int k) {
        for(int i=log+1;--i;) push(k >> i);
    }
};

template<class T> struct fenwick_tree {
    vector<T> a;
    fenwick_tree(int n): a(n + 1) {}
    void add(int i, T x) {
        i++;
        while(i < si(a)) a[i] += x, i += i & -i;
    }
    T sum(int r) {
        T s = 0;
        while(r) s += a[r], r -= r & -r;
        return s;
    }
    T sum(int l, int r) { return sum(r) - sum(l); }
    int lower_bound(T w) {
        if(w <= 0) return 0;
        int x = 0, N = si(a) + 1;
        for(int k = 1 << __lg(N); k; k >>= 1) {
            if(x + k <= N - 1 && a[x + k] < w) {
            w -= a[x + k];
            x += k;
            }
        }
        return x;
    }
};

using S=long;
S op(S l, S r) {return l+r;}
S e(){return S();}
S mpp(S f, S s){return (f*2-1)*-1*s;}
S cmp(S f, S g){return (f+g)%2;}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int K,N,Q;
    cin>>K>>N>>Q;
    vector<long> A(N+2,0);
    rep(i,N)cin>>A[i+1];
    N+=2;

    lazy_segtree<S,op,e,S,mpp,cmp,e> seg(A);
    fenwick_tree<int> bit(N);
    rep(i,N){
        if(A[i]==0){
            bit.add(i, 1);
            if(bit.sum(i)%2){
                seg.apply(bit.lower_bound(bit.sum(i))+1, i, 1);
            }
        }
    }
    while(Q--){
        int L,R;
        cin>>L>>R;
        
        if(A[L]!=0&&A[R]!=0){
            seg.set(L, (bit.sum(0,L)%2*2-1)*-1*A[R]);
            seg.set(R, (bit.sum(0,R)%2*2-1)*-1*A[L]);
            swap(A[L],A[R]);
        }
        else if(A[R]!=0){
            seg.set(L, (bit.sum(0,L)%2*2-1)*A[R]);
            seg.set(R, 0);
            seg.apply(L, R, 1);
            bit.add(L, -1);
            bit.add(R, 1);
            swap(A[L],A[R]);
        }
        else if(A[L]!=0){
            seg.set(R, (bit.sum(0,R)%2*2-1)*A[L]);
            seg.set(L, 0);
            seg.apply(L+1, R,1);
            bit.add(R, -1);
            bit.add(L, 1);
            swap(A[L],A[R]);
        }
        cout<<((seg.all_prod()%K+K)%K+1)*(bit.sum(N)%2*2-1)*-1<<"\n";
    }
}
accuracy: 1
time_ms: 50
memory_kb: 7664
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define pb push_back
#define rep(i,N) for(int i =0;i<(int)N;i++)
#define all(x) x.begin(),x.end()

class seg{
    public:
    int N;
    vector<ll>data;
    vector<ll>lazy;
    void init(int n){
        N=1;
        while(N<n)N*=2;
        data.assign(N*2,0);
        lazy.assign(N*2,1);
    }
    void eval(int k){
        if(lazy[k]!=1){
            data[k]*=lazy[k];
            if(2*k+2<N*2){
                lazy[k*2+1]*=-1;
                lazy[k*2+2]*=-1;
            }
            lazy[k]=1;
        }
    }
    void update(int x,ll v,int k=0,int l=0,int r=-1){
        if(r<0)r=N;
        eval(k);
        if(x<l||r<=x)return;
        else if(x==l&&x+1==r){
            data[k]=v;
            return;
        }
        else if(2*k+2<2*N){
            update(x,v,k*2+1,l,(l+r)/2);
            update(x,v,k*2+2,(l+r)/2,r);
            data[k]=data[k*2+1]+data[k*2+2];
        }
    }
    void flip(int a,int b,int k=0,int l=0,int r=-1){
        if(r<0)r=N;
        eval(k);
        if(b<=l||r<=a){
            return;
        }
        if(a<=l&&r<=b){
            lazy[k]*=-1;
            eval(k);
        }
        else if(2*k+2<2*N){
            flip(a,b,k*2+1,l,(l+r)/2);
            flip(a,b,k*2+2,(l+r)/2,r);
            data[k]=data[k*2+1]+data[k*2+2];
        }
    }
    ll allsum(){
        eval(0);
        return data[0];
    }
};
template<typename T>
class RSQ{
    public:
    ll size=1;
    vector<T>dataS;

    void init(int N){
        while(size<=N)size=size*2;
        dataS.assign(size*2,0);
    }

    void update(int p,T x){
        p=p+size;
        dataS[p]=x;
        while(p>1){
            p>>=1;
            dataS[p]=dataS[p*2]+dataS[p*2+1];
        }
    }

    T ISum(int l,int r,int a,int b,int u){
        if(r<=a||b<=l)return 0;
        if(l<=a&&b<=r)return dataS[u];
        int m=(a+b)>>1;
        T L=ISum(l,r,a,m,u*2);
        T R=ISum(l,r,m,b,u*2+1);
        return L+R;
    }
    T Sum(int l,int r){return ISum(l,r,0,size,1);}
};

int main(){
    ll K,N,Q;cin>>K>>N>>Q;
    vector<ll>A(N);
    rep(i,N)cin>>A[i];
    RSQ<ll>S;
    seg seg;
    S.init(N+1);
    seg.init(N);
    rep(i,N){
        if(A[i]==0){
            S.update(i+1,1);
        }
        else {
            int t = 1;
            if(S.Sum(0,i+1)%2==1)t=-1;
            seg.update(i,A[i]*t);
        }
    }
    rep(q,Q){
        int l,r;cin>>l>>r;l--;r--;
        if(A[l]!=A[r]){
            if(A[l]!=0&&A[r]!=0){
                int t=1;
                if(S.Sum(0,l+1)%2==1)t=-1;
                seg.update(l,A[r]*t);
                t=1;
                if(S.Sum(0,r+1)%2==1)t=-1;
                seg.update(r,A[l]*t);
                swap(A[l],A[r]);
            }
            else if(A[l]==0){
                S.update(r+1,1);
                S.update(l+1,0);
                int t=1;
                if(S.Sum(0,l+1)%2==1)t=-1;
                seg.update(l,A[r]*t);
                seg.update(r,0);
                seg.flip(l+1,r);
                swap(A[l],A[r]);
            }
            else if(A[r]==0){
                S.update(r+1,0);
                S.update(l+1,1);
                int t=1;
                if(S.Sum(0,r+1)%2==1)t=-1;
                seg.update(r,A[l]*t);
                seg.update(l,0);
                seg.flip(l+1,r);
                swap(A[l],A[r]);
            }
        }
        ll d = seg.allsum();
        d=1-d;
        ll k = (abs(d)+K-1)/K;
        d+=k*K;
        d%=K;
        if(d==0)d=K;
        if(S.Sum(0,N+1)%2==1)d*=-1;
        cout<<d<<endl;
    }
}
accuracy: 1
time_ms: 210
memory_kb: 10008
final_rank: 15

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

using i64 = long long;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define repi(i, a, b) for (int i = (a) - 1; i >= (b); --i)

struct S {
    i64 a, b;
    S() : a(1), b(0) {}
    S(i64 a_, i64 b_) : a(a_), b(b_) {}
};

S op(S l, S r) {
    return S(l.a * r.a, l.b + l.a * r.b);
}

S e() { return S(); }

struct Seg {
    int _n, size, log;
    vector<S> d;
    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }

    Seg() = default;
    Seg(const vector<int> &v) : _n(v.size()), log(0) {
        while ((1 << log) < _n) log++;
        size = 1 << log;
        d = vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) {
            if (v[i] == 0) d[size + i] = S(-1, 0);
            else d[size + i] = S(1, -v[i]);
            // cout << d[i].a << " " << d[i].b << endl;
        }
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p and p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p and p < _n);
        return d[p + size];
    }

    S all_prod() { return d[1]; }
};

int main() {
    int k, n, q;
    cin >> k >> n >> q;
    vector<int> a(n);
    rep (i, 0, n) cin >> a[i];
    Seg seg(a);
    // repi (i, 8, 1)
    // cout << seg.d[i].a << " " << seg.d[i].b << endl;

    rep (_, 0, q) {
        int l, r;
        cin >> l >> r;
        --l, --r;
        S left = seg.get(l), right = seg.get(r);
        seg.set(l, right);
        seg.set(r, left);
        S f = seg.all_prod();
        // cout << f.a << " " << f.b << endl;
        int pos = (1 + f.b) % k;
        if (pos < 0) pos += k;
        if (pos == 0) pos = k;
        if (f.a == 1) cout << pos << "\n";
        else cout << -pos << "\n";
    }

    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 7520
final_rank: 10

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

#define int long long

/*
6:27~
*/

template<class S,S(*op)(S,S),S(*e)()>
struct segtree{
	vector<S> dat;
	int n;

	segtree(int n_){
		int x=1;
		while(x<n_)x*=2;
		n=x;
		dat.resize(2*n,e());
	}

	void set(int p,S x){
		p+=n;
		dat[p]=x;
		while(p>1){
			p/=2;
			dat[p]=op(dat[2*p],dat[2*p+1]);
		}
	}

	S prod_sub(int l,int r,int idx,int a,int b){
		if(r<=a||b<=l)return e();
		if(a<=l&&r<=b)return dat[idx];
		int m=(l+r)/2;
		S resL=prod_sub(l,m,2*idx,a,b);
		S resR=prod_sub(m,r,2*idx+1,a,b);
		return op(resL,resR);
	}

	S prod(int l,int r){
		return prod_sub(0,n,1,l,r);
	}
};

int mod;

using S=array<array<int,2>,2>;

S op(S x,S y){
	S z={};
	for(int i=0;i<2;i++){
		for(int j=0;j<2;j++){
			for(int k=0;k<2;k++){
				z[i][j]+=x[i][k]*y[k][j];
			}
		}
	}
	z[0][0]=(z[0][0]%mod+mod)%mod;
	z[1][0]=(z[1][0]%mod+mod)%mod;
	return z;
}

S e(){
	S z;
	z[0]={1,0};
	z[1]={0,1};
	return z;
}

signed main(){
	int K,n,q;cin>>K>>n>>q;
	mod=K;
	vector<int> a(n);
	for(auto&&e:a)cin>>e;
	
	segtree<S,op,e> seg(n);

	auto update=[&](int i){
		if(a[i]==0){
			S z;
			z[0]={1,0};
			z[1]={0,-1};
			seg.set(i,z);
		}else{
			if(a[i]<0){
				S z;
				z[0]={1,0};
				z[1]={-a[i],1};
				seg.set(i,z);
			}else{
				S z;
				z[0]={1,0};
				z[1]={mod-a[i],1};
				seg.set(i,z);
			}
		}
	};

	for(int i=0;i<n;i++){
		update(i);
	}

	while(q--){
		int l,r;cin>>l>>r;l--;r--;

		swap(a[l],a[r]);
		update(l);
		update(r);

		auto ret=seg.prod(0,n);

		if(ret[1][1]==1){
			cout<<(ret[1][0])+1<<endl;
		}else{
			cout<<-(ret[1][0]+1)<<endl;
		}
	}
}
accuracy: 1
time_ms: 320
memory_kb: 12144
final_rank: 18

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

int main()
{
	long long k,n,q;
	cin >> k >> n >> q;
	vector<long long> a(n);
	long long m = 1ll;
	while(m * m < n) m++;
	vector<long long> u(m,false),v(m,0ll),t(n,true);
	for(long long i = 0ll;i < n;i++){
		cin >> a[i];
		a[i] = -a[i];
		if(i != 0ll){
			t[i] = t[i - 1ll];
		}
		if(a[i] == 0ll){
			t[i] ^= 1ll;
		}
		if(t[i]){
			v[i / m] += a[i];
		}else{
			v[i / m] -= a[i];
		}
	}
	auto f = [&](long long x,long long y){
		bool flg = false;
		if((a[x] == 0ll && y != 0ll) || (a[x] != 0ll && y == 0ll)){
			flg = true;
		}
		a[x] = y;
		for(long long i = 0ll;i < m;i++){
			long long left = i * m,right = left + m - 1ll;
			if(right < x){
				continue;
			}
			if(left <= x && x <= right){
				v[i] = 0ll;
				for(long long j = left;j <= min(right,n - 1ll);j++){
					if(u[i]){
						t[j] ^= true;
					}
					if(flg && x <= j){
						t[j] ^= true;
					}
					if(t[j]){
						v[i] += a[j];
					}else{
						v[i] -= a[j];
					}
				}
				u[i] = false;
			}else{
				u[i] ^= flg;
			}
		}
	};
	auto cal = [&]() -> long long {
		long long ans = 0ll;
		for(long long i = 0ll;i < m;i++){
			if(u[i]){
				ans -= v[i];
			}else{
				ans += v[i];
			}
		}
		ans += k * (abs(ans) / k + 10ll);
		ans %= k;
		ans++;
		if((t[n - 1ll] ^ u[(n - 1ll) / m]) == false){
			ans = -ans;
		}
		return ans;
	};
	for(long long i = 0ll;i < q;i++){
		long long x,y;
		cin >> x >> y;
		x--;y--;
		long long X = a[x],Y = a[y];
		f(x,Y);
		f(y,X);
		cout << cal() << endl;
	}
}
accuracy: 1
time_ms: 250
memory_kb: 4660
final_rank: 17

code_snippet: #include<stdio.h>
#include<iostream>
#include<vector>
#include<cassert>
#include<algorithm>
using namespace std;

static const int INFTY = (1LL<<31)-1;
static const int MAX_N = (1<<17); // 131072

long long K, N, Q, A[MAX_N];

int n;
long long D[2*MAX_N-1];
int F[2*MAX_N-1];

void init(int n_){
  n = 1;
  while( n < n_ ) n *= 2;
  for ( int i = 0; i < 2*n-1; i++ ) D[i] = 0;
  for ( int i = 0; i < 2*n-1; i++ ) F[i] = 0;
}

void update(int k, int a){
  k += n - 1;
  D[k] = a;
  F[k] = (a == 0);
  while( k > 0 ){
    k = (k - 1)/2;
    D[k] = D[k*2+1] + D[k*2+2]*(F[k*2+1]?(-1):1);
    F[k] = (F[k*2+1] + F[k*2+2])%2;
  }
}

// [a, b)
pair<long long,int> query(int a, int b, int k, int l, int r){
  if ( r <= a || b <= l ) return make_pair(0, 0);
  if ( a <= l && r <= b ) return make_pair(D[k], F[k]);
  else{
    //int vl = query(a, b, k*2 + 1, l, (l + r)/2);
    //    int vr = query(a, b, k*2 + 2, (l + r)/2, r);
    //    return min(vl, vr);
    pair<long long, int> vl = query(a, b, k*2 + 1, l, (l + r)/2);
    pair<long long, int> vr = query(a, b, k*2 + 2, (l + r)/2, r);
    pair<long long, int> res;
    res.first = vl.first + vr.first*(vl.second?(-1):1);
    res.second = (vl.second + vr.second)%2;
    return res;
  }
}

// [a, b]
pair<long long,int> findMin(int a, int b){
  return query(a, b+1, 0, 0, n);
}

void dump(){
  for ( int i = 0; i < n; i++ ) cout << D[i] << " ";
  cout << endl;
}

main(){
  int L, R;
  cin >> K >> N >> Q;
  init(N);
  for ( int i = 0; i < N; i++ ) cin >> A[i];
  for ( int i = 0; i < N; i++ ) update(i, A[i]);
  for ( int i = 0; i < Q; i++ ){
    cin >> L >> R; L--; R--;
    swap(A[L], A[R]);
    update(L, A[L]);
    update(R, A[R]);
    pair<long long, int> res = findMin(0, N-1);
    long long p = res.first;
    bool isng = (p < 0);

    if ( isng ) p *= -1;
    p %= K;
    if ( isng ) cout << (p+1)*(res.second?-1:1) << endl;
    else cout << ((K-p)%K+1)*(res.second?-1:1) << endl;
  }


}
accuracy: 1
time_ms: 180
memory_kb: 6936
final_rank: 13

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


#include <algorithm>
#include <cassert>
#include <functional>
#include <vector>


#ifdef _MSC_VER
#include <intrin.h>
#endif

#if __cplusplus >= 202002L
#include <bit>
#endif

namespace atcoder {

namespace internal {

#if __cplusplus >= 202002L

using std::bit_ceil;

#else

unsigned int bit_ceil(unsigned int n) {
    unsigned int x = 1;
    while (x < (unsigned int)(n)) x *= 2;
    return x;
}

#endif

int countr_zero(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}

constexpr int countr_zero_constexpr(unsigned int n) {
    int x = 0;
    while (!(n & (1 << x))) x++;
    return x;
}

}  // namespace internal

}  // namespace atcoder


namespace atcoder {

#if __cplusplus >= 201703L

template <class S,
          auto op,
          auto e,
          class F,
          auto mapping,
          auto composition,
          auto id>
struct lazy_segtree {
    static_assert(std::is_convertible_v<decltype(op), std::function<S(S, S)>>,
                  "op must work as S(S, S)");
    static_assert(std::is_convertible_v<decltype(e), std::function<S()>>,
                  "e must work as S()");
    static_assert(
        std::is_convertible_v<decltype(mapping), std::function<S(F, S)>>,
        "mapping must work as F(F, S)");
    static_assert(
        std::is_convertible_v<decltype(composition), std::function<F(F, F)>>,
        "compostiion must work as F(F, F)");
    static_assert(std::is_convertible_v<decltype(id), std::function<F()>>,
                  "id must work as F()");

#else

template <class S,
          S (*op)(S, S),
          S (*e)(),
          class F,
          S (*mapping)(F, S),
          F (*composition)(F, F),
          F (*id)()>
struct lazy_segtree {

#endif

  public:
    lazy_segtree() : lazy_segtree(0) {}
    explicit lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
    explicit lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) {
        size = (int)internal::bit_ceil((unsigned int)(_n));
        log = internal::countr_zero((unsigned int)size);
        d = std::vector<S>(2 * size, e());
        lz = std::vector<F>(size, id());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        return d[p];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return e();

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        S sml = e(), smr = e();
        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }

        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    void apply(int p, F f) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = mapping(f, d[p]);
        for (int i = 1; i <= log; i++) update(p >> i);
    }
    void apply(int l, int r, F f) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return;

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        {
            int l2 = l, r2 = r;
            while (l < r) {
                if (l & 1) all_apply(l++, f);
                if (r & 1) all_apply(--r, f);
                l >>= 1;
                r >>= 1;
            }
            l = l2;
            r = r2;
        }

        for (int i = 1; i <= log; i++) {
            if (((l >> i) << i) != l) update(l >> i);
            if (((r >> i) << i) != r) update((r - 1) >> i);
        }
    }

    template <bool (*g)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return g(x); });
    }
    template <class G> int max_right(int l, G g) {
        assert(0 <= l && l <= _n);
        assert(g(e()));
        if (l == _n) return _n;
        l += size;
        for (int i = log; i >= 1; i--) push(l >> i);
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!g(op(sm, d[l]))) {
                while (l < size) {
                    push(l);
                    l = (2 * l);
                    if (g(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*g)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return g(x); });
    }
    template <class G> int min_left(int r, G g) {
        assert(0 <= r && r <= _n);
        assert(g(e()));
        if (r == 0) return 0;
        r += size;
        for (int i = log; i >= 1; i--) push((r - 1) >> i);
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!g(op(d[r], sm))) {
                while (r < size) {
                    push(r);
                    r = (2 * r + 1);
                    if (g(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;
    std::vector<F> lz;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
    void all_apply(int k, F f) {
        d[k] = mapping(f, d[k]);
        if (k < size) lz[k] = composition(f, lz[k]);
    }
    void push(int k) {
        all_apply(2 * k, lz[k]);
        all_apply(2 * k + 1, lz[k]);
        lz[k] = id();
    }
};

}  // namespace atcoder

using namespace atcoder;

using S = array<long long, 2>;
using F = long long;

S op(S x, S y) {
  return S{x[0]+y[0], x[1]+y[1]};
}
S e() {
  return S{0, 0};
}
S mapping(F f, S x) {
  return S{x[0]*f, x[1]};
}
F composition(F f, F g) {
  return f*g;
}
F id() {
  return 1;
}

int main() {
  int N, Q;
  long long K;
  cin >> K >> N >> Q;
  lazy_segtree<S, op, e, F, mapping, composition, id> seg(N);
  for(int i = 0; i < N; i++) {
    long long A;
    cin >> A;
    seg.set(i, S{A, (A == 0)});
  }
  for(int i = 0; i < N; i++) {
    if(seg.get(i)[0] == 0) seg.apply(i, N, -1);
  }
  for(int q = 0; q < Q; q++) {
    int L, R;
    cin >> L >> R;
    L--, R--;
    S vl = seg.get(L), vr = seg.get(R);
    S pl = seg.prod(0, L), pr = seg.prod(0, R);
    if(vl[0] == 0) seg.apply(L, R, -1);
    if(vr[0] == 0) seg.apply(L, R, -1);
    if(vl[0] != 0 && pl[1]%2 != (pr[1]+vr[1])%2) vl[0] *= -1;
    if(vr[0] != 0 && pr[1]%2 != pl[1]%2) vr[0] *= -1;
    seg.set(L, vr), seg.set(R, vl);
    auto [ap, c0] = seg.all_prod();
    ap = ((ap%K)+K)%K;
    cout << (c0%2 == 0 ? 1 : -1)*(ap == 0 ? 1 : K+1-ap) << endl;
  }
}
accuracy: 1
time_ms: 180
memory_kb: 9904
final_rank: 14

code_snippet: #include <vector>
#include <iostream>
using namespace std;

template<class S> class segtree {
private:
	int bits;
	vector<S> val;
public:
	segtree() : bits(0) {}
	segtree(vector<S> v) : bits(v.size() >= 2 ? 32 - __builtin_clz(v.size() - 1) : 0), val(vector<S>(2 << bits, S::e())) {
		for (int i = 0; i < v.size(); i++) {
			val[(1 << bits) + i] = v[i];
		}
		for (int i = (1 << bits) - 1; i >= 0; i--) {
			val[i] = val[i * 2 + 0].op(val[i * 2 + 1]);
		}
	}
	void update(int pos, S x) {
		pos += 1 << bits;
		val[pos] = x;
		while (pos > 1) {
			pos /= 2;
			val[pos] = val[pos * 2 + 0].op(val[pos * 2 + 1]);
		}
	}
	S query(int l, int r) {
		S lq = S::e(), rq = S::e();
		l += 1 << bits;
		r += 1 << bits;
		while (l != r) {
			if (l & 1) lq = lq.op(val[l++]);
			if (r & 1) rq = val[--r].op(rq);
			l /= 2;
			r /= 2;
		}
		return lq.op(rq);
	}
};

class monoid {
private:
	int f; long long x;
public:
	monoid() : f(0), x(0) {}
	monoid(int f_, long long x_) : f(f_), x(x_) {}
	static monoid e() { return {0, 0}; }
	int get(int l) const {
		int p = int(x >= 0 ? x % l : (x + 1) % l + (l - 1));
		return (f == 0 ? p + 1 : -(p + 1));
	}
	monoid op(const monoid& m) const {
		return {f ^ m.f, f == 0 ? x + m.x : x - m.x};
	}
};

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int K, N, Q;
	cin >> K >> N >> Q;
	vector<monoid> V(N);
	for (int i = 0; i < N; i++) {
		int a;
		cin >> a;
		V[i] = (a != 0 ? monoid(0, -a) : monoid(1, 0));
	}
	segtree<monoid> seg(V);
	for (int i = 0; i < Q; i++) {
		int l, r;
		cin >> l >> r;
		l--; r--;
		swap(V[l], V[r]);
		seg.update(l, V[l]);
		seg.update(r, V[r]);
		monoid res = seg.query(0, N);
		long long ans = res.get(K);
		cout << ans << '\n';
	}
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 10296
final_rank: 2

code_snippet: #include <bits/stdc++.h>

struct Data {
    Data(long long v = 0, bool i = false): value(v), inv(i) {}
    long long value;
    bool inv;
};

Data op(Data lhs, Data rhs) {
    return Data(lhs.value + (lhs.inv ? -1 : 1) * rhs.value, lhs.inv ^ rhs.inv);
}

struct SegTree {
    int size;
    std::vector<Data> data;
    SegTree(const std::vector<long long>& vec): size(vec.size()), data(2 * vec.size()) {
        for (int i = 0; i < size; i++) {
            if (vec[i] == 0) {
                data[i + size] = Data(vec[i], true);
            } else {
                data[i + size] = Data(vec[i], false);
            }
        }
        for (int i = size - 1; i > 0; i--) {
            data[i] = op(data[2 * i], data[2 * i + 1]);
        }
    }
    void swap(int lhs, int rhs) {
        auto ld = data[lhs + size];
        auto rd = data[rhs + size];
        set(lhs, rd);
        set(rhs, ld);
    }
    void set(int pos, Data d) {
        pos += size;
        data[pos] = d;
        for (pos >>= 1; pos > 0; pos >>= 1) {
            data[pos] = op(data[2 * pos], data[2 * pos + 1]);
        }
    }
    Data all_prod() {
        Data lhs;
        Data rhs;
        for (int left = size, right = 2 * size; left < right; left >>= 1, right >>= 1) {
            if (left & 1) lhs = op(lhs, data[left++]);
            if (right & 1) rhs = op(data[--right], rhs);
        }
        return op(lhs, rhs);
    }
};

int main() {
    long long k;
    int n, q;
    std::cin >> k >> n >> q;
    const long long INF = ((long long)1e15 / k) * k;
    std::vector<long long> a(n);
    for (int i = 0; i < n; i++) std::cin >> a[i];
    SegTree segtree(a);
    // auto d = segtree.all_prod();
    // std::cerr << d.value << ' ' << d.inv << '\n';
    for (int i = 0; i < q; i++) {
        int l, r;
        std::cin >> l >> r;
        l--;
        r--;
        segtree.swap(l, r);
        auto d = segtree.all_prod();
        // std::cerr << d.value << ' ' << d.inv << '\n';
        long long pos = (INF - d.value) % k + 1;
        std::cout << (d.inv ? -1 : 1) * pos << '\n';
    }
}
accuracy: 1
time_ms: 120
memory_kb: 6996
final_rank: 9

code_snippet: #include <iostream>
#include <vector>
using namespace std;

// ============================================================== Range Sum & Range Inverse & Range Add ==============================================================
class SegmentTree {
public:
	int size_ = 1;
	vector<long long> laz;
	vector<long long> dat;
	
	// Initialize
	void init(int sz) {
		size_ = 1;
		while (size_ <= sz) size_ *= 2;
		dat.resize(size_ * 2, 0);
		laz.resize(size_ * 2, 1);
	}

	// Refresh laz
	void refresh(int pos) {
		if (pos < size_) {
			laz[pos * 2 + 0] *= laz[pos];
			laz[pos * 2 + 1] *= laz[pos];
			dat[pos] = dat[pos * 2 + 0] * laz[pos * 2 + 0] + dat[pos * 2 + 1] * laz[pos * 2 + 1];
			laz[pos] = 1;
		}
		else {
			dat[pos] *= laz[pos];
			laz[pos] = 1;
		}
	}

	// Add dat[pos] by x
	void add(int pos, long long x) {
		vector<int> path;
		int cur = pos + size_;
		while (cur >= 1) {
			path.push_back(cur);
			cur >>= 1;
		}
		for (int i = path.size() - 1; i >= 0; i--) refresh(path[i]);
		pos += size_;
		dat[pos] += x;
		while (pos >= 2) {
			pos >>= 1;
			refresh(pos);
		}
	}

	// Helper
	void inverse_(int l, int r, int a, int b, int u) {
		if (l <= a && b <= r) {
			laz[u] *= -1;
			refresh(u);
			return;
		}
		if (r <= a || b <= l) return;
		refresh(u);
		inverse_(l, r, a, (a + b) / 2, u * 2 + 0);
		inverse_(l, r, (a + b) / 2, b, u * 2 + 1);
		refresh(u);
	}

	// Inverse Range [l, r)
	void inverse(int l, int r) {
		inverse_(l, r, 0, size_, 1);
	}

	// Get Sum
	long long getsum() {
		return dat[1];
	}
};

// ============================================================== Binary Indexed Tree ==============================================================
class BIT {
public:
	int size_;
	vector<long long> dat;

	void init(int sz) {
		size_ = sz + 2;
		dat.resize(size_ + 2, 0);
	}

	void add(int pos, long long x) {
		pos += 1;
		while (pos <= size_) {
			dat[pos] += x;
			pos += (pos & -pos);
		}
	}

	long long sum(int pos) {
		long long s = 0; pos++;
		while (pos >= 1) {
			s += dat[pos];
			pos -= (pos & -pos);
		}
		return s;
	}
};

// ============================================================== Main Part ==============================================================
long long INF = 2000000000;
long long N, A[100009];
long long K;
long long Q, L[100009], R[100009];
long long Record[100009];
BIT Z1;
SegmentTree Z2;

void Add(int pos, long long x) {
	if (x == 0) {
		Z1.add(pos, 1);
		Z2.inverse(pos, N + 1);
	}
	else {
		int cnt = Z1.sum(pos);
		if (cnt % 2 == 0) Z2.add(pos, +x);
		if (cnt % 2 == 1) Z2.add(pos, -x);
	}
	Record[pos] = x;
}

void Delete(int pos) {
	if (Record[pos] == 0) {
		Z1.add(pos, -1);
		Z2.inverse(pos, N + 1);
	}
	else {
		int cnt = Z1.sum(pos);
		if (cnt % 2 == 0) Z2.add(pos, -Record[pos]);
		if (cnt % 2 == 1) Z2.add(pos, +Record[pos]);
	}
	Record[pos] = INF;
}

int main() {
	// Step 1. Input
	cin >> K >> N >> Q;
	for (int i = 1; i <= N; i++) cin >> A[i];
	for (int i = 1; i <= Q; i++) cin >> L[i] >> R[i];

	// Step 2. Initialize
	Z1.init(N + 2);
	Z2.init(N + 2);
	for (int i = 1; i <= N; i++) Record[i] = INF;
	for (int i = 1; i <= N; i++) Add(i, A[i]);
	long long cnt3 = Z1.sum(N);
	long long cnt4 = Z2.getsum();

	// Step 3. Simulation
	for (int i = 1; i <= Q; i++) {
		Delete(L[i]);
		Delete(R[i]);
		swap(A[L[i]], A[R[i]]);
		Add(L[i], A[L[i]]);
		Add(R[i], A[R[i]]);
		long long cnt1 = Z1.sum(N);
		long long cnt2 = Z2.getsum();
		long long eval = (-cnt2 + 1000000000LL * K) % K;
		if (cnt1 % 2 == 0) cout << +(eval + 1) << endl;
		if (cnt1 % 2 == 1) cout << -(eval + 1) << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 10600
final_rank: 16

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 0390.cc: Disc
 */

#include<cstdio>
#include<vector>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 100000;

/* typedef */

struct Elm {
  static int k;
  int x, d;  // 0<=x<k, d=1 or -1
  Elm(): x(0), d(1) {}
  Elm(int _x, int _d): x(_x), d(_d) {}
  Elm(int a) {
    if (a == 0) x = 0, d = -1;
    else if (a > 0) x = (k - a % k) % k, d = 1;
    else x = -a % k, d = 1;
  }

  Elm operator+(const Elm &e) const {
    int dx = (d > 0) ? e.x : (k - e.x) % k;
    return Elm((x + dx) % k, d * e.d);
  }

  int val() { return (x + 1) * d; }
};

int Elm::k;

template <typename T>
struct SegTreeSum {
  int e2;
  vector<T> nodes;
  T defv;
  SegTreeSum() {}

  void init(int n, T _defv) {
    defv = _defv;
    for (e2 = 1; e2 < n; e2 <<= 1);
    nodes.assign(e2 * 2, defv);
  }

  T &geti(int i) { return nodes[e2 - 1 + i]; }
  void seti(int i, T v) { geti(i) = v; }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
  }

  void set(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] = v;
    while (j > 0) {
      j = (j - 1) / 2;
      nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
    }
  }

  T sum_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return defv;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    int im = (i0 + i1) / 2;
    T v0 = sum_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = sum_range(r0, r1, k * 2 + 2, im, i1);
    return v0 + v1;
  }
  T sum_range(int r0, int r1) { return sum_range(r0, r1, 0, 0, e2); }
};

/* global variables */

int as[MAX_N];
SegTreeSum<Elm> st;

/* subroutines */

/* main */

int main() {
  int k, n, qn;
  scanf("%d%d%d", &k, &n, &qn);
  Elm::k = k;

  for (int i = 0; i < n; i++) scanf("%d", as + i);

  st.init(n, Elm(0, 1));
  for (int i = 0; i < n; i++) st.seti(i, Elm(as[i]));
  st.setall();

  //Elm e = st.sum_range(0, n);
  //printf("%d\n", e.val());

  while (qn--) {
    int l, r;
    scanf("%d%d", &l, &r);
    l--, r--;

    swap(as[l], as[r]);
    st.set(l, Elm(as[l]));
    st.set(r, Elm(as[r]));

    Elm e = st.sum_range(0, n);
    printf("%d\n", e.val());
  }
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 4964
final_rank: 5

code_snippet: #include<bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 1e9 + 7;
// const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;


template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in: v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e: t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

/**
 * @brief Segment Tree(セグメント木)
 * @docs docs/segment-tree.md
 */
template< typename Monoid, typename F >
struct SegmentTree {
  int n, sz;
  vector< Monoid > seg;

  const F f;
  const Monoid M1;

  SegmentTree() = default;

  explicit SegmentTree(int n, const F f, const Monoid &M1) : n(n), f(f), M1(M1) {
    sz = 1;
    while(sz < n) sz <<= 1;
    seg.assign(2 * sz, M1);
  }

  explicit SegmentTree(const vector< Monoid > &v, const F f, const Monoid &M1) :
      SegmentTree((int) v.size(), f, M1) {
    build(v);
  }

  void build(const vector< Monoid > &v) {
    assert(n == (int) v.size());
    for(int k = 0; k < n; k++) seg[k + sz] = v[k];
    for(int k = sz - 1; k > 0; k--) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  void set(int k, const Monoid &x) {
    k += sz;
    seg[k] = x;
    while(k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  Monoid get(int k) const {
    return seg[k + sz];
  }

  Monoid operator[](const int &k) const {
    return get(k);
  }

  void apply(int k, const Monoid &x) {
    k += sz;
    seg[k] = f(seg[k], x);
    while(k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  Monoid prod(int l, int r) const {
    Monoid L = M1, R = M1;
    for(l += sz, r += sz; l < r; l >>= 1, r >>= 1) {
      if(l & 1) L = f(L, seg[l++]);
      if(r & 1) R = f(seg[--r], R);
    }
    return f(L, R);
  }

  Monoid all_prod() const {
    return seg[1];
  }

  template< typename C >
  int find_first(int l, const C &check) const {
    if(l >= n) return n;
    l += sz;
    Monoid sum = M1;
    do {
      while((l & 1) == 0) l >>= 1;
      if(check(f(sum, seg[l]))) {
        while(l < sz) {
          l <<= 1;
          auto nxt = f(sum, seg[l]);
          if(not check(nxt)) {
            sum = nxt;
            l++;
          }
        }
        return l + 1 - sz;
      }
      sum = f(sum, seg[l++]);
    } while((l & -l) != l);
    return n;
  }

  template< typename C >
  int find_last(int r, const C &check) const {
    if(r <= 0) return -1;
    r += sz;
    Monoid sum = 0;
    do {
      r--;
      while(r > 1 and (r & 1)) r >>= 1;
      if(check(f(seg[r], sum))) {
        while(r < sz) {
          r = (r << 1) + 1;
          auto nxt = f(seg[r], sum);
          if(not check(nxt)) {
            sum = nxt;
            r--;
          }
        }
        return r - sz;
      }
      sum = f(seg[r], sum);
    } while((r & -r) != r);
    return -1;
  }
};

template< typename Monoid, typename F >
SegmentTree< Monoid, F > get_segment_tree(int N, const F &f, const Monoid &M1) {
  return SegmentTree{N, f, M1};
}

template< typename Monoid, typename F >
SegmentTree< Monoid, F > get_segment_tree(const vector< Monoid > &v, const F &f, const Monoid &M1) {
  return SegmentTree{v, f, M1};
}

int main() {
  int K, N, Q;
  cin >> K >> N >> Q;
  vector< int > A(N);
  cin >> A;
  using pi = pair< bool, int >;
  auto f = [&](pi a, pi b) {
    if(a.first) b.second = K - b.second;
    return pi(a.first ^ b.first, (a.second + b.second) % K);
  };
  vector< pi > dat(N);
  for(int i = 0; i < N; i++) {
    if(A[i] == 0) dat[i] = {true, 0};
    else if(A[i] > 0) dat[i] = {false, (K - A[i] % K) % K};
    else dat[i] = {false, -(A[i] % K)};
  }
  auto seg = get_segment_tree(dat, f, pi());
  while(Q--) {
    int l, r;
    cin >> l >> r;
    --l, --r;
    swap(dat[l], dat[r]);
    seg.set(l, dat[l]);
    seg.set(r, dat[r]);
    auto t = seg.all_prod();
    if(t.first) cout << "-";
    cout << t.second + 1 << "\n";
  }
}
accuracy: 1
time_ms: 40
memory_kb: 6252
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n)  for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define pb emplace_back
#define lb(v,k) (lower_bound(all(v),(k))-v.begin())
#define ub(v,k) (upper_bound(all(v),(k))-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
#define popcnt __builtin_popcountll
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=998244353;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void outset(T s){auto itr=s.begin();while(itr!=s.end()){if(itr!=s.begin())cout<<' ';cout<<*itr;itr++;}cout<<'\n';}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
#define T P
class segtree{
    vector<T> seg;ll n;
public:
    T g=P(0,0);
    T f(T a,T b){
        if(a.se==0)return P(a.fi+b.fi,a.se^b.se);
        return P(a.fi-b.fi,a.se^b.se);
    }
    segtree(vector<T> v){
        seg=vector<T>(n*2,g);
        rep(i,v.size())seg[i+n]=v[i];
        for(ll i=n-1;i;i--)seg[i]=f(seg[i*2],seg[i*2+1]);
    }
    void update(ll i,T x){
        i+=n;seg[i]=x;
        for(i>>=1;i;i>>=1)seg[i]=f(seg[i*2],seg[i*2+1]);
    }
    T get(ll l,ll r){
        T resl=g,resr=g;
        for(l+=n,r+=n;l<r;l>>=1,r>>=1){
            if(l&1)resl=f(resl,seg[l++]);
            if(r&1)resr=f(seg[--r],resr);
        }
        return f(resl,resr);
    }
};
int main(){
    ll n,m,q;cin>>m>>n>>q;
    vp v(n);
    rep(i,n){
        cin>>v[i].fi;if(v[i].fi==0)v[i].se=1;
    }
    segtree seg(v);
    rep(i,q){
        ll l,r;cin>>l>>r;l--;r--;
        seg.update(l,v[r]);
        seg.update(r,v[l]);
        P res=seg.get(0,n);
        ll ans=(m-res.fi%m)%m+1;if(res.se)ans*=-1;
        out(ans);
        swap(v[l],v[r]);
    }
}
accuracy: 1
time_ms: 150
memory_kb: 137384
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n)  for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define pb emplace_back
#define lb(v,k) (lower_bound(all(v),(k))-v.begin())
#define ub(v,k) (upper_bound(all(v),(k))-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
#define popcnt __builtin_popcountll
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=998244353;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void outset(T s){auto itr=s.begin();while(itr!=s.end()){if(itr!=s.begin())cout<<' ';cout<<*itr;itr++;}cout<<'\n';}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
class segtree{
    ll n;
    vp seg;
    P calc(ll a){
        if(a==0)return P(0,1);
        return P(a,0);
    }
    P f(P a,P b){
        if(a.se==0)return P(a.fi+b.fi,a.se^b.se);
        return P(a.fi-b.fi,a.se^b.se);
    }
public:
    segtree(vi v){
        n=v.size();
        seg=vp(n*2);
        rep(i,n)seg[i+n]=calc(v[i]);
        for(int i=n-1;i;i--)seg[i]=f(seg[i*2],seg[i*2+1]);
    }
    P get(ll l,ll r){
        P resl=P(0,0),resr=P(0,0);
        for(l+=n,r+=n;l<r;l>>=1,r>>=1){
            if(l&1)resl=f(resl,seg[l++]);
            if(r&1)resr=f(seg[--r],resr);
        }
        return f(resl,resr);
    }
    void update(ll i,ll x){
        i+=n;seg[i]=calc(x);
        for(i>>=1;i;i>>=1)seg[i]=f(seg[i*2],seg[i*2+1]);
    }
};
int main(){
    ll n,m,q;cin>>m>>n>>q;
    vi v(n);rep(i,n)cin>>v[i];
    segtree seg(v);
    rep(i,q){
        ll l,r;cin>>l>>r;l--;r--;
        seg.update(l,v[r]);
        seg.update(r,v[l]);
        P res=seg.get(0,n);
        ll ans=(m-res.fi%m)%m+1;if(res.se)ans*=-1;
        out(ans);
        swap(v[l],v[r]);
    }
}
accuracy: 1
time_ms: 120
memory_kb: 7660
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n)  for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define pb emplace_back
#define lb(v,k) (lower_bound(all(v),(k))-v.begin())
#define ub(v,k) (upper_bound(all(v),(k))-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
#define popcnt __builtin_popcountll
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=998244353;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void outset(T s){auto itr=s.begin();while(itr!=s.end()){if(itr!=s.begin())cout<<' ';cout<<*itr;itr++;}cout<<'\n';}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
class segtree{
    ll n;
    vp seg;
    P calc(ll a){
        if(a==0)return P(0,1);
        return P(a,0);
    }
    P f(P a,P b){
        if(a.se==0)return P(a.fi+b.fi,a.se^b.se);
        return P(a.fi-b.fi,a.se^b.se);
    }
public:
    segtree(vi v){
        n=v.size();
        seg=vp(n*2);
        rep(i,n)seg[i+n]=calc(v[i]);
        for(int i=n-1;i;i--)seg[i]=f(seg[i*2],seg[i*2+1]);
    }
    P get(ll l,ll r){
        stack<P> st;
        P res=P(0,0);
        for(l+=n,r+=n;l<r;l>>=1,r>>=1){
            if(l&1)res=f(res,seg[l++]);
            if(r&1)st.push(seg[--r]);
        }
        while(st.size()){
            res=f(res,st.top());st.pop();
        }
        return res;
    }
    void update(ll i,ll x){
        i+=n;seg[i]=calc(x);
        for(i>>=1;i;i>>=1)seg[i]=f(seg[i*2],seg[i*2+1]);
    }
};
int main(){
    ll n,m,q;cin>>m>>n>>q;
    vi v(n);rep(i,n)cin>>v[i];
    segtree seg(v);
    rep(i,q){
        ll l,r;cin>>l>>r;l--;r--;
        seg.update(l,v[r]);
        seg.update(r,v[l]);
        P res=seg.get(0,n);
        ll ans=(m-res.fi%m)%m+1;if(res.se)ans*=-1;
        out(ans);
        swap(v[l],v[r]);
    }
}
accuracy: 1
time_ms: 130
memory_kb: 7852
final_rank: 12

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <cstdint>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <functional>
#include <ctime>
#include <fstream>
#include <cmath>
#include <limits>
#include <chrono>
#include <numeric>
#include <type_traits>
#include <iomanip>
#include <float.h>
#include <math.h>
#include <cassert>
#include <random>
#pragma warning (disable: 4996)
using namespace std;

using ll = long long;
/*unsigned euclidean_gcd(unsigned a, unsigned b) {
    if (a < b) return euclidean_gcd(b, a);
    unsigned r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

ll ll_gcd(ll a, ll b) {
    if (a < b) return ll_gcd(b, a);
    ll r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

struct UnionFind {
    vector <ll> par;
    vector <ll> siz;
    UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    void init(ll sz_) {
        par.resize(sz_);
        siz.assign(sz_, 1LL);
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    ll root(ll x) {
        while (par[x] != x) {
            x = par[x] = par[par[x]];
        }
        return x;
    }
    bool merge(ll x, ll y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }

    bool issame(ll x, ll y) {
        return root(x) == root(y);
    }

    ll size(ll x) {
        return siz[root(x)];
    }
};

long long modpow(long long a, long long n, long long mod) {
    if (n < 0)return 0;
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

long long modinv(long long a, long long mod) {
    return modpow(a, mod - 2, mod);
}

ll merge_cnt(vector<ll>& a) {
    int n = a.size();
    if (n <= 1) { return 0; }

    ll cnt = 0;
    vector<ll> b(a.begin(), a.begin() + n / 2);
    vector<ll> c(a.begin() + n / 2, a.end());

    cnt += merge_cnt(b);
    cnt += merge_cnt(c);

    int ai = 0, bi = 0, ci = 0;
    while (ai < n) {
        if (bi < b.size() && (ci == c.size() || b[bi] <= c[ci])) {
            a[ai++] = b[bi++];
        }
        else {
            cnt += n / 2 - bi;
            a[ai++] = c[ci++];
        }
    }
    return cnt;
}

struct edge { ll to; ll cost; };
typedef pair<ll, ll> P;
struct graph {
    ll V;
    vector<vector<edge> > G;
    vector<ll> d;

    graph(ll n) {
        init(n);
    }

    void init(ll n) {
        V = n;
        G.resize(V);
        d.resize(V);
        for (int i = 0; i < V; i++) {
            d[i] = 2000000000000000000;
        }
    }

    void add_edge(ll s, ll t, ll cost) {
        edge e;
        e.to = t, e.cost = cost;
        G[s].push_back(e);
    }

    void dijkstra(ll s) {
        for (int i = 0; i < V; i++) {
            d[i] = 2000000000000000000;
        }
        d[s] = 0;
        priority_queue<P, vector<P>, greater<P> > que;
        que.push(P(0, s));
        while (!que.empty()) {
            P p = que.top(); que.pop();
            ll v = p.second;
            if (d[v] < p.first) continue;
            for (auto e : G[v]) {
                if (d[e.to] > d[v] + e.cost / (ll)1100000000 + (e.cost % (ll)1100000000)) {
                    d[e.to] = d[v] + e.cost;
                    que.push(P(d[e.to], e.to));
                }
            }
        }
    }
};

vector<int> makeTable(const string& s) {
    int n = s.size();
    vector<int> ret(n + 1);
    ret[0] = -1;
    int j = -1;
    for (int i = 0; i < n; i++) {
        while (j >= 0 && s[i] != s[j]) j = ret[j];
        ret[i + 1] = ++j;
    }
    return ret;
}

vector<int> kmp(const string& str, const string& word) {
    vector<int> table = makeTable(word), ret;
    int m = 0, i = 0, n = str.size();
    while (m + i < n) {
        if (word[i] == str[m + i]) {
            if (++i == (int)(word.size())) {
                ret.push_back(m);
                m = m + i - table[i];
                i = table[i];
            }
        }
        else {
            m = m + i - table[i];
            if (i > 0) i = table[i];
        }
    }
    return ret;
}
*/

pair<ll, ll> un(pair<ll, ll> a, pair<ll, ll> b) {
    if (a.first % 2 == 0)return make_pair(a.first + b.first, a.second + b.second);
    else return make_pair(a.first + b.first, a.second - b.second);
}

int main() {
    ll k;
    cin >> k;
    ll n;
    cin >> n;
    ll q;
    cin >> q;
    ll t = 0;
    vector<pair<ll,ll>> z(262144);
    for (int i = 0; i < n; i++) {
        cin >> z[i+131072].second;
        z[i + 131072].second *= -1;
        if (z[i+131072].second == 0) {
            z[i+131072].first = 1;
            t++;
        }

    }
    for (int i = 131071; i > 0; i--) {
        z[i] = un(z[2 * i], z[2 * i + 1]);
    }
    for (int i = 0; i < q; i++) {
        ll a, b;
        cin >> a >> b;
        a += 131071;
        b += 131071;
        pair<ll, ll> d = z[a];
        z[a] = z[b];
        z[b] = d;
        ll f = a;
        while (f > 1) {
            z[f / 2] = un(z[f / 2 * 2], z[f / 2 * 2 + 1]);
            f /= 2;
        }
        f = b;
        while (f > 1) {
            z[f / 2] = un(z[f / 2 * 2], z[f / 2 * 2 + 1]);
            f /= 2;
        }
        if (t % 2 == 0)cout << (z[1].second + k * (ll)10000000) % k+1 << endl;
        else cout << -((z[1].second + k * (ll)10000000) % k + 1) << endl;
    }
}
accuracy: 1
time_ms: 110
memory_kb: 7252
final_rank: 8

code_snippet: #define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define rep2(i,s,n) for(int i=s;i<(int)n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rebgin(),a.rend()
const int big=1e9+7;
//const int big=998'244'353;
const char newl='\n';
const int inf=1e9+10;
const ll INF=ll(1e18)+10;
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};

using P=pair<bool,ll>;//true->その向きのまま、反時計に+(llの値) false->まず裏にして、反時計に+(llの値)
P prod(P a,P b){
    if(b.first){
        a.second+=b.second;
        return a;
    }
    a.first=!a.first;
    a.second=b.second-a.second;
    return a;
}

struct segtree{
    int n,sz;
    vector<P> vec;
    segtree(int n_):n(n_),sz(1){
        while(sz<n)sz<<=1;
        vec.resize(sz*2,{true,0});
    }
    void set(int i,P val){
        i+=sz;
        vec[i]=val;
        i/=2;
        while(i){
            vec[i]=prod(vec[2*i],vec[2*i+1]);
            i/=2;
        }
    }
    P getall(){
        return vec[1];
    }
};


int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);

    int k,n,q;
    cin >> k >> n >> q;
    int a[n];
    rep(i,n)cin >> a[i];
    segtree seg(n);
    rep(i,n){
        if(a[i]==0)seg.set(i,{false,0});
        else seg.set(i,{true,-a[i]});
    }
    while(q--){
        int l,r;
        cin >> l >> r;
        l--,r--;
        swap(a[l],a[r]);
        if(a[l]==0)seg.set(l,{false,0});
        else seg.set(l,{true,-a[l]});
        if(a[r]==0)seg.set(r,{false,0});
        else seg.set(r,{true,-a[r]});

        ll x=seg.getall().second;
        if(seg.getall().first){
            x=1+x;
            x%=k;
            if(x<=0)x+=k;
        }
        else{
            x=1-x;
            x%=k;
            if(x<=0)x+=k;
            x=-x;
        }
        cout << x << newl;
    }
}
accuracy: 1
time_ms: 50
memory_kb: 7504
final_rank: 3

code_snippet: #include <bits/stdc++.h>

#define lol long long
#define gcd(x, y) __gcd(x, y)
#define mt make_tuple
#define mp make_pair
#define fi first
#define se second
#define fixed(x) fixed << setprecision(x)
#define all(x) x.begin(),x.end()
using namespace std;
using pii = pair<int, int>;
template <class A, class B>
inline bool chmax(A& a, const B& b) {
  return b > a && (a = b, true);
}
template <class A, class B>
inline bool chmin(A& a, const B& b) {
  return b < a && (a = b, true);
}
template <class A>
inline A abs(A& a) {
  return (a < 0 ? -a : a);
}
bool inLine(int x, int y, int mx, int my) {
  return (x >= 0 && y >= 0 && x < mx && y < my);
}
template<class T> using max_heap=priority_queue<T>;
template<class T> using min_heap=priority_queue<T,vector<T>,greater<T>>;
template<class T> using uset=unordered_set<T>;
template<class A,class B> using umap=unordered_map<A,B>;
const lol inf = (1LL << 62);
const int MOD = (1e9) + 7;
const int mod = 998244353;
const int dx[] = {1,  0,  -1, 0, 1, 1, -1, -1, -2, -2, -2, -2,
                  -2, -1, -1, 1, 1, 0, 0,  2,  2,  2,  2,  2};
const int dy[] = {0, 1,  0, -1, 1, -1, 1, -1, -2, -1, 0, 1,
                  2, -2, 2, -2, 2, -2, 2, -2, -1, 0,  1, 2};

template <typename X>
struct Segment{
  using FX=function<X(X,X)>; //X●X -> X となる関数の型
  int n;
  FX fx;
  const X ex;
  vector<X> dat;
  Segment(int size,FX fx_,X ex_) : fx(fx_),ex(ex_){
    n=1;
    while(size>n){
      n*=2;
    }
    dat.resize(n*2,ex_);
    return;
  }
  
  void set(int i,X x){ dat[i+n-1]=x;}
  void build(){
    for(int k=n-2;k>=0;k--) dat[k]=fx(dat[2*k+1],dat[2*k+2]);
    return;
  }
  
  void update(int i,X x){
    i+=n-1;
    dat[i]=x;
    while(i>0){
      i=(i-1)/2;
      dat[i]=fx(dat[i*2+1],dat[i*2+2]);
    }      
    return;  
  }
  
  X get(int a,int b){ return (get_sub(a,b+1,0,0,n));}
  X get_sub(int a,int b,int k,int l,int r){
    if(r<=a || b<=l) return ex;
    if(a<=l && r<=b) return dat[k];
    X vl=get_sub(a,b,k*2+1,l,(l+r)/2);
    X vr=get_sub(a,b,k*2+2,(l+r)/2,r);
    return fx(vl,vr);
  }
  
  X operator[](const int &k) const{
    return dat[k+n-1];
  }
};
struct Card{
  bool flip;
  lol num;
  Card(lol n=0, bool f=false):num(n),flip(f){}
};
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int k,n,q;
  cin >>k>>n>>q;

  auto fx=[&](Card x1,Card x2) -> Card {
    if(x1.flip){
      x2.num = k - x2.num;
    }
    return(Card((x1.num+x2.num)%k, x1.flip^x2.flip));
  };

  Segment<Card> st(n, fx, Card(0,false));

  for(int i=0;i<n;i++){
    int a;
    cin >>a;
    Card c;
    if(a==0){
      c = Card(0, true);
    }else if(a>0){
      c = Card(k-a%k, false);
    }else{
      c = Card(-(a%k), false);
    }

    st.set(i, c);
  }

  st.build();

  for(int i=0;i<q;i++){
    int l,r;
    cin >>l>>r;
    l--;r--;
    auto tmp = st[r];
    st.update(r, st[l]);
    st.update(l, tmp);
    cout <<(st.get(0,n).flip?"-":"");
    cout <<st.get(0,n).num+1<<'\n';
  }
  return (0);
}
accuracy: 1
time_ms: 110
memory_kb: 7236
final_rank: 7