QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#73534#4394. Keyboard WarriorpoiAC ✓440ms23556kbC++4.0kb2023-01-25 16:17:202023-01-25 16:17:22

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-25 16:17:22]
  • 评测
  • 测评结果:AC
  • 用时:440ms
  • 内存:23556kb
  • [2023-01-25 16:17:20]
  • 提交

answer

#include "iostream"
#include "cstring"
#include "cstdio"
#include "algorithm"
#include "queue"
#include "vector"
#include "queue"
#include "stack"
#include "ctime"
#include "set"
#include "map"
#include "cmath"
using namespace std;
#define fi first
#define se second
#define vi vector<int>
#define pb push_back
#define eb emplace_back
#define pii pair<int,int>
#define mp make_pair
#define rep( i , a , b ) for( int i = (a) , i##end = b ; i <= i##end ; ++ i )
#define per( i , a , b ) for( int i = (a) , i##end = b ; i >= i##end ; -- i )
#define mem( a ) memset( a , 0 , sizeof (a) )
#define all( x ) x.begin() , x.end()
#define int long long
typedef long long ll;
const int MAXN = 2e5 + 10;
const int P = 998244353 , Q = 418254373;
int n , m;
char ch[MAXN];

pii operator + ( pii a , pii b ) {
	return mp( ( a.fi + b.fi ) % P , ( a.se + b.se ) % Q );
}

pii operator - ( pii a , pii b ) {
	return mp( ( a.fi + P - b.fi ) % P , ( a.se + Q - b.se ) % Q );
}

pii operator * ( pii a , pii b ) {
	return mp( a.fi * 1ll * b.fi % P , a.se * 1ll * b.se % Q );
}
int Pow( int x , int a , int P ) {
	int ret = 1;
	while( a ) {
		if( a & 1 ) ret = ret * 1ll * x % P;
		x = x * 1ll * x % P , a >>= 1;
	}
	return ret;
}

pii base = mp( 233 , 131 ) , ib = mp( Pow( 232 , P - 2 , P ) , Pow( 130 , Q - 2 , Q ) );
vector<pii> G;
int clen[MAXN] , os[MAXN];
pii pr[MAXN];
set<pii> op;
pii B[MAXN] , W , sB[MAXN];

pii pw( int c ) {
	return mp( Pow( 233 , c , P ) , Pow( 131 , c , Q ) );
}

pii calc( char a , int t ) {
	return ( ( pw( t + 1 ) - mp( 1 , 1 ) ) * ib - mp( 1 , 1 ) ) * mp( a - 'a' + 1 , a - 'a' + 1 );
}

//pii calc0( char a , int t ) {
//	return ( sB[t] - sB[0] ) * mp( a - 'a' + 1 , a - 'a' + 1 );
//}

void solve() {
	cin >> n >> m;
	scanf("%s",ch + 1);
	char st = ch[1] , en = ch[n];
	int fp = -1 , ep = -1;
	rep( i , 1 , n ) if( en != ch[i] )  ep = i;
	per( i , n , 1 ) if( st != ch[i] ) fp = i;
	W = mp( 0 , 0 );
	if( fp <= en ) {
		rep( t , fp , ep ) W = W + B[t - fp + 1] * mp( ch[t] - 'a' + 1 , ch[t] - 'a' + 1 );
	}
	int len = ep - fp + 1;
	int flg = 0;
	rep( i , 1 , m ) {
		char ws[3];
		int k;
		scanf("%s%lld",ws + 1,&k);
		if( flg ) continue;
		if( fp < 0 ) {
			if( ws[1] == '-' ) {
				while( G.size() && G.back().se <= k ) {
					k -= G.back().se;
					G.pop_back();
				}
				if( k && G.size() ) {
					if( clen[G.size() - 1] ) clen[G.size() - 1] -= k;
					G.back().se -= k;
				}
			} else {
				G.eb( ws[1] , k );
				if( ws[1] == st ) clen[G.size() - 1] = ( G.size() > 1 ? clen[G.size() - 2] + k : k );
				else clen[G.size() - 1] = 0;
				if( clen[G.size() - 1] >= n ) {
					flg = 1;
				}
			}
//			rep( i , 0 , G.size() - 1 ) cout << clen[i] << ' '; puts("");
		} else {
			if( ws[1] == '-' ) {
				while( G.size() && G.back().se <= k ) {
					k -= G.back().se;
					if( os[G.size() - 1] ) op.erase( pr[G.size() - 1] );
					G.pop_back();
				}
				if( k && G.size() ) {
					k = G.back().se - k;
					if( os[G.size() - 1] ) op.erase( pr[G.size() - 1] );
					ws[1] = G.back().fi;
					G.pop_back();
				}
			}
			if( ws[1] >= 'a' && ws[1] <= 'z' ) {
//				cout << op.size() << endl;
				if( ws[1] == en && k >= n - ep && G.size() ) {
					if( clen[G.size() - 1] >= len && op.count( pr[G.size() - 1] - W * pw( clen[G.size() - 1] - len ) ) ) {
						flg = 1;
					}
				}
				
				G.eb( ws[1] , k );
				
				clen[G.size() - 1] = ( G.size() > 1 ? clen[G.size() - 2] + k : k );
				pr[G.size() - 1] = ( G.size() > 1 ? pr[G.size() - 2] + calc( ws[1] , k ) * pw( clen[G.size() - 2] ) : calc( ws[1] , k ) );
				
				if( ws[1] == st && k >= fp - 1 ) {
					os[G.size() - 1] = 1;
					op.insert( pr[G.size() - 1] );
				} else os[G.size() - 1] = 0;
			}
		}
	}
	puts( flg ? "yes" : "no" );
	op.clear();
	G.clear();
}

signed main() {
	B[0] = mp( 1 , 1 );
	rep( i , 1 , 200000 ) B[i] = B[i - 1] * base , sB[i] = sB[i - 1] + B[i];
//	freopen("in","r",stdin);
//	freopen("out","w",stdout);
	int T;cin >> T;while( T-- ) solve();
//	solve();
}

詳細信息

Test #1:

score: 100
Accepted
time: 440ms
memory: 23556kb

input:

9
6 6
iloveu
i 1
l 1
o 1
v 1
e 1
u 0
6 10
imfive
u 10
- 20
i 1
m 1
f 1
i 1
v 5
- 4
e 2
- 2
4 4
abab
a 2
b 2
- 3
b 1
2 10
dz
a 1000000000
b 1000000000
- 1000000000
d 1000000000
- 1000000000
d 1000000000
z 1000000000
c 1000000000
e 1000000000
- 111111111
5 7
abcab
a 1
b 1
a 1
b 1
c 1
a 1
b 1
200000 20...

output:

no
yes
no
yes
yes
yes
yes
no
yes

result:

ok 9 lines