malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     pair<int, int> a[n], b[m];     for (int i = 0; i < n; i++) {       cin >> a[i].first >> a[i].second;     }     for (int i = 0; i < m; i++) {       cin >> b[i].first >> b[i].second;     }     set<int> s;     for (int i = 0; i < n; i++) {       int f = 0;       for (int j = 0; j < m; j++) {         if (((a[i].first == b[j].first) || (a[i].first == b[j].second)) &&             ((a[i].second == b[j].first) || (a[i].second == b[j].second))) {           continue;         } else if ((a[i].first == b[j].first) || (a[i].first == b[j].second)) {           f = 1;         }       }       int tr = 1;       if (f) {         s.insert(a[i].first);         tr = 0;       }       for (int j = 0; j < m; j++) {         if (((a[i].first == b[j].first) || (a[i].first == b[j].second)) &&             ((a[i].second == b[j].first) || (a[i].second == b[j].second))) {           continue;         } else if ((a[i].second == b[j].first) || (a[i].second == b[j].second)) {           f += 1;           break;         }       }       if (f > 1) {         cout <<  -1 n ;         exit(0);       }       if (f && tr) {         s.insert(a[i].second);       }     }     for (int j = 0; j < m; j++) {       int f = 0;       int tr = 1;       for (int i = 0; i < n; i++) {         if (((a[i].first == b[j].first) || (a[i].first == b[j].second)) &&             ((a[i].second == b[j].first) || (a[i].second == b[j].second))) {           continue;         } else if ((b[j].first == a[i].first) || (b[j].first == a[i].second)) {           f = 1;         }       }       if (f) {         s.insert(b[j].first);         tr = 0;       }       for (int i = 0; i < n; i++) {         if (((a[i].first == b[j].first) || (a[i].first == b[j].second)) &&             ((a[i].second == b[j].first) || (a[i].second == b[j].second))) {           continue;         } else if ((b[j].second == a[i].first) || (b[j].second == a[i].second)) {           f += 1;           break;         }       }       if (f > 1) {         cout <<  -1 n ;         exit(0);       }       if (f && tr) {         s.insert(b[j].second);       }     }     if (s.size() == 1) {       auto it = s.begin();       cout << (*it);     } else {       cout <<  0 ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void solve();   int main() {     solve();     return 0;   }   void solve() {     long long int n, x, y;     cin >> n >> x >> y;     if (n > y) {       cout << -1;       return;     }     if ((y - n + 1) * (y - n + 1) + n - 1 >= x) {       for (int i = 1; i < n; ++i) {         cout << 1 <<    ;       }       cout << y - n + 1;     } else       cout << -1;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 210;   int n, m, ans[maxn];   string s[maxn];   int solve(string s) {     for (int i = 10; i >= 0; i--) {       bool flag = 0;       for (int j = 0; j < (1 << i); j++) {         string t;         for (int k = 0; k < i; k++)           if (j & (1 << k))             t +=  1 ;           else             t +=  0 ;         if (s.find(t) == s.npos) {           flag = 1;           break;         }       }       if (!flag) return i;     }     return 0;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) cin >> s[i];     scanf( %d , &m);     for (int i = n + 1; i <= n + m; i++) {       int a, b;       scanf( %d%d , &a, &b);       s[i] = s[a] + s[b];       if (s[i].length() > 1000)         s[i] = s[i].substr(0, 500) + s[i].substr(s[i].length() - 500, 500);       ans[i] = max(solve(s[i]), max(ans[a], ans[b]));       printf( %d n , ans[i]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   int A[N];   long long sum = 0, ans = 0;   bool state;   map<int, int> cnt;   int main() {     cnt[0] = 1;     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++) {       cin >> A[i];     }     for (int i = 0; i < n; i++) {       if (A[i] > m)         sum++;       else if (A[i] < m)         sum--;       state = state | (A[i] == m);       if (state) {         ans += (cnt[sum] + cnt[sum - 1]);         continue;       }       cnt[sum]++;     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<int> a(n);       for (auto& i : a) {         cin >> i;       }       vector<int> ops;       for (int i = 0; i < n - 2; ++i) {         if (a[i] == 0) {           continue;         }         int j = i + 1;         while (j < n and a[j] == 1) {           ++j;         }         int sz = j - i;         if (sz % 2 == 0 or j + 1 >= n) {           i = j;         } else if (a[j + 1] == 0) {           ops.push_back(j - 1);           a[j] = a[j + 1] = 1;           i = j;         } else {           ops.push_back(j - 1);           a[j - 1] = a[j + 1] = 0;           i = j;         }       }       for (int i = 0; i < n - 2; ++i) {         if (a[i] + a[i + 1] + a[i + 2] == 2) {           ops.push_back(i);           a[i] = a[i + 1] = a[i + 2] = 0;         }       }       for (int i = n - 1; i >= 2; --i) {         if (a[i] + a[i - 1] + a[i - 2] == 2) {           ops.push_back(i - 2);           a[i] = a[i - 1] = a[i - 2] = 0;         }       }       if (count(a.begin(), a.end(), 1)) {         cout <<  NO n ;       } else {         cout <<  YES n ;         cout << ops.size() <<   n ;         for (auto& i : ops) {           cout << i + 1 <<    ;         }         cout <<   n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   namespace IO {   char buf_[1 << 21], *p1_ = buf_, *p2_ = buf_;   inline long long in() {     long long s = 0, f = 1;     char x = (p1_ == p2_ && (p2_ = (p1_ = buf_) + fread(buf_, 1, 1 << 21, stdin),                              p1_ == p2_)                   ? EOF                   : *p1_++);     for (; x <  0  || x >  9 ;          x = (p1_ == p2_ && (p2_ = (p1_ = buf_) + fread(buf_, 1, 1 << 21, stdin),                             p1_ == p2_)                   ? EOF                   : *p1_++))       if (x ==  - ) f = -1;     for (; x >=  0  && x <=  9 ;          x = (p1_ == p2_ && (p2_ = (p1_ = buf_) + fread(buf_, 1, 1 << 21, stdin),                             p1_ == p2_)                   ? EOF                   : *p1_++))       s = (s * 10) + (x & 15);     return f == 1 ? s : -s;   }   char _buf[1 << 21];   long long _pos = -1;   inline void flush() { fwrite(_buf, 1, _pos + 1, stdout), _pos = -1; }   inline void pc(char x) {     if (_pos == (1 << 21) - 1) flush();     _buf[++_pos] = x;   }   inline void out(long long x) {     char k[30];     long long pos = 0;     if (!x) return pc( 0 );     if (x < 0) pc( - ), x = -x;     while (x) k[++pos] = (x % 10) | 48, x /= 10;     for (long long i = pos; i; i--) pc(k[i]);   }   inline void out(string x) {     long long len = x.size();     for (long long i = 0; i < len; i++) pc(x[i]);   }   }  // namespace IO   using namespace IO;   const long long A = 5e5 + 5;   long long n;   long long head[A], tot_road;   struct Road {     long long nex, to;   } road[2 * A];   inline void edge(long long x, long long y) {     road[++tot_road] = (Road){head[x], y}, head[x] = tot_road;   }   long long sz[A], res[A];   inline void prepare(long long fa, long long x) {     sz[x] = 1;     for (long long y = head[x]; y; y = road[y].nex) {       long long z = road[y].to;       if (z == fa) continue;       prepare(x, z);       sz[x] += sz[z];     }     return;   }   inline void DFS(long long fa, long long x) {     for (long long y = head[x]; y; y = road[y].nex) {       long long z = road[y].to;       if (z == fa) continue;       res[z] = res[x] + (sz[x] - sz[z]) * sz[z];       DFS(x, z);     }     return;   }   signed main() {     n = in();     for (long long i = 1; i < n; i++) {       long long u = in(), v = in();       edge(u, v), edge(v, u);     }     prepare(0, 1);     res[1] = n * (n - 1) / 2;     DFS(0, 1);     long long rt = 0;     for (long long i = 1; i <= n; i++)       if (res[i] > res[rt]) rt = i;     prepare(0, rt);     res[rt] = n * (n - 1) / 2;     DFS(0, rt);     long long ans = 0;     for (long long i = 1; i <= n; i++) ans = max(ans, res[i]);     out(ans), pc(  n );     flush();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   pair<int, int> tt[1005], kk[1005];   int n1, n2, top;   vector<int> vv[1005];   int main() {     int k, i, j, n, a, tmp;     long long ans = 0, sum = 0;     scanf( %d %d , &n, &k);     for (i = 0; i < n; i++) {       scanf( %d %d , &a, &tmp);       if (tmp == 1)         tt[n1++] = make_pair(a, i + 1);       else         kk[n2++] = make_pair(a, i + 1);       sum += (long long)a;     }     sort(tt, tt + n1);     sort(kk, kk + n2);     if (n1 < k) {       for (i = n1 - 1; i >= 0; i--) {         vv[top++].push_back(tt[i].second);         ans += (long long)tt[i].first;       }       for (i = 0; top < k; i++) vv[top++].push_back(kk[i].second);       for (; i < n2; i++) vv[top - 1].push_back(kk[i].second);     } else {       for (i = n1 - 1; top < k - 1; i--) {         vv[top++].push_back(tt[i].second);         ans += (long long)tt[i].first;       }       if (n1 > 0 && n2 > 0)         ans += (long long)min(tt[0].first, kk[0].first);       else if (n1 > 0)         ans += (long long)tt[0].first;       else         ans += (long long)kk[0].first;       for (; i >= 0; i--) vv[top].push_back(tt[i].second);       for (i = 0; i < n2; i++) vv[top].push_back(kk[i].second);     }     printf( %I64d. , sum - (ans + 1) / 2);     if (ans & 1)       printf( 5 );     else       printf( 0 );     putchar(  n );     for (i = 0; i < k; i++) {       printf( %d  , (int)vv[i].size());       for (j = 0; j < (int)vv[i].size(); j++) printf( %d  , vv[i][j]);       putchar(  n );     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1000005;   struct Node {     int left, right;     long long sum;   } L[N << 2];   void bulid(int step, int l, int r) {     L[step].left = l;     L[step].right = r;     L[step].sum = 0;     if (l == r) return;     int m = (l + r) >> 1;     bulid(step << 1, l, m);     bulid(step << 1 | 1, m + 1, r);   }   void update(int step, int pos, long long val) {     if (L[step].left == L[step].right) {       L[step].sum = (val) % 1000000007;       return;     }     int m = (L[step].left + L[step].right) >> 1;     if (pos <= m)       update(step << 1, pos, val);     else       update(step << 1 | 1, pos, val);     L[step].sum = (L[step << 1].sum + L[step << 1 | 1].sum) % 1000000007;   }   long long query(int step, int l, int r) {     if (L[step].left == l && L[step].right == r) return L[step].sum;     int m = (L[step].left + L[step].right) >> 1;     if (r <= m)       return query(step << 1, l, r);     else if (l > m)       return query(step << 1 | 1, l, r);     else       return (query(step << 1, l, m) + query(step << 1 | 1, m + 1, r)) %              1000000007;   }   long long a[N];   int main() {     int n;     scanf( %d , &n);     bulid(1, 1, 1000000);     long long ans = 0;     for (int i = 0; i < n; i++) {       scanf( %I64d , &a[i]);       long long sub = query(1, a[i], a[i]);       long long sum = (a[i] + a[i] * query(1, 1, a[i])) % 1000000007;       ans = ((ans + sum - sub) % 1000000007 + 1000000007) % 1000000007;       update(1, a[i], sum);     }     printf( %I64d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[1000][1000];   int main() {     int n, ans = 0;     cin >> n;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= n; ++j) cin >> a[i][j];     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= n; ++j) {         int sum1 = 0, sum2 = 0;         for (int k = 1; k <= n; ++k) sum1 += a[k][j];         for (int k = 1; k <= n; ++k) sum2 += a[i][k];         if (sum1 > sum2) ans++;       }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > a[5001];   int n, t;   int main() {     int m, u, v, w;     cin >> n >> m >> t;     vector<vector<int> > f(n + 1, vector<int>(2, 0));     vector<vector<int> > p(n + 1, vector<int>(2, 2e9 + 1));     vector<vector<int> > track(n + 1, vector<int>(n + 1, 0));     for (int i = 1; i <= m; i++) {       cin >> u >> v >> w;       a[v].push_back(make_pair(u, w));     }     f[1][0] = 1;     p[1][0] = 0;     int id = 1;     int res = 0;     for (int j = 2; j <= n; j++) {       for (int i = 2; i <= n; i++)         for (int k = 0; k < a[i].size(); k++) {           v = a[i][k].first;           w = a[i][k].second;           if (f[v][1 - id] &&               ((f[i][id] < f[v][1 - id] + 1 && p[v][1 - id] + w <= t) ||                (f[i][id] == f[v][1 - id] + 1 && p[v][1 - id] + w < p[i][id]))) {             f[i][id] = f[v][1 - id] + 1;             p[i][id] = p[v][1 - id] + w;             track[i][j] = v;             if (i == n) res = max(res, j);           }         }       id ^= 1;     }     vector<int> road;     cout << res << endl;     u = n;     do {       u = track[u][res--];       road.push_back(u);     } while (u > 1);     for (int i = road.size() - 1; i >= 0; i--) cout << road[i] <<    ;     cout << n;   }
#include <bits/stdc++.h>   using namespace std;   namespace jumpmelon {   const int MAXN = 52;   int A[MAXN + 1], B[MAXN + 1];   bool F[MAXN + 1];   void work() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &A[i]);     vector<vector<int>> Ans;     while (1) {       memset(F + 1, 0, sizeof(bool[n]));       for (int i = 1; i < n; i++)         if (A[i] + 1 == A[i + 1]) F[i] = 1;       int x = 0, y = 0;       for (int i = 1; i <= n; i++)         if (!F[i])           for (int j = 1; j < i; j++)             if (A[j] == A[i] + 1) {               x = i;               y = j;             }       if (x == 0 && y == 0) break;       int p = x;       while (p > 1 && F[p - 1]) p--;       vector<int> D;       if (y > 1) D.push_back(y - 1);       D.push_back(p - y);       D.push_back(x - p + 1);       if (x < n) D.push_back(n - x);       Ans.push_back(D);       int l = 1, r = n;       for (int v : D) {         r -= v;         for (int i = 1; i <= v; i++) B[r + i] = A[l + i - 1];         l += v;       }       memcpy(A + 1, B + 1, sizeof(int[n]));     }     printf( %d n , (int)Ans.size());     for (auto &&D : Ans) {       printf( %d , (int)D.size());       for (int v : D) printf(  %d , v);       putchar(  n );     }   }   }  // namespace jumpmelon   int main() {     jumpmelon::work();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     int n, m, cnt = 0;     cin >> n >> m;     char mat[n][m];     for (int i = 0; i < (int)(n); i++) {       for (int j = 0; j < (int)(m); j++) {         cin >> mat[i][j];       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         int f = 0;         char x = mat[i][j];         for (int k = 0; k < n; k++) {           if (x >= mat[k][j])             continue;           else {             f = 1;             break;           }         }         if (f == 0) {           cnt++;           break;         }       }     }     cout << cnt << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   const int N = 1000005;   const double PI = 4 * atan(1);   long long m, x, y, n, p, z, k;   long long h1, m1, h2, m2;   char c, cc;   int main() {     ios::sync_with_stdio(0);     cin >> h1 >> c >> m1 >> h2 >> cc >> m2;     x = (h2 - h1) * 60 + m2 - m1;     x /= 2;     x += m1;     if ((h1 + x / 60) < 10) cout << 0;     cout << h1 + x / 60 <<  : ;     if (((x) % 60 < 10)) cout << 0;     cout << x % 60;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     for (int i = 1; i <= 500; i++) putchar( 9 );     for (int i = 1; i <= 500; i++) putchar( 0 );     putchar( 1 );     putchar(  n );     for (int i = 1; i <= 501; i++) putchar( 9 );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 100005;   const int oo = 1e9 + 7;   const long long mod = 1e9 + 7;   const double eps = 1e-11;   long long n;   int main() {     cin.sync_with_stdio(false);     cin.tie(0);     cin >> n;     cout << (n - 2LL) * (n - 2LL) << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int y, b, r;     int l, m, n;     int ans;     cin >> y >> b >> r;     l = y;     m = b;     n = r;     int mins = min(min(y, b), r);     if (mins == r) {       ans = r + (r - 1) + (r - 2);     } else if (mins == b) {       y = b - 1;       r = b + 1;       ans = y + b + r;     } else if (mins == y) {       b = y + 1;       r = y + 2;       if (r > n) {         y--;         ans = y + (y + 1) + (y + 2);       } else {         ans = y + b + r;       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   int win(long long s, long long e) {     if (e % 2 == 1) {       if (s % 2 == 1)         return 0;       else         return 1;     } else {       if (2 * s > e) {         if (s % 2 == 1)           return 1;         else           return 0;       } else if (2 * s <= e && 4 * s > e) {         return 1;       } else         return win(s, e / 4);     }   }   int lose(long long s, long long e) {     if (2 * s > e)       return 1;     else       return win(s, e / 2);   }   int Win[N], Lose[N];   int get_lose(int x);   int get_win(int x);   int get_win(int x) {     if (x == 1) return Win[1];     return Win[x] ? get_lose(x - 1) : get_win(x - 1);   }   int get_lose(int x) {     if (x == 1) return Lose[1];     return Lose[x] ? get_lose(x - 1) : get_win(x - 1);   }   void io() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);   }   int main(int argc, char const *argv[]) {     io();     int t;     cin >> t;     long long s, e;     for (int i = 1; i <= t; i++) {       cin >> s >> e;       Win[i] = win(s, e);       Lose[i] = lose(s, e);     }     cout << get_win(t) <<     << get_lose(t) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     int c, v0, v1, a, l;     cin >> c >> v0 >> v1 >> a >> l;     int ans = 1;     c -= min(v0, v1);     int p = 0;     while (c > 0) {       v0 = v0 + a;       p = min(v0, v1);       p -= l;       c -= p;       ans++;     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long a[105];   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0), cout.tie(0);     long long n, m;     cin >> n >> m;     long long maxi = 0, mini = 107;     for (long long i = 0; i < n; i++) {       cin >> a[i];       maxi = max(maxi, a[i]);     }     long long temp = m;     for (long long i = 0; i < n; i++) {       if (m > 0) {         m -= (maxi - a[i]);         a[i] = maxi;         if (m <= 0) {           mini = maxi;           break;         }       }     }     if (m > 0) mini = maxi + ceil(m / (n + 0.0));     cout << mini <<     << maxi + temp <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MODULO = 998244353;   const int N_MAX = 200010;   vector<int> neighbors[N_MAX];   int factorials[N_MAX];   int n;   int calculateBranch(int x, int par) {     int perm = factorials[neighbors[x].size()];     for (int y : neighbors[x]) {       if (y != par) {         perm = ((long long)perm * calculateBranch(y, x)) % MODULO;       }     }     return perm;   }   void outerInit() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     auto fi = new ifstream( test.inp );     if (*fi) {       cin.rdbuf(fi->rdbuf());     }   }   void readData() {     cin >> n;     for (int i = 1; i < n; ++i) {       int x, y;       cin >> x >> y;       neighbors[x].push_back(y);       neighbors[y].push_back(x);     }   }   void initData() {     factorials[0] = 1;     for (int i = 1; i <= n; ++i) {       factorials[i] = ((long long)factorials[i - 1] * i) % MODULO;     }   }   void solveData() { cout << ((long long)n * calculateBranch(1, 0)) % MODULO; }   int main() {     outerInit();     readData();     initData();     solveData();   }
#include<bits/stdc++.h>   #define ll long long   #define ld long double   #define ull unsigned long long   #define izq nod * 2   #define der nod * 2 + 1   #define md (( a + b ) >> 1)   #define oo (1LL<<30LL)   #define len(X) (int)(X).size()   using namespace std;      const ll MOD = 998244353;      const int MX = 2 * 1e5+5;      ll F[MX],iF[MX];      ll pot( ll base, ll e){       ll ans = 1ll;              while( e ){           if( e & 1ll ) ans = (ans * base) % MOD;           base = (base * base) % MOD;           e>>=1ll;           }               return ans;     }      int main(){        ios_base::sync_with_stdio(0);    cin.tie(0);        int t;    cin>>t;        F[0] = iF[0] = 1;        for ( ll i = 1; i <= 2e5; i++ ){         F[i] = ( F[i-1] * i )%MOD;          iF[i] = pot ( F[i], MOD-2LL );    }        while ( t-- ){             int n;             cin>>n;                          string s;             cin>>s;                          int c0 = 0,c1 = 0;                          for ( int i = 0; i < len(s); i++ ){            if ( s[i] ==  1  && i < len(s)-1 && s[i+1] ==  1  ){              c1++; i++;         }         if ( s[i] ==  0  )c0++;        }              cout<<F[c0+c1] * iF[c0] % MOD * iF[c1] % MOD<<  n ;;                    }       return 0;    }
#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   enum { MOD0 = 1000000000, MOD7 = 1000000007, MOD9 = 1000000009 };   template <typename T>   class FenwickTree {     vector<T> tree;       public:     FenwickTree<T>() : tree() {}     FenwickTree<T>(vector<T> a) : tree(a.size() + 1, 0) {       for (size_t i = 0; i < a.size(); ++i) {         add(i, a[i]);       }     }     size_t size() const { return tree.size() ? tree.size() - 1 : 0; }     void add(size_t index, T value) {       for (; ++index <= size(); index |= index - 1) {         tree[index] += value;       }     }     T sum(size_t right) {       T ret{};       for (; right; right &= right - 1) {         ret += tree[right];       }       return ret;     }     T sum(size_t left, size_t right) { return sum(right) - sum(left); }   };   int a[200000];   FenwickTree<long long> ft;   int t[800000];   int n;   void build(int x = 0, int L = 0, int R = n - 1) {     if (L == R) {       t[x] = a[L];       return;     }     int M = (L + R) / 2;     build(2 * x + 1, L, M);     build(2 * x + 2, M + 1, R);     t[x] = max(t[2 * x + 1], t[2 * x + 2]);   }   void upd(int index, int val, int x = 0, int L = 0, int R = n - 1) {     if (L == R) {       t[x] = val;       return;     }     int M = (L + R) / 2;     if (index <= M) {       upd(index, val, 2 * x + 1, L, M);     } else {       upd(index, val, 2 * x + 2, M + 1, R);     }     t[x] = max(t[2 * x + 1], t[2 * x + 2]);   }   int get(long long val, int from, int x = 0, int L = 0, int R = n - 1) {     if (t[x] < val || R < from) {       return n;     } else if (L == R) {       return L;     }     int M = (L + R) / 2;     if (M < from) {       return get(val, from, 2 * x + 2, M + 1, R);     } else if (t[2 * x + 1] >= val) {       int s = get(val, from, 2 * x + 1, L, M);       if (s < n) {         return s;       }     }     return get(val, from, 2 * x + 2, M + 1, R);   }   int get_ans() {     long long cur = 0;     int index = 0;     if (a[0] == 0) {       return 1;     }     while (index < n) {       cur += a[index];       index = get(cur, index + 1);       cur = ft.sum(index);       if (a[index] == cur) {         break;       }     }     return index >= n ? -1 : index + 1;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int q;     cin >> n >> q;     vector<long long> b;     for (int i = 0; i < n; ++i) {       cin >> a[i];       b.push_back(a[i]);     }     ft = FenwickTree<long long>(b);     build();     while (q--) {       int p, x;       cin >> p >> x;       --p;       ft.add(p, x - a[p]);       a[p] = x;       upd(p, x);       cout << get_ans() <<   n ;     }     return 0;   }