Submission #3919225


Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;

#define NDEBUG
#include <cassert>


typedef long long ll;
typedef long double Double;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<ll,ll> llll;
typedef pair<double,double> dd;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<llll> vllll;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<long double> vD;

#define sz(a)  int((a).size())
#define pb  push_back
#define eb  emplace_back
#define FOR(var,from,to) for(int var=(from);var<=(to);++var)
#define rep(var,n)  for(int var=0;var<(n);++var)
#define rep1(var,n)  for(int var=1;var<=(n);++var)
#define repC2(vari,varj,n)  for(int vari=0;vari<(n)-1;++vari)for(int varj=vari+1;varj<(n);++varj)
#define repC3(vari,varj,vark,n)  for(int vari=0;vari<(n)-2;++vari)for(int varj=vari+1;varj<(n)-1;++varj)for(int vark=varj+1;vark<(n);++vark)
#define ALL(c)  (c).begin(),(c).end()
#define RALL(c)  (c).rbegin(),(c).rend()
#define tr(i,c)  for(auto i=(c).begin(); i!=(c).end(); ++i)
#define found(s,e)  ((s).find(e)!=(s).end())
#define mset(arr,val)  memset(arr,val,sizeof(arr))
#define mid(x,y) ((x)+((y)-(x))/2)
#define IN(x,a,b) ((a)<=(x)&&(x)<=(b))
#define cons make_pair

ll solve(string& s) {
    int L = s.size();

    int ofs = L+1;
    vector<ii> lastpos(ofs*2+1, ii(-2, -1));
    vector<vi> chunks(ofs*2+1);

    lastpos[ofs + 0] = ii(-1, 0);
    int level = 0;
    rep(i,L) {
        if (s[i] == '(') {
            ++level;
        } else {
            --level;
        }
        int h = ofs + level;
        if (lastpos[h].first == -2) {
            lastpos[h] = ii(i, 0);
        } else if (lastpos[h-1].first < lastpos[h].first) {
            lastpos[h] = ii(i, lastpos[h].second+1);
        } else {
            if (lastpos[h].second > 0)
                chunks[h].pb(lastpos[h].second);
            lastpos[h] = ii(i, 0);
        }
    }
    ll ans = 0;
    for (int level=-ofs; level<=ofs; ++level) {
        int h = ofs + level;
        if (lastpos[h].second > 0)
            chunks[h].pb(lastpos[h].second);

        for (int ch: chunks[h]) {
            ans += (ll)ch * (ch+1) / 2LL;
        }
    }
    return ans;
}

int main() {
    string s; cin >> s;
    cout << solve(s) << endl;
    return 0;
}

Submission Info

Submission Time
Task C - 部分文字列と括弧
User naoya_t
Language C++14 (GCC 5.4.1)
Score 500
Code Size 2504 Byte
Status
Exec Time 11 ms
Memory 7424 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 01.txt, 02.txt
All 500 / 500 01.txt, 02.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt
Case Name Status Exec Time Memory
01.txt 1 ms 256 KB
02.txt 1 ms 256 KB
10.txt 11 ms 7424 KB
11.txt 1 ms 256 KB
12.txt 1 ms 256 KB
13.txt 1 ms 256 KB
14.txt 1 ms 256 KB
15.txt 11 ms 7424 KB
16.txt 10 ms 7296 KB
17.txt 10 ms 7296 KB
18.txt 10 ms 7424 KB
19.txt 11 ms 7424 KB
20.txt 8 ms 6656 KB
21.txt 8 ms 6784 KB