Submission #486376

#TimeUsernameProblemLanguageResultExecution timeMemory
486376leinad2역사적 조사 (JOI14_historical)C++17
40 / 100
4056 ms6640 KiB
#include<bits/stdc++.h> using namespace std; int n, q, i, j, k, a, b, B, inv[100010], A[100010], cnt[100010]; vector<int>comp; long long ans[100010], seg[400010]; struct query { int i, l, r; }; vector<query>Q; bool cmp(query a, query b) { if(a.l/B==b.l/B)return a.r<b.r; else return a.l/B<b.l/B; } void update(int id, int s, int e, int x, long long v) { if(e<x||x<s)return; if(s==e) { seg[id]=v; return; } int m=s+e>>1; update(id*2, s, m, x, v);update(id*2+1, m+1, e, x, v); seg[id]=max(seg[id*2], seg[id*2+1]); } void add(int i) { cnt[A[i]]++; update(1, 1, n, A[i], 1LL*cnt[A[i]]*inv[A[i]]); } void del(int i) { cnt[A[i]]--; update(1, 1, n, A[i], 1LL*cnt[A[i]]*inv[A[i]]); } main() { for(scanf("%d %d", &n, &q);i++<n;) { scanf("%d", &A[i]); comp.push_back(A[i]); } sort(comp.begin(), comp.end()); comp.erase(unique(comp.begin(), comp.end()), comp.end()); for(i=0;i++<n;) { a=lower_bound(comp.begin(), comp.end(), A[i])-comp.begin()+1; inv[a]=A[i]; A[i]=a; } B=(int)sqrt(n); for(i=0;i++<q;) { scanf("%d %d", &a, &b); Q.push_back({i, a, b}); } sort(Q.begin(), Q.end(), cmp); for(i=Q[0].l;i<=Q[0].r;i++)add(i); ans[Q[0].i]=seg[1]; for(i=1;i<q;i++) { if(Q[i].l<Q[i-1].l)for(j=Q[i-1].l-1;j>=Q[i].l;j--)add(j); else for(j=Q[i-1].l;j<Q[i].l;j++)del(j); if(Q[i].r>Q[i-1].r)for(j=Q[i-1].r+1;j<=Q[i].r;j++)add(j); else for(j=Q[i-1].r;j>Q[i].r;j--)del(j); ans[Q[i].i]=seg[1]; } for(i=0;i++<q;)printf("%lld\n", ans[i]); }

Compilation message (stderr)

historic.cpp: In function 'void update(int, int, int, int, long long int)':
historic.cpp:24:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   24 |     int m=s+e>>1;
      |           ~^~
historic.cpp: At global scope:
historic.cpp:38:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   38 | main()
      | ^~~~
historic.cpp: In function 'int main()':
historic.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |     for(scanf("%d %d", &n, &q);i++<n;)
      |         ~~~~~^~~~~~~~~~~~~~~~~
historic.cpp:42:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |         scanf("%d", &A[i]);
      |         ~~~~~^~~~~~~~~~~~~
historic.cpp:56:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |         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...