#include <bits/stdc++.h> using namespace std; int tab[300009] {}; int sum[300009] {}; bool vis[300009] {}; priority_queue <pair<int,int> > pq; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,k,q,l,r,mini,maxi; long long suma=0; pair <int,int> odd; cin>>n>>k>>q; for (int i=0;i<n;i++) {cin>>tab[i];} for (int i=0;i<k;i++) {sum[0]=sum[0]+tab[i];} for (int i=1;i<=n-k;i++) {sum[i]=sum[i-1]+tab[k-1+i]-tab[i-1];} for (int zz=0;zz<q;zz++) { for (int i=0;i<k;i++) {vis[i]=0;} cin>>l>>r; suma=0; if (r-l+1<k) {cout<<"0\n"; continue;} l--; for (int i=l;i<r-k+1;i++) {vis[i]=0;} while (!pq.empty()) {pq.pop();} for (int i=l;i<=r-k+1;i++) {pq.push({sum[i],i});} odd=pq.top(); if (odd.first<=0) {cout<<"0\n"; continue;} while (!pq.empty()) { odd=pq.top(); pq.pop(); if (odd.first<=0) {break;} if (!vis[odd.second]) {suma=suma+odd.first; cout<<odd.first<<" "<<odd.second<<endl; mini=max(odd.second-k+1,l); maxi=min(r,odd.second+k); for (int i=mini;i<maxi;i++) {vis[i]=1;} } } cout<<suma<<"\n"; suma=0; } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | #include <bits/stdc++.h> using namespace std; int tab[300009] {}; int sum[300009] {}; bool vis[300009] {}; priority_queue <pair<int,int> > pq; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,k,q,l,r,mini,maxi; long long suma=0; pair <int,int> odd; cin>>n>>k>>q; for (int i=0;i<n;i++) {cin>>tab[i];} for (int i=0;i<k;i++) {sum[0]=sum[0]+tab[i];} for (int i=1;i<=n-k;i++) {sum[i]=sum[i-1]+tab[k-1+i]-tab[i-1];} for (int zz=0;zz<q;zz++) { for (int i=0;i<k;i++) {vis[i]=0;} cin>>l>>r; suma=0; if (r-l+1<k) {cout<<"0\n"; continue;} l--; for (int i=l;i<r-k+1;i++) {vis[i]=0;} while (!pq.empty()) {pq.pop();} for (int i=l;i<=r-k+1;i++) {pq.push({sum[i],i});} odd=pq.top(); if (odd.first<=0) {cout<<"0\n"; continue;} while (!pq.empty()) { odd=pq.top(); pq.pop(); if (odd.first<=0) {break;} if (!vis[odd.second]) {suma=suma+odd.first; cout<<odd.first<<" "<<odd.second<<endl; mini=max(odd.second-k+1,l); maxi=min(r,odd.second+k); for (int i=mini;i<maxi;i++) {vis[i]=1;} } } cout<<suma<<"\n"; suma=0; } return 0; } |