【发布时间】:2015-09-13 03:52:02
【问题描述】:
我试图用下面的代码解决这个问题。但并非所有输入的答案都是准确的。 问题陈述
Ikbal 有两个长度为 N 的数组 a 和 b,最初所有值都为零。我们有Q操作。让我们在这个数组上定义三种类型的操作:
1 l r c 将 al,al+1,...,ar 增加 c。
2 l r c 将 bl,bl+1,...,br 增加 c。
3 l r 以 1000000007 为模打印 (al∗bl)+(al+1∗bl+1)+...+(ar∗br) 输入格式
输入的第一行由 N 和 Q 组成。接下来的 Q 行包含三种操作类型之一。
约束
1≤N≤109 1≤Q≤200000 1≤c≤10000 1≤l≤r≤N 输出格式
当你得到一个类型 3 的操作时,你应该在一个新的行中打印答案。
示例输入
5 3 1 1 5 5 2 2 4 2 3 3 4 样本输出
20 说明
第一次操作后的数组是这样的:
a=5,5,5,5,5
b=0,0,0,0,0 第二次操作后的数组是这样的:
a=5,5,5,5,5
b=0,2,2,2,0
第三次运算的答案:5∗2+5∗2=20
**我的代码**
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
vector<int> a,b,c;
int n,q,r,p;
cin >> n;
cin >> q;
for(int i=0;i<q;i++) {
cin >> r;
a.push_back(r);
if(r==3) {
p = 3;
} else {
p = 4;
}
for(int j=1;j<p;j++) {
cin >> r;
a.push_back(r);
}
}
vector<int> aa(n,0),bb(n,0);
int g,start,endd,val,anss=0;
for(int i=0;i<a.size();) {
if(a[i]==3) {
start = a[i+1]-1;
endd = a[i+2]-1;
if(start==endd) {
anss = (aa[start]*bb[start])%1000000007;
} else {
anss = (aa[start]*bb[start] + aa[endd]*bb[endd])%1000000007;
}
cout << anss << endl;
i+= 3;
} else {
start = a[i+1] - 1;
endd = a[i+2];
val = a[i+3];
if(a[i]==1) {
for(int j=start;j<endd;j++) {
aa[j] += val;
}
} else {
for(int j=start;j<endd;j++) {
bb[j] += val;
}
}
i+= 4;
}
}
/*
for(int i=0;i<n;i++) {
cout << aa[i] << " " ;
cout << bb[i] << endl;
}
for(int i=0;i<a.size();i++) {
cout << a[i] << endl;
} */
return 0;
}
给定输入的预期输出: http://i.stack.imgur.com/4OsSo.jpg
【问题讨论】:
-
anss = (aa[start]*bb[start])%1000000007;和anss = (aa[start]*bb[start] + aa[endd]*bb[endd])%1000000007;可能会导致整数溢出。您可以通过这种方式在模 1000000007 中添加和乘以 32 位整数,例如:ideone.com/V0mDWb -
别忘了提到你想在编程比赛中作弊hackerrank.com/contests/worldcup/challenges/two-arrays-1
标签: c++ arrays algorithm data-structures