2 条题解

  • 1
    @ 2023-7-23 7:44:58
    #include<bits/stdc++.h>
    using namespace std;
    long long n,q,x,y,b[100010];
    struct st{
    	long long x,y;
    }a[100010];
    int main(){
    	freopen("array.in","r",stdin);
    	freopen("array.out","w",stdout);
    	scanf("%lld%lld",&n,&q);
    	for(int i=1;i<=n;i++){
    		scanf("%lld%lld",&a[i].x,&a[i].y);
    	}
    	for(int i=1;i<=n+1;i++)b[i]=b[i-1]+a[i].x;//前缀和 
    	for(int i=1;i<=q;i++){
    		scanf("%lld",&x);
    		int p1=lower_bound(b+1,b+1+n,x)-b;
    		printf("%lld\n",a[p1].y);
    	}
    	//用前缀和加上lower_bound函数很简单 
    	return 0;
    }
    
    • -7
      @ 2023-7-22 19:45:58
      #include <cstdio>
      #include <cctype>
      #include <algorithm>
      using namespace std;
      int n, q, k[100005], p, t;
      long long a[100005];
      template <typename T> /* 不知道这一段什么意思?来洛谷看我博客 */
      inline T read(T in) {
      	T res = 0;
      	char c = getchar();
      	while (!isdigit(c))
      		c = getchar();
      	while (isdigit(c)) {
      		res = (res << 3) + (res << 1) + (c - 48);
      		c = getchar();
      	}
      	return res;
      }
      int main() {
      	freopen("array.in", "r", stdin);
      	freopen("array.out", "w", stdout);
      	n = read(n), q = read(q);
      	for (int i = 1; i <= n; i++) {
      		a[i] = read(a[i]), k[i] = read(k[i]);
      		a[i] += a[i - 1];
      	}
      	sort(a + 1, a + n + 1);
      	for (; q--; ) {
      		p = read(p);
      		t = lower_bound(a + 1, a + n + 1, p) - a;
      		printf("%d\n", k[t]); 
      	}
      	return 0;
      }
      
      • 1

      信息

      ID
      407
      时间
      1000ms
      内存
      256MiB
      难度
      8
      标签
      (无)
      递交数
      146
      已通过
      28
      上传者