Code:
#include<cstdio> #include<algorithm> using namespace std; const int maxn = 200000 + 5; int n,q; struct Segment_Tree { # define lson (o << 1) # define rson (o << 1) | 1 long long sumv[maxn << 2], maxv[maxn << 2]; inline void maintain(int o){ sumv[o] = sumv[lson] + sumv[rson]; maxv[o] = max(maxv[lson], maxv[rson]); } void update(int l,int r,int pos, long long val, int o) { if(l > r || l > pos || r < pos) return ; if(l == r) { maxv[o] = sumv[o] = val; return ; } int mid = (l + r) >> 1; update(l, mid, pos, val, lson); update(mid + 1, r, pos, val, rson); maintain(o); } inline long long query(int l,int r,int L,int R,int o) { if(l > r || l > R || r < L) return 0; if(l >= L && r <= R) return sumv[o]; int mid = (l + r) >> 1; return query(l, mid, L, R, lson) + query(mid + 1, r, L, R, rson); } inline int dfs(int l,int r,long long val, int o) { if(l == r) return l; int mid = (l + r) >> 1; if(maxv[lson] >= val) return dfs(l, mid, val, lson); else return dfs(mid + 1, r, val, rson); } int get(int l,int r, int L, int R, long long val,int o) { if(l > r || l > R || r < L) return -1; if(l >= L && r <= R) { if(maxv[o] < val) return -1; return dfs(l, r, val, o); } int mid = (l + r) >> 1, h; h = get(l, mid, L, R, val, lson); if(h != -1) return h; h = get(mid + 1, r, L, R, val, rson); if(h != -1) return h; return -1; } }T; inline void solve() { int l = 1; for(;;) { long long sumv = T.query(1, n, 1, l - 1, 1); int pos = T.get(1, n, l, n, sumv, 1); if(pos == -1) { printf("-1 "); return ; } else{ if(sumv + T.query(1, n, l, pos - 1, 1) == T.query(1, n, pos, pos, 1)) { printf("%d ", pos); return ;} l = pos + 1; if(l > n) { printf("-1 "); return ;} } } } int main() { scanf("%d%d",&n,&q); for(int i = 1;i <= n; ++i) { long long a; scanf("%I64d",&a); T.update(1, n, i, a, 1); } while(q--){ int pos; long long h; scanf("%d%I64d",&pos, &h); T.update(1, n, pos, h, 1); solve(); } return 0; }