QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#440318#6509. Not Another Range Query ProblemLRWA 13ms10524kbC++202.8kb2024-06-13 16:12:372024-06-13 16:12:37

Judging History

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

  • [2024-06-13 16:12:37]
  • 评测
  • 测评结果:WA
  • 用时:13ms
  • 内存:10524kb
  • [2024-06-13 16:12:37]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long

using namespace std;
const int maxn = 5e5 + 5;
int n, m;
int f[maxn], ans[maxn];
set<int> t[2];
string s;
vector<array<int, 3>> qr[maxn];

void upd(int i, int val)
{
    for (; i <= n; i += i & -i)
        f[i] += val;
}

int get(int l, int r)
{
    int ans = 0;
    for (; r > 0 ; r -= r & -r)
        ans += f[r];
    for (l--; l > 0; l -= l & -l)
        ans -= f[l];
    return ans;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
//    freopen("test.inp", "r", stdin);
//    freopen("test.out", "w", stdout);
    cin >> n >> m >> s;
    for (int i = 1; i <= m; i++)
    {
        int l, r, k;
        cin >> l >> r >> k;
        if (k == 0) ans[i] = r - l + 1;
        if (k <= n)
            qr[k].push_back({l, r, i});
    }
    for (int i = 1; i <= n; i++)
        t[s[i - 1] - '0'].emplace(i),
        upd(i, 1);
    for (int k = 1; k <= n; k++)
    {
        int start = n + 1;
        for (int j = 0; j < 2; j++) if (t[j].size())
            start = min(start, *t[j].begin());
        if (start != n + 1)
        {
            int st = s[start - 1] == '1', pos = start;
            while (true)
            {
                upd(pos, -1);
                t[st].erase(t[st].find(pos));
                st ^= 1;
                auto it = t[st].lower_bound(pos);
                if (it == t[st].end())
                    break;
                pos = *it;
            }
        }
        for (auto i : qr[k])
            ans[i[2]] += get(1, i[1]);
    }
//    for (int i = 1; i <= m; i++)
//        cout << i << " : " << ans[i] << "\n";
    reverse(s.begin(), s.end());
    t[0].clear(); t[1].clear();
    for (int i = 1; i <= n; i++) f[i] = 0;
    for (int i = 1; i <= n; i++)
        t[s[i - 1] - '0'].emplace(i),
        upd(i, 1);
    for (int k = 1; k <= n; k++)
    {
        int start = n + 1;
        for (int j = 0; j < 2; j++) if (t[j].size())
            start = min(start, *t[j].begin());
        if (start != n + 1)
        {
            int st = s[start - 1] == '1', pos = start;
            while (true)
            {
                upd(n - pos + 1, -1);
                t[st].erase(t[st].find(pos));
                st ^= 1;
                auto it = t[st].lower_bound(pos);
                if (it == t[st].end())
                    break;
                pos = *it;
            }
        }
        for (auto i : qr[k])
            ans[i[2]] -= get(1, i[0] - 1);
//        if (k == 1)
//        {
//            for (int j = n; j >= 1; j--)
//                cout << n - j + 1 << " " << 4 - get(1, j) << " ???\n";
//        }
    }
    for (int i = 1; i <= m; i++)
        cout << ans[i] << "\n";
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 7764kb

input:

9 7
100110001
2 5 1
3 6 1
4 8 2
2 7 1
1 9 1
1 9 0
1 9 8

output:

2
1
1
3
4
9
0

result:

ok 7 numbers

Test #2:

score: -100
Wrong Answer
time: 13ms
memory: 10524kb

input:

100 100000
0000011010101000111011110110000111110101101010111111101011011010111001111010111000001000011000001010
76 99 3
25 84 7
45 83 11
10 12 10
69 86 4
27 28 1
22 42 42
4 86 25
26 91 22
20 81 17
50 78 0
77 93 50
31 50 34
7 46 13
78 89 0
79 98 0
2 84 33
58 93 11
56 75 2
55 77 68
7 9 41
44 46 11
47 ...

output:

8
13
4
0
3
0
0
0
0
4
29
0
0
0
12
20
0
0
5
0
0
-8
0
-6
-2
0
0
10
18
1
0
57
0
-2
11
0
3
0
0
3
0
0
0
0
0
-1
0
0
-2
0
-7
0
-5
0
19
0
0
-2
12
5
0
0
2
-6
0
0
-2
10
12
0
0
0
5
0
8
0
1
16
-1
19
29
40
21
12
26
0
21
6
0
10
18
0
3
-1
2
5
0
0
5
0
0
-4
51
0
0
-1
18
11
0
20
5
9
10
0
16
22
0
20
0
26
0
0
0
0
0
0
11...

result:

wrong answer 22nd numbers differ - expected: '0', found: '-8'