Submission #933757

#TimeUsernameProblemLanguageResultExecution timeMemory
933757VinhLuuStreet Lamps (APIO19_street_lamps)C++17
100 / 100
1269 ms143584 KiB
// REPUTATION RATING = #include <bits/stdc++.h> #define int long long #define ll long long #define fi first #define se second #define pb push_back #define all(lmao) lmao.begin(), lmao.end() using namespace std; typedef pair<int,int> pii; typedef tuple<int,int,int> tp; const int N = 3e5 + 5; //const int oo = 1e9; const int mod = 1e9 + 7; int n, q, a[N], L[N], R[N], X[N]; string s, T[N]; namespace sub1{ int m[105][105]; void solve(){ for(int i = 1; i <= n; i ++) m[1][i] = a[i]; for(int t = 1; t <= q; t ++){ string type; cin >> type; if(type[0] == 't'){ int x; cin >> x; a[x] = 1 - a[x]; }else{ int l, r, ans = 0; cin >> l >> r; for(int j = 1; j <= t; j ++){ bool ff = true; for(int i = l; i < r; i ++) if(m[j][i] == 0){ ff = false; break; } ans += ff; } cout << ans << "\n"; } for(int i = 1;i <= n; i ++) m[t + 1][i] = a[i]; } } } namespace sub5{ int op[N]; int time[N], kq[N], tup[N], t; struct Q{ int l,r,w; } query[N]; vector<int> node[N], BIT[N]; void fakeupdate(int x,int y){ for(; x <= n; x += x & -x) node[x].pb(y); } void fakeGet(int x,int y){ for(; x > 0; x -= x & -x) node[x].pb(y); } void FGet(int x,int y,int i,int j){ fakeGet(x - 1, y - 1); fakeGet(x - 1, j); fakeGet(i, y - 1); fakeGet(i, j); } void update(int x, int yy, int val) { for (; x <= n; x += x & -x) for (int y = lower_bound(node[x].begin(), node[x].end(), yy) - node[x].begin() + 1; y <= node[x].size(); y += y & -y) //tìm vị trí sau khi rời rạc BIT[x][y - 1] += val; } int get(int x, int yy) { int res = 0; for (; x > 0; x -= x & -x) for (int y = lower_bound(node[x].begin(), node[x].end(), yy) - node[x].begin() + 1; y > 0; y -= y & -y) res += BIT[x][y - 1]; return res; } int GET(int x,int y,int i,int j){ return get(i, j) - get(x - 1, j) - get(i, y - 1) + get(x - 1, y - 1); } void solve(){ set<int> s; s.insert(0); s.insert(n + 1); time[0] = 1; time[n + 1] = 1; for(int i = 1; i <= n; i ++){ op[i] = a[i]; if(!a[i]){ s.insert(i); time[i] = 1; } } for(int i = 1;i <= q; i ++){ string type = T[i]; if(type[0] == 't'){ int x = X[i]; if(op[x]){ auto j = s.lower_bound(x); int ri = (*j); j--; int le = (*j); if(le + 1 <= ri - 1){ tup[i]++; query[++t] = {le + 1, ri - 1, i - time[le] + 1}; fakeupdate(le + 1, ri - 1); // cout << le + 1 <<" " << ri - 1 << " " << i - time[le] + 1 << "f\n"; } time[le] = i + 1; time[x] = i + 1; s.insert(x); }else{ auto j = s.lower_bound(x); j--; int le = (*j); j = s.upper_bound(x); int ri = (*j); // cout << x << " " << le << " " << ri << "f\n"; if(le + 1 <= x - 1){ tup[i]++; query[++t] = {le + 1, x - 1, i - time[le] + 1}; fakeupdate(le + 1, x - 1); } if(x + 1 <= ri - 1){ tup[i]++; query[++t] = {x + 1, ri - 1, i - time[x] + 1}; fakeupdate(x + 1, ri - 1); // cout << x + 1 << " " << ri - 1 << "d\n"; } time[le] = i + 1; s.erase(x); } op[x] = 1 - op[x]; // for(auto j : s) cout << j << " "; // cout << "\n"; }else{ int l = L[i], r = R[i]; auto j = s.lower_bound(l); // cerr << i << " " << l << " " << r << " " << (*j) << "r\n"; if((*j) > r){ if((*j) > l) j--; // cerr << (*j) << " " << time[(*j)] << "g\n"; kq[i] = i - time[(*j)] + 1; } FGet(1, r, l, n); } // for(auto j : s) cout << j << " "; // cout << time[0] << "\n"; } for(int i = 1; i <= n; i ++){ sort(all(node[i])); node[i].resize(unique(all(node[i])) - node[i].begin()); for(auto j : node[i]) BIT[i].pb(0); } t = 1; for(int i = 1;i <= q; i ++){ string type = T[i]; if(type[0] == 't'){ int x = X[i]; while(tup[i]--){ int l = query[t].l, r = query[t].r, w = query[t].w; update(l, r, w); // cout << l << " " << r << " " << w << "d\n"; t++; } }else{ int l = L[i], r = R[i]; // cout << 1 << " " << r << " " << l << " " << n << " " << "g\n"; cout << GET(1, r, l, n) + kq[i] << "\n"; } // cerr << i << "f\n"; } // cerr << "d\n"; exit(0); } } #define LOCAL #ifdef LOCAL signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define task "v" if(fopen(task ".inp","r")){ freopen(task ".inp","r",stdin); freopen(task ".out","w",stdout); } cin >> n >> q >> s; for(int i = 1; i <= n; i ++) a[i] = s[i - 1] - '0'; // if(n <= 100 && q <= 100){ // sub1 :: solve(); // } for(int i = 1; i <= q; i ++) { cin >> T[i]; if(T[i][0] == 't') cin >> X[i]; else{ cin >> L[i] >> R[i]; R[i]--; } } sub5 :: solve(); } #endif // LOCAL

Compilation message (stderr)

street_lamps.cpp: In function 'void sub5::update(long long int, long long int, long long int)':
street_lamps.cpp:73:99: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |             for (int y = lower_bound(node[x].begin(), node[x].end(), yy) - node[x].begin() + 1; y <= node[x].size(); y += y & -y)  //tìm vị trí sau khi rời rạc
      |                                                                                                 ~~^~~~~~~~~~~~~~~~~
street_lamps.cpp: In function 'void sub5::solve()':
street_lamps.cpp:165:22: warning: unused variable 'j' [-Wunused-variable]
  165 |             for(auto j : node[i]) BIT[i].pb(0);
      |                      ^
street_lamps.cpp:171:21: warning: unused variable 'x' [-Wunused-variable]
  171 |                 int x = X[i];
      |                     ^
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:199:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  199 |         freopen(task ".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:200:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  200 |         freopen(task ".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...