终于3ac了。妈的
最后一题,查询,前缀和+二分查找就可以了,也可以for一遍,预处理设置每个位置的组号,当作水题做就好了
#include <iostream> #include <stdio.h> using namespace std; const int maxn = 1e+5 + 10; int a[maxn]; int main() { int n; while(~scanf("%d", &n)){ for(int i = 0 ; i < n; ++i){ int val ; scanf("%d", &val); if(i >= 1){ a[i] = a[i-1] + val; }else { a[i] = val; } } int q; scanf("%d", &q); for(int i = 0 ; i < q ; ++i) { int val; scanf("%d", &val); int f = 0; int e = n; int mid ; int ans = 0; while(f < e){ mid = (f+e)/2; if(a[mid] > val){ e = mid; } else if(a[mid] == val){ break; } else { f = mid+1; } } if(a[mid] == val){ ans = mid; } else if(a[mid] < val){ ans = mid+1; } else { ans = mid; } printf("%d\n", ans+1); } } return 0; }