QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#421810 | #7106. Infinite Parenthesis Sequence | pandapythoner | AC ✓ | 617ms | 10244kb | C++14 | 3.8kb | 2024-05-26 06:11:21 | 2024-05-26 06:11:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
const ll inf = 1e18;
mt19937 rnd(234);
struct SGT {
int n;
vector<ll> t;
void build(const vector<ll>& a) {
n = 1;
while (n < (int)a.size()) {
n *= 2;
}
t.assign(n * 2, 0);
for (int i = 0; i < (int)a.size(); i += 1) {
t[i + n] = a[i];
}
for (int i = n - 1; i >= 1; i -= 1) {
t[i] = max(t[i + i], t[i + i + 1]);
}
}
ll get(ll l, ll r) {
l += n;
r += n + 1;
ll rs = -inf;
while (l < r) {
if (l & 1) {
rs = max(rs, t[l]);
l += 1;
}
if (r & 1) {
r -= 1;
rs = max(rs, t[r]);
}
l /= 2;
r /= 2;
}
return rs;
}
};
int n;
string s;
int q;
vector<ll> p;
vector<ll> p0, p1;
SGT sgt0, sgt1;
ll dlm(ll a, ll b) {
if (a < 0) {
return -((abs(a) + b - 1) / b);
}
return a / n;
}
ll mod(ll a, ll b) {
ll x = a % b;
if (x < 0) {
x += b;
}
return x;
}
ll get_val(ll i) {
ll x = ((i % n) + n) % n;
return p[x] + ((i - x) / n) * (p[n] - p[0]);
}
ll get_val_at_time(ll i, ll t) {
ll rs = -inf;
ll l = i - t;
ll r = i + t;
for (int i = 0; i < 3 && l <= r; i += 1) {
ll f = r;
if (dlm(l, n) != dlm(r, n)) {
f = dlm(l, n) * n + n - 1;
if (mod(f, 2) != mod(l, 2)) {
f -= 1;
}
}
ll delta = dlm(l, n) * (p[n] - p[0]);
if (mod(mod(l, n), 2) == 0) {
rs = max(rs, sgt0.get(mod(l, n), mod(f, n)) + delta);
} else {
rs = max(rs, sgt1.get(mod(l, n), mod(f, n)) + delta);
}
l = f + 2;
}
for (int i = 0; i < 3 && l <= r; i += 1) {
ll f = l;
if (dlm(l, n) != dlm(r, n)) {
f = dlm(r, n) * n + 0;
if (mod(f, 2) != mod(r, 2)) {
f += 1;
}
}
ll delta = dlm(r, n) * (p[n] - p[0]);
if (mod(mod(r, n), 2) == 0) {
rs = max(rs, sgt0.get(mod(f, n), mod(r, n)) + delta);
} else {
rs = max(rs, sgt1.get(mod(f, n), mod(r, n)) + delta);
}
r = f - 2;
}
return rs - t;
/*
ll rs = -inf;
for (ll j = i - t; j <= i + t; j += 2) {
rs = max(rs, get_val(j) - t);
}
return rs;
*/
}
void build() {
vector<ll> p0(n, -inf), p1(n, -inf);
for (int i = 0; i < n; i += 1) {
if (i % 2 == 0) {
p0[i] = p[i];
} else {
p1[i] = p[i];
}
}
sgt0.build(p0);
sgt1.build(p1);
}
int32_t main() {
if (1) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int t;
cin >> t;
for (int itr = 0; itr < t; itr += 1) {
cin >> s;
n = (int)s.size();
p.resize(n + 1);
p[0] = 0;
for (int i = 0; i < n; i += 1) {
p[i + 1] = p[i] + (s[i] == '(' ? 1 : -1);
}
build();
cin >> q;
for (int i = 0; i < q; i += 1) {
ll t, l, r;
cin >> t >> l >> r;
++r;
ll balance = get_val_at_time(r, t) - get_val_at_time(l, t);
ll rs = (balance + r - l) / 2;
cout << rs << "\n";
}
}
return 0;
}
/*
1
))()(
1
0 -3 4
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
*/
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3836kb
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: 0
Accepted
time: 113ms
memory: 3768kb
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:
ok 271661 lines
Test #3:
score: 0
Accepted
time: 504ms
memory: 10084kb
input:
7 ((()(((()(((()((()((()((()(((()(((()((()(((()(((()((()(((()(((()(((()(((()((()(((()((()((()(((()(((()(((()((()((()(((()((()((()(((()(((()(((()((()(((()((()(((()((()((()(((()(((()(((()(((()((()(((()(((()((()((()((()(((()(((()((()(((()(((()(((()((()(((()(((()((()((()(((()(((()(((()((()((()(((()((()(...
output:
185704843 446800089 255099554 1156402 212636323 416191407 12472890 247228052 489620931 107469522 16222287 341270888 29184920 107393597 163613521 175919552 118376824 76183214 805506070 206363476 326077675 54361969 121810843 684646392 716061472 697723268 23956954 588434738 4870237 305505833 489380166 ...
result:
ok 700000 lines
Test #4:
score: 0
Accepted
time: 617ms
memory: 10244kb
input:
5571 ()()(((() 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:
ok 971661 lines
Extra Test:
score: 0
Extra Test Passed