Codeforces 101744E —— 位运算前缀

原题链接:http://codeforces.com/gym/101744/problem/E

 

E. MaratonIME rides the university bus

time limit per test

3.0 s

memory limit per test

256 MB

input

standard input

output

standard output

If we organize it correctly, ...

UNKNOWN

To make the trip to the subway less boring and tiring, the SPSU, Sao Paulo State University, tried one of its most famous inventions: buses with Infinite Inner Length! In such a modern engineering wonder, there's always a couple of empty seats for the students to sit and chat during the trip.

MaratonIME crew is very popular, so popular that they have friends at every SPSU institute. Like everyone else from this university, they need to take the bus after a long day learning how to fix the Wi-Fi network. Because they don't practice sports like rowing, every SPSU student sits right after entering the bus, making pairs whenever possible. Thinking about that, Gi, an ICPC expert, comes with a problem to think on the way to the subway: given a number n which indicates the number of institutes at SPSU and n integers ai representing the amount of people waiting for the bus at the institute i, Gi wants to know for m pairs ljrj (lj ≤ rj) if all the people waiting for the bus at any point between lj e rj (inclusive) took an empty bus, they could sit together in pairs (nobody would sit alone).

Input

The input consist in one line with two integers n and m, the number of institutes and the number of Gi's questions. In the second line there are n integers ai, the number of people waiting for the bus at the ith institute. Then follows m lines with two integers each, li and ri, the first and last institute of Gi's question.

  • 1 ≤ n, m ≤ 105
  • 0 ≤ ai ≤ 105
  • 1 ≤ li ≤ ri ≤ n

Output

Output "Sim" if it is possible to organize all the pairs in a way nobody sits alone or "Nao" otherwise.

Example

input

Copy

5 2
1 4 10 3 2
3 5
2 3

output

Nao
Sim

Note

In the first sample we have 5 institutes with 1, 4, 10, 3 and 2 students. Gi asks if it is possible to form only couples if the ones between the 3rd and the 5th institutes takes an empty bus and the ones between the 2nd and the 3rd. For the first we have 15 so we can't and for the second we have 14 so we can.

 

 

题目大意:

给出n个研究所和m个问题,问题是一个范围,在指定范围内求输入第二行的范围和,若为单数则输出Nao,否则输出Sim。

 

 

解题思路:

1、用普通方法求和,发现暴力不能过

2、通过sum数组求和、美观又文雅

3、通过等效位运算,神级算法!!

 

 

代码思路:

对第一种思路,没有代码思路

对第二种思路,用sum数组存储每个地址的当前和即可

对第三种思路,读者可慢慢想,很有意思

 

 

核心:本题关键在于这是一道水题!

 

代码(第一思路就不列举了,太low):

 

#include <bits/stdc++.h>
using namespace std;
int a[100010];
int sum[100010]={0};
int main()
{
	std::ios::sync_with_stdio(false);
	int n,m,l,r;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
	} 
	
	while(m--)
	{
		cin>>l>>r;
		if((sum[r]-sum[l-1]) %2 == 0) printf("Sim\n");
		else printf("Nao\n");
	}
	return 0;
}

高级算法:

#include <bits/stdc++.h>
using namespace std;
int a[100010];
int n, m, l, r;
int main()
{
	//ios::sync_with_stdio(false);
	//cin.tie(0);
	scanf("%d %d", &n, &m);
	a[0] = 0;
	for(int i=1; i<=n; i++)
	{
		scanf("%d", &a[i]);
		a[i] &= 1;
		a[i] ^= a[i-1];
	} 
	
	while(m--)
	{
		scanf("%d %d", &l, &r);
		if(a[r] - a[l-1]) puts("Nao");
		else puts("Sim");
	}
	return 0;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值