QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#159598#7106. Infinite Parenthesis Sequenceucup-team1453#AC ✓381ms51612kbC++142.3kb2023-09-02 18:08:002023-09-02 18:08:00

Judging History

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

  • [2023-09-02 18:08:00]
  • 评测
  • 测评结果:AC
  • 用时:381ms
  • 内存:51612kb
  • [2023-09-02 18:08:00]
  • 提交

answer

#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); i++)
#define R(i, j, k) for(int i = (j); i >= (k); i--)
#define sz(a) ((int) a.size())
#define vi vector<int>
#define ll long long
#define me(f, x) memset(f, x, sizeof(f))  
#define ull unsigned long long 
using namespace std;
const int N = 3e5 + 7;
int n, q;
char s[N];
int pre[N];
inline ll pres(ll x) {
	return pre[x % n] + x / n * pre[n - 1];
}
inline int SUM(ll l, ll r) {
	if(l <= 0) {
		ll dv = (-l + n) / n;
		l += dv * n;
		r += dv * n;
	}
	return pres(r) - pres(l - 1);
}
template < int N > struct RMQ {
	int a[N], mx[20][N], lg[N];
	void init (int n) {
		L(i, 2, n) lg[i] = lg[i >> 1] + 1;
		L(i, 1, n) mx[0][i] = i;
		L(i, 1, 19)
			L(j, 1, n - (1 << i) + 1) 
				mx[i][j] = a[mx[i - 1][j]] > 
				a[mx[i - 1][j + (1 << (i - 1))]] ? mx[i - 1][j] : 
				mx[i - 1][j + (1 << (i - 1))];
	}
	int queryp (int l, int r) {
		int p = lg[r - l + 1];
		return a[mx[p][l]] < a[mx[p][r - (1 << p) + 1]] ? mx[p][r - (1 << p) + 1] : mx[p][l];
	}
	int query (int l, int r) { return a[queryp (l, r)]; }
} ;
RMQ < N > A[2]; 
inline int calc(int p, int k) {
	int ret = -1e9, cur = SUM(p - k + 1, p);
	p -= k;
	p = (p % n + n) % n;
	int len = min(k * 2, n - 1);
	ret = A[p & 1].query(p + 1, p + len + 1) + p / 2;
	return max(-ret + cur + pre[p], 0);
}
void Main() {
	cin >> s;
	n = strlen(s);
	L(i, 0, n - 1) {
		if(s[i] == '(') {
			s[i] = '0';
		} else {
			s[i] = '1';
		}
	}
	int cnt = 0, opr = 0;
	L(i, 0, n - 1) {
		if(s[i] == '0') {
			++cnt;
		} else {
			--cnt; 
		}
	}
	if(cnt > 0) {
		opr = 1;
		L(i, 0, n - 1) {
			s[i] ^= 1;
		}
		reverse(s + 1, s + n);
	}
	L(i, n, n * 2 - 1) 
		s[i] = s[i - n];
	L(i, 0, n * 2 - 1) 
		pre[i] = s[i] == '0';
	L(i, 1, n * 2 - 1) 
		pre[i] += pre[i - 1];
	L(o, 0, 1) {
		L(i, 1, n * 2) {
			A[o].a[i] = pre[i - 1] - (i - o) / 2;
		}
		A[o].init(n * 2);
	} 
	cin >> q;
	while(q--) {
		int l, r, k;
		cin >> k >> l >> r;
		if(opr) {
			l = -l;
			r = -r;
			swap(l, r);
		}
		ll ans = SUM(l, r);
		ans += calc(l - 1, k) - calc(r, k);
		if(opr) {
			ans = r - l + 1 - ans;
		}
		cout << ans << '\n';
	}
}
int main() {
	ios :: sync_with_stdio (false);
	cin.tie (0); cout.tie (0);
	int t; cin >> t; while(t--) Main();
	return 0;
} 

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 20084kb

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: 75ms
memory: 36436kb

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: 291ms
memory: 51612kb

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: 381ms
memory: 50732kb

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