QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#440310#6509. Not Another Range Query ProblemLRWA 0ms7644kbC++202.6kb2024-06-13 15:30:262024-06-13 15:31:06

Judging History

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

  • [2024-06-13 15:31:06]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:7644kb
  • [2024-06-13 15:30:26]
  • 提交

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] << " ok\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(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[0] - 1);
    }
    for (int i = 1; i <= m; i++)
        cout << ans[i] << "\n";
}


详细

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 7644kb

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
2
1
3
4
9
0

result:

wrong answer 2nd numbers differ - expected: '1', found: '2'