QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#87801 | #4438. Magic | poi | AC ✓ | 695ms | 9392kb | C++ | 1.7kb | 2023-03-14 11:19:44 | 2023-03-14 11:19:47 |
Judging History
answer
#include "bits/stdc++.h"
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;
int n , k , q;
int p[MAXN];
vector<pii> G[MAXN];
void ade( int u , int v , int w ) {
// cout << u << ' ' << v << ' ' << w << endl;
G[u].eb( v , w );
}
queue<int> Q;
int vis[MAXN] , dis[MAXN] , tim[MAXN];
int spfa( int s , int t ) {
Q.push( s );
memset( dis , -0x3f , sizeof dis );
dis[s] = 0;
while( !Q.empty() ) {
int u = Q.front(); Q.pop();
vis[u] = 0;
for( auto t : G[u] ) {
int v = t.fi , w = t.se;
if( dis[v] < dis[u] + w ) {
dis[v] = dis[u] + w;
if( !vis[v] )
++ tim[v] , Q.push( v ) , vis[v] = 1;
if( tim[v] > n ) return -1;
}
}
}
return dis[t];
}
void solve() {
cin >> n >> k;
rep( i , 1 , n ) {
scanf("%d",p + i);
int r = min( n , i + k - 1 ) , l = max( 1 , i - k + 1);
ade( l - 1 , r , p[i] );
}
cin >> q;
rep( i , 1 , q ) {
int l , r , b;
scanf("%d%d%d",&l,&r,&b);
ade( r , l - 1 , -b );
}
rep( i , 1 , n ) ade( i - 1 , i , 0 );
int res = spfa( 0 , n );
cout << ( res < 0 ? -1 : res ) << endl;
rep( i , 0 , n ) G[i].clear() , dis[i] = tim[i] = vis[i] = 0;
while( !Q.empty() ) Q.pop();
}
signed main() {
// freopen("in","r",stdin);
// freopen("out","w",stdout);
int T;cin >> T;while( T-- ) solve();
// solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 695ms
memory: 9392kb
input:
15 5 2 1 2 2 2 1 1 2 4 1 5 2 2 2 0 10 3 1 1 5 11 5 2 2 2 0 10 3 1 2 3 0 3 2 3 7 3 1 2 2 0 3 2 3 0 6 2 1 1 0 3 3 0 4393 1769 280 628 847 751 431 684 746 842 975 487 294 339 803 123 492 662 340 590 844 431 635 831 459 470 287 133 668 865 722 829 653 734 417 452 959 343 900 355 214 627 791 811 891 537 ...
output:
-1 -1 12 7 6 1995 2227 -1 1997 10580 6929 2000 1999 2000 3810
result:
ok 15 lines