赞
踩
2 5 2 1 2 3 4 5 1 5 3 5 4 1 1 1 1 1 1 2
15
12
2
这题显然是个很水的题, 却花了我两个小时。
前缀和解法:
- #include<iostream>
- #include<cstdio>
- #define LL long long
- using namespace std;
-
- const int maxn = 1e6;
- LL qzh[maxn];
- int N, M, q1, q2;
-
-
- int main() {
- int T;
- scanf("%d", &T);
- while(T--) {
- scanf("%d%d", &N, &M);
- int x;
- qzh[0] = 0;
- for(int i = 1; i <= N; i++) {
- scanf("%d", &x); qzh[i] = qzh[i-1] + x;
- }
- for(int i = 1; i <= M; i++) {
- scanf("%d%d", &q1, &q2);
- printf("%I64d\n", qzh[q2]-qzh[q1-1]);
- }
- }
- return 0;
- }

//刚开始的时候,我用的是树状数组,很轻松的过了样例,然后提交各种TLE,算了一下时间,O(nlgn + mlgn) = O((n+m)lgn), 不知道为什么会过不了,//反正我用这个前缀和在写的时候,刚开始也是过不了,后来把cin改成了scanf, cout改成了printf就过了, 中间的时间相差真的是好几百毫秒,差的特别特别多,
//以后在数据比较大的情况下,再也不敢随便使用cin,cout了, 用的时候一定要谨慎。

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。