F. Heroes of Making Magic III
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

I’m strolling on sunshine, yeah-ah! And doesn’t it feel good! Well, it certainly feels good for our Heroes of Making Magic, who are casually walking on a one-directional road, fighting imps. Imps are weak and feeble creatures and they are not good at much. However, Heroes enjoy fighting them. For fun, if nothing else.

Our Hero, Ignatius, simply adores imps. He is observing a line of imps, represented as a zero-indexed array of integers a of length n, where ai denotes the number of imps at the i-th position. Sometimes, imps can appear out of nowhere. When heroes fight imps, they select a segment of the line, start at one end of the segment, and finish on the other end, without ever exiting the segment. They can move exactly one cell left or right from their current position and when they do so, they defeat one imp on the cell that they moved to, so, the number of imps on that cell decreases by one. This also applies when heroes appear at one end of the segment, at the beginning of their walk.

Their goal is to defeat all imps on the segment, without ever moving to an empty cell in it (without imps), since they would get bored. Since Ignatius loves imps, he doesn’t really want to fight them, so no imps are harmed during the events of this task. However, he would like you to tell him whether it would be possible for him to clear a certain segment of imps in the above mentioned way if he wanted to.

You are given q queries, which have two types:

  • a b k — denotes that k imps appear at each cell from the interval [a, b]
  • a b - asks whether Ignatius could defeat all imps on the interval [a, b] in the way described above
Input

The first line contains a single integer n (1 ≤ n ≤ 200 000), the length of the array a. The following line contains n integers a1, a2, ..., an(0 ≤ ai ≤ 5 000), the initial number of imps in each cell. The third line contains a single integer q (1 ≤ q ≤ 300 000), the number of queries. The remaining q lines contain one query each. Each query is provided by integers ab and, possibly, k (0 ≤ a ≤ b < n, 0 ≤ k ≤ 5 000).

Output

For each second type of query output 1 if it is possible to clear the segment, and 0 if it is not.

Example
input
3
2 2 2
3
2 0 2
1 1 1 1
2 0 2
output
0
1
Note

For the first query, one can easily check that it is indeed impossible to get from the first to the last cell while clearing everything. After we add 1 to the second position, we can clear the segment, for example by moving in the following way: 

题目大意:给一个区间,有两种操作:1.将[a,b]所有的数+k. 2.询问能不能将[a,b]中的数减完.规则是:每经过一个数,这个数就会-1,不能经过为0的数,并且每次只能左移或右移一格.

分析:比较难的一道题.

关键是要想到第二种操作的方案.构造出方案就好做了.可以从最左端点走,每次将当前位置变成0,方法就是不断的在当前位置和下一位置走.这样的话需要满足条件:a(i + 1) >= ai,走到点i+1后,a(i + 1)就变成了a(i + 1) - ai,对于a(i + 1)和a(i + 2)有同样的条件.那么到最后需要满足的条件就是

ar−a(r−1)+⋯≥[(r−a+1)≡1(mod2)]这里的r是区间[a,b]的任意一个数,并且ab - a(b - 1) + ...... = (b - a + 1) mod 2.

考虑如何实现,因为第一个条件有大于等于号,并且r是区间的任意一个数,那么只需要维护一下最小值就可以了,记d = ai - a(i-1) + a(i - 2)......在线段树上维护d.注意到d中ai的系数是正数还是负数取决于维护的两个端点的奇偶性,在修改和查询的时候要对奇偶性进行分类讨论.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = ,inf = 1e9; int n,m,a[maxn],b[maxn],minn[maxn << ][],tag[maxn << ][]; void pushup(int o)
{
minn[o][] = min(minn[o * ][],minn[o * + ][]);
minn[o][] = min(minn[o * ][],minn[o * + ][]);
} void pushdown(int o)
{
for (int i = ; i < ; i++)
{
if (tag[o][i])
{
minn[o * ][i] += tag[o][i];
minn[o * + ][i] += tag[o][i];
tag[o * ][i] += tag[o][i];
tag[o * + ][i] += tag[o][i];
tag[o][i] = ;
}
}
} void build(int o,int l,int r)
{
if (l == r)
{
minn[o][l & ] = b[l];
minn[o][l & ^ ] = inf;
return;
}
int mid = (l + r) >> ;
build(o * ,l,mid);
build(o * + ,mid + ,r);
pushup(o);
} void update(int o,int l,int r,int x,int y,int v,int id)
{
if (x > y)
return;
if(x <= l && r <= y)
{
tag[o][id] += v;
minn[o][id] += v;
return;
}
pushdown(o);
int mid = (l + r) >> ;
if (x <= mid)
update(o * ,l,mid,x,y,v,id);
if (y > mid)
update(o * + ,mid + ,r,x,y,v,id);
pushup(o);
} int query1(int o,int l,int r,int cur,int id)
{
if (cur == ) //易错
return ;
if (l == r)
return minn[o][id];
pushdown(o);
int mid = (l + r) >> ;
if (cur <= mid)
return query1(o * ,l,mid,cur,id);
if (cur > mid)
return query1(o * + ,mid + ,r,cur,id);
} int query2(int o,int l,int r,int x,int y,int id)
{
if (x <= l && r <= y)
return minn[o][id];
pushdown(o);
int mid = (l + r) >> ,res = inf;
if (x <= mid)
res = min(res,query2(o * ,l,mid,x,y,id));
if (y > mid)
res = min(res,query2(o * + ,mid + ,r,x,y,id));
return res;
} int main()
{
scanf("%d",&n);
for (int i = ; i <= n; i++)
scanf("%d",&a[i]);
for (int i = ; i <= n; i++)
b[i] = a[i] - b[i - ]; //维护的就是d
build(,,n);
scanf("%d",&m);
for (int i = ; i <= m; i++)
{
int id,a,b,k;
scanf("%d",&id);
if (id == )
{
scanf("%d%d%d",&a,&b,&k);
a++;
b++;
update(,,n,a,b,k,a & );
if ((b - a + ) & ) //如果区间长度是偶数的话,+-抵消了
{
update(,,n,b + ,n,k,a & ); //b维护的是类似前缀和一样的东西,所以要对区间右边的操作.
update(,,n,b + ,n,-k,a & ^ ); //这里+k,-k主要取决于有影响的数在之后的区间的系数.
}
}
else
{
scanf("%d%d",&a,&b);
a++;
b++;
if ((b - a + ) & )
{
int temp1 = query1(,,n,a - ,(a - ) & ); //这里的四个temp就是利用b数组类似前缀和的一个特点得到的.
int temp2 = query2(,,n,a,b,b & ) + temp1;
int temp3 = query2(,,n,a,b,b & ^ ) - temp1;
int temp4 = query1(,,n,b,b & ) + temp1;
if (temp4 == && temp2 >= && temp3 >= )
puts("");
else
puts("");
}
else
{
int temp1 = query1(,,n,a - ,(a - ) & );
int temp2 = query2(,,n,a,b,b & ) - temp1;
int temp3 = query2(,,n,a,b,b & ^ ) + temp1;
int temp4 = query1(,,n,b,b & ) - temp1;
if (temp4 == && temp2 >= &&temp3 >= )
puts("");
else
puts("");
}
}
} return ;
}

Codeforces 717.F Heroes of Making Magic III的更多相关文章

  1. 【Codeforces717F】Heroes of Making Magic III 线段树 + 找规律

    F. Heroes of Making Magic III time limit per test:3 seconds memory limit per test:256 megabytes inpu ...

  2. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

  3. Codeforces 835 F. Roads in the Kingdom

    \(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...

  4. Codeforces 731 F. Video Cards(前缀和)

    Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...

  5. Codeforces CF#628 Education 8 D. Magic Numbers

    D. Magic Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  6. Codeforces Round #335 (Div. 2) A. Magic Spheres 模拟

    A. Magic Spheres   Carl is a beginner magician. He has a blue, b violet and c orange magic spheres. ...

  7. Codeforces Round #335 (Div. 2) A. Magic Spheres 水题

    A. Magic Spheres Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/contest/606/ ...

  8. Codeforces Round #443 (Div. 1) D. Magic Breeding 位运算

    D. Magic Breeding link http://codeforces.com/contest/878/problem/D description Nikita and Sasha play ...

  9. Codeforces Round #350 (Div. 2) D1. Magic Powder - 1 二分

    D1. Magic Powder - 1 题目连接: http://www.codeforces.com/contest/670/problem/D1 Description This problem ...

随机推荐

  1. Hyperledger Fabric 1.1 -- Policy 构成

    Policy 规则设计 本文主要是讲解一下在fabric中Policy的规则和写法,让大家有一个初步的认识,本文是基于fabric 1.1版本 Policy Type Policy Type 目前包括 ...

  2. 技本功丨用短平快的方式告诉你:Flink-SQL的扩展实现

    2019年1月28日,阿里云宣布开源“计算王牌”实时计算平台Blink回馈给ApacheFlink社区.官方称,计算延迟已经降到毫秒级,也就是你在浏览网页的时候,眨了一下眼睛,淘宝.天猫处理的信息已经 ...

  3. js页面跳转,url带url参数解决方案

    今天,在做一个项目的时候,向后端发送了一个Ajax请求,后端返回了一个字符串,告诉我未登录.那么我需要跳转到登录页面,同时告诉登录页面,登录成功后,需要跳回的url.也就是标题所说,url中的一个参数 ...

  4. PCA(主成分析)

    PCA通过将高维空间向量映射到低维,对于数据进行处理

  5. 《我是一只IT小小鸟》 读书笔记

    <我是一只IT小小鸟>讲述了IT人员的成长经历,邀请了许多名IT行业的职员,学生,研究生写了自己的亲身经历和人生感悟,以书中可以看到我国IT行业的快速进步,以及看到IT员在这条道路上的坎坷 ...

  6. APUE(unix环境高级编程)第三版---first day---部署书中实例的运行环境(apue.h)

    操作环境:RHEL7.0 部署apue.h实例运行环境 1.下载头文件src.3e.tar.gz 2.解压 tar zxvf src.3e.tar.gz 3.创建普通用户(我仿照书上创建的sar用户) ...

  7. Linux基础语句总结

    看的视频是bilibili的网址如下:https://www.bilibili.com/video/av18069261/?p=36 然后做了点总结,可能有错误也可能有遗漏,同时参考了他人的资料. 系 ...

  8. Chrome浏览器超强调试工具

    原文:http://lizanhong2011.blog.163.com/blog/static/18028516720117301312729/ 在Google Chrome浏览器出来之前,我一直使 ...

  9. 第13章 学习shell script

    由于博客园中dollar符号有别的意义,所以文中的dollar符号使用¥表示 第一个script [root@localhost script]# cat -n sh01.sh #!/bin/bash ...

  10. 性能测试工具Loadrunner使用经验小结(原创更新版)

    1. 引言 1.1. 简介 loadrunner是一种预测系统行为和性能的负载测试工具,它可以轻松创建虚拟用户.创建真实的负载.定位性能问题.重复测试保证系统的高性能 globa-100的注册码:AE ...