Submission #908162

# Submission time Handle Problem Language Result Execution time Memory
908162 2024-01-16T08:53:54 Z Boycl07 Street Lamps (APIO19_street_lamps) C++17
0 / 100
739 ms 14652 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
 
#define rep(i, n) for(int i = 1; i <= n; ++i)
#define forn(i, l, r) for(int i = l; i <= r; ++i)
#define ford(i, r, l) for(int i = r; i >= l; --i)
#define FOR(i, n) for(int i = 0; i < n; ++i)
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define endl "\n"
#define task ""
#define sz(a) int(a.size())
#define C(x, y) make_pair(x, y)
#define all(a) (a).begin(), (a).end()
#define bit(i, mask) (mask >> i & 1)
 
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
 
inline int readInt()       {char c;while(c=getchar(),c!='-'&&(c<'0'||c>'9'));bool sign=(c=='-');if(sign)c=getchar();int n=c-'0';while(c=getchar(),c>='0'&&c<='9')n=10*n+c-'0';return(!sign)?n:-n;}
inline ll readLong()       {char c;while(c=getchar(),c!='-'&&(c<'0'||c>'9'));bool sign=(c=='-');if(sign)c=getchar();ll  n=c-'0';while(c=getchar(),c>='0'&&c<='9')n=10*n+c-'0';return(!sign)?n:-n;}
inline string readString() {char c;while(c=getchar(),c==' '||c=='\n'||c=='\t');string s({c});while(c=getchar(),c!=EOF&&c!=' '&&c!='\n'&&c!='\t')s+=c;return s;}
 
 
const int N =  3e5 + 10;
const int M = 1e3 + 3;
const int N1 = 2e3 + 10;
const int K = 1e2 + 1;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
const ll LINF = 1e17 + 2;
const int block_size = 500;
const int LOG = 29;
const int offset = N;
const int LIM = 1e4 ;
const int AL = 26;
 
int n, q;

struct fenwick
{
    int bit[N];

    void update(int u, int v) {for(; u <= n + 1; u += u & -u) bit[u] += v;}
    int get(int u) {int res = 0; for(; u; u -= u & -u) res += bit[u]; return res;}

    int find_left(int x)
    {
        int v = get(x);
        int pos = 0;
        ford(i, LOG, 0)
            if(pos + (1 << i) <= n + 1 && v - bit[pos + (1 << i)] > 0)
                pos |= 1 << i, v -= bit[pos];
        return pos + 1;
    }
    int find_right(int x)
    {
        int v = get(x);
        int pos = 0;
        ford(i, LOG, 0)
            if(pos + (1 << i) <= n + 1 && v - bit[pos + (1 << i)] >= 0)
                pos |= 1 << i, v -= bit[pos];
        return pos;
    }
} sum, cnt;

char t[N];

int ans[N];

int queries[N][3];

void cdq(int l, int r)
{
    if(l > r) return;
    if(l == r)
    {
        if(queries[l][0])
            if(cnt.get(queries[l][2]) - cnt.get(queries[l][1]) == 0)
                ans[l] += l;
        return;
    }

    int mid = l + r >> 1;

    cdq(l, mid);

    vector<array<int, 3>> ask_right;
    vector<array<int, 3>> segments;
    forn(i, max(1, l) , mid)
        if(queries[i][0] == 0)
        {
            int x = queries[i][1];
            int u = cnt.find_left(x);
            int v = cnt.find_right(x + 1);
            int val = t[x] == '0' ? -1 : 1;
            t[x] ^= 1;
            segments.pb({u, v, val * i});
            segments.pb({u, x, -val * i});
            segments.pb({x + 1, v, -val * i});
            cnt.update(x + 1, val);
        }
    forn(i, mid + 1, r) if(queries[i][0])
        ask_right.pb({queries[i][1], queries[i][2], i});
    sort(all(ask_right)); reverse(all(ask_right));
    sort(all(segments));  reverse(all(segments));
    int j = 0;
    for(auto &[l, r, idx] : ask_right)
    {
        while(j < sz(segments) && segments[j][1] >= r)
            sum.update(segments[j][0], segments[j][2]), ++j;
        ans[idx] += sum.get(l);
    }
    forn(k, 0, j - 1)
        sum.update(segments[k][0], -segments[k][2]);
    
    cdq(mid + 1, r);

    forn(i, max(1, l) , mid)
    if(queries[i][0] == 0)
        {
            int x = queries[i][1];
            int val = t[x] == '0' ? -1 : 1;
            t[x] ^= 1;
            cnt.update(x + 1, val);
        }

}


void solve()
{
    cin >> n >> q;
    rep(i, n) cin >> t[i];
    rep(i, q)
    {
        string s;
        cin >> s;
        if(s[0] == 'q')
        {
            queries[i][0] = 1;
            cin >> queries[i][1] >> queries[i][2];
        }
        else queries[i][0] = 0, cin >> queries[i][1];
    }

    rep(i, n) if(t[i] == '0') cnt.update(i + 1, 1);


    cdq(0, q);
    rep(i, q) if(queries[i][0]) cout << ans[i] << endl;
    
}
 
signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
 
    int TC = 1;
 
 
    while(TC--)
    {
        solve();
        cout << endl;
    }
 
    return 0;
}
//ha

Compilation message

street_lamps.cpp: In function 'void cdq(int, int)':
street_lamps.cpp:90:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   90 |     int mid = l + r >> 1;
      |               ~~^~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 4440 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 739 ms 14652 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4444 KB Output is correct
2 Incorrect 3 ms 4444 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4444 KB Output is correct
2 Incorrect 2 ms 4444 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 4440 KB Output isn't correct
2 Halted 0 ms 0 KB -