首页 > 其他分享 >CF992E Nastya and King-Shamans

CF992E Nastya and King-Shamans

时间:2023-11-28 19:13:01浏览次数:41  
标签:King return int tag mid edge CF992E ans Nastya

题意

给定一个序列 \(s\),记其前缀和序列为 \(g_i\),\(q\) 次修改。

每次修改后输出任意满足 \(s_i = g_{i - 1}\) 的解。

Sol

前缀和数组,每次答案使 \(s_i \times 2\)。

也就是答案的个数不会超过 \(log\)。

再想,\(s_i - g_{i - 1} \ge 0\) 的个数也不会超过 \(log\)。

于是我们考虑用线段树维护这个东西。

发现询问的复杂度对了。

对于修改操作直接区间加就行。

Code

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#define int long long
using namespace std;
#ifdef ONLINE_JUDGE

#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;

#endif
int read() {
	int p = 0, flg = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-') flg = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		p = p * 10 + c - '0';
		c = getchar();
	}
	return p * flg;
}
void write(int x) {
	if (x < 0) {
		x = -x;
		putchar('-');
	}
	if (x > 9) {
		write(x / 10);
	}
	putchar(x % 10 + '0');
}
const int N = 2e5 + 5, inf = 1e9;
array <int, N> s, g;

namespace Sgt {

array <int, N * 4> edge, tag;

void pushup(int x) {
	edge[x] = max(edge[x * 2], edge[x * 2 + 1]);
}

void pushdown(int x, int l, int r) {
	if (!tag[x]) return;
	edge[x * 2] += tag[x];
	edge[x * 2 + 1] += tag[x];
	tag[x * 2] += tag[x];
	tag[x * 2 + 1] += tag[x];
	tag[x] = 0;
}

void build(int x, int l, int r) {
	edge[x] = -inf;
	if (l == r) {
		edge[x] = s[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(x * 2, l, mid);
	build(x * 2 + 1, mid + 1, r);
	pushup(x);
}

void modify(int x, int l, int r, int L, int R, int k) {
	if (L > r || R < l) return;
	if (L <= l && R >= r) {
		edge[x] += k;
		tag[x] += k;
		return;
	}
	pushdown(x, l, r);
	int mid = (l + r) >> 1;
	if (L <= mid) modify(x * 2, l, mid, L, R, k);
	if (R > mid) modify(x * 2 + 1, mid + 1, r, L, R, k);
	pushup(x);
}

int query(int x, int l, int r) {
	if (l == r) return !edge[x] ? l : -1;
	pushdown(x, l, r);
	int mid = (l + r) >> 1, ans = -1;
	if (edge[x * 2 + 1] >= 0) ans = query(x * 2 + 1, mid + 1, r);
	if (edge[x * 2] >= 0 && ans == -1) ans = query(x * 2, l, mid);
	return ans;
}

int _query(int x, int l, int r, int k) {
	if (l == r) return edge[x];
	pushdown(x, l, r);
	int mid = (l + r) >> 1;
	if (k <= mid) return _query(x * 2, l, mid, k);
	else return _query(x * 2 + 1, mid + 1, r, k);
}

}

signed main() {
	int n = read(), q = read();
	int tp = 0;
	for (int i = 1; i <= n; i++) {
		g[i] = read();
		s[i] = g[i] - tp;
		tp += g[i];
	}
	/* for (int i = 1; i <= n; i++) */
		/* write(s[i]), putchar(32); */
	/* puts("@"); */
	Sgt::build(1, 1, n);
	while (q--) {
		using Sgt::modify; using Sgt::query;
		int x = read(), y = read();

		modify(1, 1, n, x, x, -g[x]);
		modify(1, 1, n, x + 1, n, g[x]);
		/* for (int i = 1; i <= n; i++) { */
			/* write(Sgt::_query(1, 1, n, i)), putchar(32); */
		/* } */
		/* puts(""); */
		modify(1, 1, n, x, x, y);
		modify(1, 1, n, x + 1, n, -y);
		/* for (int i = 1; i <= n; i++) { */
			/* write(Sgt::_query(1, 1, n, i)), putchar(32); */
		/* } */
		/* puts(""); */
		g[x] = y;
		write(query(1, 1, n)), puts("");
	}
	return 0;
}

标签:King,return,int,tag,mid,edge,CF992E,ans,Nastya
From: https://www.cnblogs.com/cxqghzj/p/17862720.html

相关文章

  • skywalking
    what:Skywalking是由国内开源爱好者吴晟开源并提交到Apache孵化器的产品,它同时吸收了Zipkin/Pinpoint/CAT的设计思路。特点是:支持多种插件,UI功能较强,支持非侵入式埋点数据存储支持:Elasticsearch、MySQL、H2、TiDB。默认是H2,而且是存到内存。实际我们一般将其存到ES......
  • Backing up and restoring BIG-IP configuration files (10.x)
    TopicThisarticleappliestoBIG-IP10.x.Forinformationaboutotherversions,refertothefollowingarticles:K13132:BackingupandrestoringBIG-IPconfigurationfiles(11.x-13.x)K3499:BackingupandrestoringBIG-IPconfigurationfiles(9.x)Thi......
  • BlockingQueue阻塞队列
    BlockingQueue阻塞队列BlockingQueue简介juc包下,BlockingQueue很好的解决了多线程中,高效安全的"传输数据"问题。阻塞队列,是一个队列,可以是数据从队列的一端输入,从另一端输出。当队列空时,从队列获取元素线程被阻塞,直到其他线程向空的队列插入新元素。当队列满时,向队列添加元......
  • D. Letter Picking
    D.LetterPickingAliceandBobareplayingagame.Initially,theyaregivenanon-emptystring$s$,consistingoflowercaseLatinletters.Thelengthofthestringiseven.Eachplayeralsohasastringoftheirown,initiallyempty.Alicestarts,then......
  • CF685E Travelling Through the Snow Queen's Kingdom
    题意给定一张图,走出当前边的时间为\(i\)。\(q\)次询问,问\(s\)是否能在\(l\tor\)中走到\(t\)。Sol考虑将边从大到小插入图中。注意到当前边只能对起点造成贡献。复杂度\(O(n\times\max\{n,m\})\)Code#include<iostream>#include<algorithm>#include<cstd......
  • P8907 [USACO22DEC] Making Friends P 题解
    明明看着不难的题目,却意外的卡人。思路考虑两头奶牛可以成为朋友条件是什么。存在一条路径连接这两头奶牛。且除去端点外的路径上的所有点的编号小于两端点的较小值。充分必要性都比较显然。如何维护。我们可以从小到大加入点,维护这些路径。对于每个点维护一个\(\text{se......
  • jmeter beanshell常见问题:"BeanShellInterpreter: Error invoking bsh method: eval
    jmeter使用beanshell文件经常会遇到这个问题:BeanShellInterpreter:Errorinvokingbshmethod:evalInfile:inlineevaluationof.... 原因可能有:1.jar包没有放入对应位置解决:放到lib/ext目录下,并且重启jmeter2.beanshell不支持java泛型,如List<String>list=newAr......
  • 【pwn】[FSCTF 2023]What am I thinking? --pwntools工具的利用
    这道题没给附件,直接就是nc这个题目的意思是,我们随机输入一个数,然后发给我们一段base64加密后的密文,真正num就在里面,我们现在写个pwntools脚本提取一下这段base64密文,解密一下,看看是什么东西exp:io=remote("node4.anna.nssctf.cn",28045)io.sendline(str(2))io.recvuntil(b"......
  • Fully-Convolutional Siamese Networks for Object Tracking
    论文代码......
  • T399750 Cell kingdom(Hard) 题解
    LinkT399750Cellkingdom(Hard)Qustion第一天产生\(1\)个细胞,之后的每一天,一个细胞都会分裂成\(8\)个和自己一样的细胞,每个细胞在第三天都会自爆并且带走当天产生的\(6\)个细胞,求第\(x\)天有多少细胞Solution我们设\(F[i]\)表示第\(i\)天产生的新细胞个数那么可......