Submission #1103552

#TimeUsernameProblemLanguageResultExecution timeMemory
1103552LTTrungCHLElection (BOI18_election)C++17
100 / 100
791 ms49368 KiB
///***LTT***/// /// ->NHAT QUOC GIA<- /// #include<bits/stdc++.h> //#pragma GCC optimize ("O3") //#pragma GCC optimize ("unroll-loops") //#pragma GCC target("popcnt") //#define int long long #define endl "\n" #define F first #define S second #define pb push_back using namespace std; vector <int> lg2; //void MAKE_LOG_ARR(int n){ // lg2.resize(n + 3); // for (int i = 1;i <= n;++i){ // lg2[i] = __lg(i); // } //} const long long oo = 1e9+7; const int N = 5 * 1e5 + 10; int n, q; string s; int a[N]; int tree[N * 4], lazy[N * 4]; vector <pair <int, int>> query[N]; vector <int> del; int ans[N]; void down(int id, int l, int r){ if (l != r){ tree[id * 2] += lazy[id]; tree[id * 2 + 1] += lazy[id]; lazy[id * 2] += lazy[id]; lazy[id * 2 + 1] += lazy[id]; } lazy[id] = 0; return; } void update(int id, int l, int r, int u, int v, int val){ if (lazy[id] != 0){ down(id, l, r); } if (l > v or r < u) return; if (l >= u and r <= v){ tree[id] += val; lazy[id] += val; return; } int mid = (l + r)/2; update(id * 2, l, mid, u, v, val); update(id * 2 + 1, mid + 1, r, u, v, val); tree[id] = min(tree[id * 2], tree[id * 2 + 1]); return; } int get(int id, int l, int r, int u, int v){ if (u > n) return 0; if (lazy[id] != 0){ down(id, l, r); } if (l > v or r < u) return oo; if (l >= u and r <= v){ return tree[id]; } int mid = (l + r)/2; return min(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v)); } void solve(){ cin >> n; cin >> s; for (int i = 0;i < s.size();++i){ if (s[i] == 'C'){ a[i + 1] = 1; } else a[i + 1] = -1; } for (int i = 1;i <= n;++i){ update(1,1,n,1,i,a[i]); } cin >> q; for (int i = 1;i <= q;++i){ int l, r; cin >> l >> r; query[l].pb({r, i}); } for (int i = n;i >= 1;--i){ if (a[i] == 1){ if (!del.empty()){ int zz = del.back(); update(1,1,n,1,zz,-1); del.pop_back(); } } else { update(1,1,n,1,i,1); del.pb(i); } for (int j = 0;j < query[i].size();++j){ int id = query[i][j].S; int r = query[i][j].F; int res = del.size(); int ll = 0, rr = del.size() - 1; while (ll <= rr){ int mid = (ll + rr)/2; if (del[mid] <= r){ rr = mid - 1; res = mid; } else ll = mid + 1; } int sz = del.size() - 1; // cout << id <<" : "; // for (int z = 1;z <= n;++z){ // cout << get(1,1,n,z,z) <<" "; // } // cout <<"\n"; // cout << id <<" "<<(sz - res + 1) <<" "<<get(1,1,n,i,r) <<" "<< get(1,1,n,r + 1,r + 1) <<"\n"; ans[id] = (sz - res + 1) + abs(min(0,get(1,1,n,i,r) - get(1,1,n,r + 1,r + 1))); } } for (int i = 1;i <= q;++i){ cout << ans[i] <<"\n"; } return; } int main(){ ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL); if (fopen("ltt.inp", "r")){ freopen("ltt.inp", "r", stdin); freopen("ltt.out", "w", stdout); } // int t; // cin >> t; // while(t--){ solve(); // } return 0; }

Compilation message (stderr)

election.cpp: In function 'void solve()':
election.cpp:70:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     for (int i = 0;i < s.size();++i){
      |                    ~~^~~~~~~~~~
election.cpp:95:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |         for (int j = 0;j < query[i].size();++j){
      |                        ~~^~~~~~~~~~~~~~~~~
election.cpp: In function 'int main()':
election.cpp:127:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |         freopen("ltt.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
election.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |         freopen("ltt.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...