99网
您的当前位置:首页Codeforces Round #481 (Div. 3) C. Letters

Codeforces Round #481 (Div. 3) C. Letters

来源:99网

C. Letters


解题思路
二 分 + 前 缀 和 二分+前缀和 +

  • 先求出每栋楼房间数量的前缀和b[],在lower_bound()搜索信上的编号
  • lower_bound(b,b+n)-a得到的id即为宿舍楼号,对应的房间号即为t-b[i-1]
  • 具体操作见代码

附上代码

#pragma GCC optimize("-Ofast","-funroll-all-loops")
//#pragma GCC diagnostic error "-std=c++11"
#include<bits/stdc++.h>
#define int long long
#define lowbit(x) (x &(-x))
#define endl '\n'
using namespace std;
const int INF=0x3f3f3f3f;
const int dir[4][2]={-1,0,1,0,0,-1,0,1};
const double PI=acos(-1.0);
const double e=exp(1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=2e5+10;
typedef long long ll;
typedef pair<int,int> PII;
typedef unsigned long long ull;
int a[N],b[N];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		b[i]=b[i-1]+a[i];
	}
	while(m--){
		int t;
		cin>>t;
		int id=lower_bound(b,b+n,t)-b;
		cout<<id<<" "<<t-b[id-1]<<endl;
	}
	return 0;
}

因篇幅问题不能全部显示,请点此查看更多更全内容