結果

問題 No.1890 Many Sequences Sum Queries
ユーザー もひもひーともひもひーと
提出日時 2022-04-07 12:39:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 193 ms / 2,000 ms
コード長 2,493 bytes
コンパイル時間 857 ms
コンパイル使用メモリ 87,548 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-05 22:26:03
合計ジャッジ時間 3,630 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 193 ms
5,248 KB
testcase_01 AC 190 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 69 ms
5,376 KB
testcase_08 AC 16 ms
5,376 KB
testcase_09 AC 20 ms
5,376 KB
testcase_10 AC 187 ms
5,376 KB
testcase_11 AC 91 ms
5,376 KB
testcase_12 AC 128 ms
5,376 KB
testcase_13 AC 135 ms
5,376 KB
testcase_14 AC 69 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cmath>
#include<set>
#include<stack>
#include<deque>
#include<cassert>
//#include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i<(int)(n); i++)
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef long long ll;
typedef pair<int,int> P;
using Graph = vector<vector<int>>;
//#define INF 10000000
class UnionFind{
    public:
    vector<int> par;

    UnionFind(int n) : par(n){
        for(int i=0; i<n; i++) par[i] = -1;
    }

    int root(int x){
        if(par[x] == -1) return x;
        int next = par[x];
        return root(next);
    }

    void unite(int x, int y){
        int px = root(x);
        int py = root(y);
        if(px == py) return;
        par[px] = py;
    }

    bool same(int x, int y){
        int px = root(x);
        int py = root(y);
        if(px == py) return true;
        else return false; 
    }
};



bool isPrime(int x){
    bool prime = true;
    for(int i=2; i<x; i++){
        if(x%i == 0){
            prime = false;
            break;
        }
    }
    if(!prime) return false;
    return true;
}


//------------------------------------------------------------------------//

vector<int> p;
vector<int> sum;

bool isOk(int mid,int key){
    if(sum[mid] >= key) return true;
    return false;
}

int main(){
    int n,q;
    cin >> n >> q;
    vector<int> a(n);
    rep(i,n) cin >> a[i];
    rep(i,n){
        for(int j=1; j<=a[i]; j++){
            p.emplace_back(j);
        }
    }
    /*
    for(auto it : p){
        cout << it << ":";
    }
    puts("");
    */
    int tmp = 0;
    sum.emplace_back(0);
    for(auto it : p){
        tmp += it;
        sum.emplace_back(tmp);
    }
    /*
    for(auto it : sum) cout << it << endl;
    puts("");
    */
    ///query
    vector<int> ans;
    rep(i,q){
        int s;
        cin >> s;
        int right = sum.size()+1;
        int left = -1;
        while(right - left > 1){
            int mid = (right + left)/2;
            if(isOk(mid,s)) right = mid;
            else left = mid;
        }
        if(left == sum.size()){
            ans.emplace_back(-1);
            continue;
        }
        ans.emplace_back(right);
    }
    for(auto it : ans) cout << it << endl;
    return 0;
}
0