QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#158838 | #7106. Infinite Parenthesis Sequence | ucup-team087# | WA | 233ms | 3940kb | C++20 | 18.3kb | 2023-09-02 17:05:08 | 2023-09-02 17:05:08 |
Judging History
answer
#ifndef LOCAL
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#endif
#include <bits/stdc++.h>
using namespace std;
//fast IO by yosupo
//sc.read(string) だと append される
struct Scanner {
FILE* fp = nullptr;
char line[(1 << 15) + 1];
size_t st = 0, ed = 0;
void reread() {
memmove(line, line + st, ed - st);
ed -= st;
st = 0;
ed += fread(line + ed, 1, (1 << 15) - ed, fp);
line[ed] = '\0';
}
bool succ() {
while (true) {
if (st == ed) {
reread();
if (st == ed) return false;
}
while (st != ed && isspace(line[st])) st++;
if (st != ed) break;
}
if (ed - st <= 50) reread();
return true;
}
template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
bool read_single(T& ref) {
if (!succ()) return false;
ref.clear();
while (true) {
size_t sz = 0;
while (st + sz < ed && !isspace(line[st + sz])) sz++;
ref.append(line + st, sz);
st += sz;
if (!sz || st != ed) break;
reread();
}
return true;
}
template <class T, enable_if_t<is_integral<T>::value, int> = 0>
bool read_single(T& ref) {
if (!succ()) return false;
bool neg = false;
if (line[st] == '-') {
neg = true;
st++;
}
ref = T(0);
while (isdigit(line[st])) {
ref = 10 * ref + (line[st++] - '0');
}
if (neg) ref = -ref;
return true;
}
template <class T> bool read_single(vector<T>& ref) {
for (auto& d : ref) {
if (!read_single(d)) return false;
}
return true;
}
void read() {}
template <class H, class... T> void read(H& h, T&... t) {
bool f = read_single(h);
assert(f);
read(t...);
}
Scanner(FILE* _fp) : fp(_fp) {}
};
struct Printer {
public:
template <bool F = false> void write() {}
template <bool F = false, class H, class... T>
void write(const H& h, const T&... t) {
if (F) write_single(' ');
write_single(h);
write<true>(t...);
}
template <class... T> void writeln(const T&... t) {
write(t...);
write_single('\n');
}
Printer(FILE* _fp) : fp(_fp) {}
~Printer() { flush(); }
private:
static constexpr size_t SIZE = 1 << 15;
FILE* fp;
char line[SIZE], small[50];
size_t pos = 0;
void flush() {
fwrite(line, 1, pos, fp);
pos = 0;
}
void write_single(const char& val) {
if (pos == SIZE) flush();
line[pos++] = val;
}
template <class T, enable_if_t<is_integral<T>::value, int> = 0>
void write_single(T val) {
if (pos > (1 << 15) - 50) flush();
if (val == 0) {
write_single('0');
return;
}
if (val < 0) {
write_single('-');
val = -val; // todo min
}
size_t len = 0;
while (val) {
small[len++] = char('0' + (val % 10));
val /= 10;
}
for (size_t i = 0; i < len; i++) {
line[pos + i] = small[len - 1 - i];
}
pos += len;
}
void write_single(const string& s) {
for (char c : s) write_single(c);
}
void write_single(const char* s) {
size_t len = strlen(s);
for (size_t i = 0; i < len; i++) write_single(s[i]);
}
template <class T> void write_single(const vector<T>& val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) write_single(' ');
write_single(val[i]);
}
}
void write_single(long double d){
{
long long v=d;
write_single(v);
d-=v;
}
write_single('.');
for(int _=0;_<8;_++){
d*=10;
long long v=d;
write_single(v);
d-=v;
}
}
};
Scanner sc(stdin);
Printer pr(stdout);
using ll=long long;
//#define int ll
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
using pi=pair<int,int>;
using vi=vc<int>;
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
return os<<"{"<<p.a<<","<<p.b<<"}";
}
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ";
dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif
using uint=unsigned;
using ull=unsigned long long;
template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
return os<<vc<t>(all(a));
}
template<int i,class T>
void print_tuple(ostream&,const T&){
}
template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
if(i)os<<",";
os<<get<i>(t);
print_tuple<i+1,T,Args...>(os,t);
}
template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
os<<"{";
print_tuple<0,tuple<Args...>,Args...>(os,t);
return os<<"}";
}
ll read(){
ll i;
cin>>i;
return i;
}
vi readvi(int n,int off=0){
vi v(n);
rep(i,n)v[i]=read()+off;
return v;
}
pi readpi(int off=0){
int a,b;cin>>a>>b;
return pi(a+off,b+off);
}
template<class t>
void print_single(t x,int suc=1){
cout<<x;
if(suc==1)
cout<<"\n";
if(suc==2)
cout<<" ";
}
template<class t,class u>
void print_single(const pair<t,u>&p,int suc=1){
print_single(p.a,2);
print_single(p.b,suc);
}
template<class T>
void print_single(const vector<T>&v,int suc=1){
rep(i,v.size())
print_single(v[i],i==int(v.size())-1?suc:2);
}
template<class T>
void print_offset(const vector<T>&v,ll off,int suc=1){
rep(i,v.size())
print_single(v[i]+off,i==int(v.size())-1?suc:2);
}
template<class T,size_t N>
void print_single(const array<T,N>&v,int suc=1){
rep(i,N)
print_single(v[i],i==int(N)-1?suc:2);
}
template<class T>
void print(const T&t){
print_single(t);
}
template<class T,class ...Args>
void print(const T&t,const Args&...args){
print_single(t,2);
print(args...);
}
string readString(){
string s;
cin>>s;
return s;
}
template<class T>
T sq(const T& t){
return t*t;
}
void YES(bool ex=true){
cout<<"YES\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void NO(bool ex=true){
cout<<"NO\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void Yes(bool ex=true){
cout<<"Yes\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void No(bool ex=true){
cout<<"No\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
//#define CAPITAL
/*
void yes(bool ex=true){
#ifdef CAPITAL
cout<<"YES"<<"\n";
#else
cout<<"Yes"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void no(bool ex=true){
#ifdef CAPITAL
cout<<"NO"<<"\n";
#else
cout<<"No"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}*/
void possible(bool ex=true){
#ifdef CAPITAL
cout<<"POSSIBLE"<<"\n";
#else
cout<<"Possible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void impossible(bool ex=true){
#ifdef CAPITAL
cout<<"IMPOSSIBLE"<<"\n";
#else
cout<<"Impossible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
constexpr ll ten(int n){
return n==0?1:ten(n-1)*10;
}
const ll infLL=LLONG_MAX/3;
#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif
int topbit(signed t){
return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
return t==0?-1:63-__builtin_clzll(t);
}
int topbit(ull t){
return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
return a==0?64:__builtin_ctzll(a);
}
int botbit(ull a){
return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
return __builtin_popcount(t);
}
int popcount(ll t){
return __builtin_popcountll(t);
}
int popcount(ull t){
return __builtin_popcountll(t);
}
int bitparity(ll t){
return __builtin_parityll(t);
}
bool ispow2(int i){
return i&&(i&-i)==i;
}
ll mask(int i){
return (ll(1)<<i)-1;
}
ull umask(int i){
return (ull(1)<<i)-1;
}
ll minp2(ll n){
if(n<=1)return 1;
else return ll(1)<<(topbit(n-1)+1);
}
bool inc(int a,int b,int c){
return a<=b&&b<=c;
}
template<class t> void mkuni(vc<t>&v){
sort(all(v));
v.erase(unique(all(v)),v.ed);
}
ll rand_int(ll l, ll r) { //[l, r]
//#ifdef LOCAL
static mt19937_64 gen;
/*#else
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
#endif*/
return uniform_int_distribution<ll>(l, r)(gen);
}
ll rand_int(ll k){ //[0,k)
return rand_int(0,k-1);
}
template<class t>
void myshuffle(vc<t>&a){
rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}
template<class t>
int lwb(const vc<t>&v,const t&a){
return lower_bound(all(v),a)-v.bg;
}
template<class t>
bool bis(const vc<t>&v,const t&a){
return binary_search(all(v),a);
}
vvc<int> readGraph(int n,int m){
vvc<int> g(n);
rep(i,m){
int a,b;
cin>>a>>b;
//sc.read(a,b);
a--;b--;
g[a].pb(b);
g[b].pb(a);
}
return g;
}
vvc<int> readTree(int n){
return readGraph(n,n-1);
}
template<class t>
vc<t> presum(const vc<t>&a){
vc<t> s(si(a)+1);
rep(i,si(a))s[i+1]=s[i]+a[i];
return s;
}
vc<ll> presum(const vi&a){
vc<ll> s(si(a)+1);
rep(i,si(a))s[i+1]=s[i]+a[i];
return s;
}
//BIT で数列を管理するときに使う (CF850C)
template<class t>
vc<t> predif(vc<t> a){
gnr(i,1,si(a))a[i]-=a[i-1];
return a;
}
template<class t>
vvc<ll> imos(const vvc<t>&a){
int n=si(a),m=si(a[0]);
vvc<ll> b(n+1,vc<ll>(m+1));
rep(i,n)rep(j,m)
b[i+1][j+1]=b[i+1][j]+b[i][j+1]-b[i][j]+a[i][j];
return b;
}
//verify してないや
void transvvc(int&n,int&m){
swap(n,m);
}
template<class t,class... Args>
void transvvc(int&n,int&m,vvc<t>&a,Args&...args){
assert(si(a)==n);
vvc<t> b(m,vi(n));
rep(i,n){
assert(si(a[i])==m);
rep(j,m)b[j][i]=a[i][j];
}
a.swap(b);
transvvc(n,m,args...);
}
//CF854E
void rotvvc(int&n,int&m){
swap(n,m);
}
template<class t,class... Args>
void rotvvc(int&n,int&m,vvc<t>&a,Args&...args){
assert(si(a)==n);
vvc<t> b(m,vi(n));
rep(i,n){
assert(si(a[i])==m);
rep(j,m)b[m-1-j][i]=a[i][j];
}
a.swap(b);
rotvvc(n,m,args...);
}
//ソートして i 番目が idx[i]
//CF850C
template<class t>
vi sortidx(const vc<t>&a){
int n=si(a);
vi idx(n);iota(all(idx),0);
sort(all(idx),[&](int i,int j){return a[i]<a[j];});
return idx;
}
//vs[i]=a[idx[i]]
//例えば sortidx で得た idx を使えば単にソート列になって返ってくる
//CF850C
template<class t>
vc<t> a_idx(const vc<t>&a,const vi&idx){
int n=si(a);
assert(si(idx)==n);
vc<t> vs(n);
rep(i,n)vs[i]=a[idx[i]];
return vs;
}
//CF850C
vi invperm(const vi&p){
int n=si(p);
vi q(n);
rep(i,n)q[p[i]]=i;
return q;
}
template<class t,class s=t>
s SUM(const vc<t>&a){
return accumulate(all(a),s(0));
}
template<class t>
t MAX(const vc<t>&a){
return *max_element(all(a));
}
template<class t>
pair<t,int> MAXi(const vc<t>&a){
auto itr=max_element(all(a));
return mp(*itr,itr-a.bg);
}
template<class t>
t MIN(const vc<t>&a){
return *min_element(all(a));
}
template<class t>
pair<t,int> MINi(const vc<t>&a){
auto itr=min_element(all(a));
return mp(*itr,itr-a.bg);
}
template<class t,class u>
pair<t,u> operator+(const pair<t,u>&a,const pair<t,u>&b){
return mp(a.a+b.a,a.b+b.b);
}
vi vid(int n){
vi res(n);iota(all(res),0);
return res;
}
template<class S>
S revv(S s){
reverse(all(s));
return s;
}
pi operator+(pi a,pi b){return pi(a.a+b.a,a.b+b.b);}
template<class t>
t gpp(vc<t>&vs){
assert(si(vs));
t res=move(vs.back());
vs.pop_back();
return res;
}
template<class t>
void pb(vc<t>&a,const vc<t>&b){
a.insert(a.ed,all(b));
}
template<class t>
vc<t> cat(vc<t> a,const vc<t>&b){
pb(a,b);
return a;
}
template<class t,class u>
vc<t>& operator+=(vc<t>&a,u x){
for(auto&v:a)v+=x;
return a;
}
template<class t,class u>
vc<t> operator+(vc<t> a,u x){
return a+=x;
}
template<class t,class u>
vc<t>& operator-=(vc<t>&a,u x){
for(auto&v:a)v-=x;
return a;
}
template<class t,class u>
vc<t>& operator-(vc<t> a,u x){
return a-=x;
}
bool dbg=false;
ll fld(ll a, ll b) { // floored division
return a / b - ((a ^ b) < 0 && a % b); }
ll cld(ll a, ll b) { // ceiled division
return a / b + ((a ^ b) > 0 && a % b); }
template<class t>
struct BIT{
vc<t> buf;
int s;
BIT(int n=0){init(n);}
BIT(const vc<t>&a){init(a);}
void init(int n){buf.clear();buf.resize(s=n);}
void init(const vc<t>&a){
s=si(a);
buf.resize(s);
rep(i,s)buf[i]=a[i];
rep(i,s){
int j=i+((i+1)&(-i-1));
if(j<s)buf[j]+=buf[i];
}
}
void add(int i,t v){
for(;i<s;i+=(i+1)&(-i-1))
buf[i]+=v;
}
t get(int i){
t res=t();
for(;i>=0;i-=(i+1)&(-i-1))
res+=buf[i];
return res;
}
t sum(int b,int e){
return get(e-1)-get(b-1);
}
void add_range(int b,int e,t v){
add(b,v);
add(e,-v);
}
int kth(int k){
int res=0;
for(int i=topbit(s);i>=0;i--){
int w=res+(1<<i);
if(w<=s&&buf[w-1]<=k){
k-=buf[w-1];
res=w;
}
}
return res;
}
//yukicoder No.1024
int kth_helper(int k,int i){
return kth(k+get(i-1));
}
};
/*
struct N{
int h,n;
N(int hh,int nn):h(hh),n(nn){}
vc<tuple<int,int,int>> buf;
void addtri(int x,int y,int v){
if(y<h)buf.eb(x,y,v);
}
void addpipe(int x,int y,int w){
addtri(x,0,1);
addtri(x,y+1,-1);
addtri(x+w+1,w+1,-1);
addtri(x+w+1,y+1+w+1,1);
}
vi ans;
void addquery(int k,int l,int r){
chmin(k,h-1);
assert(0<=l&&l<=r&&r<=n);
int res=0;
for(auto [x,y,v]:buf){
if(y<=k){
int a=x,b=x+(k-y)+1;
chmax(a,l);
chmin(b,r);
if(a<b)res+=(b-a)*v;
}
}
ans.pb(res);
}
vi calc(){
return ans;
}
};*/
struct M{
BIT<int> a,b;
M(int n):a(n),b(n){}
void add(int i,int v){
a.add(i,v);
b.add(i,-(i-1)*v);
}
int get(int i){
return a.get(i)*i+b.get(i);
}
};
struct N{
int h,n;
N(int hh,int nn):h(hh),n(nn){}
vc<tuple<int,int,int>> buf;
void addtri(int x,int y,int v){
if(y<h&&x<n){
buf.eb(x-y,y,v);
}
}
void addpipe(int x,int y,int w){
addtri(x,0,1);
addtri(x,y+1,-1);
addtri(x+w+1,w+1,-1);
addtri(x+w+1,y+1+w+1,1);
}
vi ans;
void addquery(int k,int l,int r){
chmin(k,h-1);
assert(0<=l&&l<=r&&r<=n);
int i=si(ans);
l--;r--;
buf.eb(l-k,k,inf+i*2);
buf.eb(r-k,k,inf+i*2+1);
ans.pb(0);
}
vi calc(){
sort(all(buf));
{
M z(h);
for(auto [tmp,y,v]:buf){
if(v<inf){
z.add(y,v);
}else{
int i=(v-inf)/2,k=(v-inf)%2;
int val=z.get(y);
if(k==1)ans[i]+=val;
else ans[i]-=val;
}
}
}
reverse(all(buf));
{
M z(n);
for(auto [tmp,y,v]:buf){
int x=tmp+y;
if(v<inf){
z.add(x,v);
}else{
int i=(v-inf)/2,k=(v-inf)%2;
int val=z.get(x);
if(k==1)ans[i]+=val;
else ans[i]-=val;
}
}
}
return ans;
}
};
void slv(){
int small;//並行四辺形は少ない要素の数を返している
//0 が少ないなら答えはそのまま出力
//1 が少ないなら(r-l)から引いて出力
int off;//off番目を0にしてあるので l-=off,r-=off しなさい
vi pos;//小さいやつの位置
int n;
{
string s;sc.read(s);
n=si(s);
vi a(n);
rep(i,n)a[i]=s[i]=='('?0:1;
if(SUM(a)*2<=n){
small=1;
}else{
small=0;
rep(i,n)a[i]=1-a[i];
reverse(all(a));
}
int cur=0,mn=inf;
off=-1;
rep(i,n){
if(a[i]==small&&chmin(mn,cur))off=i;
if(a[i]==small)cur++;
else cur--;
}
if(off==-1){
//1種
}else{
rep(i,n){
int j=(i+off)%n;
if(a[j])pos.pb(i);
}
}
}
N z[2]{N(n+1,(n+1)/2),N(n+1,n/2)};
auto addpipe=[&](int x,int y,int w){
z[x%2].addpipe(x/2,y,w);
};
{
struct Vert{
int x,y,w;
};
int lazy=0;
vc<Vert> cur;
for(auto i:pos){
int bottom=n;
while(si(cur)){
auto [x,y,w]=cur.back();
w+=lazy;
if(x+w*2<i-1){
addpipe(x,y,w);
cur.pop_back();
}else{
assert(x+w*2==i-1);
bottom=w;
break;
}
}
lazy++;
cur.pb({i,bottom,-lazy});
}
for(auto [x,y,w]:cur){
w+=lazy;
addpipe(x,y,w);
}
}
vi dst[2];
auto addq=[&](int i,int y,int l,int r){
if(l<r){
rep(k,2){
dst[k].pb(i);
z[k].addquery(y,(l+1-k)/2,(r+1-k)/2);
}
}
};
int q;sc.read(q);
vi tot(q);
vi ans(q);
rep(i,q){
int y,l,r;sc.read(y,l,r);
r++;
if(small==0){
l=-l;
r=-r;
swap(l,r);
}
tot[i]=r-l;
if(off!=-1){
l+=y;
r+=y;
l-=off;
r-=off;
int u=cld(l,n);
int v=fld(r,n);
if(u<=v){
addq(i,y,l-(u-1)*n,n);
addq(i,y,0,r-v*n);
ans[i]+=si(pos)*(v-u);
}else{
addq(i,y,l-(u-1)*n,r-v*n);
}
}
}
rep(k,2){
vi val=z[k].calc();
assert(si(val)==si(dst[k]));
rep(i,si(val))
ans[dst[k][i]]+=val[i];
}
rep(i,q){
if(small==1){
ans[i]=tot[i]-ans[i];
}
pr.writeln(ans[i]);
}
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout<<fixed<<setprecision(20);
if(dbg){
while(1)slv();
}else{
int t;sc.read(t);rep(_,t)
slv();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3708kb
input:
3 (()) 3 0 -3 2 1 -2 3 2 0 0 ))()( 3 0 -3 4 2 1 3 3 -4 -1 ))()(()( 4 1234 -5678 9012 123 -456 789 12 -34 56 1 -2 3
output:
3 3 0 4 1 1 7345 623 45 3
result:
ok 10 lines
Test #2:
score: -100
Wrong Answer
time: 233ms
memory: 3940kb
input:
5564 ()()(((() 16 0 -825489608 537105171 0 481386502 824237183 0 -32590250 515314371 0 -634830457 908018223 3 -494274112 299679416 125527658 81646800 208166552 632660143 -774899605 -551752339 4 -874787302 127206822 4 -102348267 122390255 2 -881139944 898929361 0 -656262874 -233671414 111787130 -5925...
output:
908396520 228567121 365269747 1028565788 529302353 84346502 148764845 667996084 149825682 1186712870 281727640 995600518 63752581 740373707 867951696 27044667 530591272 345487789 415550920 701803793 413364407 187916462 386485772 125057026 296666743 470522533 367131179 635722815 58970215 379425066 18...
result:
wrong answer 156th lines differ - expected: '162729201', found: '162729200'