Nastya Hasn't Written a Legend(Codeforces Round #546 (Div. 2)E+线段树)
Posted dillonh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nastya Hasn'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^ra_i\\)。
思路
我们首先存下\\(k\\)数组的前缀和\\(sum1\\),再存\\(sum1\\)的前缀和\\(sum2\\)。
那么修改操作产生的影响我们就可以先通过二分出右端点\\(r\\),然后进行区间覆盖,将\\([l,r]\\)覆盖成\\(a_i\\),然后这一段区间的\\(sum=a_i\\times (r-l+1)+sum2[r]-sum2[l]-sum1[l]\\times(r-l)\\)。
查询操作就是普通的区间求和。代码实现如下
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)
const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 1e5 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
char op[3];
int n, q, l, r;
LL sum1[maxn], sum2[maxn];
int k[maxn];
struct node
bool lazy;
int l, r;
LL val, sum;
segtree[maxn<<2];
void push_up(int rt)
segtree[rt].sum = segtree[lson].sum + segtree[rson].sum;
void push_down(int rt)
if(!segtree[rt].lazy) return;
segtree[rt].lazy = 0;
segtree[lson].lazy = segtree[rson].lazy = 1;
segtree[lson].val = segtree[rt].val;
segtree[rson].val = sum1[segtree[rson].l] - sum1[segtree[rt].l] + segtree[rt].val;
segtree[lson].sum = segtree[lson].val * (segtree[lson].r - segtree[lson].l + 1) + sum2[segtree[lson].r] - sum2[segtree[lson].l] - sum1[segtree[lson].l] * (segtree[lson].r - segtree[lson].l);
segtree[rson].sum = segtree[rson].val * (segtree[rson].r - segtree[rson].l + 1) + sum2[segtree[rson].r] - sum2[segtree[rson].l] - sum1[segtree[rson].l] * (segtree[rson].r - segtree[rson].l);
void build(int rt, int l, int r)
segtree[rt].l = l, segtree[rt].r = r;
segtree[rt].sum = segtree[rt].lazy = 0;
if(l == r)
scanf("%lld", &segtree[rt].val);
segtree[rt].sum = segtree[rt].val;
return;
int mid = (l + r) >> 1;
build(lson, l, mid);
build(rson, mid + 1, r);
push_up(rt);
void update1(int rt, int pos, int x)
if(segtree[rt].l == segtree[rt].r)
segtree[rt].val += x;
segtree[rt].sum += x;
return;
push_down(rt);
int mid = (segtree[rt].l + segtree[rt].r) >> 1;
if(pos <= mid) update1(lson, pos, x);
else update1(rson, pos, x);
push_up(rt);
void update2(int rt, int l, int r, LL x)
if(segtree[rt].l == l && segtree[rt].r == r)
segtree[rt].val = x;
segtree[rt].lazy = 1;
segtree[rt].sum = x * (r - l + 1) + sum2[r] - sum2[l] - sum1[l] * (r - l);
return;
push_down(rt);
int mid = (segtree[rt].l + segtree[rt].r) >> 1;
if(r <= mid) update2(lson, l, r, x);
else if(l > mid) update2(rson, l, r, x);
else
update2(lson, l, mid, x);
update2(rson, mid + 1, r, x + sum1[mid + 1] - sum1[l]);
push_up(rt);
LL query1(int rt, int pos)
if(segtree[rt].l == segtree[rt].r) return segtree[rt].val;
push_down(rt);
int mid = (segtree[rt].l + segtree[rt].r) >> 1;
if(pos <= mid) return query1(lson, pos);
else return query1(rson, pos);
LL query2(int rt, int l, int r)
if(segtree[rt].l == l && segtree[rt].r == r)
return segtree[rt].sum;
push_down(rt);
int mid = (segtree[rt].l + segtree[rt].r) >> 1;
if(r <= mid) return query2(lson, l, r);
else if(l > mid) return query2(rson, l, r);
else return query2(lson, l, mid) + query2(rson, mid + 1, r);
int main()
#ifndef ONLINE_JUDGE
FIN;
#endif // ONLINE_JUDGE
scanf("%d", &n);
build(1, 1, n);
for(int i = 1; i < n; ++i)
scanf("%d", &k[i]);
for(int i = 2; i <= n; ++i)
sum1[i] = sum1[i-1] + k[i-1];
for(int i = 2; i <= n; ++i)
sum2[i] = sum2[i-1] + sum1[i];
scanf("%d", &q);
while(q--)
scanf("%s%d%d", op, &l, &r);
if(op[0] == '+')
int ub = n, lb = l + 1, mid, ans = -1;
update1(1, l, r);
LL val = query1(1, l);
while(ub >= lb)
mid = (ub + lb) >> 1;
if(query1(1, mid) < val + sum1[mid] - sum1[l])
ans = mid;
lb = mid + 1;
else
ub = mid - 1;
if(ans != -1) update2(1, l, ans, val);
else
printf("%lld\\n", query2(1, l, r));
return 0;
以上是关于Nastya Hasn't Written a Legend(Codeforces Round #546 (Div. 2)E+线段树)的主要内容,如果未能解决你的问题,请参考以下文章
codeforces 1136E-Nastya Hasn't Written a Legend
Nastya Hasn't Written a Legend(Codeforces Round #546 (Div. 2)E+线段树)
Codeforces 1136E Nastya Hasn't Written a Legend (线段树教做人系列)
Mongoose Schema hasn't been registered for model
错误记录Android 中调用 Process 命令行执行指令 ( java.lang.IllegalThreadStateException: process hasn‘t exited )(代码片