#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> usingnamespace std; #define MAXN 200006 int n , m; int A[MAXN]; int b , p , f , h , c; intmain(){ int T;cin >> T; while( T-- ) { cin >> b >> p >> f >> h >> c; int res = 0; if( h > c ) { if( b > 2 * p ) b -= 2 * p , res += p * h; else { printf("%d\n",( b / 2 ) * h); continue; } if( b > 2 * f ) printf("%d\n",res + f * c); elseprintf("%d\n", res + ( b / 2 ) * c); } else { if( b > 2 * f ) b -= 2 * f , res += f * c; else { printf("%d\n",( b / 2 ) * c); continue; } if( b > 2 * p ) printf("%d\n",res + p * h); elseprintf("%d\n", res + ( b / 2 ) * h); } } }
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<iostream> usingnamespace std; #define P 998244353 #define MAXN 300006 #define int long long int n; pair<int,int> A[MAXN]; int J[300050]; boolcmp1(pair<int,int> a,pair<int,int> b){ return a.first == b.first ? a.second < b.second : a.first < b.first; } boolCMP(pair<int,int> a,pair<int,int> b){ return a.second < b.second; } signedmain(){ J[0] = 1; for (int i = 1; i <= 300000; ++i) J[i] = J[i - 1] * 1ll * i % P; scanf("%lld", &n); for (int i = 1; i <= n; ++i) scanf("%lld%lld", &A[i].first, &A[i].second); sort(A + 1, A + 1 + n, cmp1); int cur = 1 , ans = J[n]; int l = 0; for (int i = 1; i <= n; ++i) if (A[i].first != A[i - 1].first) cur *= J[l], l = 1, cur %= P; else l++; cur *= J[l] , cur %= P , ans -= cur; bool flg = 1; for (int i = 1; i <= n; ++i) if (A[i].second < A[i - 1].second) { flg = 0; break; } if (flg) { cur = 1 , l = 0; for (int i = 1; i <= n; ++i) if (A[i].first != A[i - 1].first || A[i].second != A[i - 1].second) cur *= J[l], l = 1 , cur %= P; else l++; cur *= J[l], cur %= P , ans += cur; } sort(A + 1, A + 1 + n, CMP); cur = 1, l = 0; for (int i = 1; i <= n; ++i) { if (A[i].second != A[i - 1].second) cur *= J[l] , cur %= P , l = 1; else ++ l; } cur *= J[l] , cur %= P , ans -= cur; printf("%lld\n", (ans % P + P) % P); return0; }
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<vector> #include<queue> usingnamespace std; #define MAXN 400006 int n , m; char ch[MAXN]; int trie[MAXN][26] , fail[MAXN * 26] , ncnt = 0; int T[MAXN]; int dfn; voidadd( int x , int c ){ while( x <= dfn ) T[x] += c , x += x & -x; } intque( int x ){ int ret = 0; while( x > 0 ) ret += T[x] , x -= x & -x; return ret; } vector< pair<int,int> > ff[MAXN]; intins( char* P ){ int cur = 0 , len = strlen( P ); for( int i = 0 ; i < len ; ++ i ) { int v = P[i] - 'a'; if( !trie[cur][v] ) trie[cur][v] = ++ncnt; cur = trie[cur][v]; } return cur; } queue<int> Q; vector<int> F[MAXN] , G[MAXN]; voidbuild( ){ int cur; for( int i = 0 ; i < 26 ; ++ i ) if( trie[0][i] ) Q.push( trie[0][i] ) , fail[trie[0][i]] = 0; while( !Q.empty( ) ) { cur = Q.front() , Q.pop(); F[fail[cur]].push_back( cur ); for( int i = 0 ; i < 26 ; ++ i ) { if( trie[cur][i] ) fail[trie[cur][i]] = trie[fail[cur]][i] , Q.push( trie[cur][i] ); else trie[cur][i] = trie[fail[cur]][i]; } } } int L[MAXN] , R[MAXN]; voidpredfs( int u ){ L[u] = ++ dfn; for( int i = 0 ; i < F[u].size() ; ++ i ) predfs( F[u][i] ); R[u] = dfn; } int ans[MAXN]; voidwork( int u , int p ){ p = trie[p][ch[u] - 'a']; add( L[p] , 1 ); for( int i = 0 ; i < G[u].size() ; ++ i ) work( G[u][i] , p ); for( int i = 0 ; i < ff[u].size() ; ++ i ) ans[ff[u][i].second] = que( R[ff[u][i].first] ) - que( L[ff[u][i].first] - 1 ); add( L[p] , -1 ); } char P[MAXN]; int ffa[MAXN]; intmain(){ cin >> n; for( int i = 1 , t , opt ; i <= n ; ++ i ) { scanf("%d ",&opt); if( opt == 1 ) { scanf("%c",&ch[i]) , ffa[i] = 0 , G[0].push_back( i ); continue; } scanf("%d %c",&t,&ch[i]); ffa[i] = t , G[t].push_back( i ); getchar(); } cin >> m; for( int i = 1 , t ; i <= m ; ++ i ) { scanf("%d%s",&t,P); int x = ins( P ); ff[t].emplace_back( make_pair( x , i ) ); } build( ); predfs( 0 ); for( int i = 1 ; i <= n ; ++ i ) if( !ffa[i] ) work( i , 0 ); for( int i = 1 ; i <= m ; ++ i ) printf("%d\n",ans[i]); }