Codeforces 992 E. Nastya and King-Shamans

\(>Codeforces\space992 E. Nastya and King-Shamans<\)

题目大意 : 给你一个长度为 \(n\) 的序列,有 \(q\) 次操作,每一次操作将一个数 \(A_i\) 改为另外一个数。每一次操作结束时,你需要找出一个位置 \(x\) 满足 \(A_x = sum_{x-1}\) 其中 \(sum\) 表示前缀和

$n , q \leq 2 \times 10^5   0 \leq A_i \leq 10^9 $

解题思路 :

博主亲测分块加均摊分析的做法会因为常数太大 \(TLE\) 掉,在此就不多讨论了

问题要求出满足 \(A_x = sum_{x-1}\) 的位置,这个可以转化为 \(sum_x = 2 \times sum_{x-1}\)

我们考虑从 \(A_{p=1}\) 开始跳,每一次跳到其后面一个最小的 \(k - 1\) ,满足\(sum_k \geq 2 \times sum_p\)

可以证明如果有答案且 \(sum_{ans} > 0\),那么答案一定在所有的 \(k\) 之中产生

不妨用反证法来证明,假设当且跳到点 \(k\) ,接下来选取的点是 \(k‘ \ (k < k‘)\) ,对于 \(k < i < k‘ - 1\)

如果说 \(i\) 是答案的话,设 \(y\) 为 第一个满足 $ sum_y \geq 2 \times sum_i$ 的点。

因为\(sum_y \geq sumk\) 所以必然有 $ y \geq k‘ $ ,如果 \(i < k‘ - 1\) 那么 $ y - i > 1$ , \(i\) 不是答案

所以证明了这样跳,如果有答案的话答案必然在跳到的点上

所以可以用树状数组维护前缀和,每一次暴力二分跳,跳 \(log\) 次就能跳完,总复杂度是\(O(nlogn^3)\)

/*program by mangoyang*/
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
    int f = 0, ch = 0; x = 0;
    for(; !isdigit(ch); ch = getchar()) if(ch == ‘-‘) f = 1;
    for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - ‘0‘;
    if(f) x = -x;
}
#define N (300005)
#define int ll
ll c[N], a[N], n, q;
inline void add(int x, ll y){ for(int i = x; i <= n; i += i & -i) c[i] += y; }
inline ll sum(int x){ ll ans = 0; for(int i = x; i; i -= i & -i) ans += c[i]; return ans; }
inline void solve(){
    int x = 1;
    if(sum(1) == 0) return (void)( puts("1") );
    while(x < n){
        int l = x + 1, r = n, k = x, now = 2 * sum(x);
        if(sum(x + 1) == now) return (void) (printf("%d\n", x + 1));
        while(l <= r){
            int mid = l + r >> 1;
            if(sum(mid) < now) k = mid, l = mid + 1; else r = mid - 1;
        }
        if(k + 1 > n) break;
        x = (k == x) ? k + 1 : k;
    }
    puts("-1");
}
main(){
    read(n), read(q);
    for(int i = 1; i <= n; i++) read(a[i]), add(i, a[i]);
    for(int i = 1; i <= q; i++){
        int x, y; read(x), read(y);
        add(x, y - a[x]), a[x] = y, solve();
    }
    return 0;
}

原文地址:https://www.cnblogs.com/mangoyang/p/9300930.html

时间: 2024-08-30 18:29:33

Codeforces 992 E. Nastya and King-Shamans的相关文章

Codeforces ZeptoLab Code Rush 2015

比赛链接:http://codeforces.com/contest/526/ A. King of Thieves time limit per test:1 second memory limit per test:256 megabytes In this problem you will meet the simplified model of game King of Thieves. In a new ZeptoLab game called "King of Thieves&quo

Codeforces 30D King&#39;s Problem? 模拟

首先将n个点排序,找出排序后的K,然后分情况讨论. 当 k == n+1时,显然是 k->1->n || k->n->1这两种的较小值,因为三角形的两边之和大于第三边. 当1 <= k && k <= n 时: 1 , k -> 1 -> n+1 -> k+1 ->n  ||  k -> n -> n+1 -> k-1 -> 1,当k+1 || k-1 不存在时将对应步骤忽略. 2 , k - > 1

Codeforces 1136E Nastya Hasn&#39;t Written a Legend (线段树教做人系列)

题意:有一个数组a和一个数组k,数组a一直保持一个性质:a[i + 1] >= a[i] + k[i].有两种操作:1,给某个元素加上x,但是加上之后要保持数组a的性质.比如a[i]加上x之后,a[i + 1]<a[i] + k[i],那么a[i + 1]就变成a[i] + k[i],否则不变.同理,若a[i + 2]小于了现在的a[i + 1] + k[i + 1],那么a[i + 2]也变成a[i + 1] + k[i + 1],一直保持这个性质.第二章操作,询问数组a的区间[l, r]的

Nastya Hasn&#39;t Written a Legend(Codeforces Round #546 (Div. 2)E+线段树)

题目链接 传送门 题面 题意 给你一个\(a\)数组和一个\(k\)数组,进行\(q\)次操作,操作分为两种: 将\(a_i\)增加\(x\),此时如果\(a_{i+1}<a_i+k_i\),那么就将\(a_{i+1}\)变成\(a_i+k_i\),如果\(a_{i+2}<a_i+k_i\),则将\(a_{i+2}\)变成\(a_{i+1}+k_{i+1}\),以此类推. 查询\(\sum\limits_{i=l}^{r}a_i\). 思路 我们首先存下\(k\)数组的前缀和\(sum1\),

codeforces 710A A. King Moves(水题)

题目链接: A. King Moves 题意: 给出king的位置,问有几个可移动的位置; 思路: 水题,没有思路; AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include <stack> #include &l

【模拟】Codeforces 710A King Moves

题目链接: http://codeforces.com/problemset/problem/710/A 题目大意: 国际象棋标准8X8棋盘,国王能往周围8个方向走.输入国王的位置,输出当前国王能往几个方向走. 题目思路: [模拟] 签到题(看错题目WA了一次).边界处理下就好. 1 // 2 //by coolxxx 3 //#include<bits/stdc++.h> 4 #include<iostream> 5 #include<algorithm> 6 #in

codeforces 710A King Moves(水)

output standard output The only king stands on the standard chess board. You are given his position in format "cd", where c is the column from 'a' to 'h' and dis the row from '1' to '8'. Find the number of moves permitted for the king. Check the

Codeforces Round #546 (Div. 2) C. Nastya Is Transposing Matrices

C. Nastya Is Transposing Matrices time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Nastya came to her informatics lesson, and her teacher who is, by the way, a little bit famous here gave he

CodeForces 710A King Moves (水题)

题意:给定一个坐标,问你皇后有几个方向可以走. 析:直接格举那八个方向即可. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring&g