結果

問題 No.3565 Take from Excluded
コンテスト
ユーザー akua
提出日時 2026-06-12 17:37:14
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 8,315 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,302 ms
コンパイル使用メモリ 268,448 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-06-12 17:37:48
合計ジャッジ時間 9,954 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 13 WA * 1 TLE * 1 -- * 3
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

# pragma GCC target("avx2")
# pragma GCC optimize("O3")
# pragma GCC optimize("unroll-loops")
#include<atcoder/all>

#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque #include <unordered_map> // unordered_map #include <unordered_set> // unordered_set #include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <iomanip>
#include <functional>
#include<unordered_map>
#include <random>
#include<bitset>
#include<cassert>
#include<chrono>
using namespace std;  
using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define arr(x) (x).rbegin(),(x).rend()
typedef long long ll;
typedef unsigned long long ull;
const ll inf=4e18;  
using graph = vector<vector<int> > ;
using vi=vector<int>;
using P= pair<ll,ll>;  
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vll=vector<ll>; 
using vvll=vector<vll>;
using vvvll=vector<vvll>;
using vp=vector<P>;
using vvp=vector<vp>;
using vvvp=vector<vvp>;
using vd=vector<double>;
using vvd =vector<vd>;
//using vs=vector<string>;
//string T="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//string S="abcdefghijklmnopqrstuvwxyz";
//g++ main.cpp -std=c++17 -I .  
//cout <<setprecision(20);
//cout << fixed << setprecision(10);
//cin.tie(0); ios::sync_with_stdio(false);

const double PI = acos(-1);
int vx[]={0,1,0,-1,-1,1,1,-1},vy[]={1,0,-1,0,1,1,-1,-1};
void putsYes(bool f){cout << (f?"Yes":"No") << endl;}
void putsYES(bool f){cout << (f?"YES":"NO") << endl;}
void putsFirst(bool f){cout << (f?"First":"Second") << endl;}
void debug(int test){cout << "TEST" << " " << test << endl;}
ll pow_pow(ll x,ll n,ll mod){
    if(n==0) return 1; 
    x%=mod;
    ll res=pow_pow(x*x%mod,n/2,mod);
    if(n&1)res=res*x%mod;
    return res;
}
ll gcdll(ll x,ll y){
    if(y==0)return x;
    return gcdll(y,x%y);
}
ll INFF=1000100100100100100;
ll lcmll(ll x,ll y){
    ll X=(x/gcdll(x,y)),Y=y;
    if(X<=INFF/Y)return X*Y;
    else return INFF;
}
template<class T> bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    else return false;
}
template<class T> bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    else return false;
}
struct edge{
  int to; ll cost;int id;
  edge(int to,ll cost,int id) : to(to),cost(cost),id(id) {}
};
using ve=vector<edge>;
using vve=vector<ve>;
int mod=998244353;
using mint=modint998244353;
using vm=vector<mint>;
using vvm=vector<vm>;
using vvvm=vector<vvm>;
constexpr int MAX = 10;
ll fact[MAX],finv[MAX],invv[MAX];
void initcomb(){
  fact[0]=fact[1]=1;
  finv[0]=finv[1]=1;
  invv[1]=1;
  for(int i=2;i<MAX;i++){
    fact[i]=fact[i-1]*i%mod;
    invv[i]=mod-invv[mod%i]*(mod/i)%mod;
    finv[i]=finv[i-1]*invv[i]%mod;
  }
}
ll comb(ll n,ll k){
  if(n<k) return 0;
  if(n<0||k<0) return 0;
  return fact[n]*(finv[k]*finv[n-k]%mod)%mod;
}
struct Compress{
   vll a;
   int cnt;
   Compress() :cnt(0) {}
   void add(ll x){a.push_back(x);}
   void init(){
       sort(all(a));
       a.erase(unique(all(a)),a.end());
       cnt=a.size();
   }
   ll to(ll x){
       int index=lower_bound(all(a),x)-a.begin();
       return index;
   } //変換先
   ll from(ll x){return a[x];}//逆引き
   int size(){return cnt;}
};
struct UnionFind { 
    vector<int> par, siz; 
    UnionFind(int n) : par(n, -1) , siz(n, 1) { } 
    int root(int x) { 
        if (par[x] == -1) return x;
        else return par[x] = root(par[x]);
    }
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    bool unite(int x, int y) {
        x = root(x), y = root(y);
        if (x == y) return false; 
        if (siz[x] < siz[y]) swap(x, y);
        par[y] = x;
        siz[x] += siz[y];
        return true;
    }
    int size(int x) {
        return siz[root(x)];
    }
};
std::ostream& operator<<(std::ostream& dest, __int128_t value) {
   std::ostream::sentry s(dest);
   if(s) {
      __uint128_t tmp = value < 0 ? -value : value;
      char buffer[128];
      char* d = std::end(buffer);
      do {
         --d;
         *d = "0123456789"[tmp % 10];
         tmp /= 10;
      } while(tmp != 0);
      if(value < 0) {
         --d;
         *d = '-';
      }
      int len = std::end(buffer) - d;
      if(dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); }
   }
   return dest;
}

__int128 parse(string& s) {
   __int128 ret = 0;
   for(int i = 0; i < s.length(); i++)
      if('0' <= s[i] && s[i] <= '9') ret = 10 * ret + s[i] - '0';
   return ret;
}
/*
int main() {
  string s = "187821878218782187821878218782";
  __int128 x = parse(s);
  x *= 2;
  cout << x << endl;
}
*/
using np=pair<__int128,__int128>;

vll anss; 
void solve(int test){
    int n,q; cin >> n >> q;
    vector<np> p;
    {
        vi a(n);
        rep(i,n)cin >> a[i];
        sort(all(a));
        a.erase(unique(all(a)),a.end());
        rep(i,a.size()){
            if(p.size()==0 || p.back().second+1!=a[i]){
                p.push_back(P(a[i],a[i]));
            }
            else {
                p.back().second=a[i];
            }
        }
    }
    auto next=[&](vector<np> p,__int128 m){
        __int128 res=m;
        vector<np> next;
        rep(i,p.size()-1){
            if(res==0)break;
            __int128 d=p[i+1].first-1-p[i].second;
            __int128 ti=min(d,res);
            res-=ti;
            next.push_back(P(p[i].second+1,p[i].second+ti));
        }
        if(res){
            next.push_back(P(p.back().second+1,p.back().second+res));
        }
        return next;
    };
    {
        /*
        auto damp=p;
        rep(j,10){
            cout <<"te "<< j+1 << endl;
            for(auto [l,r]:damp)cout << l << " " << r <<endl;
            damp=next(damp,4);
        }
        */

    }
    rep(qi,q){
        ll k,x,m; cin >> k >> x >> m;
        repi(i,1,k+1){
            auto nextp=next(p,m);
            if(i>=2 && nextp.size()==p.size()){
                int ti=k-i;
                __int128 bs=1ll*2*m*(ti/(2*p.size()));
                int rem=ti%(2*p.size());
                {
                    vector<np> koho;
                    rep(i,p.size()){
                        koho.push_back(p[i]);
                        koho.push_back(nextp[i]);
                    }
                    bs+=koho[0].second;
                    int id=1;
                    rep(_,rem){
                        bs+=koho[id].second-koho[id].first+1;
                        id=(id+1)%koho.size();
                    }
                    vector<np> nextpp;
                    __int128 CNT=bs;
                    rep(_,p.size()){
                        __int128 si=koho[id].second-koho[id].first+1;
                        nextpp.push_back(make_pair(CNT+1,CNT+si));
                        CNT+=si;
                        CNT+=koho[(id+1)%koho.size()].second;
                        CNT-=koho[(id+1)%koho.size()].first;
                        CNT+=1;
                        id+=2;
                        id%=koho.size();
                    }
                    p=nextpp;
                }
                break;
            }
            else {
                p=nextp;
            }
        }
        {
            int cnt=0;
            __int128 ans=-1;
            rep(i,p.size()){
                if(cnt+p[i].second-p[i].first+1>=x){
                    ans=p[i].first-1+x-cnt;
                    break;
                }
                cnt+=p[i].second-p[i].first+1;
            }
            cout << ans%mod << endl;
        }


    }





}
//cat cf.cpp | pbcopy
//g++ cf.cpp -std=c++17 -I .  
int main(){cin.tie(0);ios::sync_with_stdio(false);
    //initcomb();
    int t=1;//cin >>t;
    rep(test,t)solve(test);
    for(auto ans:anss){
        cout<< ans << endl;
    }
}
0