QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#624712 | #7618. Pattern Search | ucup-team4992# | WA | 0ms | 14072kb | C++20 | 6.6kb | 2024-10-09 16:28:08 | 2024-10-09 16:28:09 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 500000+5;
int n, q;
int tag1[MAXN<<2];
ll ans;
ll a[MAXN], mx[MAXN<<2], mn[MAXN<<2], len[MAXN<<2], tag2[MAXN<<2], sum[MAXN<<2];
void pushUpMx(int x){
mx[x] = max(mx[x<<1], mx[x<<1|1]);
}
void pushUpMn(int x){
mn[x] = min(mn[x<<1], mn[x<<1|1]);
}
void pushUp(int x){
mx[x] = max(mx[x<<1], mx[x<<1|1]);
mn[x] = min(mn[x<<1], mn[x<<1|1]);
sum[x] = sum[x<<1] + sum[x<<1|1];
}
void build(int x, int l, int r){
len[x] = r-l+1;
tag1[x] = tag2[x] = 0;
if(l == r){
sum[x] = mx[x] = mn[x] = a[l];
return;
}
int mid = (l+r)/2;
build(x<<1, l, mid);
build(x<<1|1, mid+1, r);
pushUp(x);
}
int findMnR(int x, int l, int r, int L, int R, ll v){
if(mx[x] < v){
return 0;
}
if(l == r){
return mn[x] < v ? 0 : l;
}
int mid = (l+r)/2;
if(L <= l && r <= R){
int tl, tr;
tl = findMnR(x<<1, l, mid, L, R, v);
if(tl < 1){
return 0;
}
tr = findMnR(x<<1|1, mid+1, r, L, R, v);
return max(tl, tr);
}
int tl = 0, tr = 0;
if(L <= mid){
tl = findMnR(x<<1, l, mid, L, R, v);
if(tl < 1){
return 0;
}
}
if(R > mid){
tr = findMnR(x<<1|1, mid+1, r, L, R, v);
}
return max(tl, tr);
}
int findMnL(int x, int l, int r, int L, int R, ll v){
if(mx[x] < v){
return n+1;
}
if(l == r){
return mn[x] < v ? n+1 : l;
}
int mid = (l+r)/2;
if(L <= l && r <= R){
int tl, tr;
tr = findMnL(x<<1|1, mid+1, r, L, R, v);
if(tr > n){
return n+1;
}
tl = findMnL(x<<1, l, mid, L, R, v);
return min(tl, tr);
}
int tl = n+1, tr = n+1;
if(R > mid){
tr = findMnL(x<<1|1, mid+1, r, L, R, v);
if(tr > n){
return n+1;
}
}
if(L <= mid){
tl = findMnL(x<<1, l, mid, L, R, v);
}
return min(tl, tr);
}
int findMxR(int x, int l, int r, int L, int R, ll v){
if(mn[x] > v){
return 0;
}
if(l == r){
return mx[x] > v ? 0 : l;
}
int mid = (l+r)/2;
if(L <= l && r <= R){
int tl, tr;
tl = findMxR(x<<1, l, mid, L, R, v);
if(tl < 1){
return 0;
}
tr = findMxR(x<<1|1, mid+1, r, L, R, v);
return max(tl, tr);
}
int tl = 0, tr = 0;
if(L <= mid){
tl = findMxR(x<<1, l, mid, L, R, v);
if(tl < 1){
return 0;
}
}
if(R > mid){
tr = findMxR(x<<1|1, mid+1, r, L, R, v);
}
return max(tl, tr);
}
int findMxL(int x, int l, int r, int L, int R, ll v){
if(mn[x] > v){
return n+1;
}
if(l == r){
return mx[x] > v ? n+1 : l;
}
int mid = (l+r)/2;
if(L <= l && r <= R){
int tl, tr;
tr = findMxL(x<<1|1, mid+1, r, L, R, v);
if(tr > n){
return n+1;
}
tl = findMxL(x<<1, l, mid, L, R, v);
return min(tl, tr);
}
int tl = n+1, tr = n+1;
if(R > mid){
tr = findMxL(x<<1|1, mid+1, r, L, R, v);
if(tr > n){
return n+1;
}
}
if(L <= mid){
tl = findMxL(x<<1, l, mid, L, R, v);
}
return min(tl, tr);
}
void pushDown(int x){
if(tag1[x]){
int ls = x<<1, rs = x<<1|1;
mn[ls] = mx[ls] = tag2[x];
mn[rs] = mx[rs] = tag2[x];
sum[ls] = tag2[x]*len[ls];
sum[rs] = tag2[x]*len[rs];
tag1[ls] = 1;
tag2[ls] = tag2[x];
tag1[rs] = 1;
tag2[rs] = tag2[x];
tag1[x] = 0;
}
}
void update(int x, int l, int r, int L, int R, ll v){
if(L <= l && r <= R){
mn[x] = mx[x] = v;
sum[x] = len[x]*v;
tag1[x] = 1;
tag2[x] = v;
return;
}
pushDown(x);
int mid = (l+r)/2;
if(L <= mid){
update(x<<1, l, mid, L, R, v);
}
if(R > mid){
update(x<<1|1, mid+1, r, L, R, v);
}
pushUp(x);
}
ll querySum(int x, int l, int r, int L, int R){
if(L <= l && r <= R){
return sum[x];
}
pushDown(x);
int mid = (l+r)/2;
ll res = 0;
if(L <= mid) res += querySum(x<<1, l, mid, L, R);
if(R > mid) res += querySum(x<<1|1, mid+1, r, L, R);
return res;
}
int main(){
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
build(1, 1, n);
// calc max
for(int i = n; i >= 1; i--){
// max r a[i...r] <= v
int r = findMxR(1, 1, n, i, n, a[i]);
// cout << "r = " << r << endl;
update(1, 1, n, i, r, a[i]);
ans += querySum(1, 1, n, i, n);
// for(int j = 1; j <= n; j++){
// cout << querySum(1, 1, n, j, j) << " \n"[j==n];
// }
// cout << "ans = " << ans << "\n";
cout.flush();
}
build(1, 1, n);
for(int i = n; i >= 1; i--){
int r = findMnR(1, 1, n, i, n, a[i]);
// cout << "r = " << r << endl;
update(1, 1, n, i, r, a[i]);
ans -= querySum(1, 1, n, i, n);
// for(int j = 1; j <= n; j++){
// cout << querySum(1, 1, n, j, j) << " \n"[j==n];
// }
// cout << "ans = " << ans << "\n";
cout.flush();
}
build(1, 1, n);
while(q--){
string op;
int idx;
cin >> op >> idx;
if(op == "+"){
int L, R;
// cout << "a = " << a[idx] << endl;
L = findMxL(1, 1, n, 1, idx, a[idx]);
R = findMxR(1, 1, n, idx, n, a[idx]);
// cout << "L R " << L << " " << R << endl;
ans += (ll)(idx-L+1)*(R-idx+1);
L = findMnL(1, 1, n, 1, idx-1, a[idx]+1);
R = findMnR(1, 1, n, idx+1, n, a[idx]+1);
L = min(idx, L);
R = max(idx, R);
ans -= (ll)(idx-L+1)*(R-idx+1);
a[idx]++;
}
else{
int L, R;
L = findMnL(1, 1, n, 1, idx, a[idx]);
R = findMnR(1, 1, n, idx, n, a[idx]);
ans += (ll)(idx-L+1)*(R-idx+1);
L = findMxL(1, 1, n, 1, idx-1, a[idx]-1);
R = findMxR(1, 1, n, idx+1, n, a[idx]-1);
L = min(idx, L);
R = max(idx, R);
ans -= (ll)(idx-L+1)*(R-idx+1);
a[idx]--;
}
// cout << "ans\n";
cout << ans << "\n";
update(1, 1, n, idx, idx, a[idx]);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 14072kb
input:
2 bajkaaall aal abca cba
output:
result:
wrong answer Answer contains longer sequence [length = 2], but output contains 0 elements