Submission #123656

#TimeUsernameProblemLanguageResultExecution timeMemory
123656dualityStreet Lamps (APIO19_street_lamps)C++11
100 / 100
2355 ms201528 KiB
#define DEBUG 0 #include <bits/stdc++.h> using namespace std; #if DEBUG // basic debugging macros int __i__,__j__; #define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl #define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl #define printVar(n) cout<<#n<<": "<<n<<endl #define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl #define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;} #define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;} // advanced debugging class // debug 1,2,'A',"test"; class _Debug { public: template<typename T> _Debug& operator,(T val) { cout << val << endl; return *this; } }; #define debug _Debug(), #else #define printLine(l) #define printLine2(l,c) #define printVar(n) #define printArr(a,l) #define print2dArr(a,r,c) #define print2dArr2(a,r,c,l) #define debug #endif // define #define MAX_VAL 999999999 #define MAX_VAL_2 999999999999999999LL #define EPS 1e-6 #define mp make_pair #define pb push_back // typedef typedef unsigned int UI; typedef long long int LLI; typedef unsigned long long int ULLI; typedef unsigned short int US; typedef pair<int,int> pii; typedef pair<LLI,LLI> plli; typedef vector<int> vi; typedef vector<LLI> vlli; typedef vector<pii> vpii; typedef vector<plli> vplli; // ---------- END OF TEMPLATE ---------- struct event { int x,t,l,r,k; }; bool comp(event a,event b) { if (a.x == b.x) return abs(a.k) > abs(b.k); else return a.x < b.x; } char s[300000]; set<pii> S; vector<event> events; LLI ans[300000]; LLI bit1[300002],bit2[300002]; struct myset { vpii events; int insert(int x) { events.pb(mp(x,0)); return 0; } int query(int x,int m) { events.pb(mp(x,m)); return 0; } int process(int q) { int i,j; for (i = 0; i < events.size(); i++) { if (events[i].second == 0) { for (j = events[i].first+2; j <= q; j += j & (-j)) bit1[j]++,bit2[j] += events[i].first; } else { LLI a = 0,b = 0; for (j = events[i].first+2; j > 0; j -= j & (-j)) a += bit1[j],b += bit2[j]; ans[events[i].first] += events[i].second*(a*events[i].first-b); } } for (i = 0; i < events.size(); i++) { if (events[i].second == 0) { for (j = events[i].first+2; j <= q; j += j & (-j)) bit1[j] = bit2[j] = 0; } } return 0; } }; myset tree1[1 << 20],tree2[1 << 20]; int update(int s,int e,int as,int ae,int i,int t,int k) { if ((s > ae) || (e < as)) return 0; else if ((s >= as) && (e <= ae)) { if (k == 1) tree1[i].insert(t); else tree2[i].insert(t); return 0; } int mid = (s+e) / 2; update(s,mid,as,ae,2*i+1,t,k); update(mid+1,e,as,ae,2*i+2,t,k); return 0; } int query(int s,int e,int q,int i,int t) { if ((s > q) || (e < q)) return 0; else if (s == e) { tree1[i].query(t,1),tree2[i].query(t,-1); return 0; } int mid = (s+e) / 2; tree1[i].query(t,1),tree2[i].query(t,-1); query(s,mid,q,2*i+1,t),query(mid+1,e,q,2*i+2,t); return 0; } int process(int s,int e,int i,int q) { if (s == e) { tree1[i].process(q),tree2[i].process(q); return 0; } int mid = (s+e) / 2; tree1[i].process(q),tree2[i].process(q); process(s,mid,2*i+1,q),process(mid+1,e,2*i+2,q); return 0; } int main() { int i; int n,q,p = -1; int a,b,l,r; char o[7]; scanf("%d %d",&n,&q); for (i = 0; i < n; i++) { scanf(" %c",&s[i]); if (s[i] == '0') { if (i-1 >= p+1) S.insert(mp(p+1,i-1)); p = i; } } if (i-1 >= p+1) S.insert(mp(p+1,i-1)); for (auto it = S.begin(); it != S.end(); it++) events.pb((event){it->first,-1,0,it->second,1}); for (i = 0; i < q; i++) { scanf("%s",o); if (o[0] == 't') { scanf("%d",&p),p--; if (s[p] == '0') { auto it = S.lower_bound(mp(p,0)); if (it == S.end()) r = p; else { if (it->first == p+1) r = it->second,S.erase(it); else r = p; } it = S.lower_bound(mp(p,0)); if (it == S.begin()) l = p; else { --it; if (it->second == p-1) l = it->first,S.erase(it); else l = p; } S.insert(mp(l,r)); events.pb((event){l,i,p,r,1}); events.pb((event){p+1,i,p,r,-1}); s[p] = '1'; } else { auto it = --S.upper_bound(mp(p,n)); l = it->first,r = it->second,S.erase(it); events.pb((event){l,i,p,r,-1}); events.pb((event){p+1,i,p,r,1}); if (p-1 >= l) S.insert(mp(l,p-1)); if (r >= p+1) S.insert(mp(p+1,r)); s[p] = '0'; } } else { scanf("%d %d",&a,&b); a--,b -= 2; events.pb((event){a,i,b,b,0}); } } sort(events.begin(),events.end(),comp); fill(ans,ans+q,-1); for (i = 0; i < events.size(); i++) { if (events[i].k == 0) ans[events[i].t] = 0,query(0,n-1,events[i].l,0,events[i].t); else update(0,n-1,events[i].l,events[i].r,0,events[i].t,events[i].k); } process(0,n-1,0,q+1); for (i = 0; i < q; i++) { if (ans[i] != -1) printf("%lld\n",ans[i]); } return 0; }

Compilation message (stderr)

street_lamps.cpp: In member function 'int myset::process(int)':
street_lamps.cpp:80:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (i = 0; i < events.size(); i++) {
                     ~~^~~~~~~~~~~~~~~
street_lamps.cpp:90:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (i = 0; i < events.size(); i++) {
                     ~~^~~~~~~~~~~~~~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:192:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < events.size(); i++) {
                 ~~^~~~~~~~~~~~~~~
street_lamps.cpp:140:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&n,&q);
     ~~~~~^~~~~~~~~~~~~~~
street_lamps.cpp:142:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf(" %c",&s[i]);
         ~~~~~^~~~~~~~~~~~~
street_lamps.cpp:151:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%s",o);
         ~~~~~^~~~~~~~
street_lamps.cpp:153:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d",&p),p--;
             ~~~~~~~~~~~~~~^~~~
street_lamps.cpp:184:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d %d",&a,&b);
             ~~~~~^~~~~~~~~~~~~~~
#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...