제출 #546222

#제출 시각아이디문제언어결과실행 시간메모리
546222Ooops_sorry가로등 (APIO19_street_lamps)C++14
100 / 100
2779 ms187176 KiB
#include<bits/stdc++.h> using namespace std; #define int long long #define pb push_back #define ld double #define ll __int128 mt19937 rnd(51); const int N = 3e5 + 10; vector<int> arr[N]; struct Fenwick { vector<int> t; void build(int n) { t.resize(n); } void inc(int i, int d) { for (; i < t.size(); i = (i | (i + 1))) { t[i] += d; } } int get(int r) { int ans = 0; for (; r >= 0; r = (r & (r + 1)) - 1) { ans += t[r]; } return ans; } } t[N]; void fake_add(int i, int j) { for (; i < N; i = (i | (i + 1))) { arr[i].pb(j); } } void add(int i, int j, int val) { for (; i < N; i = (i | (i + 1))) { int ind = lower_bound(arr[i].begin(), arr[i].end(), j) - arr[i].begin(); t[i].inc(ind, val); } } int get(int i, int j) { int ans = 0; for (; i >= 0; i = (i & (i + 1)) - 1) { int ind = upper_bound(arr[i].begin(), arr[i].end(), j) - arr[i].begin() - 1; ans += t[i].get(ind); } return ans; } signed main() { #ifdef LOCAL freopen("input.txt", "r", stdin); #endif // LOCAL ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; vector<int> s(n); set<int> st{-1, n}; for (int i = 0; i < n; i++) { char c; cin >> c; s[i] = c - '0'; if (s[i] == 0) { st.insert(i); } } vector<vector<int>> u; for (int i = 0; i < q; i++) { string t; cin >> t; if (t == "toggle") { int j; cin >> j; j--; if (st.find(j) == st.end()) { auto l = *prev(st.lower_bound(j)) + 1, r = *st.upper_bound(j) - 1; u.pb({l, r, i + 1, j}); fake_add(l, j); fake_add(l, r + 1); fake_add(j + 1, j); fake_add(j + 1, r + 1); st.insert(j); } else { st.erase(j); auto l = *prev(st.lower_bound(j)) + 1, r = *st.upper_bound(j) - 1; u.pb({l, r, -(i + 1), j}); fake_add(l, j); fake_add(l, r + 1); fake_add(j + 1, j); fake_add(j + 1, r + 1); } } else { int l, r; cin >> l >> r; l--, r -= 2; auto val = *st.lower_bound(l); if (val > r) { u.pb({l, r, 1}); } else { u.pb({l, r, 0}); } } } for (int i = 0; i < N; i++) { sort(arr[i].begin(), arr[i].end()); arr[i].erase(unique(arr[i].begin(), arr[i].end()), arr[i].end()); t[i].build(arr[i].size()); } for (int i = 0; i < u.size(); i++) { if (u[i].size() == 3) { int l = u[i][0], r = u[i][1]; cout << get(l, r) + (i + 1) * u[i][2] << endl; } else { int l = u[i][0], r = u[i][1], val = u[i][2], j = u[i][3]; add(l, j, val); add(l, r + 1, -val); add(j + 1, j, -val); add(j + 1, r + 1, val); } } return 0; }

컴파일 시 표준 에러 (stderr) 메시지

street_lamps.cpp: In member function 'void Fenwick::inc(long long int, long long int)':
street_lamps.cpp:21:18: 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]
   21 |         for (; i < t.size(); i = (i | (i + 1))) {
      |                ~~^~~~~~~~~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:116:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |     for (int i = 0; i < u.size(); i++) {
      |                     ~~^~~~~~~~~~
#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...