QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#250602 | #7627. Phony | KKT89 | RE | 0ms | 0kb | C++17 | 12.2kb | 2023-11-13 13:54:21 | 2023-11-13 13:54:22 |
answer
#pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cassert>
#include <chrono>
#include <random>
#include <set>
#include <utility>
#include <array>
#include <bitset>
#include <unordered_set>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
return (ull)rng() % B;
}
inline double time() {
return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}
////////////////////////////////////////////////////////////////////////////////
#ifndef __x86_64__
#error Only x86-64 targets are supported
#endif
#include<cstdint>
#include<vector>
#include<string>
#include<iosfwd>
#define __builtin_ia32_adc(x,y,flag) __asm__("addb %3, %0\n\t" "adcq %2, %1\n\t" "setc %0":"+r"(flag),"+r"(x):"r"(y),"i"(-1):"cc")
struct bigint{// made by dengyaotriangle!
typedef unsigned long long u64;
typedef unsigned __int128 u128;
typedef std::size_t st;
std::vector<u64> data;
bigint(){}
bigint(u64 x):data(x?std::vector<u64>{x}:std::vector<u64>{}){}
bigint(const std::string &s){
st pos=s.length();
int cnt=0;
u64 val=0;
while(pos){
pos--;
if(cnt==64){
data.push_back(val);
val=0;cnt=0;
}
val|=(u64)(s[pos]=='1')<<cnt;
++cnt;
}
if(cnt&&val)data.push_back(val);
}
explicit operator std::string()const{
if(data.empty())return "0";
bool t=0;
std::string ret;
for(int i=63;i>=0;i--){
t|=(data.back()>>i)&1;
if(t)ret+='0'|((data.back()>>i)&1);
}
st i=data.size()-1;
while(i){
i--;
for(int j=63;j>=0;j--)ret+='0'|((data[i]>>j)&1);
}
return ret;
}
explicit operator bool()const{return !data.empty();}
explicit operator u64()const{return data.empty()?0:data[0];}
st digit()const{
if(data.empty())return 0;
return (data.size()<<6)-__builtin_clzll(data.back());
}
bool operator==(const bigint &a)const{return a.data==data;}
bool operator!=(const bigint &a)const{return a.data!=data;}
bool operator<(const bigint &a)const{
if(data.size()!=a.data.size())return data.size()<a.data.size();
for(st i=data.size();i;){
i--;
if(data[i]!=a.data[i])return data[i]<a.data[i];
}
return 0;
}
bool operator>(const bigint &a)const{return a<(*this);}
bool operator<=(const bigint &a)const{return !(*this>a);}
bool operator>=(const bigint &a)const{return !(*this<a);}
bigint &operator<<=(st n){
if(data.empty())return *this;
int w=n&63;st z=n>>6;
st i=data.size();
bool flg=0;
if(w&&(data.back()>>(64-w)))data.push_back(0),flg=1;
data.resize(data.size()+z);
while(i){
i--;
if(flg)data[i+z+1]|=data[i]>>(64-w);
data[i+z]=data[i]<<w;
flg|=bool(w);
}
for(st i=0;i<z;i++)data[i]=0;
return *this;
}
bigint &operator>>=(st n){
int w=n&63;st z=n>>6,i=0;
for(;i+z<data.size();i++){
if(w&&i)data[i-1]|=data[i+z]<<(64-w);
data[i]=data[i+z]>>w;
}
while(data.size()>i)data.pop_back();
while(!data.empty()&&data.back()==0)data.pop_back();
return *this;
}
bigint operator<<(st n)const{return bigint(*this)<<=n;}
bigint operator>>(st n)const{return bigint(*this)>>=n;}
bigint &operator+=(const bigint &a){
data.resize(std::max(data.size(),a.data.size()));
bool carry=0;
for(st i=0;i<data.size();i++){
u64 rg=0;
if(i<a.data.size())rg=a.data[i];
__builtin_ia32_adc(data[i],rg,carry);
}
if(carry)data.push_back(1);
return *this;
}
bigint &operator-=(const bigint &a){
bool carry=1;
for(st i=0;i<data.size();i++){
u64 rg=-1;
if(i<a.data.size())rg=~a.data[i];
__builtin_ia32_adc(data[i],rg,carry);
}
while(!data.empty()&&data.back()==0)data.pop_back();
return *this;
}
bigint &operator++(){return *this+=bigint(1);}
bigint &operator--(){return *this-=bigint(1);}
bigint operator++(int){bigint tmp=*this;++*this;return tmp;}
bigint operator--(int){bigint tmp=*this;--*this;return tmp;}
bigint &operator*=(const bigint &a){
std::vector<u64> ret(data.size()+a.data.size());
for(st i=0;i<data.size();i++){
u64 carry=0;bool wcarry=0;
st k=i;
for(st j=0;j<a.data.size();j++,k++){
u128 r=data[i]*(u128)a.data[j]+carry;
u64 cur=r;
carry=r>>64;
__builtin_ia32_adc(ret[k],cur,wcarry);
}
while(carry||wcarry){
__builtin_ia32_adc(ret[k],carry,wcarry);
carry=0;k++;
}
}
while(!ret.empty()&&ret.back()==0)ret.pop_back();
data=ret;
return *this;
}
bigint &operator/=(const bigint &a){
if(a.digit()>digit()){
data.clear();
return *this;
}
st z=digit()-a.digit();
std::vector<u64> ret;
while(1){
bigint tmp=a<<z;
if(tmp<=*this){
*this-=tmp;
st v1=z>>6;
if(ret.size()<=v1)ret.resize(v1+1);
ret[v1]|=(u64)(1)<<(z&63);
}
if(!z)break;
z--;
}
data=ret;
return *this;
}
bigint &operator%=(const bigint &a){
if(a.digit()>digit())return *this;
st z=digit()-a.digit();
while(1){
bigint tmp=a<<z;
if(tmp<=*this)*this-=tmp;
if(!z)break;
z--;
}
return *this;
}
bigint operator+(const bigint &a)const{return bigint(*this)+=a;}
bigint operator-(const bigint &a)const{return bigint(*this)-=a;}
bigint operator*(const bigint &a)const{return bigint(*this)*=a;}
bigint operator/(const bigint &a)const{return bigint(*this)/=a;}
bigint operator%(const bigint &a)const{return bigint(*this)%=a;}
};
std::istream &operator>>(std::istream &st,bigint &a){
std::string s;st>>s;a=bigint(s);return st;
}
std::ostream &operator<<(std::ostream &st,const bigint &a){
return st<<(std::string)(a);
}
////////////////////////////////////////////////////////////////////////////////
// 0-indexed
template<typename T>
struct BIT{
int n;
vector<T> bit,ary;
BIT(int n = 0) : n(n),bit(n+1),ary(n) {}
T operator[](int k) {
return ary[k];
}
// [0, i)
T sum(int i) {
T res = 0;
for (; i > 0; i -= (i&-i)) {
res += bit[i];
}
return res;
}
// [l, r)
T sum(int l, int r) {
return sum(r) - sum(l);
}
void add(int i, T a) {
ary[i] += a;
i++;
for (; i <= n; i += (i&-i)) {
bit[i] += a;
}
}
int lower_bound(T k) { // k <= sum(res)
if (k <= 0) return 0;
int res = 0, i = 1;
while ((i << 1) <= n) i <<= 1;
for (; i ; i >>= 1) {
if (res+i <= n and bit[res+i] < k) {
k -= bit[res += i];
}
}
return res;
}
// The 2nd UC Stage 9: Qinhuangdao - I
// 円環状で見たときに bit[i]+bit[i-1]+...+bit[j] を求める
T sum_cyc(int i, int j) {
if (j <= i) return sum(j, i+1);
else return sum(0, i+1) + sum(j, n);
}
// The 2nd UC Stage 9: Qinhuangdao - I
// 円環状で見たときに bit[i]+bit[i-1]+...+bit[j] >= k となる最近の j と左辺の総和を求める
// 雑にlog2つ
pair<int, T> lower_bound_cyc(int j, T k) {
T prefix = sum(j+1);
if (prefix < k) {
k -= prefix;
int l = 0, r = n;
while (r-l > 1) {
int mid = (l+r)/2;
T s = sum(mid, n);
if (s >= k) {
l = mid;
}
else {
r = mid;
}
}
return {l, prefix+sum(l,n)};
}
else {
int l = 0, r = j+1;
while (r-l > 1) {
int mid = (l+r)/2;
T s = sum(mid, j+1);
if (s >= k) {
l = mid;
}
else {
r = mid;
}
}
return {l, sum(l, j+1)};
}
}
};
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n,q; cin >> n >> q;
ll K; cin >> K;
vector<ll> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
sort(a.rbegin(), a.rend());
vector<ll> z(n);
for (int i = 0; i < n; ++i) {
z[i] = a[i]%K;
}
sort(z.begin(), z.end());
z.erase(unique(z.begin(), z.end()), z.end());
int m = z.size();
int sz = 0;
BIT<ll> bit(m);
auto add = [&](int i) -> void {
sz += 1;
bit.add(i, 1);
};
vector<ll> md(n);
for (int i = 0; i < n; ++i) {
md[i] = lower_bound(z.begin(), z.end(), a[i]%K) - z.begin();
if (a[0]-a[i] < K) {
add(md[i]);
}
}
ll pos = md[0];
bigint cnt = 0;
bigint mx = a[0];
bigint nx = 0;
auto calNext = [&]() -> void {
if (sz >= n) {
return;
}
else {
ll dif = z[pos]-z[md[sz]];
if (dif < 0) dif += K;
nx = bit.sum_cyc(pos, md[sz]);
bigint uo = mx-dif;
nx += (bigint)(uo-a[sz]-K)/K*(bigint)sz;
}
};
calNext();
while (q--) {
char c; cin >> c;
ll in; cin >> in;
if (c == 'A') {
int x = in;
if (sz < x) {
cout << a[x-1] << "\n";
}
else {
// 最大値との差分を返すように
auto findKth = [&](int i, int k, int cnt) -> ll {
if (bit[i]-cnt >= k) return 0;
if (sz-cnt < k) return K;
k += cnt;
auto p = bit.lower_bound_cyc(i, k);
ll dif = z[i]-z[p.first];
if (dif < 0) dif += K;
return dif;
};
ll dif = findKth(pos, x, stoll(string(cnt)));
cout << stoll(string(mx - bigint(dif))) << "\n";
}
}
else {
auto update = [&]() -> void {
if (pos) {
mx -= z[pos]-z[pos-1];
pos -= 1;
cnt = 0;
}
else {
mx -= z[0]-z[m-1]+K;
pos = m-1;
cnt = 0;
}
};
bigint t = in;
while (sz < n and t >= nx) {
t -= nx;
mx = a[sz]+K;
pos = md[sz];
ll cp = a[sz];
while (sz < n and a[sz] == cp) {
add(md[sz]);
}
update();
calNext();
}
bigint u = t/sz;
mx -= u*K;
t -= u*sz;
nx -= t;
if ((bigint)(bit[pos])-cnt >= t) {
cnt += t;
}
else {
t -= ((bigint)bit[pos]-cnt);
update();
auto p = bit.lower_bound_cyc(pos, stoll((string)t));
ll dif = z[pos] - z[p.first];
if (dif < 0) dif += K;
mx -= dif;
pos = p.first;
cnt = t-(p.second-bit[pos]);
}
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
3 5 5 7 3 9 A 3 C 1 A 2 C 2 A 3